Loading...
Searching...
No Matches
MetatomicEngineAbi.cpp
Go to the documentation of this file.
1/*
2** libmetatomic_engine.so — C ABI for RGPOT-metatomic (wraps
3*MetatomicPotential).
4** Fat eOn still uses MetatomicPotential directly via potential=Metatomic.
5*/
6#define RGPOT_MTA_ENGINE_BUILD
7#include "eon/Parameters.h"
10#include <cstdio>
11#include <cstring>
12#include <exception>
13#include <memory>
14#include <string>
15#include <vector>
16
18 std::unique_ptr<MetatomicPotential> impl;
19};
20
21static const char *nz(const char *s) { return s ? s : ""; }
22static void set_err(char *b, size_t n, const char *m) {
23 if (b && n)
24 std::snprintf(b, n, "%s", m ? m : "unknown");
25}
26
27extern "C" {
28
30int rgpot_mta_available(void) { return 1; }
31
33 size_t errlen) {
34 if (!cfg || !cfg->model_path || !cfg->model_path[0]) {
35 set_err(errbuf, errlen, "model_path required");
36 return nullptr;
37 }
38 try {
39 Parameters p;
40 p.potential_options.potential = PotType::METATOMIC;
41 auto &o = p.metatomic_options;
42 o.model_path = cfg->model_path;
43 o.device = nz(cfg->device)[0] ? cfg->device : "cpu";
44 o.length_unit = nz(cfg->length_unit)[0] ? cfg->length_unit : "angstrom";
45 o.extensions_directory = nz(cfg->extensions_directory);
46 o.check_consistency = cfg->check_consistency != 0;
47 o.uncertainty_threshold = cfg->uncertainty_threshold;
48 o.deterministic = !cfg->torch_determinism_strict; // map: strict => det
49 o.deterministic_strict = cfg->torch_determinism_strict != 0;
50 auto pot = std::make_unique<RgpotMtaPot>();
51 pot->impl = std::make_unique<MetatomicPotential>(p);
52 return pot.release();
53 } catch (const std::exception &e) {
54 set_err(errbuf, errlen, e.what());
55 return nullptr;
56 } catch (...) {
57 set_err(errbuf, errlen, "create failed");
58 return nullptr;
59 }
60}
61
62void rgpot_mta_destroy(RgpotMtaPot *pot) { delete pot; }
63
64int rgpot_mta_force(RgpotMtaPot *pot, long nAtoms, const double *positions,
65 const int *atomicNrs, double *forces, double *energy,
66 double *variance, const double *box) {
67 if (!pot || !pot->impl || !positions || !atomicNrs || !forces || !energy ||
68 !box)
69 return 1;
70 try {
71 pot->impl->force(nAtoms, positions, atomicNrs, forces, energy, variance,
72 box);
73 return 0;
74 } catch (...) {
75 return 2;
76 }
77}
78
79} // extern "C"
int rgpot_mta_force(RgpotMtaPot *pot, long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energy, double *variance, const double *box)
Energy + forces.
static void set_err(char *b, size_t n, const char *m)
static const char * nz(const char *s)
int rgpot_mta_abi_version(void)
RgpotMtaPot * rgpot_mta_create(const RgpotMtaConfig *cfg, char *errbuf, size_t errlen)
void rgpot_mta_destroy(RgpotMtaPot *pot)
int rgpot_mta_available(void)
C ABI between Metatomic frontend and libmetatomic_engine.so.
#define RGPOT_MTA_ABI_VERSION
void force(long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energy, double *variance, const double *box) override
Calculates the energy and forces for a given atomic configuration.
struct eonc::Parameters::potential_options_t potential_options
struct eonc::Parameters::metatomic_options_t metatomic_options
Flat config (subset of MetatomicConfig).
const char * extensions_directory
double uncertainty_threshold
const char * device
const char * model_path
const char * length_unit
int torch_determinism_strict
0=Fast, 1=Strict
std::unique_ptr< MetatomicPotential > impl