Skip to content

Commit 8a01158

Browse files
authored
Integrate with DofManager from GeomOpt.jl (#24)
1 parent cd16ffa commit 8a01158

File tree

11 files changed

+511
-124
lines changed

11 files changed

+511
-124
lines changed

Project.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
1919
UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"
2020

2121
[compat]
22-
ASEconvert = "0.1.8"
2322
AtomsBase = "0.5"
2423
AtomsBuilder = "0.2.2"
2524
AtomsCalculators = "0.2.3"
@@ -36,12 +35,10 @@ UnitfulAtomic = "1"
3635
julia = "1.10"
3736

3837
[extras]
39-
ASEconvert = "3da9722f-58c2-4165-81be-b4d7253e8fd2"
4038
AtomsBuilder = "f5cc8831-eeb7-4288-8d9f-d6c1ddb77004"
4139
EmpiricalPotentials = "38527215-9240-4c91-a638-d4250620c9e2"
42-
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
4340
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4441
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
4542

4643
[targets]
47-
test = ["ASEconvert", "AtomsBuilder", "EmpiricalPotentials", "PythonCall", "Test", "TestItemRunner"]
44+
test = ["AtomsBuilder", "EmpiricalPotentials", "Test", "TestItemRunner"]

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ makedocs(;
2626
"Home" => "index.md",
2727
"Examples" => [
2828
"examples/aluminium_dftk.md",
29+
"examples/variablecell.md",
2930
"examples/other_solvers.md",
3031
"examples/tial_lj.md",
3132
],

docs/src/examples/variablecell.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Silicon variable cell relaxation
2+
3+
In this example we optimise both atomic positions and lattice geometry
4+
of a rattled silicon structure using a Stillinger-Weber potential.
5+
6+
We build an initial structure by first rattling the positions
7+
and than the lattice geometry:
8+
9+
```@example silicon
10+
using AtomsBase
11+
using AtomsBuilder
12+
using LinearAlgebra
13+
using Unitful
14+
using UnitfulAtomic
15+
16+
silicon_posrattle = rattle!(bulk(:Si, cubic=true) * (2, 2, 2), 0.1u"Å")
17+
F = I + 1e-3randn(3, 3)
18+
new_cell_vectors = tuple([F * v for v in cell_vectors(silicon_posrattle)]...)
19+
silicon_rattle = AbstractSystem(silicon_posrattle; cell_vectors=new_cell_vectors)
20+
```
21+
22+
and optimise with `variablecell=true`
23+
24+
```@example silicon
25+
using EmpiricalPotentials
26+
using GeometryOptimization
27+
28+
sw = StillingerWeber()
29+
silicon = minimize_energy!(silicon_posrattle, sw;
30+
variablecell=true, verbosity=1, tol_virial=1e-4u"hartree").system
31+
nothing # hide
32+
```
33+
34+
Final structure:
35+
36+
```@example silicon
37+
silicon
38+
```

src/GeometryOptimization.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
module GeometryOptimization
22

3+
using AtomsBase
4+
using AtomsCalculators
35
using DocStringExtensions
46
using LinearAlgebra
5-
using StaticArrays
67
using Optimization
7-
using AtomsBase
8-
using AtomsCalculators
8+
using StaticArrays
99
using Unitful
1010
using UnitfulAtomic
1111

1212
# Make sure Optim is always available
1313
using OptimizationOptimJL
1414
using LineSearches
1515

16+
# Useful shortcuts
17+
using AtomsCalculators: Energy, Forces, Virial
1618
AC = AtomsCalculators
1719

1820
@template METHODS =
@@ -22,7 +24,7 @@ $(TYPEDSIGNATURES)
2224
$(DOCSTRING)
2325
"""
2426

25-
include("clamping_updating_positions.jl")
27+
include("dof_management.jl")
2628
include("optimization.jl")
2729
include("callbacks.jl")
2830

src/callbacks.jl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ produces output as well.
1111
struct GeoOptDefaultCallback
1212
verbosity::Int
1313
always_show_header::Bool
14+
show_virial::Bool
1415
prev_time::Ref{UInt64}
1516
end
16-
function GeoOptDefaultCallback(verbosity=1; always_show_header=verbosity > 1)
17-
GeoOptDefaultCallback(verbosity, always_show_header, Ref{UInt64}(0))
17+
function GeoOptDefaultCallback(verbosity=1;
18+
show_virial=true, always_show_header=verbosity > 1)
19+
GeoOptDefaultCallback(verbosity, always_show_header, show_virial, Ref{UInt64}(0))
1820
end
1921

2022
format_log8(value) = (value < 0 ? " " : "+") * (@sprintf "%8.2f" log10(abs(value)))
@@ -29,7 +31,11 @@ function (cb::GeoOptDefaultCallback)(optim_state, geoopt_state)
2931
cb.prev_time[] = runtime_ns
3032

3133
Estr = (@sprintf "%+15.12f" round(austrip(geoopt_state.energy), sigdigits=13))[1:15]
32-
logΔE = optim_state.iter < 1 ? "" : format_log8(austrip(geoopt_state.energy_change))
34+
if iszero(geoopt_state.energy_change) && optim_state.iter < 1
35+
logΔE = ""
36+
else
37+
logΔE = format_log8(austrip(geoopt_state.energy_change))
38+
end
3339

3440
maxforce = austrip(maximum(norm, geoopt_state.forces))
3541
fstr = iszero(maxforce) ? "" : round(maxforce, sigdigits=8)
@@ -38,13 +44,20 @@ function (cb::GeoOptDefaultCallback)(optim_state, geoopt_state)
3844
("n", 3, optim_state.iter),
3945
("Energy", 15, Estr),
4046
("log10(ΔE)", 9, logΔE),
41-
("max(Force)", 10, fstr),
47+
("max(Force)", 11, fstr),
4248
# TODO Maximal atomic displacement
43-
# TODO Current virial, trace of lattice deformation matrix
44-
("Δtime", 6, tstr),
45-
# TODO Would be nice to have some simple way to add in
46-
# a few calculator-specific things (e.g. total number of SCF iterations)
4749
]
50+
if cb.show_virial
51+
maxvirial = austrip(maximum(abs, geoopt_state.virial))
52+
pressure = -austrip(tr(geoopt_state.virial)) / 3
53+
vstr = iszero(maxvirial) ? "" : round(maxvirial, sigdigits=8)
54+
pstr = iszero(pressure) ? "" : round(pressure, sigdigits=2)
55+
push!(fields, ("max(Virial)", 11, vstr))
56+
push!(fields, ("Pressure", 8, pstr))
57+
end
58+
push!(fields, ("Δtime", 6, tstr))
59+
# TODO Would be nice to have some simple way to add in
60+
# a few calculator-specific things (e.g. total number of SCF iterations)
4861

4962
if cb.always_show_header
5063
hlines = :all

src/clamping_updating_positions.jl

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)