Loading...
Searching...
No Matches
GprEngineAbi.cpp File Reference
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <exception>
#include <limits>
#include <memory>
#include <new>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
#include "ActiveGP.h"
#include "AotiPackageIsa.h"
#include "NestedEngineOracle.h"
#include "gpr/auxiliary/ProblemSetUp.h"
#include "gpr/capi/gpridge_c.h"
#include "gpr/neb/Session.h"
#include "gpr_engine_curvature.h"
#include "gpridge_version.h"
#include "rgpot_engine_c_abi.h"

Go to the source code of this file.

Classes

struct  RgpotEnginePot

Macros

#define RGPOT_ENGINE_BUILD

Functions

int rgpot_engine_abi_version (void)
int rgpot_engine_abi_compatible (const RgpotEngineAbiStamp *stamp)
int rgpot_engine_available (void)
RgpotEnginePot * rgpot_engine_create (const void *config, std::size_t config_len, char *errbuf, std::size_t errlen)
void rgpot_engine_destroy (RgpotEnginePot *pot)
int rgpot_engine_set_callbacks (RgpotEnginePot *pot, const RgpotEngineCallbacks *callbacks)
unsigned long long rgpot_engine_model_state (const RgpotEnginePot *pot)
size_t rgpot_engine_last_error (const RgpotEnginePot *pot, char *buf, size_t len)
int rgpot_engine_caps (const RgpotEnginePot *pot, RgpotEngineCaps *out)
const char * rgpot_engine_name (void)
const char * rgpot_engine_version (void)
size_t rgpot_engine_model_digest (const RgpotEnginePot *pot, char *buf, size_t len)
int rgpot_engine_set_num_threads (RgpotEnginePot *pot, int intra_op, int inter_op)
int rgpot_engine_set_charge_spin (RgpotEnginePot *pot, int charge, int spin)
int rgpot_engine_select_orbitals (RgpotEnginePot *pot, long long key)
int rgpot_engine_copy_orbitals (RgpotEnginePot *pot, long long from_key, long long to_key)
long rgpot_engine_neighbor_options (const RgpotEnginePot *pot, RgpotEngineNeighborOptions *opts, long capacity)
int rgpot_engine_hvp (RgpotEnginePot *pot, long nAtoms, const double *positions, const int *atomicNrs, const double *direction, double *hv_out, const double *box)
int rgpot_engine_gpr_stats (const RgpotEnginePot *pot, RgpotEngineGprStats *stats_out)
int rgpot_engine_force (RgpotEnginePot *pot, long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energy, double *variance, const double *box, rgpot_engine_coord_transform transform, void *transform_user)
int rgpot_engine_force_batch (RgpotEnginePot *pot, long nSystems, long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energies, double *variances, const double *boxes, rgpot_engine_coord_transform transform, void *transform_user)
int rgpot_engine_hessian_checked (RgpotEnginePot *pot, long nAtoms, const double *positions, size_t positions_len, const int *atomicNrs, double *hessian_out, size_t hessian_len, const double *box)
int rgpot_engine_hessian_action_checked (RgpotEnginePot *pot, long nAtoms, const double *positions, size_t positions_len, const int *atomicNrs, const double *direction, size_t direction_len, double *hv_out, size_t hv_len, const double *box, unsigned long long *ops_out, double *scale_out)
int rgpot_engine_prediction_supported_checked (RgpotEnginePot *pot, long nAtoms, const double *positions, size_t positions_len, const int *atomicNrs, const double *box, int *supported_out)
int rgpot_engine_predict_variance_checked (RgpotEnginePot *pot, long nSystems, long nAtoms, const double *positions, size_t positions_len, const int *atomicNrs, double *var_energy_out, size_t var_energy_len, double *var_gradient_out, size_t var_gradient_len, const double *boxes)
int rgpot_engine_hint (RgpotEnginePot *pot, const void *msg, std::size_t msg_len, char *errbuf, std::size_t errlen)

Macro Definition Documentation

◆ RGPOT_ENGINE_BUILD

#define RGPOT_ENGINE_BUILD

Definition at line 12 of file GprEngineAbi.cpp.

Function Documentation

◆ rgpot_engine_abi_compatible()

int rgpot_engine_abi_compatible ( const RgpotEngineAbiStamp * stamp)

Definition at line 1550 of file GprEngineAbi.cpp.

1551{
1552 if (stamp == nullptr) return 0;
1553 return stamp->abi_major == RGPOT_ENGINE_ABI_VERSION &&
1554 stamp->layout_revision == RGPOT_ENGINE_ABI_LAYOUT_REVISION &&
1555 RGPOT_ENGINE_ABI_MINOR <= stamp->abi_minor
1556 ? 1
1557 : 0;
1558}

◆ rgpot_engine_abi_version()

int rgpot_engine_abi_version ( void )

Definition at line 1545 of file GprEngineAbi.cpp.

1546{
1547 return RGPOT_ENGINE_ABI_VERSION;
1548}

◆ rgpot_engine_available()

int rgpot_engine_available ( void )

Definition at line 1560 of file GprEngineAbi.cpp.

1561{
1562 return 1;
1563}

◆ rgpot_engine_caps()

int rgpot_engine_caps ( const RgpotEnginePot * pot,
RgpotEngineCaps * out )

Definition at line 1729 of file GprEngineAbi.cpp.

1730{
1731 if (pot == nullptr || out == nullptr || out->size < sizeof(unsigned))
1732 return RGPOT_ENGINE_ERR_INVALID;
1733 RgpotEngineCaps caps{};
1734 caps.size = sizeof(RgpotEngineCaps);
1735 caps.reentrancy = 1;
1736 caps.per_image_instances = 0;
1737 caps.batched = 0;
1738 caps.periodic = 0;
1739 caps.stress = 0;
1740 caps.group_collective = 0;
1741 caps.orbital_selection = 0;
1742 caps.orbital_copy = 0;
1743 caps.analytic_hvp = pot->residual != nullptr ? 1 : 0;
1744 const unsigned n = out->size < sizeof(RgpotEngineCaps)
1745 ? out->size
1746 : sizeof(RgpotEngineCaps);
1747 std::memcpy(out, &caps, n);
1748 out->size = n;
1749 return RGPOT_ENGINE_OK;
1750}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Potential wrappers and the batched oracle interface.

◆ rgpot_engine_copy_orbitals()

int rgpot_engine_copy_orbitals ( RgpotEnginePot * pot,
long long from_key,
long long to_key )

Definition at line 1798 of file GprEngineAbi.cpp.

1800{
1801 (void)from_key;
1802 (void)to_key;
1803 if (pot == nullptr) return RGPOT_ENGINE_ERR_INVALID;
1804 pot->last_error = "gpr engine: backend has no orbital copy";
1805 return RGPOT_ENGINE_ERR_UNSUPPORTED;
1806}

◆ rgpot_engine_create()

