Loading...
Searching...
No Matches
MetatomicCAbi.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** C ABI surface of libmetatomic_pot.so.
7*/
8#define EON_MTA_BUILD
10
12
13#include <cstdio>
14#include <exception>
15#include <memory>
16#include <string>
17
18struct EonMtaPot {
19 std::unique_ptr<MetatomicPotential> impl;
20};
21
22static const char *nz(const char *s) { return s ? s : ""; }
23
24static void set_err(char *errbuf, size_t errlen, const char *msg) {
25 if (!errbuf || errlen == 0)
26 return;
27 std::snprintf(errbuf, errlen, "%s", msg ? msg : "unknown error");
28}
29
30extern "C" {
31
33
34EonMtaPot *eon_mta_pot_create(const EonMtaConfig *cfg, char *errbuf,
35 size_t errlen) {
36 if (!cfg || !cfg->model_path || cfg->model_path[0] == '\0') {
37 set_err(errbuf, errlen, "eon_mta_pot_create: model_path is required");
38 return nullptr;
39 }
40 try {
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";
52 o.random_rotation = cfg->random_rotation != 0;
54 o.deterministic = cfg->deterministic != 0;
56 o.variant.base = nz(cfg->variant_base);
59 o.variant.force = nz(cfg->variant_force);
60
61 auto pot = std::make_unique<EonMtaPot>();
62 pot->impl = std::make_unique<MetatomicPotential>(o);
63 return pot.release();
64 } catch (const std::exception &e) {
65 set_err(errbuf, errlen, e.what());
66 return nullptr;
67 } catch (...) {
68 set_err(errbuf, errlen, "eon_mta_pot_create: unknown exception");
69 return nullptr;
70 }
71}
72
74 std::unique_ptr<EonMtaPot> owned(pot);
75}
76
77int eon_mta_pot_force(EonMtaPot *pot, long nAtoms, const double *positions,
78 const int *atomicNrs, double *forces, double *energy,
79 double *variance, const double *box) {
80 if (!pot || !pot->impl || !positions || !atomicNrs || !forces || !energy ||
81 !box) {
82 return 1;
83 }
84 try {
85 pot->impl->force(nAtoms, positions, atomicNrs, forces, energy, variance,
86 box);
87 return 0;
88 } catch (...) {
89 return 2;
90 }
91}
92
93} // extern "C"
static void set_err(char *errbuf, size_t errlen, const char *msg)
static const char * nz(const char *s)
int eon_mta_pot_force(EonMtaPot *pot, long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energy, double *variance, const double *box)
Evaluate energy + forces.
EonMtaPot * eon_mta_pot_create(const EonMtaConfig *cfg, char *errbuf, size_t errlen)
Create pot.
void eon_mta_pot_destroy(EonMtaPot *pot)
int eon_mta_abi_version(void)
ABI version for loader checks.
#define EON_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.
eonc::metatomic_options_t metatomic_options_t
Definition Parameters.h:77
Flat config for create (mirrors eonc::Parameters::metatomic_options_t).
const char * device
const char * extensions_directory
const char * variant_force
const char * model_path
const char * length_unit
long n_symmetry_rotations
const char * energy_output
const char * force_output
const char * variant_base
const char * energy_uncertainty_output
const char * variant_energy
const char * variant_energy_uncertainty
double uncertainty_threshold
std::unique_ptr< MetatomicPotential > impl
struct eonc::metatomic_options_t::variants_t variant