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 eonc::Parameters params;
46 o.model_path = cfg->model_path;
47 o.device = nz(cfg->device)[0] ? cfg->device : "cpu";
48 o.length_unit = nz(cfg->length_unit)[0] ? cfg->length_unit : "angstrom";
49 o.extensions_directory = nz(cfg->extensions_directory);
50 o.check_consistency = cfg->check_consistency != 0;
51 o.uncertainty_threshold = cfg->uncertainty_threshold;
52 o.energy_output = nz(cfg->energy_output);
53 o.energy_uncertainty_output = nz(cfg->energy_uncertainty_output);
54 o.force_output = nz(cfg->force_output);
55 o.non_conservative = cfg->non_conservative != 0;
56 o.random_rotation = cfg->random_rotation != 0;
57 o.n_symmetry_rotations = cfg->n_symmetry_rotations;
58 o.deterministic = cfg->deterministic != 0;
59 o.deterministic_strict = cfg->deterministic_strict != 0;
60 o.variant.base = nz(cfg->variant_base);
61 o.variant.energy = nz(cfg->variant_energy);
62 o.variant.energy_uncertainty = nz(cfg->variant_energy_uncertainty);
63 o.variant.force = nz(cfg->variant_force);
64
65 auto pot = std::make_unique<EonMtaPot>();
66 pot->impl = std::make_unique<MetatomicPotential>(params);
67 return pot.release();
68 } catch (const std::exception &e) {
69 set_err(errbuf, errlen, e.what());
70 return nullptr;
71 } catch (...) {
72 set_err(errbuf, errlen, "eon_mta_pot_create: unknown exception");
73 return nullptr;
74 }
75}
76
77void eon_mta_pot_destroy(EonMtaPot *pot) { delete pot; }
78
79int eon_mta_pot_force(EonMtaPot *pot, long nAtoms, const double *positions,
80 const int *atomicNrs, double *forces, double *energy,
81 double *variance, const double *box) {
82 if (!pot || !pot->impl || !positions || !atomicNrs || !forces || !energy ||
83 !box) {
84 return 1;
85 }
86 try {
87 pot->impl->force(nAtoms, positions, atomicNrs, forces, energy, variance,
88 box);
89 return 0;
90 } catch (...) {
91 return 2;
92 }
93}
94
95} // 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.
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
static metatomic_options_t & metatomic_options(Parameters &p)
static potential_options_t & potential_options(Parameters &p)