Skip to content

Commit 81c39d1

Browse files
committed
tests
1 parent 45abfc7 commit 81c39d1

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

src/response/chi0.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,8 @@ function compute_δψ!(δψ, basis::PlaneWaveBasis{T}, H, ψ, εF, ε, δHψ, ε
344344
δψk = δψ[ik]
345345
tolk = bandtol[ik]
346346
εk_minus_q = ε_minus_q[ik]
347-
348-
rnorm_k = residual_norms[ik]
349-
n_iter_k = n_iter[ik]
350-
sizehint!(rnorm_k, length(εk_minus_q))
351-
sizehint!(n_iter_k, length(εk_minus_q))
347+
sizehint!(residual_norms[ik], length(εk_minus_q))
348+
sizehint!(n_iter[ik], length(εk_minus_q))
352349

353350
ψk_extra = ψ_extra[ik]
354351
Hψk_extra = Hk * ψk_extra
@@ -377,8 +374,8 @@ function compute_δψ!(δψ, basis::PlaneWaveBasis{T}, H, ψ, εF, ε, δHψ, ε
377374
res.tol, res.residual_norm)
378375

379376
δψk[:, n] .+= res.δψkn
380-
push!(rnorm_k, res.residual_norm)
381-
push!(n_iter_k, res.n_iter)
377+
push!(residual_norms[ik], res.residual_norm)
378+
push!(n_iter[ik], res.n_iter)
382379
converged = converged && res.converged
383380
end
384381
end

src/response/hessian.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ function (cb::OmegaPlusKDefaultCallback)(info)
196196
label_time[1], " Comment")
197197
@printf(io, "%s %s %s%s %s %s%s %s\n",
198198
"-"^4, "-"^7, "-"^6, label_s[2], "-"^10, "-"^7, label_time[2], "-"^15)
199-
@printf(io, "%21s%s %10s %7.2f%s %15s\n",
199+
@printf(io, "%21s%s %10s %7.1f%s %15s\n",
200200
"", label_s[3], "", avgCG, tstr, "Non-interacting")
201201
elseif info.stage == :iterate
202202
n_iter = info.n_iter
203203
resstr = format_log8(info.resid_history[n_iter])
204204
comment = ((n_iter-1) in info.restart_history) ? "Restart" : ""
205205
sstr = cb.show_s ? (@sprintf " %5.2f" info.s) : ""
206206
restart = length(info.restart_history)
207-
@printf(io, "%4i %7i %6i%s %10s %7.2f%s %s\n",
207+
@printf(io, "%4i %7i %6i%s %10s %7.1f%s %s\n",
208208
n_iter, restart, info.k, sstr, resstr, avgCG, tstr, comment)
209209
elseif info.stage == :final
210210
@printf(io, "%21s%s %10s %7.2f%s %s\n",
@@ -247,7 +247,7 @@ Input parameters:
247247
eigenvalues; occupation_threshold),
248248
q=zero(Vec3{real(T)}),
249249
maxiter_sternheimer=100,
250-
maxiter=100, krylovdim=10, s=10.0,
250+
maxiter=100, krylovdim=20, s=krylovdim,
251251
callback=verbose ? OmegaPlusKDefaultCallback() : identity,
252252
kwargs...) where {T}
253253
# Using χ04P = -Ω^-1, E extension operator (2P->4P) and R restriction operator:

test/hessian.jl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,74 @@ end
120120
atol=1e-7)
121121
end
122122
end
123+
124+
@testitem "Test solve_ΩplusK_split achieves promised accuracies" begin
125+
using DFTK
126+
using LinearAlgebra
127+
using PseudoPotentialData
128+
129+
function test_solve_ΩplusK(scfres, δVext)
130+
# Compute a reference solution
131+
δHψ = DFTK.multiply_ψ_by_blochwave(scfres.basis, scfres.ψ, δVext)
132+
bandtolalg = DFTK.BandtolGuaranteed(scfres)
133+
ref = DFTK.solve_ΩplusK_split(scfres, -δHψ; tol=1e-12, verbose=false, bandtolalg)
134+
135+
# Test agreement of non-interacting response
136+
δρ0 = apply_χ0(scfres, δVext, tol=1e-14).δρ
137+
@test maximum(abs, δρ0 - ref.δρ0) < 5e-11
138+
@test norm(δρ0 - ref.δρ0) < 5e-11
139+
140+
# Check residual is small
141+
ε = DFTK.DielectricAdjoint(scfres; bandtolalg=DFTK.BandtolGuaranteed(scfres))
142+
εδρ = reshape(DFTK.inexact_mul(ε, ref.δρ; tol=1e-14).Ax, size(δρ0))
143+
@test norm(δρ0 - εδρ) < 1e-12
144+
@test maximum(abs, δρ0 - εδρ) < 1e-12
145+
146+
# TODO δψ agreement not tested
147+
@assert length(scfres.basis.kpoints) == 1
148+
149+
for tol in (1e-2, 1e-4, 1e-6, 1e-8, 1e-10)
150+
res = DFTK.solve_ΩplusK_split(scfres, -δHψ; tol, verbose=false)
151+
@test norm(res.δρ - ref.δρ) < tol
152+
153+
# This is probably a bit flaky ... orbitals could rotate
154+
@test maximum(abs, res.δψ[1] - ref.δψ[1]) < tol
155+
end
156+
157+
for s in (1, 10, 100, 10^5)
158+
tol = 1e-8
159+
res = DFTK.solve_ΩplusK_split(scfres, -δHψ; tol, s, verbose=false)
160+
@test norm(res.δρ - ref.δρ) < tol
161+
@test maximum(abs, res.δψ[1] - ref.δψ[1]) < tol
162+
end
163+
end
164+
165+
@testset "Polarisability in Helium" begin
166+
# Helium at the centre of the box
167+
a = 10.0
168+
lattice = a * I(3) # cube of ``a`` bohrs
169+
pseudopotentials = PseudoFamily("cp2k.nc.sr.lda.v0_1.largecore.gth")
170+
atoms = [ElementPsp(:He, pseudopotentials)]
171+
positions = [[1/2, 1/2, 1/2]]
172+
173+
model = model_DFT(lattice, atoms, positions;
174+
functionals=PBE(), symmetries=false)
175+
basis = PlaneWaveBasis(model; Ecut=30, kgrid=(1, 1, 1))
176+
scfres = self_consistent_field(basis; tol=1e-12)
177+
178+
# δVext is the potential from a uniform field interacting with the dielectric dipole
179+
# of the density. Note, that this expression only makes sense for a molecule
180+
δVext = [-(r[1] - a/2) for r in r_vectors_cart(basis)]
181+
δVext = cat(δVext; dims=4)
182+
183+
test_solve_ΩplusK(scfres, δVext)
184+
end
185+
186+
@testset "Aluminium atomic displacements" begin
187+
188+
189+
# TODO displace some atoms and solve response
190+
191+
192+
end
193+
end

0 commit comments

Comments
 (0)