-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinterface.jl
More file actions
250 lines (216 loc) · 8.88 KB
/
interface.jl
File metadata and controls
250 lines (216 loc) · 8.88 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import ForwardDiff: Dual
# TODO Based on this type do some generic things like use spin-scaling relations
# Note: Kind is needed because GGA enhancement or spin scaling etc. work differently
# for exchange and correlation ... if it does not help, remove it again
abstract type Functional{Family,Kind} end
"""Return the family of a functional. Results are `:lda`, `:gga`, `:mgga` and
`:mggal` (Meta-GGA requiring Laplacian of ρ)"""
family(::Functional{F}) where {F} = F
"""
Return the functional kind: `:x` (exchange), `:c` (correlation), `:k` (kinetic) or
`:xc` (exchange and correlation combined)
"""
kind(::Functional{F,K}) where {F,K} = K
"""
Return the identifier corresponding to a functional, if available,
and `nothing` otherwise.
"""
function identifier end
function Base.show(io::IO, fun::Functional)
id = identifier(fun)
if isnothing(id)
Base.show_default(io, fun)
else
print(io, id)
end
end
@doc raw"""
True if the functional needs ``σ = 𝛁ρ ⋅ 𝛁ρ``.
"""
needs_σ(::Functional{F}) where {F} = (F in (:gga, :mgga, :mggal))
@doc raw"""
True if the functional needs ``τ`` (kinetic energy density).
"""
needs_τ(::Functional{F}) where {F} = (F in (:mgga, :mggal))
@doc raw"""
True if the functional needs ``Δ ρ``.
"""
needs_Δρ(::Functional{F}) where {F} = (F in (:mggal,))
"""
Does this functional support energy evaluations? Some don't, in which case
energy terms will not be returned by `potential_terms` and `kernel_terms`,
i.e. `e` will be `false` (a strong zero).
"""
has_energy(::Functional) = true
"""
The type of the parameters of the functional. If the functional has multiple parameters,
the result of `promote_type(...)` of the parameter types should be returned.
This allows the functional to influence the computed type, for example if it contains
Dual numbers.
"""
parameters_type(::Functional) = Bool # default: Bool, which promotes to any number type
# TODO These values are read-only for now and their defaults hard-coded for Float64
"""
Threshold for the density (below this value, functionals and derivatives
evaluate to zero). The threshold may depend on the floating-point type used
to represent densities and potentials, which is passed as the second argument.
"""
threshold_ρ(::Functional, T=Float64) = T(1e-15) # TODO This might differ between functionals
threshold_σ(f::Functional, T=Float64) = threshold_ρ(f, T)^(4 // 3)
threshold_τ(::Functional, T=Float64) = T(1e-20)
threshold_ζ(::Functional, T=Float64) = eps(T)
# Drop dual types from threshold functions
threshold_ρ(f::Functional, T::Type{<:Dual}) = threshold_ρ(f, ForwardDiff.valtype(T))
threshold_σ(f::Functional, T::Type{<:Dual}) = threshold_σ(f, ForwardDiff.valtype(T))
threshold_τ(f::Functional, T::Type{<:Dual}) = threshold_τ(f, ForwardDiff.valtype(T))
threshold_ζ(f::Functional, T::Type{<:Dual}) = threshold_ζ(f, ForwardDiff.valtype(T))
# Silently drop extra arguments from evaluation functions
for fun in (:potential_terms, :kernel_terms)
@eval begin
$fun(func::Functional{:lda}, ρ, σ, args...) = $fun(func, ρ)
$fun(func::Functional{:gga}, ρ, σ, τ, args...) = $fun(func, ρ, σ)
$fun(func::Functional{:mgga}, ρ, σ, τ, Δρ, args...) = $fun(func, ρ, σ, τ)
end
end
@doc raw"""
potential_terms(f::Functional, ρ, [σ, τ, Δρ])
Evaluate energy and potential terms at a real-space grid of densities, density
derivatives etc. Not required derivatives for the functional type will be ignored.
Returns a named tuple with keys `e` (Energy per unit volume),
`Vρ` (``\frac{∂e}{∂ρ}``), `Vσ` (``\frac{∂e}{∂σ}``),
`Vτ` (``\frac{∂e}{∂τ}``), `Vl` (``\frac{∂e}{∂(Δρ)}``).
"""
function potential_terms end
@doc raw"""
kernel_terms(f::Functional, ρ, [σ, τ, Δρ])
Evaluate energy, potential and kernel terms at a real-space grid of densities, density
derivatives etc. Not required derivatives for the functional type will be ignored.
Returns a named tuple with the same keys as `potential_terms` and additionally
second-derivative cross terms such as `Vρσ` (``\frac{∂^2e}{∂ρ∂σ}``).
"""
function kernel_terms end
#
# LDA
#
function potential_terms(func::Functional{:lda}, ρ::AbstractMatrix{T}) where {T}
@assert has_energy(func) # Otherwise custom implementation of this function needed
s_ρ, n_p = size(ρ)
TT = arithmetic_type(func, T)
e = similar(ρ, TT, n_p)
Vρ = similar(ρ, TT, s_ρ, n_p)
@views for i = 1:n_p
potential_terms!(e[i:i], Vρ[:, i], func, ρ[:, i])
end
(; e, Vρ)
end
function potential_terms!(e, Vρ, func::Functional{:lda}, ρ::AbstractVector{T}) where {T}
res = ForwardDiff.gradient!(DiffResults.DiffResult(zero(eltype(e)), Vρ),
ρ -> energy(func, ρ), ρ)
e .= DiffResults.value(res)
nothing
end
function kernel_terms(func::Functional{:lda}, ρ::AbstractMatrix{T}) where {T}
@assert has_energy(func)
s_ρ, n_p = size(ρ)
TT = arithmetic_type(func, T)
e = similar(ρ, TT, n_p)
Vρ = similar(ρ, TT, s_ρ, n_p)
Vρρ = similar(ρ, TT, s_ρ, s_ρ, n_p)
# TODO Needed to make forward-diff work with !isbits floating-point types (e.g. BigFloat)
Vρ .= zero(T)
Vρρ .= zero(T)
@views for i = 1:n_p
kernel_terms!(e[i:i], Vρ[:, i], Vρρ[:, :, i], func, ρ[:, i])
end
(; e, Vρ, Vρρ)
end
function kernel_terms!(e, Vρ, Vρρ, func::Functional{:lda}, ρ::AbstractVector{T}) where {T}
res = ForwardDiff.hessian!(DiffResults.DiffResult(zero(eltype(e)), Vρ, Vρρ),
ρ -> energy(func, ρ), ρ)
e .= DiffResults.value(res)
nothing
end
function energy(func::Functional{:lda}, ρ::AbstractVector{T}) where {T}
length(ρ) == 1 || error("Multiple spins not yet implemented for fallback functionals")
ρtotal = ρ[1]
if ρtotal ≤ threshold_ρ(func, T)
zero(T)
else
energy(func, ρtotal)
end
end
#
# GGA
#
function potential_terms(func::Functional{:gga}, ρ::AbstractMatrix{T},
σ::AbstractMatrix{U}) where {T,U}
@assert has_energy(func) # Otherwise custom implementation of this function needed
s_ρ, n_p = size(ρ)
s_σ = size(σ, 1)
TT = arithmetic_type(func, T, U)
e = similar(ρ, TT, n_p)
Vρ = similar(ρ, TT, s_ρ, n_p)
Vσ = similar(ρ, TT, s_σ, n_p)
@views for i = 1:n_p
potential_terms!(e[i:i], Vρ[:, i], Vσ[:, i], func, ρ[:, i], σ[:, i])
end
(; e, Vρ, Vσ)
end
function potential_terms!(e, Vρ, Vσ, func::Functional{:gga},
ρ::AbstractVector, σ::AbstractVector)
res = ForwardDiff.gradient!(DiffResults.DiffResult(zero(eltype(e)), Vρ),
ρ -> energy(func, ρ, σ), ρ)
ForwardDiff.gradient!(DiffResults.DiffResult(zero(eltype(e)), Vσ),
σ -> energy(func, ρ, σ), σ)
e .= DiffResults.value(res)
nothing
end
function kernel_terms(func::Functional{:gga}, ρ::AbstractMatrix{T},
σ::AbstractMatrix{U}) where {T,U}
@assert has_energy(func) # Otherwise custom implementation of this function needed
s_ρ, n_p = size(ρ)
s_σ = size(σ, 1)
TT = arithmetic_type(func, T, U)
e = similar(ρ, TT, n_p)
Vρ = similar(ρ, TT, s_ρ, n_p)
Vσ = similar(ρ, TT, s_σ, n_p)
Vρρ = similar(ρ, TT, s_ρ, s_ρ, n_p)
Vρσ = similar(ρ, TT, s_ρ, s_σ, n_p)
Vσσ = similar(ρ, TT, s_σ, s_σ, n_p)
# TODO Needed to make forward-diff work with !isbits floating-point types (e.g. BigFloat)
Vρ .= zero(TT)
Vσ .= zero(TT)
Vρρ .= zero(TT)
Vρσ .= zero(TT)
Vσσ .= zero(TT)
@views for i = 1:n_p
kernel_terms!(e[i:i], Vρ[:, i], Vσ[:, i],
Vρρ[:, :, i], Vρσ[:, :, i], Vσσ[:, :, i],
func, ρ[:, i], σ[:, i])
end
(; e, Vρ, Vσ, Vρρ, Vρσ, Vσσ)
end
function kernel_terms!(e, Vρ, Vσ, Vρρ, Vρσ, Vσσ, func::Functional{:gga},
ρ::AbstractVector, σ::AbstractVector)
res = ForwardDiff.hessian!(DiffResults.DiffResult(zero(eltype(e)), Vρ, Vρρ),
ρ -> energy(func, ρ, σ), ρ)
res = ForwardDiff.hessian!(DiffResults.DiffResult(zero(eltype(e)), Vσ, Vσσ),
σ -> energy(func, ρ, σ), σ)
dedρ = σ -> ForwardDiff.gradient(ρ -> energy(func, ρ, σ), ρ)
ForwardDiff.jacobian!(Vρσ, dedρ, σ)
e .= DiffResults.value(res)
nothing
end
function energy(func::Functional{:gga}, ρ::AbstractVector{T},
σ::AbstractVector{U}) where {T,U}
length(ρ) == 1 || error("Multiple spins not yet implemented for fallback functionals")
@assert length(ρ) == 1
ρtotal = ρ[1]
σtotal = σ[1]
if ρtotal ≤ threshold_ρ(func, T)
zero(arithmetic_type(func, T, U))
else
σstable = max(σtotal, threshold_σ(func, U))
energy(func, ρtotal, σstable)
end
end