Skip to content

Commit b0f0fac

Browse files
author
Christoph Ortner
committed
many bugfixes related committees
1 parent 4f65594 commit b0f0fac

File tree

10 files changed

+140
-7822
lines changed

10 files changed

+140
-7822
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/Tutorial/ACEpotentials-Tutorial.ipynb

Lines changed: 65 additions & 7817 deletions
Large diffs are not rendered by default.

src/ace1_compat.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ function ace1_model(; kwargs...)
334334
end
335335

336336
model_spec = Dict{Symbol, Any}(pairs(kwargs)...)
337-
model_spec[:Eref] = ACEpotentials.Models._convert_E0s(kwargs[:Eref])
337+
if haskey(model_spec, :Eref)
338+
model_spec[:Eref] = ACEpotentials.Models._convert_E0s(kwargs[:Eref])
339+
end
338340
model_spec[:model_name] = "ACE1"
339341

340342
kwargs = _clean_args(kwargs)

src/models/calculators.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,23 @@ function energy_forces_virial_basis(
301301

302302
return (energy = E, forces = F, virial = V)
303303
end
304+
305+
306+
function potential_energy_basis(at, calc::ACEPotential{<: ACEModel},
307+
ps = calc.ps, st = calc.st;
308+
domain = 1:length(at),
309+
nlist = PairList(at, cutoff_radius(calc)),
310+
kwargs...)
311+
N_basis = length_basis(calc)
312+
_e0 = AtomsCalculators.zero_energy(at, calc)
313+
T = typeof(ustrip(_e0))
314+
E = fill(zero(T) * energy_unit(calc), N_basis)
315+
316+
for i in domain
317+
Js, Rs, Zs, z0 = get_neighbours(at, calc, nlist, i)
318+
v = evaluate_basis(calc.model, Rs, Zs, z0, ps, st)
319+
E += v * energy_unit(calc)
320+
end
321+
322+
return E
323+
end

src/models/committee.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ function co_length(model::ACEPotential)
3030
end
3131

3232
function committee(f, sys::AbstractSystem, model::ACEPotential)
33-
E = f(sys, model)
33+
if f == potential_energy
34+
return co_potential_energy(sys, model)
35+
end
36+
F = f(sys, model)
3437
ps0 = model.ps
35-
co_E = [ (model.ps = model.co_ps[i]; f(sys, model))
38+
co_F = [ (model.ps = model.co_ps[i]; f(sys, model))
3639
for i = 1:length(model.co_ps) ]
3740
model.ps = ps0
38-
return E, co_E
41+
return F, co_F
3942
end
4043

4144
macro committee(ex)
@@ -44,3 +47,22 @@ macro committee(ex)
4447
committee($(esc_args...))
4548
end
4649
end
50+
51+
function co_potential_energy(sys::AbstractSystem, model::ACEPotential)
52+
basis_E = potential_energy_basis(sys, model)
53+
eref = potential_energy(sys, model.model.Vref) * u"eV"
54+
E = dot(basis_E, destructure(model.ps)[1]) + eref
55+
co_E = [ dot(basis_E, destructure(model.co_ps[i])[1]) + eref
56+
for i = 1:length(model.co_ps) ]
57+
return E, co_E
58+
end
59+
60+
function co_potential_energy_2(sys::AbstractSystem, model::ACEPotential)
61+
f = potential_energy
62+
F = f(sys, model)
63+
ps0 = model.ps
64+
co_F = [ (model.ps = model.co_ps[i]; f(sys, model))
65+
for i = 1:length(model.co_ps) ]
66+
model.ps = ps0
67+
return F, co_F
68+
end

test/test_bugs.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,16 @@ maxdiff = maximum(abs(E_per_at[i] - E_per_at[j]) for i = 1:10, j = 1:10 )
3535
@test ustrip(u"eV", maxdiff) < 1e-9
3636

3737
@info(" ============================================================")
38+
@info(" ============== Testing for no Eref bug ====================")
39+
40+
# there was never an issue filed for this, but it was an annoying issue
41+
# that came up twice by making changes in the model construction heuristics
42+
43+
params1 = (elements = [:Si], rcut = 5.5, order = 3, totaldegree = 12)
44+
params2 = (; :Eref => [:Si => 0.0], pairs(params1)...)
45+
model1 = ace1_model(; params1...)
46+
model2 = ace1_model(; params2...)
47+
sys = bulk(:Si, cubic=true) * 2
48+
println_slim(@test potential_energy(sys, model1) == potential_energy(sys, model2) == 0.0u"eV")
49+
50+
@info(" ============================================================")

test/test_silicon.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ println_slim(@test m2.ps == model.ps)
104104

105105
##
106106

107-
info("Fit a potential with committee")
107+
@info("Fit a potential with committee")
108108

109109
co_size = 10
110110
solver = ACEfit.BLR(factorization = :svd, committee_size = co_size)
@@ -116,6 +116,19 @@ acefit!(data, model;
116116

117117
println_slim(@test length(model.co_ps) == co_size)
118118

119+
E, co_E = @committee potential_energy(data[3], model)
120+
E
121+
co_E
122+
123+
using LinearAlgebra
124+
M = ACEpotentials.Models
125+
efv = M.energy_forces_virial_basis(data[3], model)
126+
e = M.potential_energy_basis(data[3], model)
127+
println_slim(@test all(efv.energy .≈ e))
128+
e1, co_e1 = @committee potential_energy(data[3], model)
129+
e2, co_e2 = M.co_potential_energy_2(data[3], model)
130+
println_slim(@test e1 e2)
131+
println_slim(@test all(co_e1 .≈ co_e2))
119132

120133
##
121134
# Add a descriptor test

0 commit comments

Comments
 (0)