Datasets:

Modalities:
Image
Size:
< 1K
Libraries:
Datasets
License:
opeltre commited on
Commit
0587dae
·
1 Parent(s): d4fc45a

add Integrals.jl

Browse files
Files changed (1) hide show
  1. generation/Integrals.jl +191 -0
generation/Integrals.jl ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using JSON3
2
+ using HDF5
3
+ using GaussianBasis
4
+ using StaticArrays
5
+ using Base
6
+
7
+ include("Shells.jl")
8
+
9
+ abstract type ArrayFields end
10
+
11
+ Base.iterate(data::F, state=1) where F <:ArrayFields = begin
12
+ nF = fieldcount(F)
13
+ state > nF ?
14
+ nothing :
15
+ ((fieldname(F, state), getfield(data, state)), state + 1)
16
+ end
17
+
18
+ Base.length(data::F) where F <:ArrayFields = fieldcount(F)
19
+
20
+ """
21
+ Mono-electronic Integrals.
22
+
23
+ Input wave functions (ψ1, ψ2) are primitive, spherical GTO-shells
24
+ with unit coefficients, i.e.
25
+
26
+ ψ(C + r) = rˡ ⋅ Yₗₘ(r/|r|) ⋅ exp(-α |r|²)
27
+
28
+ where C is `ψ.center`, α is `ψ.exp`, and the magnetic quantum number m
29
+ takes all possible values in {-l, ..., l} within each subshell.
30
+
31
+ # Inputs
32
+ - `xyz` : center of ψ2 (ψ1 is centered at 0)
33
+ - `l` : pair of angular momenta (l₁, l₂)
34
+ - `exp` : exponents (α₁, α₂)
35
+ - `Z` : atomic charges used to compute the nuclear integral.
36
+
37
+ # Targets
38
+ - `overlap` integrals `S₁₂ = ∫ ψ1 ⋅ ψ2`
39
+ - `kinetic` integrals `T₁₂ = 1/2 * ∫ ∇ψ1 ⋅ ∇ψ2`
40
+ - `nuclear` attraction integrals
41
+
42
+ `N₁₂ = ∫ ψ1 ⋅ [(Z₁ / |r|) + (Z₂ / |r - xyz|)] ⋅ ψ2`
43
+
44
+ # Note
45
+
46
+ Mono-electronic integrals are square matrices of shape `D × D` with
47
+
48
+ D = (2 * l1 + 1) + (2 * l2 + 1)
49
+
50
+ Indices correspond to increasing values of `m1 ∈ {-l1, …, l1}` first,
51
+ then increasing values of `m2 ∈ {-l2, …, l2}`.
52
+ """
53
+ struct MonoIntegral{T} <: ArrayFields
54
+ l :: Vector{Int64}
55
+ exp :: Union{SArray, Array{T}}
56
+ xyz :: Union{SArray, Array{T}}
57
+ overlap :: Array{T}
58
+ kinetic :: Array{T}
59
+ nuclear :: Array{T}
60
+ Z :: Array{Int64}
61
+ end
62
+
63
+ """
64
+ Object storing 2-electron 2-center integrals.
65
+
66
+ Electronic interactions (ij|ij) and (ij|ji) are quadratic
67
+ w.r.t. two input wave functions (ψi, ψj), characterized
68
+ by the same entries as `MonoIntegral`.
69
+
70
+ Targets:
71
+ - `coulomb` integral J
72
+ """
73
+ struct BiIntegral2c{T} <: ArrayFields
74
+ l :: Tuple{Integer}
75
+ exp :: Array{T}
76
+ xyz :: Array{T}
77
+ coulomb :: Array{T}
78
+ end
79
+
80
+ """Object for storing bi-electronic integrals"""
81
+ struct BiIntegral4c{T} <: ArrayFields
82
+ l :: Vector{Int64}
83
+ exp :: Array{T}
84
+ xyz :: Array{T}
85
+ ijkl :: Array{Int16}
86
+ Bijkl :: Array{Float32}
87
+ index :: Vector{Int16}
88
+ end
89
+ function BiIntegral4c(
90
+ l :: Vector{Int64},
91
+ exp :: Array{T},
92
+ xyz :: Array{T},
93
+ ijkl :: Array{Int16},
94
+ Bijkl :: Array{Float32}
95
+ ) where T<:Real
96
+ index :: Array{Int16} = fill(0, size(Bijkl))
97
+ BiIntegral4c{T}(l, exp, xyz, ijkl, Bijkl, index)
98
+ end
99
+
100
+ "Stack array fields, excluding constant fields"
101
+ function stack(rows::Vector{F}, exclude::Vector{Symbol}) where F<:ArrayFields
102
+ out = []
103
+ for f in fieldnames(F)
104
+ out_f = f ∈ exclude ?
105
+ getfield(rows[1], f) :
106
+ Base.stack([getfield(row, f) for row in rows])
107
+ push!(out, out_f)
108
+ end
109
+ F(out...)
110
+ end
111
+ function stack(rows::Vector{F}) where F<:ArrayFields
112
+ stack(rows, Symbol[])
113
+ end
114
+ function stack(rows::Vector{MonoIntegral}) :: MonoIntegral
115
+ stack(rows, [:l])
116
+ end
117
+ function stack(rows::Vector{BiIntegral4c}) :: BiIntegral4c
118
+ out = map(rows[1]) do field
119
+ k, fk = field
120
+ if k ∈ (:ijkl, :Bijkl)
121
+ reduce(vcat, map(r -> getfield(r, k), rows))
122
+ elseif k == :index
123
+ idx(r::BiIntegral4c, i::Int) :: Vector{Int16} = i .+ r.index
124
+ reduce(vcat, map(idx, rows, 1:length(rows)))
125
+ elseif k == :l
126
+ rows[1].l
127
+ else
128
+ Base.stack(map(r -> getfield(r, k), rows))
129
+ end
130
+ end
131
+ BiIntegral4c(out...)
132
+ end
133
+
134
+ "Dump JSON output"
135
+ function JSONdump(out::String, dset::Any)
136
+ open(out, "w") do io
137
+ JSON3.pretty(io, dset)
138
+ end
139
+ end
140
+
141
+ "Dump HDF5 output"
142
+ function h5dump(out::String, dset::F) where F<:ArrayFields
143
+ h5open(out, "w") do io
144
+ foreach(dset) do (k, xk)
145
+ T = eltype(xk)
146
+ S = isa(xk, AbstractArray) ? size(xk) : (length(xk),)
147
+ dset = create_dataset(io, String(k), datatype(T), S)
148
+ write(dset, xk)
149
+ end
150
+ end
151
+ end
152
+
153
+ """
154
+ mono_integral(basis::Basis)
155
+
156
+ Compute a dense mono-electronic integral matrix.
157
+ """
158
+ function mono_integral(basis::Basis)
159
+ mol, shells = basis
160
+ bset = BasisSet("A-B*", mol, shells)
161
+ l = [shell.l for shell in shells]
162
+ a1, a2 = bset[1].exp, bset[2].exp
163
+ xyz = bset[2].atom.xyz
164
+ S = overlap(bset)
165
+ T = kinetic(bset)
166
+ N = nuclear(bset)
167
+ Z = [bset[1].atom.Z, bset[2].atom.Z]
168
+ MonoIntegral(l, [a1; a2], xyz, S, T, N, Z)
169
+ end
170
+
171
+ """
172
+ bi_integral(basis::Basis[, cutoff=.000_1])
173
+
174
+ Compute a sparse bi-electronic integral matrix."""
175
+ function bi_integral(basis::Basis, cutoff::Float64 = .000_1)
176
+ # initialize basis set
177
+ mol, shells = basis
178
+ bset = BasisSet("mol", mol, shells)
179
+ l = [shell.l for shell in shells]
180
+ # compute integrals
181
+ B = sparseERI_2e4c(bset, .000_1)
182
+ if length(B[1]) >= 1
183
+ ijkl, Bijkl = Base.stack(B[1], dims=1), Vector{Float32}(B[2])
184
+ else
185
+ println("no ERI")
186
+ ijkl, Bijkl = Array{Int16}([1, 1, 1, 1]'), Vector{Float32}([0.])
187
+ end
188
+ exp = Base.stack(map(shell -> shell.exp, bset.basis))
189
+ xyz = Base.stack(map(shell -> shell.atom.xyz, bset.basis))
190
+ BiIntegral4c(l, exp, xyz, ijkl, Bijkl)
191
+ end