Skip to content

Commit b587304

Browse files
authored
Refactor and extend tests for forces (#1080)
1 parent e9f7a33 commit b587304

File tree

7 files changed

+202
-180
lines changed

7 files changed

+202
-180
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DFTK"
22
uuid = "acf6eb54-70d9-11e9-0013-234b7a5f5337"
33
authors = ["Michael F. Herbst <[email protected]>", "Antoine Levitt <[email protected]>"]
4-
version = "0.7.10"
4+
version = "0.7.11"
55

66
[deps]
77
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/terms/magnetic.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ struct Magnetic
1010
end
1111
(M::Magnetic)(basis) = TermMagnetic(basis, M.Afunction)
1212

13+
struct MagneticFromValues
14+
# Apotential[α] is an array of size fft_size for α=1:3
15+
Apotential::Vector{<:AbstractArray}
16+
end
17+
function (M::MagneticFromValues)(Apotential)
18+
@assert length(Apotential) == 3
19+
@assert size(Apotential[1]) == basis.fft_size
20+
@assert size(Apotential[2]) == basis.fft_size
21+
@assert size(Apotential[3]) == basis.fft_size
22+
TermMagnetic(basis, Apotential)
23+
end
24+
1325
struct TermMagnetic <: Term
1426
# Apotential[α] is an array of size fft_size for α=1:3
1527
Apotential::Vector{<:AbstractArray}

test/forces.jl

Lines changed: 163 additions & 175 deletions
Large diffs are not rendered by default.

test/forwarddiff.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ end
8686
function compute_properties(lattice)
8787
model_strained = Model(model; lattice)
8888
basis = PlaneWaveBasis(model_strained; Ecut, kgrid)
89-
scfres = self_consistent_field(basis; tol=1e-10, nbandsalg)
89+
scfres = self_consistent_field(basis; tol=1e-11, nbandsalg)
9090
ComponentArray(
9191
eigenvalues=stack([ev[1:10] for ev in scfres.eigenvalues]),
9292
ρ=scfres.ρ,
@@ -97,9 +97,11 @@ end
9797
end
9898

9999
strain_isotropic(ε) = (1 + ε) * model.lattice
100-
strain_anisotropic(ε) = DFTK.voigt_strain_to_full([ε, 0., 0., 0., 0., 0.]) * model.lattice
100+
function strain_anisotropic(ε)
101+
DFTK.voigt_strain_to_full([ε, 0., 0., 0., 0., 0.]) * model.lattice
102+
end
101103

102-
@testset "$strain_fn" for strain_fn in [strain_isotropic, strain_anisotropic]
104+
@testset "$strain_fn" for strain_fn in (strain_isotropic, strain_anisotropic)
103105
f(ε) = compute_properties(strain_fn(ε))
104106
dx = ForwardDiff.derivative(f, 0.)
105107

@@ -109,7 +111,7 @@ end
109111
dx_findiff = (x2 - x1) / 2h
110112
@test norm(dx.ρ - dx_findiff.ρ) * sqrt(basis.dvol) < 1e-6
111113
@test maximum(abs, dx.eigenvalues - dx_findiff.eigenvalues) < 1e-6
112-
@test maximum(abs, dx.energies - dx_findiff.energies) < 1e-5
114+
@test maximum(abs, dx.energies - dx_findiff.energies) < 3e-5
113115
@test dx.εF - dx_findiff.εF < 1e-6
114116
@test maximum(abs, dx.occupation - dx_findiff.occupation) < 1e-4
115117
end

test/structures/Fe_rattled.extxyz

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2
2+
pbc=[T, T, T] Lattice="2.87000000 0.00000000 0.00000000 0.00000000 2.87000000 0.00000000 0.00000000 0.00000000 2.87000000" Properties=species:S:1:pos:R:3:velocities:R:3:Z:I:1
3+
Fe 0.10000000 -0.03000000 0.05000000 0.00000000 0.00000000 0.00000000 26
4+
Fe 1.43500000 1.43500000 1.43500000 0.00000000 0.00000000 0.00000000 26

test/structures/GeO2_rattled.extxyz

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
6
2+
name=model_0 pbc=[T, T, T] Lattice="4.48609000 0.00000000 0.00000000 0.00000000 4.48609000 0.00000000 0.00000000 0.00000000 2.91186000" Properties=species:S:1:pos:R:3:velocities:R:3:Z:I:1
3+
Ge 0.00629788 0.00222026 0.01567598 0.00000000 0.00000000 0.00000000 32
4+
Ge 2.23696804 2.24090229 1.45515035 0.00000000 0.00000000 0.00000000 32
5+
O 1.38588856 1.40290802 -0.00302997 0.00000000 0.00000000 0.00000000 8
6+
O 3.09582248 3.09525318 0.00021341 0.00000000 0.00000000 0.00000000 8
7+
O 0.85224093 3.64776914 1.45805119 0.00000000 0.00000000 0.00000000 8
8+
O 3.62813145 0.85721112 1.45468113 0.00000000 0.00000000 0.00000000 8

test/structures/GeO2_stretched.extxyz

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
6
2+
name=model_0 pbc=[T, T, T] Lattice="4.48609000 0.00000000 0.00000000 0.00000000 4.48609000 0.00000000 0.00000000 0.00000000 2.91186000" Properties=species:S:1:pos:R:3:charge:R:1:occupancy:R:1:velocities:R:3:vdw_radius:R:1:covalent_radius:R:1:Z:I:1
3+
Ge 0.10000000 0.00000000 0.00000000 0.00000000 1.00000000 0.00000000 0.00000000 0.00000000 2.10000000 1.22000000 32
4+
Ge 2.24304500 2.22304500 1.45593000 0.00000000 1.00000000 0.00000000 0.00000000 0.00000000 2.10000000 1.22000000 32
5+
O 1.39068790 1.39068790 0.00000000 0.00000000 1.00000000 0.00000000 0.00000000 0.00000000 1.55000000 0.73000000 8
6+
O 3.09540210 3.08540210 0.00000000 0.00000000 1.00000000 0.00000000 0.00000000 0.00000000 1.55000000 0.73000000 8
7+
O 0.75235710 3.53373290 1.45593000 0.00000000 1.00000000 0.00000000 0.00000000 0.00000000 1.55000000 0.73000000 8
8+
O 3.73373290 0.85235710 1.45593000 0.00000000 1.00000000 0.00000000 0.00000000 0.00000000 1.55000000 0.73000000 8

0 commit comments

Comments
 (0)