Basic Workflow
This short tutorial introduces the minimal workflow for people working purely in Julia. There are (or are in development) separate tutorials on using ACEpotentials
via shell scripts or via Python, and tutorials on more advanced usage.
Start by importing ACEpotentials
(and possibly other required libraries)
using ACEpotentials
We need a dataset TiAl_tutorial.xyz
for this tutorial. Normally we would get the path to a datset and then use read_extxyz
to load in the training set.
# (don't execute this block)
using ExtXYZ
data_file = "path/to/TiAl_tutorial.xyz"
data = ExtXYZ.load(data_file)
For convenience we provide this dataset as a Julia artifact and make it accessible via ACEpotentials.example_dataset
. We keep only a small subset of the structures for training and testing to keep the regression problem small.
data, _, meta = ACEpotentials.example_dataset("TiAl_tutorial")
train_data = data[1:5:end];
test_data = data[2:10:end];
Downloading artifact: TiAl_tutorial
Failure artifact: TiAl_tutorial
Downloading artifact: TiAl_tutorial
The next step is to generate a model. Here we generate a linear ACE model using the ACE1compat
interface, which generates models that are essentially equivalent to those provided by the discontinued ACE1.jl
package.
order = 3
: We take 3-correlation, i.e. a 4-body potential,totaldegree = 6
: a very low polynomial degree just for testingrcut = 5.5
: this is a typical cutoff radius for metals
These three are the most important approximation parameters to explore when trying to improve the fit-accuracy. There are many other parameters to explore, which are documented in ?acemodel
. Even further model refinements are possible by studying the internals of ACE1.jl
and ACE1x.jl
. We also specify a reference potential that will be added to the learned 2-body and many-body potential components. Here we use a one-body potential i.e. a reference atom energy for each individual species. Usage of a one-body reference potential generally results in very slightly reduced fit accuracy but significantly improved 2-body potentials with a realistic dimer shape and improved robustness in predictions.
hyperparams = (elements = [:Ti, :Al],
order = 3,
totaldegree = 6,
rcut = 5.5,
Eref = [:Ti => -1586.0195, :Al => -105.5954])
model = ace1_model(; hyperparams...)
@show length_basis(model);
length_basis(model) = 270
The next line specifies the regression weights: in the least squares loss different observations are given different weights,
\[ \sum_{R} \Big( w_{E,R}^2 | E(R) - y_R^E |^2 + w_{F,R}^2 | {\rm forces}(R) - y_R^F |^2 + w_{V,R}^2 | {\rm virial}(R) - y_R^V |^2 \Big),\]
and this is specificed via the following dictionary. The keys correspond to the config_type
of the training structures.
weights = Dict(
"FLD_TiAl" => Dict("E" => 60.0, "F" => 1.0 , "V" => 1.0 ),
"TiAl_T5000" => Dict("E" => 5.0, "F" => 1.0 , "V" => 1.0 ));
To estimate the parameters we still need to choose a solver for the least squares system. In this tutorial we use a Bayesian linear regression, which is the recommended default at the moment. Many other solvers are available, and can be explored by looking at the documentation of ACEfit.jl
.
solver = ACEfit.BLR()
ACEfit.BLR(Dict{Any, Any}())
ACEpotentials provides a heuristic smoothness prior which assigns to each basis function Bi
a scaling parameter si
that estimates how "rough" that basis function is. The following line generates a regularizer (prior) with si^q
on the diagonal, thus penalizing rougher basis functions and enforcing a smoother fitted potential.
P = algebraic_smoothness_prior(model; p = 4) # (p = 4 is in fact the default)
270×270 LinearAlgebra.Diagonal{Float64, Vector{Float64}}:
1.0 ⋅ ⋅ ⋅ ⋅ ⋅ … ⋅ ⋅ ⋅ ⋅
⋅ 16.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ 81.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ 256.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ 625.0 ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ 1296.0 … ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋮ ⋮ ⋱
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ … ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 6561.0 ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 10000.0 ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 14641.0 ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 20736.0
We are now ready to estimate the parameters. We take a subset of the training data to speed up the tutorial. The prior is passed to the acefit!
function via the prior
keyword argument.
result = acefit!(train_data, model; solver=solver, prior = P, weights=weights);
┌────────────┬──────────┬───────┬────┬──────┬─────┐
│ Type │ #Configs │ #Envs │ #E │ #F │ #V │
├────────────┼──────────┼───────┼────┼──────┼─────┤
│ FLD_TiAl │ 63 │ 126 │ 63 │ 378 │ 378 │
│ TiAl_T5000 │ 3 │ 310 │ 3 │ 930 │ 18 │
├────────────┼──────────┼───────┼────┼──────┼─────┤
│ total │ 66 │ 436 │ 66 │ 1308 │ 396 │
│ missing │ 0 │ 0 │ 0 │ 0 │ 0 │
└────────────┴──────────┴───────┴────┴──────┴─────┘
[ Info: Assembling linear problem.
[ Info: - Creating feature matrix with size (1770, 270).
[ Info: - Beginning assembly with processor count: 1.
Progress: 3%|█▎ | ETA: 0:08:16
Progress: 8%|███▏ | ETA: 0:03:29
Progress: 12%|█████ | ETA: 0:02:10
Progress: 17%|██████▉ | ETA: 0:01:34
Progress: 21%|████████▊ | ETA: 0:01:13
Progress: 26%|██████████▌ | ETA: 0:00:59
Progress: 41%|████████████████▊ | ETA: 0:00:34
Progress: 50%|████████████████████▌ | ETA: 0:00:25
Progress: 55%|██████████████████████▍ | ETA: 0:00:22
Progress: 70%|████████████████████████████▋ | ETA: 0:00:13
Progress: 85%|██████████████████████████████████▊ | ETA: 0:00:06
Progress: 100%|█████████████████████████████████████████| Time: 0:00:34
[ Info: - Assembly completed.
[ Info: Assembling full weight vector.
Progress: 3%|█▎ | ETA: 0:00:05
Progress: 100%|█████████████████████████████████████████| Time: 0:00:00
Iter Function value Gradient norm
0 4.531070e+03 2.176975e+03
* time: 0.03007984161376953
1 4.511962e+03 3.381033e+02
* time: 1.047334909439087
2 3.126360e+03 4.498989e+02
* time: 1.0596270561218262
3 3.075386e+03 3.787742e+02
* time: 1.0677838325500488
4 3.042578e+03 3.110060e+02
* time: 1.0718810558319092
5 2.996447e+03 1.577183e+02
* time: 1.0759739875793457
6 2.956635e+03 1.426538e+02
* time: 1.0800139904022217
7 2.904828e+03 5.021431e+02
* time: 1.0859758853912354
8 2.633319e+03 3.264234e+03
* time: 1.0937700271606445
9 2.132198e+03 9.266791e+02
* time: 1.0995550155639648
10 1.305131e+03 2.650596e+01
* time: 1.14164400100708
11 1.300301e+03 1.728341e+02
* time: 1.1472280025482178
12 1.136213e+03 1.788406e+03
* time: 1.1513829231262207
13 9.266028e+02 1.724264e+03
* time: 1.1554839611053467
14 8.683957e+02 8.369458e+02
* time: 1.159822940826416
15 8.542065e+02 1.202458e+01
* time: 1.1629219055175781
16 8.460816e+02 6.359850e+02
* time: 1.1675188541412354
17 8.383414e+02 2.348007e+02
* time: 1.1721279621124268
18 8.375127e+02 1.452398e+01
* time: 1.1767489910125732
19 8.374630e+02 3.587759e+00
* time: 1.1812679767608643
20 8.374628e+02 8.462346e-02
* time: 1.184380054473877
21 8.374628e+02 1.975340e-04
* time: 1.1888329982757568
22 8.374628e+02 4.682667e-05
* time: 1.1933560371398926
23 8.374628e+02 9.691217e-06
* time: 1.197808027267456
24 8.374628e+02 2.150332e-05
* time: 1.200788974761963
25 8.374628e+02 5.243516e-06
* time: 1.2046608924865723
* Status: success (objective increased between iterations)
* Candidate solution
Final objective value: 8.374628e+02
* Found with
Algorithm: L-BFGS
* Convergence measures
|x - x'| = 9.30e-09 ≤ 1.0e-08
|x - x'|/|x'| = 7.09e-11 ≰ 0.0e+00
|f(x) - f(x')| = 3.15e-06 ≰ 0.0e+00
|f(x) - f(x')|/|f(x')| = 3.76e-09 ≰ 0.0e+00
|g(x)| = 5.24e-06 ≰ 0.0e+00
* Work counters
Seconds run: 1 (vs limit Inf)
Iterations: 25
f(x) calls: 96
∇f(x) calls: 96
We can display an error table as follows:
@info("Training Error Table")
err_train = ACEpotentials.compute_errors(train_data, model; weights=weights);
[ Info: Training Error Table
[ Info: RMSE Table
┌────────────┬─────────┬──────────┬─────────┐
│ Type │ E [meV] │ F [eV/A] │ V [meV] │
├────────────┼─────────┼──────────┼─────────┤
│ FLD_TiAl │ 3.613 │ 0.043 │ 59.871 │
│ TiAl_T5000 │ 10.251 │ 0.300 │ 26.326 │
├────────────┼─────────┼──────────┼─────────┤
│ set │ 4.152 │ 0.254 │ 58.763 │
└────────────┴─────────┴──────────┴─────────┘
[ Info: MAE Table
┌────────────┬─────────┬──────────┬─────────┐
│ Type │ E [meV] │ F [eV/A] │ V [meV] │
├────────────┼─────────┼──────────┼─────────┤
│ FLD_TiAl │ 2.555 │ 0.029 │ 38.602 │
│ TiAl_T5000 │ 9.725 │ 0.232 │ 21.096 │
├────────────┼─────────┼──────────┼─────────┤
│ set │ 2.881 │ 0.173 │ 37.806 │
└────────────┴─────────┴──────────┴─────────┘
We should of course also look at test errors, which can be done as follows. Depending on the choice of solver, and solver parameters, the test errors might be very poor. Exploring different parameters in different applications can lead to significantly improved predictions.
@info("Test Error Table")
err_test = ACEpotentials.compute_errors(test_data, model; weights=weights);
[ Info: Test Error Table
[ Info: RMSE Table
┌────────────┬─────────┬──────────┬─────────┐
│ Type │ E [meV] │ F [eV/A] │ V [meV] │
├────────────┼─────────┼──────────┼─────────┤
│ FLD_TiAl │ 7.540 │ 0.049 │ 66.310 │
│ TiAl_T5000 │ 9.388 │ 0.402 │ 93.993 │
├────────────┼─────────┼──────────┼─────────┤
│ set │ 7.603 │ 0.274 │ 67.316 │
└────────────┴─────────┴──────────┴─────────┘
[ Info: MAE Table
┌────────────┬─────────┬──────────┬─────────┐
│ Type │ E [meV] │ F [eV/A] │ V [meV] │
├────────────┼─────────┼──────────┼─────────┤
│ FLD_TiAl │ 4.455 │ 0.035 │ 41.051 │
│ TiAl_T5000 │ 9.388 │ 0.313 │ 83.319 │
├────────────┼─────────┼──────────┼─────────┤
│ set │ 4.605 │ 0.162 │ 42.332 │
└────────────┴─────────┴──────────┴─────────┘
If we want to save the fitted potentials to disk to later use we can simply save the hyperparameters and the parameters. At the moment this must be done manually but a more complete and convenient interface for this will be provided, also adding various sanity checks.
using JSON
open("TiAl_model.json", "w") do f
JSON.print(f, Dict("hyperparams" => hyperparams, "params" => model.ps))
end
To load the model back from disk it is safest to work within the same Julia project, i.e. the same version of all packages; ideally the the Manifest should not be changed. One then generates the model again, loads the parameters from disk and then sets them in the model. Again, this will be automated in the future.
Finally, we delete the model to clean up.
rm("TiAl_model.json")
Fast Evaluator
ACEpotentials.jl
provides an experimental "fast evaluator". This tries to merge some of the operations in the full model resulting in a "slimmer" and usually faster evaluator. In some cases the performance gain can be multiple factors up to an order of magnitude. This is particularly important when using a parameter estimation solver that sparsifies. In that case, the performance gain can be significant.
To construct the fast evaluator, simply use
fpot = fast_evaluator(model)
An optional keyword argument aa_static = true
can be used to optimize the n-correlation layer for very small models (at most a few hundred parameters). For larger models this leads to a stack overflow.
This page was generated using Literate.jl.