using JSON3 using HDF5 using GaussianBasis using StaticArrays using Base include("Shells.jl") abstract type ArrayFields end Base.iterate(data::F, state=1) where F <:ArrayFields = begin nF = fieldcount(F) state > nF ? nothing : ((fieldname(F, state), getfield(data, state)), state + 1) end Base.length(data::F) where F <:ArrayFields = fieldcount(F) """ Mono-electronic Integrals. Input wave functions (ψ1, ψ2) are primitive, spherical GTO-shells with unit coefficients, i.e. ψ(C + r) = rˡ ⋅ Yₗₘ(r/|r|) ⋅ exp(-α |r|²) where C is `ψ.center`, α is `ψ.exp`, and the magnetic quantum number m takes all possible values in {-l, ..., l} within each subshell. # Inputs - `xyz` : center of ψ2 (ψ1 is centered at 0) - `l` : pair of angular momenta (l₁, l₂) - `exp` : exponents (α₁, α₂) - `Z` : atomic charges used to compute the nuclear integral. # Targets - `overlap` integrals `S₁₂ = ∫ ψ1 ⋅ ψ2` - `kinetic` integrals `T₁₂ = 1/2 * ∫ ∇ψ1 ⋅ ∇ψ2` - `nuclear` attraction integrals `N₁₂ = ∫ ψ1 ⋅ [(Z₁ / |r|) + (Z₂ / |r - xyz|)] ⋅ ψ2` # Note Mono-electronic integrals are square matrices of shape `D × D` with D = (2 * l1 + 1) + (2 * l2 + 1) Indices correspond to increasing values of `m1 ∈ {-l1, …, l1}` first, then increasing values of `m2 ∈ {-l2, …, l2}`. """ struct MonoIntegral{T} <: ArrayFields l :: Vector{Int64} exp :: Union{SArray, Array{T}} xyz :: Union{SArray, Array{T}} overlap :: Array{T} kinetic :: Array{T} nuclear :: Array{T} Z :: Array{Int64} end """ Object storing 2-electron 2-center integrals. Electronic interactions (ij|ij) and (ij|ji) are quadratic w.r.t. two input wave functions (ψi, ψj), characterized by the same entries as `MonoIntegral`. Targets: - `coulomb` integral J """ struct BiIntegral2c{T} <: ArrayFields l :: Tuple{Integer} exp :: Array{T} xyz :: Array{T} coulomb :: Array{T} end """Object for storing bi-electronic integrals""" struct BiIntegral4c{T} <: ArrayFields l :: Vector{Int64} exp :: Array{T} xyz :: Array{T} ijkl :: Array{Int16} Bijkl :: Array{Float32} target_size :: Vector{Int32} end function BiIntegral4c( l :: Vector{Int64}, exp :: Array{T}, xyz :: Array{T}, ijkl :: Array{Int16}, Bijkl :: Array{Float32} ) where T<:Real target_size :: Array{Int32} = [length(Bijkl)] BiIntegral4c{T}(l, exp, xyz, ijkl, Bijkl, target_size) end "Stack array fields, excluding constant fields" function stack(rows::Vector{F}, exclude::Vector{Symbol}) where F<:ArrayFields out = [] for f in fieldnames(F) out_f = f ∈ exclude ? getfield(rows[1], f) : Base.stack([getfield(row, f) for row in rows]) push!(out, out_f) end F(out...) end function stack(rows::Vector{F}) where F<:ArrayFields stack(rows, Symbol[]) end function stack(rows::Vector{MonoIntegral}) :: MonoIntegral stack(rows, [:l]) end function stack(rows::Vector{BiIntegral4c}) :: BiIntegral4c out = map(rows[1]) do field k, fk = field if k ∈ (:ijkl, :Bijkl, :target_size) reduce(vcat, map(r -> getfield(r, k), rows)) elseif k == :l rows[1].l else Base.stack(map(r -> getfield(r, k), rows)) end end BiIntegral4c(out...) end "Dump JSON output" function JSONdump(out::String, dset::Any) open(out, "w") do io JSON3.pretty(io, dset) end end "Dump HDF5 output" function h5dump(out::String, dset::F) where F<:ArrayFields h5open(out, "w") do io foreach(dset) do (k, xk) T = eltype(xk) S = isa(xk, AbstractArray) ? size(xk) : (length(xk),) dset = create_dataset(io, String(k), datatype(T), S) write(dset, xk) end end end """ mono_integral(basis::Basis) Compute a dense mono-electronic integral matrix. """ function mono_integral(basis::Basis) mol, shells = basis bset = BasisSet("A-B*", mol, shells) l = [shell.l for shell in shells] a1, a2 = bset[1].exp, bset[2].exp xyz = bset[2].atom.xyz S = overlap(bset) T = kinetic(bset) N = nuclear(bset) Z = [bset[1].atom.Z, bset[2].atom.Z] MonoIntegral(l, [a1; a2], xyz, S, T, N, Z) end """ bi_integral(basis::Basis[, cutoff=.000_01]) Compute a sparse bi-electronic integral matrix. """ function bi_integral(basis::Basis, cutoff::Float64 = .000_01) # initialize basis set mol, shells = basis bset = BasisSet("mol", mol, shells) l = [shell.l for shell in shells] # compute integrals B = sparseERI_2e4c(bset, .000_1) if length(B[1]) >= 1 ijkl, Bijkl = Base.stack(B[1], dims=1), Vector{Float32}(B[2]) else println("no ERI") ijkl, Bijkl = Array{Int16}([1, 1, 1, 1]'), Vector{Float32}([0.]) end exp = Base.stack(map(shell -> shell.exp, bset.basis)) xyz = Base.stack(map(shell -> shell.atom.xyz, bset.basis)) BiIntegral4c(l, exp, xyz, ijkl, Bijkl) end