Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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: 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

- name: Run package tests
uses: julia-actions/julia-runtest@v1
24 changes: 24 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,3 +39,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"]
7 changes: 6 additions & 1 deletion src/ACEdensitymatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -23,4 +28,4 @@ include("io.jl")
include("utils/model_validation.jl")
@reexport using ACEdensitymatrix.ModelValidation

end # module
end # module
21 changes: 11 additions & 10 deletions src/model_construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)],
Expand All @@ -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)],
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -354,4 +355,4 @@ end

include("tuned_models.jl")

end # module
end # module
116 changes: 116 additions & 0 deletions src/utils/coupling_transformation.jl
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading