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
11#include "eon/Parameters.h"
13
14#include <cstdio>
15#include <exception>
16#include <memory>
17#include <string>
18
19struct EonMtaPot {
20 std::unique_ptr<MetatomicPotential> impl;
21};
22
23static const char *nz(const char *s) { return s ? s : ""; }
24
25static void set_err(char *errbuf, size_t errlen, const char *msg) {
26 if (!errbuf || errlen == 0)
27 return;
28 std::snprintf(errbuf, errlen, "%s", msg ? msg : "unknown error");
29}
30
31extern "C" {
32
34
35EonMtaPot *eon_mta_pot_create(const EonMtaConfig *cfg, char *errbuf,
36 size_t errlen) {
37 if (!cfg || !cfg->model_path || cfg->model_path[0] == '\0') {
38 set_err(errbuf, errlen, "eon_mta_pot_create: model_path is required");
39 return nullptr;
40 }
41 try {
42 Parameters params;
43 params.potential_options.potential = PotType::METATOMIC;
44 auto &o = params.metatomic_options;
45 o.model_path = cfg->model_path;
46 o.device = nz(cfg->device)[0] ? cfg->device : "cpu";
47 o.length_unit = nz(cfg->length_unit)[0] ? cfg->length_unit : "angstrom";
48 o.extensions_directory = nz(cfg->extensions_directory);
49 o.check_consistency = cfg->check_consistency != 0;
50 o.uncertainty_threshold = cfg->uncertainty_threshold;
51 o.energy_output = nz(cfg->energy_output);
52 o.energy_uncertainty_output = nz(cfg->energy_uncertainty_output);
53 o.force_output = nz(cfg->force_output);
54 o.non_conservative = cfg->non_conservative != 0;
55 o.random_rotation = cfg->random_rotation != 0;
56 o.n_symmetry_rotations = cfg->n_symmetry_rotations;
57 o.deterministic = cfg->deterministic != 0;
58 o.deterministic_strict = cfg->deterministic_strict != 0;
59 o.variant.base = nz(cfg->variant_base);
60 o.variant.energy = nz(cfg->variant_energy);
61 o.variant.energy_uncertainty = nz(cfg->variant_energy_uncertainty);
62 o.variant.force = nz(cfg->variant_force);
63
64 auto pot = std::make_unique<EonMtaPot>();
65 pot->impl = std::make_unique<MetatomicPotential>(params);
66 return pot.release();
67 } catch (const std::exception &e) {
68 set_err(errbuf, errlen, e.what());
69 return nullptr;
70 } catch (...) {
71 set_err(errbuf, errlen, "eon_mta_pot_create: unknown exception");
72 return nullptr;
73 }
74}
75
76void eon_mta_pot_destroy(EonMtaPot *pot) { delete pot; }
77
78int eon_mta_pot_force(EonMtaPot *pot, long nAtoms, const double *positions,
79 const int *atomicNrs, double *forces, double *energy,
80 double *variance, const double *box) {
81 if (!pot || !pot->impl || !positions || !atomicNrs || !forces || !energy ||
82 !box) {
83 return 1;
84 }
85 try {
86 pot->impl->force(nAtoms, positions, atomicNrs, forces, energy, variance,
87 box);
88 return 0;
89 } catch (...) {
90 return 2;
91 }
92}
93
94} // 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.
struct eonc::Parameters::potential_options_t potential_options
struct eonc::Parameters::metatomic_options_t metatomic_options
Flat config for create (mirrors 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