RgpotEnginePot * rgpot_engine_create ( const void * config,
std::size_t config_len,
char * errbuf,
std::size_t errlen )

Definition at line 1565 of file GprEngineAbi.cpp.

1567{
1568 // The config arm will be a capnp GprParams message whose oracle
1569 // field is a nested PotentialSpec. Until that schema lands, accept
1570 // the interim gate struct so a host can at least turn on the
1571 // convergence certificate; dispatch is on the magic word, so the
1572 // capnp message can arrive here later without ambiguity. A config
1573 // that is neither is not silently ignored: a caller that meant to
1574 // configure the engine and did not would otherwise get a surface
1575 // that never checks its own convergence claim.
1576 try {
1577 auto pot = std::make_unique<RgpotEnginePot>();
1578 pot->session = std::make_shared<gpr::neb::ActiveLearningSession>();
1579 if (config != nullptr && config_len > 0) {
1580 unsigned magic = 0;
1581 if (config_len >= sizeof(unsigned)) {
1582 std::memcpy(&magic, config, sizeof(unsigned));
1583 }
1585 if (!create_residual_session(pot.get(), config, config_len,
1586 errbuf, errlen)) {
1587 return nullptr;
1588 }
1589 return pot.release();
1590 }
1591#ifdef GPR_ENGINE_HAS_POTENTIAL_SPEC
1592 // A capnp message is word-aligned and a multiple of a word;
1593 // the interim struct is neither, in general. Dispatch on that
1594 // before the magic check so a GprParams message is read as
1595 // one rather than rejected for the wrong size.
1596 if (config_len % sizeof(capnp::word) == 0 &&
1597 config_len != sizeof(RgpotEngineGprGate) &&
1598 magic != RGPOT_ENGINE_GPR_GATE_MAGIC) {
1599 try {
1600 const kj::ArrayPtr<const capnp::word> words(
1601 reinterpret_cast<const capnp::word*>(config),
1602 config_len / sizeof(capnp::word));
1603 capnp::FlatArrayMessageReader reader(words);
1604 const auto params = reader.getRoot<::GprParams>();
1605
1606 if (params.getSigmaLoFactor() > 0.0)
1607 pot->config.sigma_lo_factor = params.getSigmaLoFactor();
1608 if (params.getSigmaHiFactor() > 0.0)
1609 pot->config.sigma_hi_factor = params.getSigmaHiFactor();
1610 pot->config.certify_below_force =
1611 params.getCertifyBelowForce();
1612 pot->config.refit_training_revisions =
1613 params.getRefitTrainingRevisions();
1614 if (!(pot->config.sigma_hi_factor >=
1615 pot->config.sigma_lo_factor)) {
1617 "gpr engine: need sigmaLoFactor <= "
1618 "sigmaHiFactor");
1619 return nullptr;
1620 }
1621
1622 std::string library, oerr;
1623 std::vector<unsigned char> ocfg;
1624 if (!oracleFromSpec(params.getOracle(), library, ocfg,
1625 oerr)) {
1627 ("gpr engine: " + oerr).c_str());
1628 return nullptr;
1629 }
1630 pot->nested_oracle =
1631 pot::NestedEngineOracle::open(library, ocfg, oerr);
1632 if (pot->nested_oracle == nullptr) {
1634 ("gpr engine: oracle " + oerr).c_str());
1635 return nullptr;
1636 }
1637 return pot.release();
1638 } catch (const kj::Exception&) {
1639 // A zero-filled buffer is not a Cap'n Proto root. The
1640 // KJ exception text includes a process-specific stack, so
1641 // malformed wire data receives the stable diagnosis below.
1642 // Standard exceptions from GprParams validation and nested
1643 // engine construction escape to the outer handler with
1644 // their actionable message intact.
1646 "gpr engine: config is neither an "
1647 "RgpotEngineGprGate nor a GprParams message");
1648 return nullptr;
1649 }
1650 }
1651#endif
1652 if (config_len != sizeof(RgpotEngineGprGate)) {
1654 "gpr engine: config is neither an RgpotEngineGprGate "
1655 "nor a GprParams message");
1656 return nullptr;
1657 }
1658 RgpotEngineGprGate gate{};
1659 std::memcpy(&gate, config, sizeof(gate));
1660 if (gate.magic != RGPOT_ENGINE_GPR_GATE_MAGIC) {
1661 set_err(errbuf, errlen, "gpr engine: bad config magic");
1662 return nullptr;
1663 }
1664 if (gate.version != RGPOT_ENGINE_GPR_GATE_VERSION) {
1666 "gpr engine: unsupported config "
1667 "version");
1668 return nullptr;
1669 }
1670 if (!(gate.sigmaLoFactor > 0.0) || !(gate.sigmaHiFactor > 0.0) ||
1671 gate.sigmaHiFactor < gate.sigmaLoFactor) {
1673 "gpr engine: need 0 < sigmaLoFactor <= sigmaHiFactor");
1674 return nullptr;
1675 }
1676 pot->config.sigma_lo_factor = gate.sigmaLoFactor;
1677 pot->config.sigma_hi_factor = gate.sigmaHiFactor;
1678 pot->config.certify_below_force = gate.certifyBelowForce;
1679 pot->config.refit_training_revisions =
1680 static_cast<std::uint64_t>(gate.refitTrainingRevisions);
1681 }
1682 return pot.release();
1683 } catch (const std::exception& e) {
1684 set_err(errbuf, errlen, e.what());
1685 return nullptr;
1686 }
1687}
static void set_err(char *errbuf, size_t errlen, const char *msg)
static std::unique_ptr< NestedEngineOracle > open(const std::string &library, const std::vector< unsigned char > &config, std::string &err)
size_t char size_t errlen
size_t char * errbuf
size_t config_len
#define RGPOT_ENGINE_GPR_RESIDUAL_PRIOR_MAGIC

◆ rgpot_engine_destroy()

void rgpot_engine_destroy ( RgpotEnginePot * pot)

Definition at line 1689 of file GprEngineAbi.cpp.

1690{
1691 delete pot;
1692}

◆ rgpot_engine_force()

int rgpot_engine_force ( RgpotEnginePot * pot,
long nAtoms,
const double * positions,
const int * atomicNrs,
double * forces,
double * energy,
double * variance,
const double * box,
rgpot_engine_coord_transform transform,
void * transform_user )

Definition at line 1910 of file GprEngineAbi.cpp.

1916 {
1917 if (pot == nullptr || positions == nullptr || energy == nullptr ||
1918 nAtoms <= 0) {
1919 return -1;
1920 }
1921 if (pot->residual) {
1922 if (nAtoms != pot->residual->n_atoms) {
1923 pot->last_error = "gpr engine: nAtoms mismatch with saddle";
1924 return RGPOT_ENGINE_ERR_INVALID;
1925 }
1926 const std::size_t d = pot->residual->n_dof;
1927 std::size_t cart = 0;
1928 std::vector<double> R;
1929 std::string scratch_err;
1930 if (!residual_callback_scratch(static_cast<std::size_t>(nAtoms), d, 1,
1931 positions, cart, R, scratch_err,
1932 false)) {
1933 pot->last_error = scratch_err;
1934 return RGPOT_ENGINE_ERR_INVALID;
1935 }
1936 std::vector<double> cell(9, 0.0);
1937 if (box != nullptr) std::copy(box, box + 9, cell.begin());
1938 if (transform != nullptr) {
1939 const int rc =
1940 transform(transform_user, nAtoms, R.data(), cell.data());
1941 if (rc != 0) return rc;
1942 }
1943 std::vector<double> geom;
1944 residual_pack_prefix(R, 1, d, cart, geom);
1945 if (pot->residual->phase == RGPOT_GPR_PHASE_SURROGATE) {
1946 std::string err;
1947 // Zero would report the surrogate as exact and floor its
1948 // uncertainty. The energy variance is the posterior's; with no
1949 // observations the answer is the reference quadratic. Gradient
1950 // variance stays on rgpot_engine_predict_variance_checked.
1951 if (variance != nullptr && !pot->residual->rows_e.empty()) {
1952 std::vector<double> var_g(d);
1953 const int vrc = pot->residual->predict_variance(
1954 geom.data(), d, 1, variance, 1, var_g.data(), d, err);
1955 if (vrc != RGPOT_ENGINE_OK) {
1956 pot->last_error = err;
1957 return vrc;
1958 }
1959 }
1960 const int rc =
1961 pot->residual->predict(geom.data(), 1, energy, forces, err);
1962 if (rc != RGPOT_ENGINE_OK) {
1963 pot->last_error = err;
1964 return rc;
1965 }
1966 if (variance != nullptr && pot->residual->rows_e.empty()) {
1967 *variance = 0.0;
1968 }
1969 pot->last_error.clear();
1970 return RGPOT_ENGINE_OK;
1971 }
1972 if (!pot->callbacks_set || pot->callbacks.oracle == nullptr) {
1973 pot->last_error =
1974 "gpr engine: no oracle; call rgpot_engine_set_callbacks first";
1975 return RGPOT_ENGINE_ERR_INVALID;
1976 }
1977 std::vector<double> f_scratch;
1978 double* f_oracle = nullptr;
1979 if (forces != nullptr) {
1980 f_scratch.assign(cart, 0.0);
1981 f_oracle = f_scratch.data();
1982 }
1983 const int rc = pot->callbacks.oracle(
1984 pot->callbacks.ctx, nAtoms, R.data(), atomicNrs, f_oracle, energy,
1985 box != nullptr ? cell.data() : nullptr);
1986 if (rc != 0) return rc;
1987 std::string finite_err;
1988 if (!oracle_sample_finite(energy, f_oracle, cart, finite_err)) {
1989 pot->last_error = finite_err;
1990 return RGPOT_ENGINE_ERR_INVALID;
1991 }
1992 pot->residual->stats.true_energy_calls += 1;
1993 if (forces != nullptr) {
1994 pot->residual->stats.true_gradient_calls += 1;
1995 }
1996 if (forces != nullptr && !pot->residual->hill_from_oracle()) {
1997 std::vector<double> grad(d);
1998 for (std::size_t i = 0; i < d; ++i)
1999 grad[i] = -f_scratch[i];
2000 std::string err;
2001 if (!pot->residual->learn(geom.data(), 1, energy, grad.data(),
2002 err)) {
2003 pot->last_error = err;
2004 return RGPOT_ENGINE_ERR_INVALID;
2005 }
2006 }
2007 if (forces != nullptr) {
2008 std::copy(f_scratch.data(), f_scratch.data() + d, forces);
2009 }
2010 if (variance != nullptr) *variance = 0.0;
2011 pot->last_error.clear();
2012 return RGPOT_ENGINE_OK;
2013 }
2014 if (forces == nullptr) return -1;
2015 char err[512] = {0};
2016 if (!ensure_surrogate(pot, err, sizeof err, nAtoms, positions, atomicNrs)) {
2017 pot->last_error = err;
2018 std::fprintf(stderr, "rgpot_engine_force(gpr): %s\n", err);
2019 return -1;
2020 }
2021 const std::size_t n_atoms = static_cast<std::size_t>(nAtoms);
2022 std::size_t stride = 0;
2023 if (!checked_mul_size(n_atoms, 3, stride)) {
2024 pot->last_error = "gpr engine: force coordinate count overflows";
2025 return RGPOT_ENGINE_ERR_INVALID;
2026 }
2027 std::vector<double> R(positions, positions + stride);
2028 std::vector<double> cell(9, 0.0);
2029 if (box != nullptr) std::copy(box, box + 9, cell.begin());
2030 if (transform != nullptr) {
2031 const int rc = transform(transform_user, nAtoms, R.data(), cell.data());
2032 if (rc != 0) return rc;
2033 }
2034 try {
2035 return pot->surrogate->force(nAtoms, R.data(), atomicNrs, forces,
2036 energy, variance,
2037 box != nullptr ? cell.data() : nullptr);
2038 } catch (const std::exception& e) {
2039 pot->last_error = e.what();
2040 std::fprintf(stderr, "rgpot_engine_force(gpr): %s\n", e.what());
2041 return -2;
2042 }
2043} catch (const std::bad_alloc&) {
2044 if (pot != nullptr)
2045 pot->last_error = "gpr engine: force coordinate count overflows";
2046 return RGPOT_ENGINE_ERR_INVALID;
2047} catch (const std::length_error&) {
2048 if (pot != nullptr)
2049 pot->last_error = "gpr engine: force coordinate count overflows";
2050 return RGPOT_ENGINE_ERR_INVALID;
2051} catch (const std::exception& ex) {
2052 if (pot != nullptr) pot->last_error = ex.what();
2053 std::fprintf(stderr, "rgpot_engine_force(gpr): %s\n", ex.what());
2054 return -2;
2055}
@ RGPOT_GPR_PHASE_SURROGATE
double variance(const Weights &w, const Matrix &covariance)

◆ rgpot_engine_force_batch()

int rgpot_engine_force_batch ( RgpotEnginePot * pot,
long nSystems,
long nAtoms,
const double * positions,
const int * atomicNrs,
double * forces,
double * energies,
double * variances,
const double * boxes,
rgpot_engine_coord_transform transform,
void * transform_user )

Definition at line 2057 of file GprEngineAbi.cpp.

2063 {
2064 if (pot == nullptr || positions == nullptr || energies == nullptr ||
2065 nSystems <= 0 || nAtoms <= 0) {
2066 return -1;
2067 }
2068 if (pot->residual) {
2069 if (nAtoms != pot->residual->n_atoms) {
2070 pot->last_error = "gpr engine: nAtoms mismatch with saddle";
2071 return RGPOT_ENGINE_ERR_INVALID;
2072 }
2073 const std::size_t d = pot->residual->n_dof;
2074 const std::size_t n_sys = static_cast<std::size_t>(nSystems);
2075 std::size_t packed = 0;
2076 std::size_t cell_count = 0;
2077 if (!checked_mul_size(d, n_sys, packed) ||
2078 !checked_mul_size(static_cast<std::size_t>(9), n_sys, cell_count)) {
2079 pot->last_error =
2080 "gpr engine: force_batch coordinate count overflows";
2081 return RGPOT_ENGINE_ERR_INVALID;
2082 }
2083 std::size_t cart = 0;
2084 std::vector<double> R;
2085 std::string scratch_err;
2086 if (!residual_callback_scratch(static_cast<std::size_t>(nAtoms), d,
2087 n_sys, positions, cart, R, scratch_err,
2088 true)) {
2089 pot->last_error = scratch_err;
2090 return RGPOT_ENGINE_ERR_INVALID;
2091 }
2092 std::vector<double> cells(cell_count, 0.0);
2093 if (boxes != nullptr) {
2094 std::copy(boxes, boxes + cell_count, cells.begin());
2095 }
2096 if (transform != nullptr) {
2097 std::size_t coord_off = 0;
2098 std::size_t cell_off = 0;
2099 for (long s = 0; s < nSystems; ++s) {
2100 const int rc =
2101 transform(transform_user, nAtoms, R.data() + coord_off,
2102 cells.data() + cell_off);
2103 if (rc != 0) return rc;
2104 coord_off += cart;
2105 cell_off += 9;
2106 }
2107 }
2108 std::vector<double> geom;
2109 residual_pack_prefix(R, n_sys, d, cart, geom);
2110 if (pot->residual->phase == RGPOT_GPR_PHASE_SURROGATE) {
2111 std::string err;
2112 // Same variance rule as rgpot_engine_force: a trained posterior
2113 // reports its energy variance, and an untrained reference is 0.
2114 if (variances != nullptr && !pot->residual->rows_e.empty()) {
2115 std::vector<double> var_g(packed);
2116 const int vrc = pot->residual->predict_variance(
2117 geom.data(), packed, n_sys, variances, n_sys, var_g.data(),
2118 packed, err);
2119 if (vrc != RGPOT_ENGINE_OK) {
2120 pot->last_error = err;
2121 return vrc;
2122 }
2123 }
2124 const int rc = pot->residual->predict(geom.data(), n_sys, energies,
2125 forces, err);
2126 if (rc != RGPOT_ENGINE_OK) {
2127 pot->last_error = err;
2128 return rc;
2129 }
2130 if (variances != nullptr && pot->residual->rows_e.empty()) {
2131 std::fill(variances, variances + n_sys, 0.0);
2132 }
2133 pot->last_error.clear();
2134 return RGPOT_ENGINE_OK;
2135 }
2136 if (!pot->callbacks_set || pot->callbacks.oracle == nullptr) {
2137 pot->last_error =
2138 "gpr engine: no oracle; call rgpot_engine_set_callbacks first";
2139 return RGPOT_ENGINE_ERR_INVALID;
2140 }
2141 std::vector<double> f_scratch;
2142 if (forces != nullptr) f_scratch.assign(n_sys * cart, 0.0);
2143 std::size_t cell_off = 0;
2144 for (long s = 0; s < nSystems; ++s) {
2145 const std::size_t coord_off = static_cast<std::size_t>(s) * cart;
2146 double* f_ptr =
2147 forces != nullptr ? f_scratch.data() + coord_off : nullptr;
2148 const double* b_ptr =
2149 boxes != nullptr ? cells.data() + cell_off : nullptr;
2150 const int rc = pot->callbacks.oracle(
2151 pot->callbacks.ctx, nAtoms, R.data() + coord_off, atomicNrs,
2152 f_ptr, energies + s, b_ptr);
2153 if (rc != 0) return rc;
2154 std::string finite_err;
2155 if (!oracle_sample_finite(energies + s, f_ptr, cart, finite_err)) {
2156 pot->last_error = finite_err;
2157 return RGPOT_ENGINE_ERR_INVALID;
2158 }
2159 cell_off += 9;
2160 }
2161 pot->residual->stats.true_energy_calls +=
2162 static_cast<unsigned long long>(n_sys);
2163 if (forces != nullptr) {
2164 pot->residual->stats.true_gradient_calls +=
2165 static_cast<unsigned long long>(n_sys);
2166 }
2167 if (forces != nullptr && !pot->residual->hill_from_oracle()) {
2168 std::vector<double> grads(packed);
2169 for (std::size_t s = 0; s < n_sys; ++s) {
2170 for (std::size_t a = 0; a < d; ++a) {
2171 grads[s * d + a] = -f_scratch[s * cart + a];
2172 }
2173 }
2174 std::string err;
2175 if (!pot->residual->learn(geom.data(), n_sys, energies,
2176 grads.data(), err)) {
2177 pot->last_error = err;
2178 return RGPOT_ENGINE_ERR_INVALID;
2179 }
2180 }
2181 if (forces != nullptr) {
2182 for (std::size_t s = 0; s < n_sys; ++s) {
2183 std::copy(f_scratch.data() + s * cart,
2184 f_scratch.data() + s * cart + d, forces + s * d);
2185 }
2186 }
2187 if (variances != nullptr) {
2188 std::fill(variances, variances + n_sys, 0.0);
2189 }
2190 pot->last_error.clear();
2191 return RGPOT_ENGINE_OK;
2192 }
2193 if (forces == nullptr) return -1;
2194 char err[512] = {0};
2195 if (!ensure_surrogate(pot, err, sizeof err, nAtoms, positions, atomicNrs)) {
2196 pot->last_error = err;
2197 std::fprintf(stderr, "rgpot_engine_force_batch(gpr): %s\n", err);
2198 return -1;
2199 }
2200 const std::size_t n_atoms = static_cast<std::size_t>(nAtoms);
2201 const std::size_t n_sys = static_cast<std::size_t>(nSystems);
2202 std::size_t stride = 0;
2203 std::size_t coord_count = 0;
2204 std::size_t cell_count = 0;
2205 if (!checked_mul_size(n_atoms, 3, stride) ||
2206 !checked_mul_size(stride, n_sys, coord_count) ||
2207 !checked_mul_size(static_cast<std::size_t>(9), n_sys, cell_count)) {
2208 pot->last_error = "gpr engine: force_batch coordinate count overflows";
2209 return RGPOT_ENGINE_ERR_INVALID;
2210 }
2211 std::vector<double> R(positions, positions + coord_count);
2212 std::vector<double> cells(cell_count, 0.0);
2213 if (boxes != nullptr) {
2214 std::copy(boxes, boxes + cell_count, cells.begin());
2215 }
2216 if (transform != nullptr) {
2217 std::size_t coord_off = 0;
2218 std::size_t cell_off = 0;
2219 for (long s = 0; s < nSystems; ++s) {
2220 const int rc =
2221 transform(transform_user, nAtoms, R.data() + coord_off,
2222 cells.data() + cell_off);
2223 if (rc != 0) return rc;
2224 coord_off += stride;
2225 cell_off += 9;
2226 }
2227 }
2228 try {
2229 return pot->surrogate->forceBatch(
2230 nSystems, nAtoms, R.data(), atomicNrs, forces, energies, variances,
2231 boxes != nullptr ? cells.data() : nullptr);
2232 } catch (const std::exception& e) {
2233 pot->last_error = e.what();
2234 std::fprintf(stderr, "rgpot_engine_force_batch(gpr): %s\n", e.what());
2235 return -2;
2236 }
2237} catch (const std::bad_alloc&) {
2238 if (pot != nullptr)
2239 pot->last_error = "gpr engine: force_batch coordinate count overflows";
2240 return RGPOT_ENGINE_ERR_INVALID;
2241} catch (const std::length_error&) {
2242 if (pot != nullptr)
2243 pot->last_error = "gpr engine: force_batch coordinate count overflows";
2244 return RGPOT_ENGINE_ERR_INVALID;
2245} catch (const std::exception& ex) {
2246 if (pot != nullptr) pot->last_error = ex.what();
2247 std::fprintf(stderr, "rgpot_engine_force_batch(gpr): %s\n", ex.what());
2248 return -2;
2249}

◆ rgpot_engine_gpr_stats()

int rgpot_engine_gpr_stats ( const RgpotEnginePot * pot,
RgpotEngineGprStats * stats_out )

Read the operation counters accumulated by the engine instance.

Definition at line 1884 of file GprEngineAbi.cpp.

1886{
1887 if (pot == nullptr || stats_out == nullptr || stats_out->struct_size == 0) {
1888 return RGPOT_ENGINE_ERR_INVALID;
1889 }
1890 RgpotEngineGprStats current{};
1891 current.struct_size = sizeof(RgpotEngineGprStats);
1892 if (pot->residual) {
1893 current = pot->residual->stats;
1894 } else if (pot->surrogate) {
1895 current.true_energy_calls =
1896 static_cast<unsigned long long>(pot->surrogate->oracleCalls());
1897 current.true_gradient_calls =
1898 static_cast<unsigned long long>(pot->surrogate->oracleCalls());
1899 current.observations =
1900 static_cast<unsigned long long>(pot->surrogate->trainingSize());
1901 current.fits = pot->surrogate->modelState();
1902 }
1903 const std::size_t room =
1904 std::min(stats_out->struct_size, sizeof(RgpotEngineGprStats));
1905 std::memcpy(stats_out, &current, room);
1906 stats_out->struct_size = room;
1907 return RGPOT_ENGINE_OK;
1908}
unsigned long long true_gradient_calls
unsigned long long fits
unsigned long long true_energy_calls
unsigned long long observations

◆ rgpot_engine_hessian_action_checked()

int rgpot_engine_hessian_action_checked ( RgpotEnginePot * pot,
long nAtoms,
const double * positions,
size_t positions_len,
const int * atomicNrs,
const double * direction,
size_t direction_len,
double * hv_out,
size_t hv_len,
const double * box,
unsigned long long * ops_out,
double * scale_out )

Extent-checked Hessian-vector product H(positions) * direction. positions_len, direction_len and hv_len must each be at least n_coords (for residual sessions) or 3*nAtoms. When ops_out and scale_out are non-NULL, the owning implementation writes the floating-point operation count and the magnitude scale used for roundoff certification (ops * DBL_EPSILON * scale). Returns 0 on success, RGPOT_ENGINE_ERR_OUTSIDE_SUPPORT (4) when the query lies outside the trained prediction kernel support.

Definition at line 2287 of file GprEngineAbi.cpp.

2292 {
2293 (void)atomicNrs;
2294 (void)box;
2295 if (pot == nullptr) return RGPOT_ENGINE_ERR_INVALID;
2296 if (pot->residual) {
2297 if (nAtoms != pot->residual->n_atoms) {
2298 pot->last_error = "gpr engine: nAtoms mismatch with saddle";
2299 return RGPOT_ENGINE_ERR_INVALID;
2300 }
2301 std::string err;
2302 const int rc = pot->residual->hessian_action(
2303 positions, positions_len, direction, direction_len, hv_out, hv_len,
2304 ops_out, scale_out, err);
2305 if (rc != RGPOT_ENGINE_OK) {
2306 pot->last_error = err;
2307 return rc;
2308 }
2309 pot->last_error.clear();
2310 return RGPOT_ENGINE_OK;
2311 }
2312 pot->last_error =
2313 "gpr engine: Hessian action requires a configured session";
2314 return RGPOT_ENGINE_ERR_UNSUPPORTED;
2315} catch (const std::bad_alloc&) {
2316 if (pot != nullptr) pot->last_error = "gpr engine: size overflows";
2317 return RGPOT_ENGINE_ERR_INVALID;
2318} catch (const std::length_error&) {
2319 if (pot != nullptr) pot->last_error = "gpr engine: size overflows";
2320 return RGPOT_ENGINE_ERR_INVALID;
2321} catch (const std::exception& ex) {
2322 if (pot != nullptr) pot->last_error = ex.what();
2323 return -2;
2324}

◆ rgpot_engine_hessian_checked()

int rgpot_engine_hessian_checked ( RgpotEnginePot * pot,
long nAtoms,
const double * positions,
size_t positions_len,
const int * atomicNrs,
double * hessian_out,
size_t hessian_len,
const double * box )

Extent-checked dense Hessian evaluation at one geometry. positions_len must be at least n_dof, and hessian_len at least n_dof * n_dof. n_dof is n_coords when that count is positive, otherwise 3*nAtoms. A longer positions buffer does not enlarge the Hessian. Returns 0 on success.

Definition at line 2251 of file GprEngineAbi.cpp.

2255 {
2256 (void)atomicNrs;
2257 (void)box;
2258 if (pot == nullptr) return RGPOT_ENGINE_ERR_INVALID;
2259 if (pot->residual) {
2260 if (nAtoms != pot->residual->n_atoms) {
2261 pot->last_error = "gpr engine: nAtoms mismatch with saddle";
2262 return RGPOT_ENGINE_ERR_INVALID;
2263 }
2264 std::string err;
2265 const int rc = pot->residual->hessian(positions, positions_len,
2266 hessian_out, hessian_len, err);
2267 if (rc != RGPOT_ENGINE_OK) {
2268 pot->last_error = err;
2269 return rc;
2270 }
2271 pot->last_error.clear();
2272 return RGPOT_ENGINE_OK;
2273 }
2274 pot->last_error = "gpr engine: dense Hessian requires a configured session";
2275 return RGPOT_ENGINE_ERR_UNSUPPORTED;
2276} catch (const std::bad_alloc&) {
2277 if (pot != nullptr) pot->last_error = "gpr engine: size overflows";
2278 return RGPOT_ENGINE_ERR_INVALID;
2279} catch (const std::length_error&) {
2280 if (pot != nullptr) pot->last_error = "gpr engine: size overflows";
2281 return RGPOT_ENGINE_ERR_INVALID;
2282} catch (const std::exception& ex) {
2283 if (pot != nullptr) pot->last_error = ex.what();
2284 return -2;
2285}

