From efd10a85fcdfccc83afe2b89e14591e18a7379d7 Mon Sep 17 00:00:00 2001 From: Liwei Zhang <68692847+zhanglw0521@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:08:57 +0200 Subject: [PATCH 1/3] Add new coupling coeff backend --- Project.toml | 6 + src/ACEdensitymatrix.jl | 7 +- src/model_construction.jl | 21 +- src/utils/coupling_transformation.jl | 116 +++++++++ src/utils/extended_eqm.jl | 343 ++++++++++++++++++++++--- src/utils/gepi_couplings.jl | 336 ++++++++++++++++++++++++ src/utils/transformations.jl | 6 +- test/runtests.jl | 9 + test/test_backend_equivalence.jl | 94 +++++++ test/test_explicit_spec_constructor.jl | 90 +++++++ test/test_gepi_couplings.jl | 44 ++++ test/test_sparse_evaluation.jl | 30 +++ test/test_transformations.jl | 35 +++ 13 files changed, 1095 insertions(+), 42 deletions(-) create mode 100644 src/utils/coupling_transformation.jl create mode 100644 src/utils/gepi_couplings.jl create mode 100644 test/runtests.jl create mode 100644 test/test_backend_equivalence.jl create mode 100644 test/test_explicit_spec_constructor.jl create mode 100644 test/test_gepi_couplings.jl create mode 100644 test/test_sparse_evaluation.jl create mode 100644 test/test_transformations.jl diff --git a/Project.toml b/Project.toml index 01833a3..4dd6358 100644 --- a/Project.toml +++ b/Project.toml @@ -21,3 +21,9 @@ RepLieGroups = "f07d36f2-91c4-427a-b67b-965fe5ebe1d2" Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Test"] diff --git a/src/ACEdensitymatrix.jl b/src/ACEdensitymatrix.jl index 48d0f9e..f9c94cb 100644 --- a/src/ACEdensitymatrix.jl +++ b/src/ACEdensitymatrix.jl @@ -4,6 +4,11 @@ using Reexport # Some useful transformations needed for the densitymatrixlearning project include("utils/transformations.jl") +# Julia-1.9-compatible local adaptation of the GEPI Lie-algebra construction. +# The associated vector-to-matrix transformations are included by +# utils/transformations.jl. +include("utils/gepi_couplings.jl") + # Codes that should lie in ACEOperator.jl include("radial_basis.jl") include("utils/extended_eqm.jl") @@ -23,4 +28,4 @@ include("io.jl") include("utils/model_validation.jl") @reexport using ACEdensitymatrix.ModelValidation -end # module \ No newline at end of file +end # module diff --git a/src/model_construction.jl b/src/model_construction.jl index b5d8688..e538eaa 100644 --- a/src/model_construction.jl +++ b/src/model_construction.jl @@ -128,7 +128,8 @@ ao_dict = Dict( 1 => Dict("n_orbs" => [2], "maxdeg" => degree, "ord" => order, " model = Density_Model(ao_dict) """ -function Density_Model(ao_dict::Dict{TP, Dict{String, Any}}) where TP # There is a potential risk that DM tries to convert an unknown type dictionary to a Density_Model +function Density_Model(ao_dict::Dict{TP, Dict{String, Any}}; coupling_backend::Symbol = :old) where TP # There is a potential risk that DM tries to convert an unknown type dictionary to a Density_Model + coupling_backend = ACEdensitymatrix._normalize_coupling_backend(coupling_backend) Zs = collect(keys(ao_dict)) |> sort on_classes = classify_ao_dict_on(ao_dict) off_classes = classify_ao_dict_off(ao_dict) @@ -140,7 +141,7 @@ function Density_Model(ao_dict::Dict{TP, Dict{String, Any}}) where TP # There is for zs in on_classes onsite_cutoff = haskey(ao_dict[zs[1]], "rcut_on") ? "rcut_on" : "rcut" - push!(dict, zs[1] => On_Model(ao_dict[zs[1]]["maxdeg"], ao_dict[zs[1]]["ord"], ao_dict[zs[1]][onsite_cutoff], zs[1], Zs, length(ao_dict[zs[1]]["n_orbs"])-1, ao_dict[zs[1]]["n_orbs"])) + push!(dict, zs[1] => On_Model(ao_dict[zs[1]]["maxdeg"], ao_dict[zs[1]]["ord"], ao_dict[zs[1]][onsite_cutoff], zs[1], Zs, length(ao_dict[zs[1]]["n_orbs"])-1, ao_dict[zs[1]]["n_orbs"]; coupling_backend = coupling_backend)) if length(zs) > 1 AA2BB = Dict("AA2BBmap" => [ dict[zs[1]].model.layers.AA2BB.layers[i].op for i = 1:length(dict[zs[1]].model.layers.AA2BB.layers)], @@ -153,7 +154,7 @@ function Density_Model(ao_dict::Dict{TP, Dict{String, Any}}) where TP # There is for zs in off_classes offsite_cutoff = haskey(ao_dict[zs[1][1]], "rcut_off") ? "rcut_off" : "rcut" - push!(dict, zs[1] => Off_Model(maximum([ao_dict[zs[1][1]]["maxdeg"],ao_dict[zs[1][2]]["maxdeg"]]), maximum([ao_dict[zs[1][1]]["ord"],ao_dict[zs[1][2]]["ord"]]), ao_dict[zs[1][1]][offsite_cutoff], ao_dict[zs[1][2]][offsite_cutoff], maximum([ao_dict[zs[1][1]]["zcut"], ao_dict[zs[1][2]]["zcut"]]), zs[1][1], zs[1][2], Zs, length(ao_dict[zs[1][1]]["n_orbs"])-1, length(ao_dict[zs[1][2]]["n_orbs"])-1, ao_dict[zs[1][1]]["n_orbs"], ao_dict[zs[1][2]]["n_orbs"])) + push!(dict, zs[1] => Off_Model(maximum([ao_dict[zs[1][1]]["maxdeg"],ao_dict[zs[1][2]]["maxdeg"]]), maximum([ao_dict[zs[1][1]]["ord"],ao_dict[zs[1][2]]["ord"]]), ao_dict[zs[1][1]][offsite_cutoff], ao_dict[zs[1][2]][offsite_cutoff], maximum([ao_dict[zs[1][1]]["zcut"], ao_dict[zs[1][2]]["zcut"]]), zs[1][1], zs[1][2], Zs, length(ao_dict[zs[1][1]]["n_orbs"])-1, length(ao_dict[zs[1][2]]["n_orbs"])-1, ao_dict[zs[1][1]]["n_orbs"], ao_dict[zs[1][2]]["n_orbs"]; coupling_backend = coupling_backend)) if length(zs) > 1 AA2BB = Dict("AA2BBmap" => [ dict[zs[1]].model.layers.AA2BB.layers[i].op for i = 1:length(dict[zs[1]].model.layers.AA2BB.layers)], @@ -271,8 +272,8 @@ replace_stablize(model::On_Model) = On_Model( Chain(embed = model.model.layers.e replace_stablize(model::Off_Model) = Off_Model( Chain(embed = model.model.layers.embed, A = model.model.layers.A, AA = model.model.layers.AA, AA2BB = model.model.layers.AA2BB, stablize = WrappedFunction(cc -> real.(cc)), dot = model.model.layers.dot), model.ps, model.st, model.n_orbs1, model.n_orbs2, model.fitted) # An onsite submodel - input is a (local) one center environment, output is the corresponding onsite block of the density matrix -On_Model(maxdeg::Union{Int64,Vector{Int64}}, ord::Int64, rcut::Float64, Zi::T, Zs::Vector{T}, Lmax::Int64, n_orbs::Vector{Int64}=ones(Int64,Lmax+1); AA2BB=nothing) where{T} = - On_Model{Lmax+1}(equivariant_operator(maxdeg,ord,onsite_radial_basis(maximum(maxdeg), rcut),Lmax,n_orbs;categories=unique([(Zi,Z) for Z in Zs]), AA2BB = AA2BB)..., SVector{Lmax+1}(n_orbs),false) +On_Model(maxdeg::Union{Int64,Vector{Int64}}, ord::Int64, rcut::Float64, Zi::T, Zs::Vector{T}, Lmax::Int64, n_orbs::Vector{Int64}=ones(Int64,Lmax+1); AA2BB=nothing, coupling_backend::Symbol = :old) where{T} = + On_Model{Lmax+1}(equivariant_operator(maxdeg,ord,onsite_radial_basis(maximum(maxdeg), rcut),Lmax,n_orbs;categories=unique([(Zi,Z) for Z in Zs]), AA2BB = AA2BB, coupling_backend = coupling_backend)..., SVector{Lmax+1}(n_orbs),false) # get the categories of a offsite state @@ -285,11 +286,11 @@ On_Model(maxdeg::Union{Int64,Vector{Int64}}, ord::Int64, rcut::Float64, Zi::T, Z end # An offsite submodel - input is a (local) two-center environment, output is the corresponding offsite block of the density matrix -Off_Model(maxdeg::Union{Int64,Vector{Int64}}, ord::Int64, rcut::Float64, zcut::Float64, Zi::T, Zj::T, Zs::Vector{T}, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); AA2BB=nothing) where {T} = - Off_Model{L1+1,L2+1}(equivariant_operator(maxdeg,ord,offsite_radial_basis(maximum(maxdeg), rcut, zcut),L1,L2,n_orbs1,n_orbs2;categories=union([(Zi,Zj,Zj,true)],unique([(Zi,Zj,Zk,false) for Zk in Zs])),_get_cat = _get_cat_offsite, cat_extension = offsite_extension, AA2BB = AA2BB)..., SVector{L1+1}(n_orbs1), SVector{L2+1}(n_orbs2), false) +Off_Model(maxdeg::Union{Int64,Vector{Int64}}, ord::Int64, rcut::Float64, zcut::Float64, Zi::T, Zj::T, Zs::Vector{T}, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); AA2BB=nothing, coupling_backend::Symbol = :old) where {T} = + Off_Model{L1+1,L2+1}(equivariant_operator(maxdeg,ord,offsite_radial_basis(maximum(maxdeg), rcut, zcut),L1,L2,n_orbs1,n_orbs2;categories=union([(Zi,Zj,Zj,true)],unique([(Zi,Zj,Zk,false) for Zk in Zs])),_get_cat = _get_cat_offsite, cat_extension = offsite_extension, AA2BB = AA2BB, coupling_backend = coupling_backend)..., SVector{L1+1}(n_orbs1), SVector{L2+1}(n_orbs2), false) -Off_Model(maxdeg::Union{Int64,Vector{Int64}}, ord::Int64, rcut1::Float64, rcut2::Float64, zcut::Float64, Zi::T, Zj::T, Zs::Vector{T}, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); AA2BB=nothing) where {T} = - Off_Model{L1+1,L2+1}(equivariant_operator(maxdeg,ord,offsite_radial_basis(maximum(maxdeg), rcut1, rcut2, zcut),L1,L2,n_orbs1,n_orbs2;categories=union([(Zi,Zj,Zj,true)],unique([(Zi,Zj,Zk,false) for Zk in Zs])),_get_cat = _get_cat_offsite, cat_extension = offsite_extension, AA2BB = AA2BB)..., SVector{L1+1}(n_orbs1), SVector{L2+1}(n_orbs2), false) +Off_Model(maxdeg::Union{Int64,Vector{Int64}}, ord::Int64, rcut1::Float64, rcut2::Float64, zcut::Float64, Zi::T, Zj::T, Zs::Vector{T}, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); AA2BB=nothing, coupling_backend::Symbol = :old) where {T} = + Off_Model{L1+1,L2+1}(equivariant_operator(maxdeg,ord,offsite_radial_basis(maximum(maxdeg), rcut1, rcut2, zcut),L1,L2,n_orbs1,n_orbs2;categories=union([(Zi,Zj,Zj,true)],unique([(Zi,Zj,Zk,false) for Zk in Zs])),_get_cat = _get_cat_offsite, cat_extension = offsite_extension, AA2BB = AA2BB, coupling_backend = coupling_backend)..., SVector{L1+1}(n_orbs1), SVector{L2+1}(n_orbs2), false) # The above rcut1 and rcut2 could have different meaning # in the different choices of offsite environment, in the @@ -354,4 +355,4 @@ end include("tuned_models.jl") -end # module \ No newline at end of file +end # module diff --git a/src/utils/coupling_transformation.jl b/src/utils/coupling_transformation.jl new file mode 100644 index 0000000..b20b594 --- /dev/null +++ b/src/utils/coupling_transformation.jl @@ -0,0 +1,116 @@ +# Coupling transformations used to compare the legacy matrix-valued +# construction with the vector-valued GEPI construction. + +# This follows EquivariantTensors.O3.Ctran: +# https://github.com/ACEsuit/EquivariantTensors.jl/blob/main/src/O3/O3_utils.jl +function _Ctran_entry(m::Integer, mu::Integer, ::Type{T}=ComplexF64) where {T} + abs(m) == abs(mu) || return zero(T) + m == mu == 0 && return one(T) + m > 0 && mu > 0 && return T((-1)^m / sqrt(2)) + m < 0 && mu < 0 && return T(im / sqrt(2)) + m < 0 && mu > 0 && return T((-1)^(m + 1) * im / sqrt(2)) + @assert m > 0 && mu < 0 + return T(1 / sqrt(2)) +end + +""" + Ctran(L) + +Sparse complex-to-real spherical-harmonic transformation in the SpheriCart +ordering. For component vectors, `v_real = Ctran(L) * v_complex`. +""" +Ctran(L::Integer) = sparse([ + _Ctran_entry(m, mu) for m in -L:L, mu in -L:L +]) + +""" + complex_to_real_vector(L, vector) + +Transform the output irrep of complex-basis GEPI coefficients to the real +spherical-harmonic convention used for ACEDensityMatrix output blocks. The +input-product columns remain complex because `xx2AA(...; rSH=false)` constructs +the package's one-particle basis from complex spherical harmonics. +""" +function complex_to_real_vector(L::Integer, vector::AbstractVector) + @assert length(vector) == 2L + 1 + return Ctran(L) * vector +end + +channel_parity(l::Integer, lp::Integer, lambda::Integer) = + iseven(l + lp + lambda) ? :even : :odd + +""" + cg_block([T=Float64], l, lp, lambda) + +Construct the sparse real Clebsch-Gordan map whose transpose maps a real +`lambda`-vector to a `(2l+1) x (2lp+1)` matrix block. The convention and +`transform_λ` name follow ACEoperators.jl: +https://github.com/ACEsuit/ACEoperators.jl/blob/main/src/linear2c/coupling.jl +""" +function cg_block(::Type{T}, l::Integer, lp::Integer, + lambda::Integer) where {T<:Real} + block = zeros(T, 2lambda + 1, (2l + 1) * (2lp + 1)) + abs(l - lp) <= lambda <= l + lp || return sparse(block) + + Tl = Ctran(l) + Tlp = Ctran(lp) + Tlambda = Ctran(lambda) + even_channel = channel_parity(l, lp, lambda) == :even + + for nu in -lambda:lambda, a in -l:l, b in -lp:lp + value = 0.0im + for m in -l:l, mp in -lp:lp + mu = m + mp + abs(mu) <= lambda || continue + coefficient = cg(l, m, lp, mp, lambda, mu) + iszero(coefficient) && continue + value += conj(Tlambda[nu + lambda + 1, mu + lambda + 1]) * + Tl[a + l + 1, m + l + 1] * + Tlp[b + lp + 1, mp + lp + 1] * coefficient + end + matrix_column = (b + lp) * (2l + 1) + (a + l + 1) + block[nu + lambda + 1, matrix_column] = + even_channel ? T(real(value)) : T(imag(value)) + end + return sparse(block) +end + +cg_block(l::Integer, lp::Integer, lambda::Integer) = + cg_block(Float64, l, lp, lambda) + +struct BlockCoupling{T} + l::Int + lp::Int + lambdas::UnitRange{Int} + maps::Vector{SparseMatrixCSC{T,Int}} +end + +function BlockCoupling(::Type{T}, l::Integer, lp::Integer) where {T<:Real} + lambdas = abs(l - lp):(l + lp) + maps = [cg_block(T, l, lp, lambda) for lambda in lambdas] + return BlockCoupling{T}(Int(l), Int(lp), lambdas, maps) +end + +BlockCoupling(l::Integer, lp::Integer) = BlockCoupling(Float64, l, lp) + +function _lambda_index(coupling::BlockCoupling, lambda::Integer) + index = Int(lambda) - first(coupling.lambdas) + 1 + 1 <= index <= length(coupling.lambdas) || + error("lambda = $lambda is not admissible for " * + "(l, lp) = ($(coupling.l), $(coupling.lp))") + return index +end + +""" + transform_λ(coupling, lambda, vector) + +Map one real-spherical `lambda` channel to its contribution to the matrix block. +The multiplication uses the sparse linear map stored by `BlockCoupling`. +""" +function transform_λ(coupling::BlockCoupling, lambda::Integer, + vector::AbstractVector) + @assert length(vector) == 2lambda + 1 + map = coupling.maps[_lambda_index(coupling, lambda)] + matrix_vector = transpose(map) * vector + return reshape(matrix_vector, 2coupling.l + 1, 2coupling.lp + 1) +end diff --git a/src/utils/extended_eqm.jl b/src/utils/extended_eqm.jl index 7a9b9f9..3aa0b9c 100644 --- a/src/utils/extended_eqm.jl +++ b/src/utils/extended_eqm.jl @@ -4,7 +4,17 @@ using Polynomials4ML: LinearLayer using Lux: AbstractExplicitLayer import EquivariantModels: _rpi_A2B_matrix, _valtype, rpe_basis, RPE_filter, simple_extension -export equivariant_operator, extend_n_orbs, LinearLayer_loc, ConstLinearLayer_loc +export equivariant_operator, extend_n_orbs, LinearLayer_loc, ConstLinearLayer_loc, + _gepi_A2B_matrix_real + +function _normalize_coupling_backend(backend::Symbol) + backend in (:old, :legacy) && return :old + backend in (:new, :gepi) && return :new + throw(ArgumentError( + "coupling_backend must be :old or :new " * + "(:legacy and :gepi remain accepted as compatibility aliases)", + )) +end ## Construct a new EQM that generates also tensorial basis # TODO: Lux has also a ReshapeLayer that could be used - might be a way to get rid of Rot3DCoeffs_loc @@ -208,6 +218,205 @@ function _rpi_A2B_matrix_real(cgen::Rot3DCoeffs_loc{L1,L2,T}, spec) where {L1,L2 return sparse(Irow, Jcol, SMatrix{2L1+1,2L2+1}.(Ref(ctran(L1)) .* vals .* Ref(ctran(L2)')), idxB, length(spec)) end +# ----------------------------------------------------------------------------- +# Category-aware GE-PI coupling construction +# ----------------------------------------------------------------------------- + +_nonmagnetic_label(onep) = haskey(onep, :s) ? + (n=onep.n, l=onep.l, s=onep.s) : (n=onep.n, l=onep.l) + +function _label_sort_key(label) + return haskey(label, :s) ? (label.l, label.n, label.s) : + (label.l, label.n, ()) +end + +function _canonical_group_key(pib) + labels = _nonmagnetic_label.(pib) + sort!(labels; by=_label_sort_key) + return Tuple(labels) +end + +function _canonical_magnetic_tuple(pib) + ordered = sort( + collect(pib); + by=onep -> (_label_sort_key(_nonmagnetic_label(onep)), onep.m), + ) + return SVector{length(ordered),Int}(Tuple(onep.m for onep in ordered)) +end + +function _canonical_magnetic_tuple(pib, ::Val{N}) where {N} + ordered = sort( + collect(pib); + by=onep -> (_label_sort_key(_nonmagnetic_label(onep)), onep.m), + ) + return SVector{N,Int}(Tuple(onep.m for onep in ordered)) +end + +function _gepi_matrix_value(coupling, lambda, coefficient) + complex_vector = lambda == 0 ? + SVector{1,Float64}((coefficient,)) : coefficient + real_vector = complex_to_real_vector(lambda, complex_vector) + value = transform_λ(coupling, lambda, real_vector) + + # ACEoperators' odd-parity real matrix channel differs by an imaginary + # phase from the complex spherical-harmonic AA input basis used here. + # Without this phase, the subsequent `real.(...)` stabilization silently + # discards the odd channel. + return channel_parity(coupling.l, coupling.lp, lambda) == :odd ? + im * value : value +end + +function _gepi_channel_pattern(key) + channel_map = Dict{Any,Int}() + next_channel = 1 + return Tuple( + get!(channel_map, haskey(label, :s) ? (label.n, label.s) : label.n) do + channel = next_channel + next_channel += 1 + channel + end + for label in key + ) +end + +function _gepi_coefficients!(cache, lambda, ll, channels) + cache_key = (lambda, ll, channels) + return get!(cache, cache_key) do + GEPICouplings.gepi_coupling_coeffs(lambda, ll, channels) + end +end + +function _append_gepi_coefficients!( + row_indices::Vector{Int}, + column_indices::Vector{Int}, + values::Vector{MT}, + row_offset::Int, + target_index::Dict{SVector{N,Int},Int}, + coupling, + ::Val{K}, + coefficients, + magnetic_set, + tolerance::Real, +) where {MT,N,K} + for local_row in axes(coefficients, 1) + row_offset += 1 + for source_column in axes(coefficients, 2) + column = get(target_index, magnetic_set[source_column], 0) + column == 0 && continue + value = MT(_gepi_matrix_value( + coupling, + K, + coefficients[local_row, source_column], + )) + norm(value) <= tolerance && continue + push!(row_indices, row_offset) + push!(column_indices, column) + push!(values, value) + end + end + return row_offset +end + +function _append_gepi_group!( + row_indices, + column_indices, + values, + row_offset, + coupling, + spec, + key::NTuple{N}, + columns, + coefficient_cache, + lambdas, + tolerance, +) where {N} + target_index = Dict{SVector{N,Int},Int}() + for column in columns + magnetic = _canonical_magnetic_tuple(spec[column], Val(N)) + haskey(target_index, magnetic) && + error("duplicate canonical magnetic tuple in GE-PI group") + target_index[magnetic] = column + end + + ll = ntuple(index -> key[index].l, Val(N)) + channels = _gepi_channel_pattern(key) + for lambda in lambdas + coefficients, magnetic_set = _gepi_coefficients!( + coefficient_cache, lambda, ll, channels, + ) + row_offset = _append_gepi_coefficients!( + row_indices, + column_indices, + values, + row_offset, + target_index, + coupling, + Val(lambda), + coefficients, + magnetic_set, + tolerance, + ) + end + return row_offset +end + +""" + _gepi_A2B_matrix_real(Rot3DCoeffs_loc(L1, L2), spec) + +Construct the real `(L1,L2)` matrix-valued A-to-B coupling map using the +local GE-PI Lie-algebra kernel. Products are grouped by the complete +non-magnetic one-particle labels. In particular, categorical labels `s` are +folded into the GE-PI channel identity, matching the EquivariantTensors +convention that its integer channel label distinguishes every non-magnetic +one-particle feature. +""" +function _gepi_A2B_matrix_real( + ::Rot3DCoeffs_loc{L1,L2,T}, spec; + tolerance::Real=1e-12, + coefficient_cache=Dict{Any,Any}(), +) where {L1,L2,T} + matrix_type = SMatrix{ + 2L1 + 1, 2L2 + 1, ComplexF64, (2L1 + 1) * (2L2 + 1), + } + isempty(spec) && return sparse(Int[], Int[], matrix_type[], 0, 0) + + group_order = Any[] + group_columns = Dict{Any,Vector{Int}}() + for (column, pib) in enumerate(spec) + key = _canonical_group_key(pib) + if !haskey(group_columns, key) + push!(group_order, key) + group_columns[key] = Int[] + end + push!(group_columns[key], column) + end + + coupling = BlockCoupling(L1, L2) + row_indices = Int[] + column_indices = Int[] + values = matrix_type[] + row_offset = 0 + + for key in group_order + columns = group_columns[key] + row_offset = _append_gepi_group!( + row_indices, + column_indices, + values, + row_offset, + coupling, + spec, + key, + columns, + coefficient_cache, + abs(L1-L2):(L1+L2), + tolerance, + ) + end + + return sparse(row_indices, column_indices, values, row_offset, length(spec)) +end + # Locally defined ConstLinearLayer and LinearLayer ======================== using Polynomials4ML: @reqfields, _make_reqfields using ObjectPools: ArrayPool, FlexArray, FlexArrayCache, unwrap, release! @@ -215,11 +424,68 @@ using ObjectPools: ArrayPool, FlexArray, FlexArrayCache, unwrap, release! struct ConstLinearLayer_loc{T} <: AbstractExplicitLayer op::T pos::Vector{Int} + + ConstLinearLayer_loc{T}(op::T, pos::Vector{Int}) where {T} = + new{T}(op, pos) end +function _concrete_static_sparse( + op::SparseMatrixCSC{TM,TI}, +) where {TM<:StaticMatrix,TI<:Integer} + concrete_type = typeof(zero(TM)) + TM === concrete_type && return op + return SparseMatrixCSC( + size(op, 1), size(op, 2), + op.colptr, op.rowval, concrete_type.(op.nzval), + ) +end + +function ConstLinearLayer_loc( + op::SparseMatrixCSC{<:StaticMatrix}, + pos::AbstractVector{<:Integer}, +) + concrete_op = _concrete_static_sparse(op) + return ConstLinearLayer_loc{typeof(concrete_op)}( + concrete_op, Vector{Int}(pos), + ) +end + +ConstLinearLayer_loc(op, pos::AbstractVector{<:Integer}) = + ConstLinearLayer_loc{typeof(op)}(op, Vector{Int}(pos)) + (l::ConstLinearLayer_loc)(x::AbstractArray, ps, st) = (l(x), st) (l::ConstLinearLayer_loc)(x) = l.op * x[l.pos] +# The matrix-valued sparse entries use a fully concrete SMatrix element type. +# Their inline storage can therefore be viewed as scalar coefficients without +# copying. Accumulating directly into one flat scalar buffer avoids allocating +# an immutable SMatrix temporary for every sparse multiply-add. +function (l::ConstLinearLayer_loc{<:SparseMatrixCSC{TM}})( + x::AbstractVector{TX}, +) where {TM<:StaticMatrix,TX<:Number} + @assert isbitstype(TM) + output_type = typeof(zero(TM) * zero(TX)) + output_scalar_type = eltype(output_type) + coefficient_scalar_type = eltype(TM) + component_count = length(output_type) + flat_result = zeros( + output_scalar_type, component_count * size(l.op, 1), + ) + scalar_values = reinterpret(coefficient_scalar_type, l.op.nzval) + for local_column in axes(l.op, 2) + input_value = x[l.pos[local_column]] + for pointer in l.op.colptr[local_column]:(l.op.colptr[local_column+1]-1) + output_offset = (l.op.rowval[pointer] - 1) * component_count + value_offset = (pointer - 1) * component_count + @inbounds @simd for component in 1:component_count + flat_result[output_offset + component] += + scalar_values[value_offset + component] * input_value + end + end + end + return reinterpret(output_type, flat_result) +end + struct LinearLayer_loc{FEATFIRST} <: AbstractExplicitLayer in_dim::Int out_dim::Int @@ -255,7 +521,7 @@ LuxCore.initialstates(rng::AbstractRNG, l::LinearLayer_loc) = ## ===================================================================================== # fix something stupid in EQM using EquivariantModels: make_nlms_spec, getspec1idx, gensparse, getspecnlm, specnlm2spec1p -function degord2spec_loc(radial::Radial_basis; totaldegree, order, Lmax, catagories = [], filtered_extension = simple_extension, wL = 1, islong = true, rSH = false) +function degord2spec_loc(radial::Radial_basis; totaldegree, order, Lmax, catagories = [], filtered_extension = simple_extension, wL = 1, rSH = false) # Rn = radial.radial_basis(totaldegree) if typeof(totaldegree) == Int64 totaldegree = repeat([totaldegree], order) @@ -270,11 +536,13 @@ function degord2spec_loc(radial::Radial_basis; totaldegree, order, Lmax, catagor tup2b = vv -> [ spec1p[v] for v in vv[vv .> 0] ] default_admissible = bb -> length(bb) == 0 || sum(b.n for b in bb) + wL * sum(b.l for b in bb) <= totaldegree[length(bb)] - # to construct SS, SD blocks + # Matrix-valued density models construct all (l1, l2) output blocks. + # Keep the complete admissible complex-SH product specification here; + # each output block applies its own RPE_filter later. if rSH filter_ = bb -> (length(bb) == 0) || iseven(sum(b.l for b in bb) + Lmax) && ( length(bb) == 1 && Lmax == 0 ? bb[1].l == 0 : true ) else - filter_ = islong ? bb -> true : bb -> (length(bb) == 0) || iseven(sum(b.l for b in bb) + Lmax) && ( length(bb) == 1 && Lmax == 0 ? bb[1].l == 0 : true ) + filter_ = bb -> true end specAA = gensparse(; NU = order, tup2b = tup2b, filter = filter_, @@ -294,7 +562,7 @@ function degord2spec_loc(radial::Radial_basis; totaldegree, order, Lmax, catagor end # Can add a reduce = true/false option to simplify onsite basis -function equivariant_model_loc(spec_nlm, radial::Radial_basis, L1::Int64, L2::Int64; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing) +function equivariant_model_loc(spec_nlm, radial::Radial_basis, L1::Int64, L2::Int64; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) # # first filt out those unfeasible spec_nlm # filter_init = RPE_filter_long(L1+L2) @@ -308,8 +576,10 @@ function equivariant_model_loc(spec_nlm, radial::Radial_basis, L1::Int64, L2::In LLset = [(l1,l2) for l1 = 0:L1 for l2 = 0:L2] if isnothing(AA2BB) + coupling_backend = _normalize_coupling_backend(coupling_backend) C = Vector{Any}(undef, length(LLset)) pos = Vector{Any}(undef, length(LLset)) + gepi_coefficient_cache = Dict{Any,Any}() for (l,(l1,l2)) in enumerate(LLset) filter = isnothing(tuned_filter) ? RPE_filter(l1+l2) : tuned_filter(l1,l2,maximum(length.(spec_nlm))) @@ -317,7 +587,17 @@ function equivariant_model_loc(spec_nlm, radial::Radial_basis, L1::Int64, L2::In tmp = spec_nlm[findall(x -> filter(x) == 1, spec_nlm)] - C[l] = isreal ? _rpi_A2B_matrix_real(cgen, tmp) : _rpi_A2B_matrix(cgen, tmp) + C[l] = if coupling_backend == :new + isreal || throw(ArgumentError( + "the GE-PI backend currently supports isreal=true only", + )) + _gepi_A2B_matrix_real( + cgen, tmp; coefficient_cache=gepi_coefficient_cache, + ) + else + isreal ? _rpi_A2B_matrix_real(cgen, tmp) : + _rpi_A2B_matrix(cgen, tmp) + end pos[l] = findall(x -> filter(x) == 1, spec_nlm) # [ dict[tmp[j]] for j = 1:length(tmp)] end else @@ -346,33 +626,33 @@ function equivariant_model_loc(spec_nlm, radial::Radial_basis, L1::Int64, L2::In return luxchain, ps, st, LLset end -equivariant_model_loc(spec_nlm, radial::Radial_basis, L::Int64; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing) = - equivariant_model_loc(spec_nlm, radial, L, L; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter) +equivariant_model_loc(spec_nlm, radial::Radial_basis, L::Int64; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) = + equivariant_model_loc(spec_nlm, radial, L, L; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter, coupling_backend) # more constructors equivariant_model -equivariant_model_loc(totdeg::Int64, ν::Int64, radial::Radial_basis, L1::Int64, L2::Int64; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, cat_extension = simple_extension, tuned_filter = nothing) = - equivariant_model_loc(degord2spec_loc(radial; totaldegree = totdeg, order = ν, Lmax = L1+L2, catagories = categories, filtered_extension = cat_extension, islong = islong)[2], radial, L1, L2; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter) +equivariant_model_loc(totdeg::Int64, ν::Int64, radial::Radial_basis, L1::Int64, L2::Int64; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, cat_extension = simple_extension, tuned_filter = nothing, coupling_backend::Symbol = :old) = + equivariant_model_loc(degord2spec_loc(radial; totaldegree = totdeg, order = ν, Lmax = L1+L2, catagories = categories, filtered_extension = cat_extension)[2], radial, L1, L2; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter, coupling_backend) # With the _close function, the input could simply be an nnlllist (nlist,llist) -equivariant_model_loc(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis, L1::Int64, L2::Int64; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing) = begin +equivariant_model_loc(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis, L1::Int64, L2::Int64; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) = begin filter = RPE_filter_long(L1+L2) - equivariant_model_loc(_close(nn, ll; filter = filter), radial, L1, L2; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter) + equivariant_model_loc(_close(nn, ll; filter = filter), radial, L1, L2; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter, coupling_backend) end -equivariant_model_loc(totdeg::Int64, ν::Int64, radial::Radial_basis, L; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing) = - equivariant_model_loc(totdeg, ν, radial, L, L; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter) +equivariant_model_loc(totdeg::Int64, ν::Int64, radial::Radial_basis, L; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) = + equivariant_model_loc(totdeg, ν, radial, L, L; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter, coupling_backend) -equivariant_model_loc(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis, L; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing) = - equivariant_model_loc(nn, ll, radial, L, L; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter) +equivariant_model_loc(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis, L; categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState = true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) = + equivariant_model_loc(nn, ll, radial, L, L; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter, coupling_backend) # extend_n_orbs(n_orbs, LLset) = [ n_orbs[l1+1] * n_orbs[l2+1] for (l1,l2) in LLset] # extend_n_orbs(n_orbs) = extend_n_orbs(n_orbs, [(l1,l2) for l1 = 0:length(n_orbs)-1 for l2 = 0:length(n_orbs)-1]) extend_n_orbs(n_orbs1, n_orbs2) = [ n_orbs1[l1+1] * n_orbs2[l2+1] for l1 = 0:length(n_orbs1)-1, l2 = 0:length(n_orbs2)-1] extend_n_orbs(n_orbs1, n_orbs2, LLset) = [ n_orbs1[l1+1] * n_orbs2[l2+1] for (l1,l2) in LLset] -function equivariant_operator(spec_nlm, radial::Radial_basis, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing) - luxchain, ps, st, LLset = equivariant_model_loc(spec_nlm, radial, L1, L2; categories, _get_cat = _get_cat, AA2BB= AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter) +function equivariant_operator(spec_nlm, radial::Radial_basis, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) + luxchain, ps, st, LLset = equivariant_model_loc(spec_nlm, radial, L1, L2; categories, _get_cat = _get_cat, AA2BB= AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter, coupling_backend = coupling_backend) @assert length(n_orbs1) == L1 + 1 && length(n_orbs2) == L2 + 1 @assert length(LLset) == (L1 + 1) * (L2 + 1) ext_n_orbs = extend_n_orbs(n_orbs1, n_orbs2, LLset) @@ -387,23 +667,26 @@ function equivariant_operator(spec_nlm, radial::Radial_basis, L1::Int64, L2::Int return luxchain, ps, st end -equivariant_operator(spec_nlm, radial::Radial_basis, L::Int64, n_orbs::Vector{Int64}=ones(Int64,L+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing) = - equivariant_operator(spec_nlm, radial, L, L, n_orbs, n_orbs; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter) +equivariant_operator(spec_nlm, radial::Radial_basis, L::Int64, n_orbs::Vector{Int64}=ones(Int64,L+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) = + equivariant_operator(spec_nlm, radial, L, L, n_orbs, n_orbs; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter, coupling_backend = coupling_backend) -function equivariant_operator(totdeg::Union{Int64,Vector{Int64}}, ν::Int64, radial::Radial_basis, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, cat_extension = simple_extension, tuned_filter = nothing) - # equivariant_operator(degord2spec_loc(radial; totaldegree = totdeg, order = ν, Lmax=maximum(L1+L2), catagories = categories, islong = true)[2], radial, L1, L2, n_orbs1, n_orbs2; categories = categories, _get_cat = _get_cat, d = d, group = group, isState = isState, isreal = isreal) - equivariant_operator(degord2spec_loc(radial; totaldegree = totdeg, order = ν, Lmax = L1+L2, catagories = categories, filtered_extension = cat_extension, islong = true)[2], radial, L1, L2, n_orbs1, n_orbs2; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter) +function equivariant_operator(totdeg::Union{Int64,Vector{Int64}}, ν::Int64, radial::Radial_basis, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, cat_extension = simple_extension, tuned_filter = nothing, coupling_backend::Symbol = :old) + equivariant_operator(degord2spec_loc(radial; totaldegree = totdeg, order = ν, Lmax = L1+L2, catagories = categories, filtered_extension = cat_extension)[2], radial, L1, L2, n_orbs1, n_orbs2; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter, coupling_backend = coupling_backend) end -equivariant_operator(totdeg::Union{Int64,Vector{Int64}}, ν::Int64, radial::Radial_basis, L::Int64, n_orbs::Vector{Int64}=ones(Int64,L+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing) = - equivariant_operator(totdeg, ν, radial, L, L, n_orbs, n_orbs; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter) +equivariant_operator(totdeg::Union{Int64,Vector{Int64}}, ν::Int64, radial::Radial_basis, L::Int64, n_orbs::Vector{Int64}=ones(Int64,L+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) = + equivariant_operator(totdeg, ν, radial, L, L, n_orbs, n_orbs; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter, coupling_backend = coupling_backend) -function equivariant_operator(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis, L1::Int64, L2, Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing) +function equivariant_operator(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis, L1::Int64, L2::Int64, n_orbs1::Vector{Int64}=ones(Int64,L1+1), n_orbs2::Vector{Int64}=ones(Int64,L2+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) filter = RPE_filter_long(L1+L2) - # equivariant_operator(_close(nn, ll; filter = filter), radial, maximum(L1,L2), n_orbs; categories = categories, _get_cat = _get_cat, d = d, group = group, isState = isState, isreal = isreal) - equivariant_operator(_close(nn, ll; filter = filter), radial, L1+L2, n_orbs; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter) + equivariant_operator(_close(nn, ll; filter = filter), radial, L1, L2, + n_orbs1, n_orbs2; categories = categories, + _get_cat = _get_cat, AA2BB = AA2BB, d = d, + group = group, isState = isState, isreal = isreal, + tuned_filter = tuned_filter, + coupling_backend = coupling_backend) end -equivariant_operator(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis, L::Int64, n_orbs::Vector{Int64}=ones(Int64,L+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing) = - equivariant_operator(nn, ll, radial, L, L, n_orbs, n_orbs; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter) \ No newline at end of file +equivariant_operator(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis, L::Int64, n_orbs::Vector{Int64}=ones(Int64,L+1); categories=[], _get_cat = _get_cat_default, AA2BB = nothing, d=3, group="O3", isState=true, isreal = true, tuned_filter = nothing, coupling_backend::Symbol = :old) = + equivariant_operator(nn, ll, radial, L, L, n_orbs, n_orbs; categories = categories, _get_cat = _get_cat, AA2BB = AA2BB, d = d, group = group, isState = isState, isreal = isreal, tuned_filter = tuned_filter, coupling_backend = coupling_backend) diff --git a/src/utils/gepi_couplings.jl b/src/utils/gepi_couplings.jl new file mode 100644 index 0000000..13e0e37 --- /dev/null +++ b/src/utils/gepi_couplings.jl @@ -0,0 +1,336 @@ +module GEPICouplings + +using LinearAlgebra +using SparseArrays +using StaticArrays + +export gepi_coupling_coeffs, categorical_channel_ids + +# Adapted from constructing_GE-PI_basis_via_lie_alg.jl in the code archive +# https://doi.org/10.5281/zenodo.20331400. The implementation is kept local +# so that ACEDensityMatrix can retain its Julia 1.9 environment. In +# particular, `binomial` is provided by Base, so Combinatorics is not needed. + +@inline function _fill_mm!(out, current, lset, nset, block, local_depth, + global_depth, min_val, idx) + if global_depth > length(current) + @inbounds out[idx] = SVector(current) + return idx + 1 + end + + if local_depth > @inbounds nset[block] + next_l = @inbounds lset[block + 1] + return _fill_mm!(out, current, lset, nset, block + 1, 1, + global_depth, -next_l, idx) + end + + l = @inbounds lset[block] + for value in min_val:l + @inbounds current[global_depth] = value + idx = _fill_mm!(out, current, lset, nset, block, local_depth + 1, + global_depth + 1, value, idx) + end + return idx +end + +function _all_mm_blocks(lset::AbstractVector{Int}, nset::AbstractVector{Int}, + ::Val{N}) where {N} + @assert length(lset) == length(nset) + @assert sum(nset) == N + + total = prod(binomial(2 * l + n, n) for (l, n) in zip(lset, nset)) + out = Vector{SVector{N,Int}}(undef, total) + current = MVector{N,Int}(undef) + _fill_mm!(out, current, lset, nset, 1, 1, 1, -lset[1], 1) + return out +end + +struct _PermutableBlocks{N,T1,T2} + nn::SVector{N,T1} + ll::SVector{N,T2} +end + +function Base.iterate(iter::_PermutableBlocks{N}, state=1) where {N} + state > N && return nothing + start_idx = state + @inbounds for i in (start_idx + 1):N + if iter.ll[i] != iter.ll[start_idx] || iter.nn[i] != iter.nn[start_idx] + return (start_idx:(i - 1), i) + end + end + return (start_idx:N, N + 1) +end + +_permutable_blocks(nn::SVector{N,T1}, ll::SVector{N,T2}) where {N,T1,T2} = + _PermutableBlocks(nn, ll) + +""" + categorical_channel_ids(nn, ss) + +Encode the complete non-angular one-particle label `(n, s)` as an integer +channel index. EquivariantTensors uses its integer `n` label in precisely +this role: permutations are allowed only inside blocks with identical +`(channel, l)` labels. ACEDensityMatrix stores the categorical label `s` +separately, so it must be folded into the channel index before constructing +permutation-invariant coupling coefficients. +""" +function categorical_channel_ids(nn, ss) + length(nn) == length(ss) || + throw(DimensionMismatch("nn and ss must have the same length")) + channel_map = Dict{Any,Int}() + next_channel = 1 + return [get!(channel_map, (nn[i], ss[i])) do + channel = next_channel + next_channel += 1 + channel + end for i in eachindex(nn)] +end + +function _block_multiplicities(nn::SVector{N,Int}, ll::SVector{N,Int}) where {N} + lset = Int[] + nset = Int[] + for block in _permutable_blocks(nn, ll) + push!(lset, ll[first(block)]) + push!(nset, length(block)) + end + return lset, nset +end + +# Derivative with respect to the second Euler angle at the identity. +_db(l::Int, m::Int, mu::Int) = + m - mu == 1 ? -0.5 * sqrt((l - mu) * (l + m)) : + m - mu == -1 ? 0.5 * sqrt((l + mu) * (l - m)) : 0.0 + +function _lie_matrix(K::Int, ll::SVector{N,Int}, nn::SVector{N,Int}) where {N} + lset, nset = _block_multiplicities(nn, ll) + all_mm = _all_mm_blocks(lset, nset, Val(N)) + + muset = all_mm[findall(x -> abs(sum(x)) <= K, all_mm)] + mmset = if K != 0 + all_mm[findall(x -> (-K <= sum(x) <= K - 1) || sum(x) == K + 1, + all_mm)] + else + all_mm[findall(x -> sum(x) == K + 1, all_mm)] + end + + sort!(muset, by=sum) + sort!(mmset, by=sum) + mu_index = Dict{SVector{N,Int},Int}(mu => i for (i, mu) in enumerate(muset)) + + row_idx = Int[] + col_idx = Int[] + values = Float64[] + ncols = 0 + + for mm in mmset + S = sum(mm) + if -K <= S <= K - 1 + ncols += 1 + I = S + 1 + push!(row_idx, mu_index[mm]) + push!(col_idx, ncols) + push!(values, -_db(K, I - 1, I)) + + block_end = 0 + for (l, n) in zip(lset, nset) + block_start = block_end + 1 + block_end += n + for i in block_end:-1:block_start + m = mm[i] + if m < l && (i == block_end || m < mm[i + 1]) + mu = setindex(mm, m + 1, i) + multiplicity = count(==(mu[i]), view(mu, block_start:block_end)) + push!(row_idx, mu_index[mu]) + push!(col_idx, ncols) + push!(values, multiplicity * _db(l, mu[i] - 1, mu[i])) + end + end + end + elseif S == K + 1 + ncols += 1 + block_end = 0 + for (l, n) in zip(lset, nset) + block_start = block_end + 1 + block_end += n + for i in block_start:block_end + m = mm[i] + if m > -l && (i == block_start || m > mm[i - 1]) + mu = setindex(mm, m - 1, i) + multiplicity = count(==(mu[i]), view(mu, block_start:block_end)) + push!(row_idx, mu_index[mu]) + push!(col_idx, ncols) + push!(values, multiplicity * _db(l, mu[i] + 1, mu[i])) + end + end + end + end + end + + @assert ncols == length(mmset) + return sparse(row_idx, col_idx, values, length(muset), ncols), muset, mmset +end + +function _truncate_zeros!(C::AbstractMatrix, tol::Real=1e-12) + @inbounds @simd for i in eachindex(C) + abs(C[i]) < tol && (C[i] = zero(eltype(C))) + end + return C +end + +function _nullspace_upper_sparse(U::AbstractMatrix{T}) where {T<:Number} + m, n = size(U) + @assert m <= n + nullity = n - m + nullity == 0 && return zeros(T, n, 0) + + rhs = -Matrix(U[1:m, (m + 1):n]) + X = UpperTriangular(U[1:m, 1:m]) \ rhs + basis = zeros(T, n, nullity) + basis[1:m, :] .= X + @inbounds for j in 1:nullity + basis[m + j, j] = one(T) + end + return basis ./ norm(basis) +end + +function _solve_kernel(M::AbstractMatrix{T}, mmset::Vector{SVector{N,Int}}, + muset::Vector{SVector{N,Int}}) where {N,T<:Number} + M = sparse(transpose(M)) + C = zeros(Float64, size(M, 2) - size(M, 1), size(M, 2)) + + row_sum = sum.(mmset) + column_sum = sum.(muset) + row_range = findall(i -> i == 1 || row_sum[i] != row_sum[i - 1], + eachindex(mmset)) + column_range = findall(i -> i == 1 || column_sum[i] != column_sum[i - 1], + eachindex(muset)) + push!(row_range, length(mmset) + 1) + push!(column_range, length(muset) + 1) + + row_block = row_range[end - 1]:(row_range[end] - 1) + previous_columns = column_range[end - 1]:(column_range[end] - 1) + + if length(row_range) == length(column_range) + B = M[row_block, previous_columns] + F = lu(transpose(B)) + invp = invperm(F.p) + sparse_ns = _nullspace_upper_sparse(sparse(transpose(F.L))) + C[:, previous_columns] .= + (Diagonal(F.Rs) * sparse_ns[invp, :])' + start_block = length(row_range) - 2 + else + for (i, column) in enumerate(previous_columns) + C[i, column] = 1.0 + end + start_block = length(row_range) - 1 + end + + for block in start_block:-1:1 + row_block = row_range[block]:(row_range[block + 1] - 1) + current_columns = column_range[block]:(column_range[block + 1] - 1) + scale = -1 / M[first(row_block), first(current_columns)] + + for (j_local, j_global) in enumerate(previous_columns) + for p in M.colptr[j_global]:(M.colptr[j_global + 1] - 1) + i_global = M.rowval[p] + if first(row_block) <= i_global <= last(row_block) + i_local = i_global - first(row_block) + 1 + value = M.nzval[p] * scale + for k in axes(C, 1) + C[k, current_columns[i_local]] += + value * C[k, previous_columns[j_local]] + end + end + end + end + previous_columns = current_columns + end + + return _truncate_zeros!(cholesky(Symmetric(C * C')).L \ C) +end + +function _embed_onehot(C::AbstractMatrix{T}, mmset::AbstractVector, + ::Val{K}) where {T,K} + rows, columns = size(C) + Vec = SVector{2 * K + 1,T} + result = Matrix{Vec}(undef, rows, columns) + zero_vec = zero(Vec) + for j in 1:columns + component = sum(mmset[j]) + K + 1 + for i in 1:rows + result[i, j] = setindex(zero_vec, C[i, j], component) + end + end + return result +end + +function _empty_coefficients(K::Int, ll::SVector{N,Int}, + nn::SVector{N,Int}) where {N} + lset, nset = _block_multiplicities(nn, ll) + all_mm = _all_mm_blocks(lset, nset, Val(N)) + muset = all_mm[findall(x -> abs(sum(x)) <= K, all_mm)] + sort!(muset, by=sum) + T = K == 0 ? Float64 : SVector{2 * K + 1,Float64} + return Matrix{T}(undef, 0, length(muset)), muset +end + +""" + gepi_coupling_coeffs(K, ll, nn) + +Construct the permutation-invariant, `K`-equivariant coupling coefficients by +the GEPI Lie-algebra kernel/back-substitution method. Rows enumerate independent +couplings and columns correspond to the returned ordered magnetic tuples. +""" +function gepi_coupling_coeffs(K::Integer, ll, nn) + @assert K >= 0 + @assert length(ll) == length(nn) + @assert all(value -> value isa Integer, ll) + @assert all(value -> value isa Integer, nn) + N = length(ll) + ll_static_input = SVector{N,Int}(ll) + nn_static_input = SVector{N,Int}(nn) + + # EquivariantTensors' GE-PI kernel assumes lexicographic ordering by the + # complete `(l, channel)` label. Sort internally and restore the caller's + # factor order in the returned magnetic tuples. + permutation = Vector(sortperm(collect(zip(ll_static_input, nn_static_input)))) + inverse_permutation = invperm(permutation) + ll_static = SVector{N,Int}(ll_static_input[permutation]) + nn_static = SVector{N,Int}(nn_static_input[permutation]) + + if all(iszero, ll_static) && K == 0 + return [1.0;;], [zero(SVector{N,Int})] + end + + if K > sum(ll_static) + coefficients, muset = + _empty_coefficients(Int(K), ll_static, nn_static) + return coefficients, [mu[inverse_permutation] for mu in muset] + end + + M, muset, mmset = _lie_matrix(Int(K), ll_static, nn_static) + if size(M, 1) == size(M, 2) + coefficients, empty_muset = + _empty_coefficients(Int(K), ll_static, nn_static) + return coefficients, + [mu[inverse_permutation] for mu in empty_muset] + end + + C = _solve_kernel(M, mmset, muset) + coefficients = K == 0 ? C : _embed_onehot(C, muset, Val(Int(K))) + return coefficients, [mu[inverse_permutation] for mu in muset] +end + +""" + gepi_coupling_coeffs(K, ll, nn, ss) + +Category-aware GE-PI coupling coefficients. Two factors are permutable only +when their radial labels, categorical labels, and angular momenta are all +identical. This mirrors the EquivariantTensors convention in which the +one-particle channel index contains every non-magnetic label. +""" +function gepi_coupling_coeffs(K::Integer, ll, nn, ss) + return gepi_coupling_coeffs(K, ll, categorical_channel_ids(nn, ss)) +end + +end # module GEPICouplings diff --git a/src/utils/transformations.jl b/src/utils/transformations.jl index 2b58b4e..98cea49 100644 --- a/src/utils/transformations.jl +++ b/src/utils/transformations.jl @@ -336,4 +336,8 @@ function eigen_retraction(D::Matrix, n_pairs::Int) @assert norm(retr_D * retr_D - retr_D, Inf) < 1e-10 @assert norm(retr_D' - retr_D, Inf) < 1e-10 return retr_D -end \ No newline at end of file +end + +# Transform vector-valued irreducible couplings into the real, +# matrix-valued convention used by the density-matrix models. +include("coupling_transformation.jl") diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..2e5cef7 --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,9 @@ +using Test + +@testset "ACEdensitymatrix" begin + include("test_transformations.jl") + include("test_gepi_couplings.jl") + include("test_backend_equivalence.jl") + include("test_sparse_evaluation.jl") + include("test_explicit_spec_constructor.jl") +end diff --git a/test/test_backend_equivalence.jl b/test/test_backend_equivalence.jl new file mode 100644 index 0000000..ef11a66 --- /dev/null +++ b/test/test_backend_equivalence.jl @@ -0,0 +1,94 @@ +module TestBackendEquivalence + +using ACEdensitymatrix +using LinearAlgebra +using StaticArrays +using Test + +const ADM = ACEdensitymatrix +const RANK_RTOL = 1e-8 + +function category_distinct_spec(case) + spec = [ + sort([ + (n=case.nn[1], l=case.ll[1], m=m1, s=case.ss[1]), + (n=case.nn[2], l=case.ll[2], m=m2, s=case.ss[2]), + ]) + for m1 in -case.ll[1]:case.ll[1] + for m2 in -case.ll[2]:case.ll[2] + ] + return unique(spec) +end + +function clebsch_gordan_reference(case, spec) + matrix_type = SMatrix{ + 2case.L1 + 1, + 2case.L2 + 1, + ComplexF64, + } + lambdas = abs(case.L1 - case.L2):(case.L1 + case.L2) + result = fill(zero(matrix_type), length(lambdas), length(spec)) + coupling = ADM.BlockCoupling(case.L1, case.L2) + + for (column, pib) in enumerate(spec) + mm = ADM._canonical_magnetic_tuple(pib) + for (row, lambda) in enumerate(lambdas) + mu = sum(mm) + abs(mu) > lambda && continue + coefficient = ADM.cg( + case.ll[1], mm[1], case.ll[2], mm[2], lambda, mu, + ) + iszero(coefficient) && continue + complex_vector = setindex( + zero(SVector{2lambda + 1,Float64}), + coefficient, + mu + lambda + 1, + ) + real_vector = ADM.complex_to_real_vector(lambda, complex_vector) + result[row, column] = matrix_type( + ADM.transform_λ(coupling, lambda, real_vector), + ) + end + end + return result +end + +function coefficient_gramian(coefficients) + rows, columns = size(coefficients) + gramian = zeros(ComplexF64, rows, rows) + for i in 1:rows, j in i:rows + value = sum(dot(coefficients[i, column], coefficients[j, column]) + for column in 1:columns) + gramian[i, j] = value + gramian[j, i] = conj(value) + end + return Matrix(Hermitian(gramian)) +end + +@testset "old and new coupling spaces" begin + cases = ( + (L1=1, L2=2, nn=(0, 0), ll=(1, 2), ss=(:C, :O)), + (L1=2, L2=2, nn=(0, 0), ll=(2, 2), ss=(:C, :O)), + (L1=2, L2=3, nn=(0, 0), ll=(2, 3), ss=(:C, :O)), + ) + + for case in cases + spec = category_distinct_spec(case) + reference = clebsch_gordan_reference(case, spec) + gepi = Matrix(ADM._gepi_A2B_matrix_real( + ADM.Rot3DCoeffs_loc(case.L1, case.L2), spec, + )) + + @test size(reference) == size(gepi) + @test size(first(reference)) == size(first(gepi)) + + reference_rank = rank(coefficient_gramian(reference); rtol=RANK_RTOL) + gepi_rank = rank(coefficient_gramian(gepi); rtol=RANK_RTOL) + stacked_rank = rank( + coefficient_gramian(vcat(reference, gepi)); rtol=RANK_RTOL, + ) + @test reference_rank == gepi_rank == stacked_rank + end +end + +end diff --git a/test/test_explicit_spec_constructor.jl b/test/test_explicit_spec_constructor.jl new file mode 100644 index 0000000..4af0f6a --- /dev/null +++ b/test/test_explicit_spec_constructor.jl @@ -0,0 +1,90 @@ +module TestExplicitSpecConstructor + +using ACEdensitymatrix +using Test + +const ADM = ACEdensitymatrix + +function coupling_maps(model) + return [layer.op for layer in model.layers.AA2BB.layers] +end + +function coupling_positions(model) + return [layer.pos for layer in model.layers.AA2BB.layers] +end + +function output_dimensions(model) + return [(layer.in_dim, layer.out_dim) for layer in model.layers.dot.layers] +end + +@testset "explicit nn,ll operator constructors" begin + radial = ADM.onsite_radial_basis(2, 4.0) + nn = [0, 0] + ll = [1, 2] + + @testset "rectangular output" begin + L1, L2 = 1, 2 + n_orbs1 = [2, 1] + n_orbs2 = [1, 2, 1] + filter = ADM.RPE_filter_long(L1 + L2) + spec = ADM._close(nn, ll; filter=filter) + + explicit_model, _, _ = ADM.equivariant_operator( + nn, ll, radial, L1, L2, n_orbs1, n_orbs2; + coupling_backend=:new, + ) + reference_model, _, _ = ADM.equivariant_operator( + spec, radial, L1, L2, n_orbs1, n_orbs2; + coupling_backend=:new, + ) + + @test length(explicit_model.layers.AA2BB.layers) == + (L1 + 1) * (L2 + 1) + @test coupling_positions(explicit_model) == + coupling_positions(reference_model) + @test coupling_maps(explicit_model) == coupling_maps(reference_model) + @test output_dimensions(explicit_model) == + output_dimensions(reference_model) + end + + @testset "single-L wrapper" begin + L = 2 + n_orbs = [1, 2, 1] + filter = ADM.RPE_filter_long(2L) + spec = ADM._close(nn, ll; filter=filter) + + wrapped_model, _, _ = ADM.equivariant_operator( + nn, ll, radial, L, n_orbs; coupling_backend=:new, + ) + reference_model, _, _ = ADM.equivariant_operator( + spec, radial, L, L, n_orbs, n_orbs; + coupling_backend=:new, + ) + + @test coupling_positions(wrapped_model) == + coupling_positions(reference_model) + @test coupling_maps(wrapped_model) == coupling_maps(reference_model) + end + + @testset "degree/order constructors use the same full specification" begin + degree, order = 2, 2 + L1, L2 = 1, 2 + n_orbs1 = [2, 1] + n_orbs2 = [1, 2, 1] + + basis_model, _, _, outputs = ADM.equivariant_model_loc( + degree, order, radial, L1, L2; coupling_backend=:new, + ) + operator_model, _, _ = ADM.equivariant_operator( + degree, order, radial, L1, L2, n_orbs1, n_orbs2; + coupling_backend=:new, + ) + + @test outputs == [(l1, l2) for l1 in 0:L1 for l2 in 0:L2] + @test coupling_positions(basis_model) == + coupling_positions(operator_model) + @test coupling_maps(basis_model) == coupling_maps(operator_model) + end +end + +end diff --git a/test/test_gepi_couplings.jl b/test/test_gepi_couplings.jl new file mode 100644 index 0000000..d643116 --- /dev/null +++ b/test/test_gepi_couplings.jl @@ -0,0 +1,44 @@ +module TestGEPICouplings + +using ACEdensitymatrix +using Test + +const ADM = ACEdensitymatrix + +@testset "category-aware GE-PI couplings" begin + cases = ( + (L1=1, L2=2, ll=(1, 2), nn=(0, 0), ss=(:C, :O)), + (L1=2, L2=2, ll=(2, 2), nn=(0, 0), ss=(:C, :O)), + (L1=2, L2=3, ll=(2, 3), nn=(0, 0), ss=(:C, :O)), + ) + + for case in cases + for lambda in abs(case.L1 - case.L2):(case.L1 + case.L2) + coefficients, magnetic_set = + ADM.GEPICouplings.gepi_coupling_coeffs( + lambda, case.ll, case.nn, case.ss, + ) + @test size(coefficients, 1) == 1 + @test size(coefficients, 2) == length(magnetic_set) + end + end + + # Equal categories form a symmetric square. For l=(2,2), only even + # output channels remain, whereas distinct categories retain 0:4. + same_category_rows = [ + size(ADM.GEPICouplings.gepi_coupling_coeffs( + lambda, (2, 2), (0, 0), (:C, :C), + )[1], 1) + for lambda in 0:4 + ] + distinct_category_rows = [ + size(ADM.GEPICouplings.gepi_coupling_coeffs( + lambda, (2, 2), (0, 0), (:C, :O), + )[1], 1) + for lambda in 0:4 + ] + @test same_category_rows == [1, 0, 1, 0, 1] + @test distinct_category_rows == ones(Int, 5) +end + +end diff --git a/test/test_sparse_evaluation.jl b/test/test_sparse_evaluation.jl new file mode 100644 index 0000000..4a1a08d --- /dev/null +++ b/test/test_sparse_evaluation.jl @@ -0,0 +1,30 @@ +module TestSparseEvaluation + +using ACEdensitymatrix +using SparseArrays +using StaticArrays +using Test + +const ADM = ACEdensitymatrix + +@testset "concrete sparse static-matrix evaluation" begin + matrix_values = SMatrix{2,2,Float64}[ + (@SMatrix [1.0 2.0; 3.0 4.0]), + (@SMatrix [-1.0 0.5; 0.25 2.0]), + ] + operator = SparseMatrixCSC( + 2, 2, [1, 2, 3], [1, 2], matrix_values, + ) + input = [2.0, -0.5] + concrete_values = typeof(first(matrix_values)).(matrix_values) + concrete_operator = SparseMatrixCSC( + 2, 2, [1, 2, 3], [1, 2], concrete_values, + ) + reference = concrete_operator * input + + layer = ADM.ConstLinearLayer_loc(operator, [1, 2]) + @test isbitstype(eltype(layer.op)) + @test collect(layer(input)) == reference +end + +end diff --git a/test/test_transformations.jl b/test/test_transformations.jl new file mode 100644 index 0000000..badcd39 --- /dev/null +++ b/test/test_transformations.jl @@ -0,0 +1,35 @@ +module TestCouplingTransformations + +using ACEdensitymatrix +using LinearAlgebra +using SparseArrays +using Test + +const ADM = ACEdensitymatrix + +@testset "coupling transformations" begin + @testset "complex-to-real convention" begin + for L in 0:5 + @test Matrix(ADM.Ctran(L)) ≈ Matrix(ADM.ctran(L)) + @test Matrix(ADM.Ctran(L) * ADM.Ctran(L)') ≈ + Matrix{ComplexF64}(I, 2L + 1, 2L + 1) + end + end + + @testset "vector-to-matrix maps" begin + for (L1, L2) in ((1, 2), (2, 2), (2, 3)) + coupling = ADM.BlockCoupling(L1, L2) + @test coupling.lambdas == abs(L1 - L2):(L1 + L2) + for (index, lambda) in enumerate(coupling.lambdas) + map = coupling.maps[index] + @test issparse(map) + @test size(map) == (2lambda + 1, + (2L1 + 1) * (2L2 + 1)) + @test Matrix(map * transpose(map)) ≈ + Matrix{Float64}(I, 2lambda + 1, 2lambda + 1) + end + end + end +end + +end From c990e406c7c36bcd22a2c7e0b76286b8337cba7f Mon Sep 17 00:00:00 2001 From: Liwei Zhang <68692847+zhanglw0521@users.noreply.github.com> Date: Wed, 30 Sep 2026 21:43:10 +0200 Subject: [PATCH 2/3] CI initialization --- .github/workflows/ci.yml | 40 ++++++++++++++++++++++++++++++++++++++++ Project.toml | 18 ++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3ae2b17 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: CI + +on: + push: + branches: + - main + - new-backend + pull_request: + +permissions: + contents: read + actions: write + +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + name: Julia 1.9 + runs-on: ubuntu-latest + timeout-minutes: 30 + + steps: + - name: Check out repository + uses: actions/checkout@v6 + + - name: Install Julia + uses: julia-actions/setup-julia@v3 + with: + version: "1.9" + + - name: Cache Julia packages + uses: julia-actions/cache@v2 + + - name: Build package + uses: julia-actions/julia-buildpkg@v1 + + - name: Run package tests + uses: julia-actions/julia-runtest@v1 diff --git a/Project.toml b/Project.toml index 4dd6358..017554e 100644 --- a/Project.toml +++ b/Project.toml @@ -2,6 +2,24 @@ name = "ACEdensitymatrix" uuid = "01d2a810-3755-443d-adc8-83bcf7da2e21" version = "0.1.0" +[compat] +ACEbase = "0.4" +ACEfit = "0.3" +DecoratedParticles = "0.0.7" +EquivariantModels = "0.0.5" +HDF5 = "0.17" +JLD2 = "0.6" +LinearOperators = "2" +Lux = "0.5" +LuxCore = "0.1" +ObjectPools = "0.3" +Polynomials4ML = "0.3" +Reexport = "1" +RepLieGroups = "0.0.4" +Setfield = "1" +StaticArrays = "1" +julia = "1.9" + [deps] ACEbase = "14bae519-eb20-449c-a949-9c58ed33163e" ACEfit = "ad31a8ef-59f5-4a01-b543-a85c2f73e95c" From dd41b08ed6ebb1cb78ae1d33b34619f455e4bda8 Mon Sep 17 00:00:00 2001 From: Liwei Zhang <68692847+zhanglw0521@users.noreply.github.com> Date: Wed, 30 Sep 2026 21:50:01 +0200 Subject: [PATCH 3/3] update ACE registration --- .github/workflows/ci.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3ae2b17..e53d72a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,15 @@ jobs: - name: Cache Julia packages uses: julia-actions/cache@v2 + - name: Add package registries + run: | + julia --project=. -e ' + using Pkg + installed = Set(registry.name for registry in Pkg.Registry.reachable_registries()) + "General" in installed || Pkg.Registry.add("General") + "ACE" in installed || Pkg.Registry.add(Pkg.RegistrySpec(url="https://github.com/ACEsuit/ACEregistry.git")) + ' + - name: Build package uses: julia-actions/julia-buildpkg@v1