===================================================================== Fit a free-energy surface on collective variables and estimate a rate ===================================================================== ``gpr_fes_t`` is a Gaussian process over a free energy :math:`F(s)` on a low-dimensional space of collective variables :math:`s`. It takes estimates of :math:`F`, and optionally of its gradient (the negative mean force), each with its own variance. It predicts :math:`F`, :math:`\nabla F` and the latent variance of :math:`F`, and samples the posterior jointly along a path to put an interval on an overdamped Kramers-Langer rate. This guide follows one surface through that lifecycle and states the layout and units the arrays need. **Status:** implemented (ABI 1.23). The squared-exponential kernel is the only one supported; another kernel returns ``GPR_ERR_UNSUPPORTED``. The reference is :doc:`C API <../reference/bindings/c>`. What the surrogate is given --------------------------- The surrogate learns :math:`F`, never a bias. A metadynamics bias :math:`V(s)`, an umbrella restraint or a raw histogram has to be turned into an estimate of :math:`F` first, by whatever reweighting the sampling method calls for; gpridge does not do that step. .. table:: +--------------------+-----------------------+-----------------------------+-------------------+ | Array | Shape (row-major) | Unit | Required | +====================+=======================+=============================+===================+ | ``s`` | ``n_obs x dimension`` | the unit of each variable | yes | +--------------------+-----------------------+-----------------------------+-------------------+ | ``F`` | ``n_obs`` | energy, e.g. kJ/mol | yes | +--------------------+-----------------------+-----------------------------+-------------------+ | ``F_noise_var`` | ``n_obs`` | energy squared | no | +--------------------+-----------------------+-----------------------------+-------------------+ | ``grad_F`` | ``n_obs x dimension`` | energy per unit of variable | with the next two | +--------------------+-----------------------+-----------------------------+-------------------+ | ``grad_noise_var`` | ``n_obs x dimension`` | its square | with ``grad_F`` | +--------------------+-----------------------+-----------------------------+-------------------+ | ``grad_present`` | ``n_obs``, 0 or 1 | none | with ``grad_F`` | +--------------------+-----------------------+-----------------------------+-------------------+ The library converts no units, so every array shares one energy unit and one unit per variable, and ``beta`` of the rate is :math:`1/k_BT` in the inverse energy unit. The rate takes a diagonal diffusion tensor, one entry per variable in the squared unit of that variable per time, so variables in different units each get a coefficient in their own unit; the unstable rate at the saddle is the negative eigenvalue of :math:`\beta D H_\ddagger`. Rows with ``grad_present`` 0 are values only; their gradient entries are not read. ``gpr_model_config_t.sigma2`` and ``.jitter_sigma2`` add to the variance of every :math:`F` observation, in energy squared, so set them small when the variances are measured. Gradient components carry a different unit, energy squared per unit of their variable squared, so they get their own added variance: the ``grad_noise_var`` argument of ``gpr_fes_create``, one entry per variable, or by default :math:`(\sigma^2 + \sigma^2_\mathrm{jitter}) / \mathrm{spread}_k^2` with :math:`\mathrm{spread}_k` the standard deviation of the training points along variable :math:`k`. Either way, expressing a variable in another unit rescales its gradient noise with it and leaves the predictions unchanged. Data from rgflux, d-SEAMS and other tools ----------------------------------------- The surrogate reads arrays, not the files of any tool, so each export is mapped onto the roles above: - **Collective variables** are the columns of ``s``. A structural order parameter from d-SEAMS, a distance or a path variable all qualify, one column each, in their own units. - **Free-energy estimates** go to ``F``. An estimate on bins is used at the bin centres, and its variance (from block averaging or bootstrap) goes to ``F_noise_var``. Unsampled bins are left out rather than given a large value. - **Mean forces**, as an adaptive-biasing or thermodynamic-integration run produces them, are :math:`-\nabla F`; negate them into ``grad_F``. - **Sample weights** without a variance estimate can be turned into variances with ``gpr_fes_noise_from_weights_checked``, which treats the variance as inversely proportional to the weight and ignores correlation between samples. Measured variances are better when they exist. Before training, check that every exported quantity is in one energy unit (kJ/mol and kcal/mol are both common) and that the sign of each force matches :math:`\nabla F`. Train, predict and estimate a rate in C --------------------------------------- The program fits the double well :math:`F = 10\,((x^2 - 1)^2 + y^2)` kJ/mol from values and gradients on a 7 x 5 grid, predicts at a point between the basins, and estimates the rate from :math:`(-1, 0)` to :math:`(1, 0)` through the saddle at the origin at 300 K with :math:`D = \mathrm{diag}(1, 1)` (variable unit squared per time unit). .. code:: c #include #include #include #include "gpr/capi/gpridge_c.h" static double well(double x, double y) { return 10.0 * ((x * x - 1.0) * (x * x - 1.0) + y * y); } int main(void) { double s[70], F[35], F_var[35], grad[70], grad_var[70]; uint8_t present[35]; int n = 0; for (int i = 0; i < 7; ++i) { for (int j = 0; j < 5; ++j, ++n) { const double x = -1.5 + 0.5 * i, y = -1.0 + 0.5 * j; s[2 * n] = x; s[2 * n + 1] = y; F[n] = well(x, y); F_var[n] = 1e-4; grad[2 * n] = 40.0 * x * (x * x - 1.0); grad[2 * n + 1] = 20.0 * y; grad_var[2 * n] = grad_var[2 * n + 1] = 1e-4; present[n] = 1; } } gpr_model_config_t model = gpr_model_config_defaults(); model.sigma2 = 1e-8; model.jitter_sigma2 = 1e-8; gpr_fes_t fes = gpr_fes_create(2, &model, NULL, NULL, 0); if (!fes || gpr_fes_train_checked(fes, s, 70, F, 35, F_var, 35, grad, 70, grad_var, 70, present, 35, n, 2) != GPR_SUCCESS) { fprintf(stderr, "%s\n", gpr_last_error_message()); return 1; } const double query[2] = {0.4, 0.1}; double mean, mean_grad[2], var; gpr_fes_predict_batch_checked(fes, query, 2, &mean, 1, mean_grad, 2, &var, 1, 1, 2); printf("F(0.4, 0.1) = %.3f kJ/mol (exact %.3f), sd %.1e\n", mean, well(0.4, 0.1), sqrt(var)); double path[18]; for (int i = 0; i < 9; ++i) { path[2 * i] = -1.0 + 0.25 * i; path[2 * i + 1] = 0.0; } gpr_fes_rate_config_t rate; gpr_fes_rate_config_init(&rate, sizeof(rate)); rate.beta = 1.0 / (0.0083144626 * 300.0); rate.seed = 2026; const double diffusion[2] = {1.0, 1.0}; gpr_fes_rate_report_t report; report.struct_size = sizeof(report); if (gpr_fes_rate_path_checked(fes, path, 18, 9, 2, 0, 8, diffusion, 2, &rate, &report) != GPR_SUCCESS) { fprintf(stderr, "%s\n", gpr_last_error_message()); return 1; } printf("barrier %.3f kJ/mol, 95%% interval [%.3f, %.3f]\n", report.barrier_mean, report.barrier_lower, report.barrier_upper); printf("rate %.4e per time unit from %d of %d draws\n", report.rate_mean, report.samples_accepted, report.samples_requested); if (report.rate_lower <= report.rate_mean && report.rate_mean <= report.rate_upper) printf("rate interval brackets the mean\n"); gpr_fes_destroy(fes); return 0; } The exact barrier is 10 kJ/mol. With :math:`H_A = \mathrm{diag}(80, 20)` and :math:`H_\ddagger = \mathrm{diag}(-40, 20)` the rate is :math:`\beta D_x \cdot 40 / 2\pi \cdot \sqrt{2}\, e^{-10\beta}`, about :math:`6.55 \times 10^{-2}` per time unit at 300 K; the posterior mean lies close to it and the interval says how far the data allow it to move. The same surface in Python -------------------------- .. code:: python import numpy as np from gpridge import FreeEnergySurrogate xs, ys = np.meshgrid(np.linspace(-1.5, 1.5, 7), np.linspace(-1.0, 1.0, 5)) s = np.column_stack([xs.ravel(), ys.ravel()]) F = 10.0 * ((s[:, 0] ** 2 - 1.0) ** 2 + s[:, 1] ** 2) grad = np.column_stack([40.0 * s[:, 0] * (s[:, 0] ** 2 - 1.0), 20.0 * s[:, 1]]) fes = FreeEnergySurrogate(2, noise_variance=1e-8, jitter_variance=1e-8) fes.train(s, F, F_noise_var=np.full(len(s), 1e-4), grad_F=grad, grad_noise_var=np.full(s.shape, 1e-4)) path = np.column_stack([np.linspace(-1.0, 1.0, 9), np.zeros(9)]) report = fes.rate_path(path, 0, 8, beta=1.0 / (0.0083144626 * 300.0), diffusion=[1.0, 1.0], seed=2026) assert report.barrier_lower <= 10.0 <= report.barrier_upper print(f"barrier {report.barrier_mean:.3f} kJ/mol") When the rate is refused ------------------------ The path only guides the saddle search: the saddle is the stationary point of the posterior mean with one negative Hessian eigenvalue, found by following Hessian eigenvectors from the highest interior path point, so it need not be a path point. ``gpr_fes_rate_path_checked`` returns ``GPR_ERR_NOT_CONVERGED`` when the search does not reach ``saddle_tolerance``, reaches a stationary point that is not an index-one saddle, or reaches one more than a length scale from the path or beyond the reactant or product. Draw the path across the barrier between the two basins, or add data where the saddle should be. At the saddle, a mean barrier that is not positive returns ``GPR_ERR_UNSUPPORTED``: the reactant is not below the saddle. A draw counts only when its barrier is positive, the reactant Hessian is positive definite and the saddle Hessian has exactly one negative eigenvalue. When fewer than 90% of the draws count, the call returns ``GPR_ERR_UNSUPPORTED`` and writes no report: the data do not resolve the saddle, or the reactant is not a resolved minimum. Add data near the reactant and the saddle rather than lowering the threshold.