C++: a GP-NEB band on a Lennard-Jones cluster¶
This tutorial calls the C++ entry point gpr::neb::gpNEB twice on the same reaction: once as a plain climbing-image nudged elastic band (NEB), which evaluates the potential at every image of every iteration, and once in the GP_OIE mode, which relaxes the band on a Gaussian-process (GP) surrogate and evaluates one image per outer iteration.
Both find the same barrier; the GP band needs a fraction of the oracle calls.
The program is compiled and run by the documentation test.
The C++ classes have no version contract; the versioned boundary is the C API.
Run a GP-NEB band from C++ lists the fields of GPNEBConfig, and Run a GP-NEB band describes every mode.
What you need¶
A build tree of GPRidge. A C++ program that includes
gpr/neb/GPNEB.hneeds the include paths and macros that the build givesgpr/neb/GPNEB.cpp; take them fromcompile_commands.jsonin the build directory, add the source root and the build directory to the include path, and link with-lgpridge. The documentation test builds the program below that way. With an installed prefix the headers are under<gpridge/...>; see C++ API.A C++20 compiler.
The reaction¶
Five atoms bound by a 12-6 Lennard-Jones potential with \(\epsilon = 1\) eV and \(\sigma = 1\) Angstrom form a trigonal bipyramid.
The lowest path between two orientations of the bipyramid is a Berry pseudorotation: two axial atoms move into the equatorial plane while two equatorial atoms take the axis, and the fifth atom pivots in place.
The endpoints are the two files of examples/lj-pentamer/, reactant.con (axis along \(z\)) and product.con (axis along \(y\)), copied into the program.
First gprn band runs the same reaction with the command-line driver, which reports a barrier of 0.6232 eV.
Its potential is shifted to zero at a 15 Angstrom cutoff; the one here is not, which changes the barrier by much less than the 10 meV the program allows.
The program¶
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <vector>
#include "gpr/ParameterDefaults.h"
#include "gpr/neb/GPNEB.h"
#include "gpr/potentials/AnalyticalPotentials.h"
namespace {
constexpr int kAtoms = 5;
// The two trigonal bipyramids of examples/lj-pentamer, in Angstrom.
const double kReactant[3 * kAtoms] = {
15.000000, 15.000000, 15.912939, 15.000000, 15.000000, 14.087061,
15.648996, 15.000000, 15.000000, 14.675502, 15.562047, 15.000000,
14.675502, 14.437953, 15.000000};
const double kProduct[3 * kAtoms] = {
14.675502, 15.000000, 15.562047, 14.675502, 15.000000, 14.437953,
15.648996, 15.000000, 15.000000, 15.000000, 15.912939, 15.000000,
15.000000, 14.087061, 15.000000};
// Lennard-Jones with epsilon = 1 eV and sigma = 1 Angstrom.
std::pair<double, Eigen::VectorXd> lennardJones(const Eigen::VectorXd& x)
{
std::vector<double> flat(x.data(), x.data() + x.size());
auto [energy, gradient] = gpr::potentials::lj_energy_gradient(flat);
return {energy, Eigen::Map<Eigen::VectorXd>(gradient.data(), x.size())};
}
// The kernel sees five moving atoms of one type.
gpr::AtomsConfiguration fiveMovingAtoms(const Eigen::VectorXd& x)
{
gpr::AtomsConfiguration atoms;
atoms.positions.resize(1, 3 * kAtoms);
for (int k = 0; k < 3 * kAtoms; ++k) atoms.positions(0, k) = x(k);
atoms.is_frozen.setConstant(1, kAtoms, MOVING_ATOM);
atoms.id.resize(1, kAtoms);
for (int i = 0; i < kAtoms; ++i) atoms.id(0, i) = i;
atoms.atomicNrs.setConstant(1, kAtoms, 10);
atoms.type.setZero(1, kAtoms);
atoms.atoms_mov.resize(kAtoms);
atoms.atoms_mov.type.setZero(1, kAtoms);
for (int i = 0; i < kAtoms; ++i)
gpr::coord::set(atoms.atoms_mov.positions, 0, i,
gpr::coord::at(atoms.positions, i));
atoms.pairtype.setZero(1, 1);
atoms.n_pt = 1;
return atoms;
}
} // namespace
int main()
{
const Eigen::VectorXd reactant =
Eigen::Map<const Eigen::VectorXd>(kReactant, 3 * kAtoms);
const Eigen::VectorXd product =
Eigen::Map<const Eigen::VectorXd>(kProduct, 3 * kAtoms);
int calls = 0;
auto oracle = [&calls](const Eigen::VectorXd& x) {
++calls;
return lennardJones(x);
};
gpr::neb::GPNEBConfig config;
config.n_atoms = kAtoms;
config.atomic_numbers.assign(kAtoms, 10);
config.climbing_image = true;
config.conv_tol = 0.05;
config.verbose = false;
// A plain climbing-image band: every image is evaluated every iteration.
config.mode = gpr::neb::NEBMode::Standard;
config.max_outer_iter = 2000;
const auto plain = gpr::neb::gpNEB(oracle, reactant, product, 7, config);
const int plain_calls = calls;
std::printf("standard: converged %d, barrier %.4f eV, %d oracle calls\n",
plain.converged ? 1 : 0, plain.barrier_energy, plain_calls);
// GP-OIE: the band relaxes on the GP, and the oracle measures one image
// per outer iteration.
gpr::InputParameters parameters;
gpr::applyGprdDimerDefaults(parameters);
parameters.report_level.value = 0;
const gpr::AtomsConfiguration atoms = fiveMovingAtoms(reactant);
config.gp_setup = [parameters, atoms](gpr::InputParameters& p,
gpr::AtomsConfiguration& a) {
p = parameters;
a = atoms;
};
config.mode = gpr::neb::NEBMode::GP_OIE;
config.max_outer_iter = 200;
calls = 0;
const auto gp = gpr::neb::gpNEB(oracle, reactant, product, 7, config);
std::printf("gp_oie: converged %d, barrier %.4f eV, %d oracle calls, "
"%d refits\n",
gp.converged ? 1 : 0, gp.barrier_energy, gp.oracle_calls,
gp.hyperparameter_refits);
const bool ok = plain.converged && gp.converged &&
gp.oracle_calls == calls && calls < plain_calls &&
std::abs(gp.barrier_energy - plain.barrier_energy) < 1e-2;
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
standard: converged 1, barrier 0.6232 eV, <n> oracle calls
gp_oie: converged 1, barrier <s> eV, <n> oracle calls, <n> refits
In the run that checked this page the plain band took 93 oracle calls and the GP band 13, with 4 hyperparameter refits. The counts can differ by a few with another build or BLAS library; the program fails unless the GP band needs fewer calls and agrees on the barrier to 10 meV.
What each piece does¶
The oracle¶
gpNEB takes any callable that maps a flat position vector \((x_1, y_1, z_1, \dots, x_5, y_5, z_5)\) to an energy and its gradient \(\partial E / \partial x\).
lennardJones wraps gpr::potentials::lj_energy_gradient from gpr/potentials/AnalyticalPotentials.h.
The lambda counts its calls, so the program can compare that count with oracle_calls of the result.
The configuration¶
n_atoms and atomic_numbers describe the atoms.
climbing_image turns on the climbing image, which converges to the saddle instead of sitting next to it, and conv_tol is the force tolerance in eV per Angstrom.
The band has 7 intermediate images between the fixed endpoints.
NEBMode::Standard needs nothing else.
NEBMode::GP_OIE, the default mode, also needs gp_setup: a callback that fills the gpr::InputParameters and the gpr::AtomsConfiguration the GP is built from.
Without it gpNEB throws std::invalid_argument.
The GP setup¶
gpr::applyGprdDimerDefaults from gpr/ParameterDefaults.h fills the parameter block with the values an unflagged gprd run uses: the kernel, its priors, the hyperparameter optimiser and the trust settings.
report_level = 0 silences the fit.
fiveMovingAtoms builds the atom configuration of the kernel: the positions of one geometry, every atom moving (MOVING_ATOM), every atom of type 0, and one pair type (pairtype is the \(1 \times 1\) matrix 0 and n_pt is 1).
The kernel compares geometries by their inverse interatomic distances, so the pair types decide which distances share a length scale; one type gives every pair the same one.
A system with frozen atoms lists them in is_frozen and in atoms_froz_active; gprn builds this structure from the .con frame.
The result¶
converged is true when the climbing-image band meets conv_tol on the true potential.
barrier_energy is the energy of the highest image minus that of the reactant, and reverse_barrier_energy the same from the product.
oracle_calls counts every call of the oracle, the two endpoints included, and hyperparameter_refits the fits of the GP.
path holds the final band.
The full list of fields is the GPNEBResult declaration in gpr/neb/GPNEB.h.
Where to go next¶
Run a GP-NEB band describes the modes, acquisition rules and the command-line flags of
gprn.OCI-NEB: a climbing image with dimer bursts adds dimer refinement of the climbing image to a GP-OIE band through the C API.
C API: from a GP model to a saddle point uses the versioned C interface.