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)
data_file = "path/to/TiAl_tutorial.xyz"
data = read_extxyz(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 testing
  • rcut = 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:00
Progress:   8%|███▏                                     |  ETA: 0:03:22
Progress:  23%|█████████▍                               |  ETA: 0:01:06
Progress:  27%|███████████▏                             |  ETA: 0:00:54
Progress:  32%|█████████████                            |  ETA: 0:00:45
Progress:  47%|███████████████████▎                     |  ETA: 0:00:27
Progress:  52%|█████████████████████▏                   |  ETA: 0:00:23
Progress:  67%|███████████████████████████▍             |  ETA: 0:00:14
Progress:  71%|█████████████████████████████▎           |  ETA: 0:00:11
Progress:  76%|███████████████████████████████          |  ETA: 0:00:09
Progress:  91%|█████████████████████████████████████▎   |  ETA: 0:00:03
Progress: 100%|█████████████████████████████████████████| Time: 0:00:33
[ 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.028280019760131836
     1     4.511962e+03     3.381033e+02
 * time: 1.1047260761260986
     2     3.126360e+03     4.498989e+02
 * time: 1.1169610023498535
     3     3.075386e+03     3.787742e+02
 * time: 1.124993085861206
     4     3.042578e+03     3.110060e+02
 * time: 1.1290040016174316
     5     2.996447e+03     1.577183e+02
 * time: 1.133146047592163
     6     2.956635e+03     1.426538e+02
 * time: 1.1370971202850342
     7     2.904828e+03     5.021431e+02
 * time: 1.1429851055145264
     8     2.633319e+03     3.264234e+03
 * time: 1.150871992111206
     9     2.132198e+03     9.266791e+02
 * time: 1.1567809581756592
    10     1.305131e+03     2.650596e+01
 * time: 1.1705341339111328
    11     1.300301e+03     1.728341e+02
 * time: 1.190412998199463
    12     1.136213e+03     1.788406e+03
 * time: 1.1945230960845947
    13     9.266028e+02     1.724264e+03
 * time: 1.198617935180664
    14     8.683957e+02     8.369458e+02
 * time: 1.2026710510253906
    15     8.542065e+02     1.202458e+01
 * time: 1.205374002456665
    16     8.460816e+02     6.359850e+02
 * time: 1.2093830108642578
    17     8.383414e+02     2.348007e+02
 * time: 1.2133500576019287
    18     8.375127e+02     1.452398e+01
 * time: 1.21738600730896
    19     8.374630e+02     3.587759e+00
 * time: 1.2215099334716797
    20     8.374628e+02     8.462346e-02
 * time: 1.2241699695587158
    21     8.374628e+02     1.975340e-04
 * time: 1.22878098487854
    22     8.374628e+02     4.682667e-05
 * time: 1.2332720756530762
    23     8.374628e+02     9.691217e-06
 * time: 1.2378239631652832
    24     8.374628e+02     2.150332e-05
 * time: 1.240813970565796
    25     8.374628e+02     5.243516e-06
 * time: 1.243786096572876
 * 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.