◆ rgpot_engine_hint()

int rgpot_engine_hint ( RgpotEnginePot * pot,
const void * msg,
std::size_t msg_len,
char * errbuf,
std::size_t errlen )

Definition at line 2417 of file GprEngineAbi.cpp.

2419 {
2420 if (pot == nullptr) {
2421 set_err(errbuf, errlen, "gpr engine: null potential");
2422 return -1;
2423 }
2424 if (pot->residual) {
2425 if (msg == nullptr || msg_len == 0) {
2426 std::string err;
2427 const int rc = pot->residual->fit(err);
2428 if (rc != RGPOT_ENGINE_OK) {
2429 pot->last_error = err;
2430 set_err(errbuf, errlen, err.c_str());
2431 }
2432 return rc;
2433 }
2434 if (msg_len < sizeof(RgpotEngineGprHint)) {
2435 set_err(errbuf, errlen, "gpr engine: hint shorter than header");
2436 return RGPOT_ENGINE_ERR_INVALID;
2437 }
2438 RgpotEngineGprHint hint{};
2439 std::memcpy(&hint, msg, sizeof(hint));
2440 if (hint.magic != RGPOT_ENGINE_GPR_HINT_MAGIC ||
2443 "gpr engine: invalid hint magic or version");
2444 return RGPOT_ENGINE_ERR_INVALID;
2445 }
2447 std::string err;
2448 const int rc = pot->residual->fit(err);
2449 if (rc != RGPOT_ENGINE_OK) {
2450 pot->last_error = err;
2451 set_err(errbuf, errlen, err.c_str());
2452 }
2453 return rc;
2454 }
2456 if (hint.phase < RGPOT_GPR_PHASE_SADDLE ||
2458 set_err(errbuf, errlen, "gpr engine: invalid phase in hint");
2459 return RGPOT_ENGINE_ERR_INVALID;
2460 }
2461 pot->residual->phase = hint.phase;
2462 return RGPOT_ENGINE_OK;
2463 }
2465 if (hint.n_points < 0 || static_cast<std::size_t>(hint.n_coords) !=
2466 pot->residual->n_dof) {
2468 "gpr engine: invalid LEARN_POINTS dimensions");
2469 return RGPOT_ENGINE_ERR_INVALID;
2470 }
2471 const auto n = static_cast<std::size_t>(hint.n_points);
2472 const std::size_t d = pot->residual->n_dof;
2473 std::size_t nd = 0;
2474 std::size_t doubles = 0;
2475 std::size_t expected = 0;
2476 if (!checked_mul_size(n, d, nd) ||
2477 !checked_mul_size(nd, 2, doubles) ||
2478 !checked_add_size(doubles, n, doubles) ||
2479 !checked_mul_size(doubles, sizeof(double), expected) ||
2480 !checked_add_size(expected, sizeof(RgpotEngineGprHint),
2481 expected)) {
2483 "gpr engine: LEARN_POINTS payload size overflows");
2484 return RGPOT_ENGINE_ERR_INVALID;
2485 }
2486 if (msg_len != expected) {
2488 "gpr engine: LEARN_POINTS payload size mismatch");
2489 return RGPOT_ENGINE_ERR_INVALID;
2490 }
2491 if (n > 0) {
2492 const auto* data = reinterpret_cast<const double*>(
2493 static_cast<const unsigned char*>(msg) +
2494 sizeof(RgpotEngineGprHint));
2495 const double* xs = data;
2496 const double* es = data + nd;
2497 const double* gs = data + nd + n;
2498 std::string err;
2499 if (!pot->residual->learn(xs, n, es, gs, err)) {
2500 pot->last_error = err;
2501 set_err(errbuf, errlen, err.c_str());
2502 return RGPOT_ENGINE_ERR_INVALID;
2503 }
2504 }
2505 return RGPOT_ENGINE_OK;
2506 }
2508 if (hint.n_points < 0 || (hint.phase != 0 && hint.phase != 1)) {
2510 "gpr engine: invalid SET_TRAINING arguments");
2511 return RGPOT_ENGINE_ERR_INVALID;
2512 }
2513 std::string err;
2514 const int rc = pot->residual->set_training(
2515 static_cast<std::size_t>(hint.n_points), hint.phase == 1, err);
2516 if (rc != RGPOT_ENGINE_OK) {
2517 pot->last_error = err;
2518 set_err(errbuf, errlen, err.c_str());
2519 }
2520 return rc;
2521 }
2523 if (hint.n_points < 0 || hint.phase < 0 || hint.phase > 2) {
2525 "gpr engine: invalid RECORD_TRUE_CALLS arguments");
2526 return RGPOT_ENGINE_ERR_INVALID;
2527 }
2528 const auto count = static_cast<unsigned long long>(hint.n_points);
2529 pot->residual->stats.true_energy_calls += count;
2530 if (hint.phase >= 1) {
2531 pot->residual->stats.true_gradient_calls += count;
2532 }
2533 if (hint.phase >= 2) {
2534 pot->residual->stats.true_hessian_calls += count;
2535 }
2536 return RGPOT_ENGINE_OK;
2537 }
2538 set_err(errbuf, errlen, "gpr engine: unknown hint kind");
2539 return RGPOT_ENGINE_ERR_INVALID;
2540 }
2541 // The hint arm is the host-initiated incorporation boundary. The
2542 // capnp payload is reserved for tunables; an empty hint means
2543 // "incorporate now".
2544 (void)msg;
2545 (void)msg_len;
2546 if (!pot->surrogate) return 0;
2547 try {
2548 pot->surrogate->incorporate();
2549 return 0;
2550 } catch (const std::exception& e) {
2551 set_err(errbuf, errlen, e.what());
2552 return -2;
2553 }
2554} catch (const std::bad_alloc&) {
2555 const char* overflow = "gpr engine: size overflows";
2556 if (pot != nullptr) pot->last_error = overflow;
2557 set_err(errbuf, errlen, overflow);
2558 return RGPOT_ENGINE_ERR_INVALID;
2559} catch (const std::length_error&) {
2560 const char* overflow = "gpr engine: size overflows";
2561 if (pot != nullptr) pot->last_error = overflow;
2562 set_err(errbuf, errlen, overflow);
2563 return RGPOT_ENGINE_ERR_INVALID;
2564} catch (const std::exception& ex) {
2565 if (pot != nullptr) pot->last_error = ex.what();
2566 set_err(errbuf, errlen, ex.what());
2567 return -2;
2568}
@ RGPOT_ENGINE_GPR_HINT_LEARN_POINTS
@ RGPOT_ENGINE_GPR_HINT_SET_PHASE
@ RGPOT_ENGINE_GPR_HINT_RECORD_TRUE_CALLS
@ RGPOT_ENGINE_GPR_HINT_SET_TRAINING
@ RGPOT_ENGINE_GPR_HINT_INCORPORATE
#define RGPOT_ENGINE_GPR_HINT_VERSION
#define RGPOT_ENGINE_GPR_HINT_MAGIC
@ RGPOT_GPR_PHASE_SADDLE

