Datasets:

Modalities:
Image
Size:
< 1K
Libraries:
Datasets
License:
File size: 5,086 Bytes
0587dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93423ab
0587dae
 
 
 
 
 
 
 
93423ab
 
0587dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93423ab
0587dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27ad58c
0587dae
27ad58c
 
 
0587dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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