Skip to content

Commit

Permalink
crude regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
epolack committed Dec 12, 2023
1 parent 95c8d0e commit 535c016
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
4 changes: 2 additions & 2 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ using TestItemRunner

const SUITE = BenchmarkGroup()

SUITE["minimal"] = BenchmarkGroup()
SUITE["minimal"] = @benchmarkable @run_package_tests filter=ti->(:minimal ti.tags)
SUITE["fast"] = BenchmarkGroup()
SUITE["fast"] = @run_package_tests filter=ti->(:regression ti.tags)
108 changes: 108 additions & 0 deletions benchmark/regression/testcases.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
@testsetup module Regression
using DFTK
using Unitful
using UnitfulAtomic
using AtomsBase
using ..TestCases: magnesium

high_symmetry = let
a = 4.474
lattice = [[0, a, a], [a, 0, a], [a, a, 0]]u"bohr"
x = 6.711
y = 2.237
atoms = [
Atom(:Cu, [0, 0, 0]u"bohr", magnetic_moment=0),
Atom(:O, [x, y, x]u"bohr", magnetic_moment=0),
Atom(:O, [x, y, y]u"bohr", magnetic_moment=0),
]
system = periodic_system(atoms, lattice)
merge(DFTK.parse_system(system), (; temperature=0.03, Ecut=10, kgrid=[4,4,4],
n_electrons=45))
end
high_kpoints = merge(magnesium, (; kgrid=[13,13,13], Ecut=10))
high_Ecut = merge(magnesium, (; kgrid=[4,4,4], Ecut=60))
testcases = (; high_symmetry, high_kpoints, high_Ecut)
end

@testitem "Hamiltonian application" tags=[:regression] setup=[TestCases, Regression] begin
using DFTK
using LinearAlgebra
using BenchmarkTools

for testcase in Regression.testcases
model = Model(testcase.lattice, testcase.atoms, testcase.positions;
testcase.temperature, terms=[Kinetic()])
basis = PlaneWaveBasis(model; testcase.Ecut, testcase.kgrid)

n_electrons = testcase.n_electrons
n_bands = div(n_electrons, 2, RoundUp)
ψ = [Matrix(qr(randn(ComplexF64, length(G_vectors(basis, kpt)), n_bands)).Q)
for kpt in basis.kpoints]
filled_occ = DFTK.filled_occupation(model)
occupation = [filled_occ * rand(n_bands) for _ = 1:length(basis.kpoints)]
occ_scaling = n_electrons / sum(sum(occupation))
occupation = [occ * occ_scaling for occ in occupation]

(; ham) = energy_hamiltonian(basis, ψ, occupation)

@benchmarkable for ik = 1:length(basis.kpoints)
ham.blocks[ik]*ψ[ik]
end
end
end

@testitem "Single SCF step" tags=[:regression] setup=[TestCases, Regression] begin
using DFTK
using BenchmarkTools

for testcase in Regression.testcases
model = model_LDA(testcase.lattice, testcase.atoms, testcase.positions;
testcase.temperature)
basis = PlaneWaveBasis(model; testcase.Ecut, testcase.kgrid)
@benchmarkable self_consistent_field(basis; tol=1e5)
end
end

@testitem "Density + symmetrization" tags=[:regression] setup=[TestCases, Regression] begin
using DFTK
using BenchmarkTools

for testcase in Regression.testcases
model = model_LDA(testcase.lattice, testcase.atoms, testcase.positions;
testcase.temperature)
basis = PlaneWaveBasis(model; testcase.Ecut, testcase.kgrid)
scfres = self_consistent_field(basis; tol=10)

ψ, occupation = DFTK.select_occupied_orbitals(basis, scfres.ψ, scfres.occupation;
threshold=1e-6)

ρ = @benchmarkable compute_density(basis, ψ, occupation)
@benchmarkable DFTK.symmetrize_ρ(basis, ρ)
end
end

@testitem "Basis construction" tags=[:regression] setup=[TestCases, Regression] begin
using DFTK
using BenchmarkTools

for testcase in Regression.testcases
model = model_LDA(testcase.lattice, testcase.atoms, testcase.positions;
testcase.temperature)
@benchmarkable basis = PlaneWaveBasis(model; testcase.Ecut, testcase.kgrid)
end
end

@testitem "Sternheimer" tags=[:regression] setup=[TestCases, Regression] begin
using DFTK
using BenchmarkTools

for testcase in Regression.testcases
model = model_LDA(testcase.lattice, testcase.atoms, testcase.positions;
testcase.temperature)
basis = PlaneWaveBasis(model; testcase.Ecut, testcase.kgrid)
scfres = self_consistent_field(basis; tol=10)

rhs = DFTK.compute_projected_gradient(basis, scfres.ψ, scfres.occupation)
@benchmarkable DFTK.solve_ΩplusK_split(scfres, rhs)
end
end
2 changes: 1 addition & 1 deletion benchmark/run.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ROOTPATH = joinpath(@__DIR__, "../..")
ROOTPATH = joinpath(@__DIR__, "..")
import Pkg
Pkg.activate(@__DIR__)
if !isfile(joinpath(@__DIR__, "Manifest.toml"))
Expand Down

0 comments on commit 535c016

Please sign in to comment.