◆ rgpot_engine_hvp()

int rgpot_engine_hvp ( RgpotEnginePot * pot,
long nAtoms,
const double * positions,
const int * atomicNrs,
const double * direction,
double * hv_out,
const double * box )

Definition at line 1818 of file GprEngineAbi.cpp.

1821 {
1822 if (pot == nullptr) return RGPOT_ENGINE_ERR_INVALID;
1823 if (!pot->residual) {
1824 pot->last_error =
1825 "gpr engine: analytic HVP requires a configured residual session";
1826 return RGPOT_ENGINE_ERR_UNSUPPORTED;
1827 }
1828 if (positions == nullptr || atomicNrs == nullptr || direction == nullptr ||
1829 hv_out == nullptr || box == nullptr || nAtoms <= 0 ||
1830 nAtoms != pot->residual->n_atoms) {
1831 pot->last_error = "gpr engine: invalid HVP argument";
1832 return RGPOT_ENGINE_ERR_INVALID;
1833 }
1834 for (long i = 0; i < nAtoms; ++i) {
1835 if (atomicNrs[i] <= 0) {
1836 pot->last_error = "gpr engine: invalid atomic number in HVP";
1837 return RGPOT_ENGINE_ERR_INVALID;
1838 }
1839 }
1840 const std::size_t d = pot->residual->n_dof;
1841 std::size_t coord_bytes = 0;
1842 std::size_t atomic_bytes = 0;
1843 if (!checked_mul_size(d, sizeof(double), coord_bytes) ||
1844 !checked_mul_size(static_cast<std::size_t>(nAtoms), sizeof(int),
1845 atomic_bytes)) {
1846 pot->last_error = "gpr engine: HVP coordinate count overflows";
1847 return RGPOT_ENGINE_ERR_INVALID;
1848 }
1849 const auto hb = reinterpret_cast<std::uintptr_t>(hv_out);
1850 const auto bytes = static_cast<std::uintptr_t>(coord_bytes);
1851 const auto overlaps = [hb, bytes](const void* input,
1852 std::uintptr_t input_bytes) {
1853 const auto begin = reinterpret_cast<std::uintptr_t>(input);
1854 return hb < begin + input_bytes && begin < hb + bytes;
1855 };
1856 if (overlaps(direction, bytes) || overlaps(positions, bytes) ||
1857 overlaps(atomicNrs, static_cast<std::uintptr_t>(atomic_bytes)) ||
1858 overlaps(box, 9 * sizeof(double))) {
1859 pot->last_error = "gpr engine: HVP output overlaps an input buffer";
1860 return RGPOT_ENGINE_ERR_INVALID;
1861 }
1862 std::string err;
1863 const int rc = pot->residual->hessian_action(
1864 positions, d, direction, d, hv_out, d, nullptr, nullptr, err);
1865 if (rc != RGPOT_ENGINE_OK) {
1866 pot->last_error = err;
1867 return rc;
1868 }
1869 pot->last_error.clear();
1870 return RGPOT_ENGINE_OK;
1871} catch (const std::bad_alloc&) {
1872 if (pot != nullptr)
1873 pot->last_error = "gpr engine: HVP coordinate count overflows";
1874 return RGPOT_ENGINE_ERR_INVALID;
1875} catch (const std::length_error&) {
1876 if (pot != nullptr)
1877 pot->last_error = "gpr engine: HVP coordinate count overflows";
1878 return RGPOT_ENGINE_ERR_INVALID;
1879} catch (const std::exception& ex) {
1880 if (pot != nullptr) pot->last_error = ex.what();
1881 return -2;
1882}

◆ rgpot_engine_last_error()

size_t rgpot_engine_last_error ( const RgpotEnginePot * pot,
char * buf,
size_t len )

Definition at line 1717 of file GprEngineAbi.cpp.

1718{
1719 if (pot == nullptr) return 0;
1720 const std::size_t full = pot->last_error.size();
1721 if (buf != nullptr && len > 0) {
1722 const std::size_t copied = std::min(full, len - 1);
1723 std::memcpy(buf, pot->last_error.data(), copied);
1724 buf[copied] = '\0';
1725 }
1726 return full;
1727}

◆ rgpot_engine_model_digest()

size_t rgpot_engine_model_digest ( const RgpotEnginePot * pot,
char * buf,
size_t len )

Definition at line 1762 of file GprEngineAbi.cpp.

1764{
1765 (void)pot;
1766 if (buf != nullptr && len > 0) buf[0] = '\0';
1767 return 0;
1768}

◆ rgpot_engine_model_state()

unsigned long long rgpot_engine_model_state ( const RgpotEnginePot * pot)

Definition at line 1709 of file GprEngineAbi.cpp.

1710{
1711 if (pot == nullptr) return 0;
1712 if (pot->residual) return pot->residual->epoch;
1713 if (!pot->surrogate) return 0;
1714 return pot->surrogate->modelState();
1715}

◆ rgpot_engine_name()

const char * rgpot_engine_name ( void )

Definition at line 1752 of file GprEngineAbi.cpp.

1753{
1754 return "gpr";
1755}

◆ rgpot_engine_neighbor_options()

long rgpot_engine_neighbor_options ( const RgpotEnginePot * pot,
RgpotEngineNeighborOptions * opts,
long capacity )

Definition at line 1808 of file GprEngineAbi.cpp.

1811{
1812 (void)pot;
1813 (void)opts;
1814 (void)capacity;
1815 return 0;
1816}

◆ rgpot_engine_predict_variance_checked()

int rgpot_engine_predict_variance_checked ( RgpotEnginePot * pot,
long nSystems,
long nAtoms,
const double * positions,
size_t positions_len,
const int * atomicNrs,
double * var_energy_out,
size_t var_energy_len,
double * var_gradient_out,
size_t var_gradient_len,
const double * boxes )

Extent-checked posterior energy and gradient variance query at nSystems geometries. positions_len and var_gradient_len must each be at least nSystems * n_coords (for residual sessions) or nSystems * 3 * nAtoms, and var_energy_len at least nSystems. Surrogate-phase rgpot_engine_force and rgpot_engine_force_batch write this energy variance into their variance slot, so a host does not treat the GP as exact. Oracle phases write 0: those evaluations are the true potential. The gradient variance is only available here. Returns 0 on success.

