Run a GP-NEB band from C++

This page shows how to call the C++ entry point gpr::neb::gpNEB with an oracle lambda, and which GPNEBConfig fields choose the mode, the start path and the evaluation pattern. The command-line and Python routes are in Run a GP-NEB band. The C++ classes have no version contract; the versioned boundary is the C API, and C++ API says where the headers are.

Run a band on the Muller-Brown surface

gpNEB(oracle, initial, final, n_images, config, session) takes the oracle, the two endpoints as flat position vectors, the number of intermediate images and a GPNEBConfig. session is optional and defaults to nullptr. The oracle maps a position vector to an energy and a gradient. The result holds converged, oracle_calls and barrier_energy.

The program below runs NEBMode::Standard, which evaluates every image at every iteration, on the two-dimensional Muller-Brown potential from gpr/potentials/AnalyticalPotentials.h. It counts the oracle calls itself and checks that the result agrees. The values are in the units of that potential. config.conv_tol is the force tolerance, and 0.5 is loose for a surface whose energies run to a few hundred units.

#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>

#include "gpr/neb/GPNEB.h"
#include "gpr/potentials/AnalyticalPotentials.h"

int main()
{
    int calls = 0;
    auto oracle = [&calls](const Eigen::VectorXd& x) {
        ++calls;
        std::vector<double> xy = {x(0), x(1)};
        auto [e, g] = gpr::potentials::muller_brown_energy_gradient(xy);
        Eigen::VectorXd grad = Eigen::Map<Eigen::VectorXd>(g.data(), 2);
        return std::make_pair(e, grad);
    };

    gpr::neb::GPNEBConfig config;
    config.mode = gpr::neb::NEBMode::Standard;
    config.max_outer_iter = 400;
    config.conv_tol = 0.5;
    config.verbose = false;

    Eigen::VectorXd min1(2), min2(2);
    min1 << -0.558224, 1.441726;
    min2 << 0.623499, 0.028038;

    auto result = gpr::neb::gpNEB(oracle, min1, min2, 7, config);

    std::cout << "converged " << result.converged << " oracle_calls "
              << result.oracle_calls << " counted " << calls << " barrier "
              << result.barrier_energy << "\n";
    return (result.converged && result.oracle_calls == calls) ? EXIT_SUCCESS
                                                               : EXIT_FAILURE;
}

The documentation test compiles the program with the include paths and macros that the build tree gives gpr/neb/GPNEB.cpp, read from compile_commands.json, and links it with -lgpridge and the build’s librgpot (subprojects/rgpot/CppCore/librgpot.so). muller_brown_energy_gradient is inline in gpr/potentials/AnalyticalPotentials.h and calls rgpot’s builtin model, so the example’s link line names that library. That header is part of the library sources. GPNEB.h includes headers of the subprojects (minimage.h, linkcell, readcon-core, rgmin and others), so a program of your own needs the same include flags. With an installed prefix the NEB header is <gpridge/GPNEB.h>. The installed library evaluates this surface through gpr_potential_muller_brown. The program prints:

converged 1 oracle_calls 163 counted 163 barrier 106.035

config.verbose defaults to true and prints the progress report to standard output; the program turns it off.

Choose the mode

config.mode takes the NEBMode values Standard, AIE, OIE, GP_AIE, GP_OIE and GP_LOCAL; the modes are described in Run a GP-NEB band.

The GP modes need a GP. config.gp_setup is the callback that builds the parameters and the atom configuration the GP is initialised with. It is empty by default, and gpNEB throws std::invalid_argument when config.mode is NEBMode::GP_AIE or NEBMode::GP_OIE (the default) and gp_setup is empty, because there would be no GP to fit (GPNEBConfig::gp_setup, gpr/neb/GPNEB.h). Set gp_setup, or choose NEBMode::Standard, AIE or OIE, which run without a GP. gprn fills gp_setup from the loaded .con frame. C++: a GP-NEB band on a Lennard-Jones cluster fills it by hand and runs GP_OIE. A program that needs the GP modes behind a stable interface should call the C API: gpr_neb_search_gp runs GPR_NEB_GP_AIE and GPR_NEB_GP_OIE, and gpr_neb_search_local runs GPR_NEB_GP_LOCAL. gpr_neb_search accepts GPR_NEB_GP_OIE and GPR_NEB_GP_AIE when n_coords is a multiple of 3: it builds all-moving atoms with gpr_atoms_create_simple and calls gpr_neb_search_gp. Only GPR_NEB_GP_LOCAL returns GPR_ERR_INVALID_ARG from gpr_neb_search in that case. A GP mode whose n_coords is not a multiple of 3 returns GPR_ERR_INVALID_ARG as well. See C API.

Set the start path

By default gpNEB interpolates linearly between the endpoints. When config.n_atoms is greater than zero and config.use_idpp is true (the default is false), the interior images are then replaced by an IDPP interpolation of the n_atoms atoms (NEBPath::initializeIDPP). config.use_sidpp selects the sequential variant instead, and config.sidpp_geodesic selects its inverse-distance form (GPNEBConfig comments in gpr/neb/GPNEB.h). To start from your own band, set config.has_initial_path = true and fill config.initial_path with one image per row, endpoints included.

Choose the evaluation pattern

In the OIE modes config.evals_per_iter (default 1) is the number of images evaluated per outer iteration, and config.use_triplet (default false) evaluates the images i-1, i and i+1 instead of the single image i, for a better tangent. config.acquisition selects the rule that picks the image, and its default is AcquisitionType::TPE; the values are in the acquisition table of Run a GP-NEB band.