Loading...
Searching...
No Matches
gpr_engine_curvature.h File Reference

Go to the source code of this file.

Classes

struct  RgpotEngineGprResidualPrior
struct  RgpotEngineGprHint
struct  RgpotEngineGprStats

Macros

#define RGPOT_ENGINE_ERR_OUTSIDE_SUPPORT   4
#define RGPOT_ENGINE_GPR_RESIDUAL_PRIOR_MAGIC   0x47505251u /* "GPRQ" */
#define RGPOT_ENGINE_GPR_RESIDUAL_PRIOR_VERSION   1u
#define RGPOT_ENGINE_GPR_HINT_MAGIC   0x47505248u /* "GPRH" */
#define RGPOT_ENGINE_GPR_HINT_VERSION   1u

Enumerations

enum  rgpot_engine_gpr_hill_policy { RGPOT_GPR_HILL_TRUE = 0 , RGPOT_GPR_HILL_GP = 1 }
enum  rgpot_engine_gpr_phase { RGPOT_GPR_PHASE_SADDLE , RGPOT_GPR_PHASE_ORBIT , RGPOT_GPR_PHASE_HILL = 2 , RGPOT_GPR_PHASE_SURROGATE = 3 }
enum  rgpot_engine_gpr_hint_kind {
  RGPOT_ENGINE_GPR_HINT_INCORPORATE = 0 , RGPOT_ENGINE_GPR_HINT_SET_PHASE = 1 , RGPOT_ENGINE_GPR_HINT_LEARN_POINTS = 2 , RGPOT_ENGINE_GPR_HINT_RECORD_TRUE_CALLS = 3 ,
  RGPOT_ENGINE_GPR_HINT_SET_TRAINING = 4
}

Functions

RGPOT_ENGINE_API 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)
RGPOT_ENGINE_API 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)
RGPOT_ENGINE_API 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)
RGPOT_ENGINE_API 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)
RGPOT_ENGINE_API int rgpot_engine_gpr_stats (const RgpotEnginePot *pot, RgpotEngineGprStats *stats_out)

Macro Definition Documentation

◆ RGPOT_ENGINE_ERR_OUTSIDE_SUPPORT

#define RGPOT_ENGINE_ERR_OUTSIDE_SUPPORT   4

Definition at line 16 of file gpr_engine_curvature.h.

◆ RGPOT_ENGINE_GPR_HINT_MAGIC

#define RGPOT_ENGINE_GPR_HINT_MAGIC   0x47505248u /* "GPRH" */

Hint message for a curvature learning engine, passed to rgpot_engine_hint.

For RGPOT_ENGINE_GPR_HINT_LEARN_POINTS, the struct is followed immediately by n_points * n_coords doubles of positions, n_points doubles of energies, and n_points * n_coords doubles of gradients (dE/dx = -force).

RGPOT_ENGINE_GPR_HINT_SET_TRAINING takes effect at the next fit. n_points is the training window: a full fit trains on the most recent n_points observations, or on all of them at zero. phase 1 selects the Cholesky posterior and, after the first full fit, conditions later observations on the fitted hyperparameters through gpr_model_append_fixed_checked; phase 0 refits in full. An append needs a zero window and a Cartesian session and falls back to a full fit where the posterior cannot be extended.

Definition at line 101 of file gpr_engine_curvature.h.

◆ RGPOT_ENGINE_GPR_HINT_VERSION

#define RGPOT_ENGINE_GPR_HINT_VERSION   1u

Definition at line 102 of file gpr_engine_curvature.h.

◆ RGPOT_ENGINE_GPR_RESIDUAL_PRIOR_MAGIC

#define RGPOT_ENGINE_GPR_RESIDUAL_PRIOR_MAGIC   0x47505251u /* "GPRQ" */

Saddle-quadratic residual-prior configuration for a learning curvature engine, passed as the config blob to rgpot_engine_create.

The engine trains a Gaussian process on the residual of the quadratic expansion at the reference geometry (the saddle point), so far from training data its curvature returns to the reference Hessian.

The config buffer consists of this header followed immediately by:

  • n_coords doubles for the reference positions x_0;
  • n_coords doubles for the reference gradient g_0 (only when has_gradient is nonzero; otherwise g_0 = 0);
  • n_coords * n_coords doubles for the row-major reference Hessian H_0;
  • n_atoms ints for atomic numbers (only when has_atomic_nrs is nonzero).

Definition at line 33 of file gpr_engine_curvature.h.

◆ RGPOT_ENGINE_GPR_RESIDUAL_PRIOR_VERSION

#define RGPOT_ENGINE_GPR_RESIDUAL_PRIOR_VERSION   1u

Definition at line 34 of file gpr_engine_curvature.h.

Enumeration Type Documentation

◆ rgpot_engine_gpr_hill_policy

Where fluctuation Hessians in the Hill phase come from.

Enumerator
RGPOT_GPR_HILL_TRUE 

Oracle answers deriv=2 in the Hill phase: the engine refuses Hill curvature with RGPOT_ENGINE_ERR_UNSUPPORTED and does not learn the oracle points it evaluates there

RGPOT_GPR_HILL_GP 

GP mean + prior Hessian after incorporating the Hill energies and gradients

Definition at line 37 of file gpr_engine_curvature.h.

◆ rgpot_engine_gpr_hint_kind

Enumerator
RGPOT_ENGINE_GPR_HINT_INCORPORATE 
RGPOT_ENGINE_GPR_HINT_SET_PHASE 
RGPOT_ENGINE_GPR_HINT_LEARN_POINTS 
RGPOT_ENGINE_GPR_HINT_RECORD_TRUE_CALLS 
RGPOT_ENGINE_GPR_HINT_SET_TRAINING 

Definition at line 104 of file gpr_engine_curvature.h.

◆ rgpot_engine_gpr_phase

Evaluation phase of a curvature learning engine.

Enumerator
RGPOT_GPR_PHASE_SADDLE 

Saddle point: oracle answers, rows join fit

RGPOT_GPR_PHASE_ORBIT 

Orbit solve: oracle answers force, GP answers curvature

RGPOT_GPR_PHASE_HILL 

Hill determinant: follows hill_policy

RGPOT_GPR_PHASE_SURROGATE 

Surrogate only: no oracle calls

Definition at line 47 of file gpr_engine_curvature.h.

47 {
49 0,
51 1,
rgpot_engine_gpr_phase
@ RGPOT_GPR_PHASE_ORBIT
@ RGPOT_GPR_PHASE_HILL
@ RGPOT_GPR_PHASE_SADDLE
@ RGPOT_GPR_PHASE_SURROGATE

Function Documentation

◆ rgpot_engine_gpr_stats()

RGPOT_ENGINE_API 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}
Potential wrappers and the batched oracle interface.
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()

RGPOT_ENGINE_API 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()

RGPOT_ENGINE_API 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_predict_variance_checked()

RGPOT_ENGINE_API 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()

RGPOT_ENGINE_API 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}