Definition at line 2365 of file GprEngineAbi.cpp.

2370 {
2371 (void)atomicNrs;
2372 (void)boxes;
2373 if (pot == nullptr || nSystems < 0) return RGPOT_ENGINE_ERR_INVALID;
2374 if (pot->residual) {
2375 if (nAtoms != pot->residual->n_atoms) {
2376 pot->last_error = "gpr engine: nAtoms mismatch with saddle";
2377 return RGPOT_ENGINE_ERR_INVALID;
2378 }
2379 const std::size_t n_sys = static_cast<std::size_t>(nSystems);
2380 std::size_t coord_count = 0;
2381 std::size_t model_count = 0;
2382 if (!checked_mul_size(n_sys, pot->residual->n_dof, coord_count) ||
2383 !checked_mul_size(n_sys, pot->residual->model_dof, model_count)) {
2384 pot->last_error =
2385 "predict_variance: geometry batch length overflows";
2386 return RGPOT_ENGINE_ERR_INVALID;
2387 }
2388 (void)coord_count;
2389 (void)model_count;
2390 std::string err;
2391 const int rc = pot->residual->predict_variance(
2392 positions, positions_len, n_sys, var_energy_out, var_energy_len,
2393 var_gradient_out, var_gradient_len, err);
2394 if (rc != RGPOT_ENGINE_OK) {
2395 pot->last_error = err;
2396 return rc;
2397 }
2398 pot->last_error.clear();
2399 return RGPOT_ENGINE_OK;
2400 }
2401 pot->last_error =
2402 "gpr engine: predict_variance requires a configured session";
2403 return RGPOT_ENGINE_ERR_UNSUPPORTED;
2404} catch (const std::bad_alloc&) {
2405 if (pot != nullptr)
2406 pot->last_error = "predict_variance: geometry batch length overflows";
2407 return RGPOT_ENGINE_ERR_INVALID;
2408} catch (const std::length_error&) {
2409 if (pot != nullptr)
2410 pot->last_error = "predict_variance: geometry batch length overflows";
2411 return RGPOT_ENGINE_ERR_INVALID;
2412} catch (const std::exception& ex) {
2413 if (pot != nullptr) pot->last_error = ex.what();
2414 return -2;
2415}

◆ rgpot_engine_prediction_supported_checked()

int rgpot_engine_prediction_supported_checked ( RgpotEnginePot * pot,
long nAtoms,
const double * positions,
size_t positions_len,
const int * atomicNrs,
const double * box,
int * supported_out )

Extent-checked prediction-support query at one geometry. positions_len must be at least n_coords (for residual sessions) or 3*nAtoms. On success, *supported_out is 1 inside the prediction kernel support and 0 outside it.

Definition at line 2326 of file GprEngineAbi.cpp.

2332 {
2333 (void)atomicNrs;
2334 (void)box;
2335 if (pot == nullptr) return RGPOT_ENGINE_ERR_INVALID;
2336 if (pot->residual) {
2337 if (nAtoms != pot->residual->n_atoms) {
2338 pot->last_error = "gpr engine: nAtoms mismatch with saddle";
2339 return RGPOT_ENGINE_ERR_INVALID;
2340 }
2341 std::string err;
2342 const int rc = pot->residual->prediction_supported(
2343 positions, positions_len, supported_out, err);
2344 if (rc != RGPOT_ENGINE_OK) {
2345 pot->last_error = err;
2346 return rc;
2347 }
2348 pot->last_error.clear();
2349 return RGPOT_ENGINE_OK;
2350 }
2351 pot->last_error =
2352 "gpr engine: prediction support query requires a configured session";
2353 return RGPOT_ENGINE_ERR_UNSUPPORTED;
2354} catch (const std::bad_alloc&) {
2355 if (pot != nullptr) pot->last_error = "gpr engine: size overflows";
2356 return RGPOT_ENGINE_ERR_INVALID;
2357} catch (const std::length_error&) {
2358 if (pot != nullptr) pot->last_error = "gpr engine: size overflows";
2359 return RGPOT_ENGINE_ERR_INVALID;
2360} catch (const std::exception& ex) {
2361 if (pot != nullptr) pot->last_error = ex.what();
2362 return -2;
2363}

◆ rgpot_engine_select_orbitals()

int rgpot_engine_select_orbitals ( RgpotEnginePot * pot,
long long key )

Definition at line 1790 of file GprEngineAbi.cpp.

1791{
1792 (void)key;
1793 if (pot == nullptr) return RGPOT_ENGINE_ERR_INVALID;
1794 pot->last_error = "gpr engine: backend has no orbital selection";
1795 return RGPOT_ENGINE_ERR_UNSUPPORTED;
1796}

◆ rgpot_engine_set_callbacks()

int rgpot_engine_set_callbacks ( RgpotEnginePot * pot,
const RgpotEngineCallbacks * callbacks )

Definition at line 1694 of file GprEngineAbi.cpp.

1696{
1697 if (pot == nullptr || callbacks == nullptr) return -1;
1698 if (callbacks->oracle == nullptr) return -1;
1699 pot->callbacks = *callbacks;
1700 pot->callbacks_set = true;
1701 if (pot->residual) {
1702 pot->residual->model_update = callbacks->on_model_update;
1703 pot->residual->model_update_ctx = callbacks->ctx;
1704 }
1705 pot->surrogate.reset();
1706 return 0;
1707}

◆ rgpot_engine_set_charge_spin()

int rgpot_engine_set_charge_spin ( RgpotEnginePot * pot,
int charge,
int spin )

Definition at line 1782 of file GprEngineAbi.cpp.

1783{
1784 (void)charge;
1785 if (pot == nullptr || spin < 1) return RGPOT_ENGINE_ERR_INVALID;
1786 pot->last_error = "gpr engine: charge/spin state is unsupported";
1787 return RGPOT_ENGINE_ERR_UNSUPPORTED;
1788}

◆ rgpot_engine_set_num_threads()

int rgpot_engine_set_num_threads ( RgpotEnginePot * pot,
int intra_op,
int inter_op )

Definition at line 1770 of file GprEngineAbi.cpp.

1772{
1773 if (pot == nullptr) return RGPOT_ENGINE_ERR_INVALID;
1774 if (intra_op < 1 && inter_op < 1) {
1775 pot->last_error.clear();
1776 return RGPOT_ENGINE_OK;
1777 }
1778 pot->last_error = "gpr engine: backend has no tensor runtime";
1779 return RGPOT_ENGINE_ERR_UNSUPPORTED;
1780}

◆ rgpot_engine_version()

const char * rgpot_engine_version ( void )

Definition at line 1757 of file GprEngineAbi.cpp.

1758{
1759 return "gpr/" GPRIDGE_VERSION;
1760}