diff --git a/src/model_construction.jl b/src/model_construction.jl index e538eaa..24de3cd 100644 --- a/src/model_construction.jl +++ b/src/model_construction.jl @@ -112,11 +112,15 @@ function classify_ao_dict_off(ao_dict::Dict{TP, Dict{String, Any}}) where TP end """ - Density_Model(ao_dict::Dict{TP, Dict{String, Any}}) where TP -> Density_Model + Density_Model(ao_dict::Dict{TP, Dict{String, Any}}; + coupling_backend=:old, fixed_particle_number=false) where TP # Arguments - `ao_dict`: a dictionary containing all the metadata needed for constructing a Density_Model including chemical types, number of orbitals, and cutoffs +- `coupling_backend`: coupling-coefficient construction, either `:old` or `:new` +- `fixed_particle_number`: remove higher-order AA specifications obtained by padding + lower-order specifications with fixed-count zero channels # Examples ao_dict = Dict( 1 => Dict("n_orbs" => [2], "maxdeg" => degree, "ord" => order, "rcut" => rcut, "zcut" => zcut), @@ -128,7 +132,7 @@ 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}}; coupling_backend::Symbol = :old) 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, fixed_particle_number::Bool = false) 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) @@ -141,26 +145,26 @@ function Density_Model(ao_dict::Dict{TP, Dict{String, Any}}; coupling_backend::S 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"]; coupling_backend = coupling_backend)) + 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, fixed_particle_number = fixed_particle_number)) 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)], "AA2BBpos" => [ dict[zs[1]].model.layers.AA2BB.layers[i].pos for i = 1:length(dict[zs[1]].model.layers.AA2BB.layers)]) for k = 2:length(zs) - push!(dict, zs[k] => On_Model(ao_dict[zs[k]]["maxdeg"], ao_dict[zs[k]]["ord"], ao_dict[zs[k]][onsite_cutoff], zs[k], Zs, length(ao_dict[zs[k]]["n_orbs"])-1, ao_dict[zs[k]]["n_orbs"], AA2BB = AA2BB)) + push!(dict, zs[k] => On_Model(ao_dict[zs[k]]["maxdeg"], ao_dict[zs[k]]["ord"], ao_dict[zs[k]][onsite_cutoff], zs[k], Zs, length(ao_dict[zs[k]]["n_orbs"])-1, ao_dict[zs[k]]["n_orbs"], AA2BB = AA2BB, coupling_backend = coupling_backend, fixed_particle_number = fixed_particle_number)) end end end 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"]; coupling_backend = coupling_backend)) + 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, fixed_particle_number = fixed_particle_number)) 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)], "AA2BBpos" => [ dict[zs[1]].model.layers.AA2BB.layers[i].pos for i = 1:length(dict[zs[1]].model.layers.AA2BB.layers)]) for k = 2:length(zs) - push!(dict, zs[k] => Off_Model(maximum([ao_dict[zs[k][1]]["maxdeg"],ao_dict[zs[k][2]]["maxdeg"]]), maximum([ao_dict[zs[k][1]]["ord"],ao_dict[zs[k][2]]["ord"]]), ao_dict[zs[k][1]][offsite_cutoff], ao_dict[zs[k][2]][offsite_cutoff], maximum([ao_dict[zs[k][1]]["zcut"], ao_dict[zs[k][2]]["zcut"]]), zs[k][1], zs[k][2], Zs, length(ao_dict[zs[k][1]]["n_orbs"])-1, length(ao_dict[zs[k][2]]["n_orbs"])-1, ao_dict[zs[k][1]]["n_orbs"], ao_dict[zs[k][2]]["n_orbs"], AA2BB = AA2BB) ) + push!(dict, zs[k] => Off_Model(maximum([ao_dict[zs[k][1]]["maxdeg"],ao_dict[zs[k][2]]["maxdeg"]]), maximum([ao_dict[zs[k][1]]["ord"],ao_dict[zs[k][2]]["ord"]]), ao_dict[zs[k][1]][offsite_cutoff], ao_dict[zs[k][2]][offsite_cutoff], maximum([ao_dict[zs[k][1]]["zcut"], ao_dict[zs[k][2]]["zcut"]]), zs[k][1], zs[k][2], Zs, length(ao_dict[zs[k][1]]["n_orbs"])-1, length(ao_dict[zs[k][2]]["n_orbs"])-1, ao_dict[zs[k][1]]["n_orbs"], ao_dict[zs[k][2]]["n_orbs"], AA2BB = AA2BB, coupling_backend = coupling_backend, fixed_particle_number = fixed_particle_number) ) end end end @@ -272,8 +276,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, 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) +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, fixed_particle_number::Bool = false) 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, fixed_particle_number = fixed_particle_number)..., SVector{Lmax+1}(n_orbs),false) # get the categories of a offsite state @@ -286,11 +290,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, 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, 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, fixed_particle_number::Bool = false) 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, fixed_particle_number = fixed_particle_number)..., 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) +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, fixed_particle_number::Bool = false) 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, fixed_particle_number = fixed_particle_number)..., 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 diff --git a/src/utils/extended_eqm.jl b/src/utils/extended_eqm.jl index 3aa0b9c..74d7188 100644 --- a/src/utils/extended_eqm.jl +++ b/src/utils/extended_eqm.jl @@ -521,7 +521,21 @@ 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, rSH = false) + +_is_zero_channel(b) = b.n == 0 && b.l == 0 && b.m == 0 + +function _remove_redundant_zero_padding(AAspec) + available = Set(Tuple(bb) for bb in AAspec) + return filter(AAspec) do bb + length(bb) == 1 && return true + !any(eachindex(bb)) do i + _is_zero_channel(bb[i]) && + Tuple(bb[j] for j in eachindex(bb) if j != i) in available + end + end +end + +function degord2spec_loc(radial::Radial_basis; totaldegree, order, Lmax, catagories = [], filtered_extension = simple_extension, wL = 1, rSH = false, fixed_particle_number::Bool = false) # Rn = radial.radial_basis(totaldegree) if typeof(totaldegree) == Int64 totaldegree = repeat([totaldegree], order) @@ -557,6 +571,9 @@ function degord2spec_loc(radial::Radial_basis; totaldegree, order, Lmax, catagor if !isempty(catagories) AAspec = filtered_extension(AAspec, catagories) end + if fixed_particle_number + AAspec = _remove_redundant_zero_padding(AAspec) + end Aspec = specnlm2spec1p(AAspec)[1] return Aspec, AAspec # Aspecgetspecnlm(spec1p, spec) end @@ -630,8 +647,8 @@ equivariant_model_loc(spec_nlm, radial::Radial_basis, L::Int64; categories=[], _ 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, 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) +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, fixed_particle_number::Bool = false) = + equivariant_model_loc(degord2spec_loc(radial; totaldegree = totdeg, order = ν, Lmax = L1+L2, catagories = categories, filtered_extension = cat_extension, fixed_particle_number = fixed_particle_number)[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) @@ -640,8 +657,8 @@ equivariant_model_loc(nn::Vector{Int64}, ll::Vector{Int64}, radial::Radial_basis 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, 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(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, fixed_particle_number::Bool = false) = + equivariant_model_loc(totdeg, ν, radial, L, L; categories, _get_cat, AA2BB, d, group, isState, isreal, tuned_filter, coupling_backend, fixed_particle_number) 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) @@ -670,12 +687,12 @@ 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, 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, 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) +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, fixed_particle_number::Bool = false) + equivariant_operator(degord2spec_loc(radial; totaldegree = totdeg, order = ν, Lmax = L1+L2, catagories = categories, filtered_extension = cat_extension, fixed_particle_number = fixed_particle_number)[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, 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) +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, fixed_particle_number::Bool = false) = + 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, fixed_particle_number = fixed_particle_number) 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) diff --git a/test/runtests.jl b/test/runtests.jl index 7053ee0..645fff5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,6 +6,7 @@ using Test include("test_backend_equivalence.jl") include("test_sparse_evaluation.jl") include("test_explicit_spec_constructor.jl") + include("test_fixed_particle_specs.jl") include("test_mwe.jl") include("test_reorder.jl") end diff --git a/test/test_fixed_particle_specs.jl b/test/test_fixed_particle_specs.jl new file mode 100644 index 0000000..16a66e7 --- /dev/null +++ b/test/test_fixed_particle_specs.jl @@ -0,0 +1,180 @@ +module TestFixedParticleSpecs + +using ACEdensitymatrix +using Test + +const ADM = ACEdensitymatrix + +spec_key(bb) = Tuple(bb) +without_index(bb, i) = Tuple(bb[j] for j in eachindex(bb) if j != i) + +function removable_zero(bb, available) + for i in eachindex(bb) + if ADM._is_zero_channel(bb[i]) && without_index(bb, i) in available + return i, without_index(bb, i) + end + end + return nothing +end + +function test_fixed_count_redundancy(full, reduced, counts) + full_by_key = Dict(spec_key(bb) => bb for bb in full) + full_keys = Set(keys(full_by_key)) + reduced_keys = Set(spec_key(bb) for bb in reduced) + removed = [bb for bb in full if spec_key(bb) ∉ reduced_keys] + + @test !isempty(removed) + @test reduced_keys == Set(spec_key(bb) for bb in ADM._remove_redundant_zero_padding(full)) + @test all(isnothing(removable_zero(bb, full_keys)) for bb in reduced) + + channel_values = Dict{Any, ComplexF64}() + next_value = 1 + for bb in full, b in bb + haskey(channel_values, b) && continue + if ADM._is_zero_channel(b) + channel_values[b] = counts[b.s] + else + channel_values[b] = complex(next_value, next_value / 7) + next_value += 1 + end + end + feature(bb) = prod(channel_values[b] for b in bb) + + missing_relations = [ + bb for bb in removed if isnothing(removable_zero(bb, full_keys)) + ] + @test isempty(missing_relations) + + failed_identities = Any[] + for bb in removed + relation = removable_zero(bb, full_keys) + isnothing(relation) && continue + i, lower_key = relation + lhs = feature(bb) + rhs = counts[bb[i].s] * feature(full_by_key[lower_key]) + isapprox(lhs, rhs) || push!(failed_identities, + (spec=bb, lower=full_by_key[lower_key], lhs=lhs, rhs=rhs)) + end + @test isempty(failed_identities) +end + +@testset "fixed-particle AA specifications" begin + degree = 3 + order = 3 + + @testset "default path is unchanged" begin + radial = ADM.onsite_radial_basis(degree, 4.0) + categories = [(6, 1), (6, 6)] + default = ADM.degord2spec_loc( + radial; totaldegree=degree, order=order, Lmax=2, + catagories=categories, + ) + explicit_false = ADM.degord2spec_loc( + radial; totaldegree=degree, order=order, Lmax=2, + catagories=categories, fixed_particle_number=false, + ) + @test default == explicit_false + + model_default, ps_default, _ = ADM.equivariant_operator( + 2, 2, radial, 1, [1, 1]; categories=categories, + coupling_backend=:new, + ) + model_false, ps_false, _ = ADM.equivariant_operator( + 2, 2, radial, 1, [1, 1]; categories=categories, + coupling_backend=:new, fixed_particle_number=false, + ) + @test model_default.layers.A.basis.spec == model_false.layers.A.basis.spec + @test model_default.layers.AA.basis.specs == model_false.layers.AA.basis.specs + @test ps_default == ps_false + for (default_layer, false_layer) in zip( + model_default.layers.AA2BB.layers, model_false.layers.AA2BB.layers, + ) + @test default_layer.pos == false_layer.pos + @test default_layer.op == false_layer.op + end + end + + @testset "onsite zero padding is redundant at fixed counts" begin + radial = ADM.onsite_radial_basis(degree, 4.0) + categories = [(6, 1), (6, 6)] + _, full = ADM.degord2spec_loc( + radial; totaldegree=degree, order=order, Lmax=2, + catagories=categories, + ) + _, reduced = ADM.degord2spec_loc( + radial; totaldegree=degree, order=order, Lmax=2, + catagories=categories, fixed_particle_number=true, + ) + + @test length(reduced) < length(full) + test_fixed_count_redundancy( + full, reduced, Dict((6, 1) => 4.0, (6, 6) => 2.0), + ) + end + + @testset "offsite bond anchors are retained" begin + radial = ADM.offsite_radial_basis(degree, 4.0, 10.0) + categories = [ + (6, 8, 8, true), + (6, 8, 1, false), + (6, 8, 6, false), + (6, 8, 8, false), + ] + spec_args = ( + totaldegree=degree, + order=order, + Lmax=2, + catagories=categories, + filtered_extension=ADM.ModelConstruction.offsite_extension, + ) + _, full = ADM.degord2spec_loc(radial; spec_args...) + _, reduced = ADM.degord2spec_loc( + radial; spec_args..., fixed_particle_number=true, + ) + + @test length(reduced) < length(full) + @test all(sum(b.s[4] for b in bb) == 1 for bb in reduced) + @test any(reduced) do bb + any(ADM._is_zero_channel(b) && b.s[4] for b in bb) && + isnothing(removable_zero(bb, Set(spec_key.(full)))) + end + test_fixed_count_redundancy( + full, + reduced, + Dict( + (6, 8, 8, true) => 1.0, + (6, 8, 1, false) => 4.0, + (6, 8, 6, false) => 2.0, + (6, 8, 8, false) => 1.0, + ), + ) + end + + @testset "Density_Model propagates the flag" begin + ao_dict = Dict( + 1 => Dict( + "n_orbs" => [1], "maxdeg" => 2, "ord" => 2, + "rcut" => 4.0, "zcut" => 10.0, + ), + 6 => Dict( + "n_orbs" => [1], "maxdeg" => 2, "ord" => 2, + "rcut" => 4.0, "zcut" => 10.0, + ), + ) + full = Density_Model(ao_dict; coupling_backend=:new) + reduced = Density_Model( + ao_dict; coupling_backend=:new, fixed_particle_number=true, + ) + + for key in keys(full.Models) + full_count = sum(length, full.Models[key].model.layers.AA.basis.specs) + reduced_count = sum(length, reduced.Models[key].model.layers.AA.basis.specs) + @test reduced_count < full_count + for layer in reduced.Models[key].model.layers.AA2BB.layers + @test isempty(layer.pos) || maximum(layer.pos) <= reduced_count + end + end + end +end + +end