Loading...
Searching...
No Matches
MetatomicCAbi.cpp File Reference
#include "eon/potentials/Metatomic/metatomic_c_abi.h"
#include "eon/Parameters.h"
#include "eon/potentials/Metatomic/MetatomicPotential.h"
#include <cstdio>
#include <exception>
#include <memory>
#include <string>

Go to the source code of this file.

Classes

struct  EonMtaPot

Macros

#define EON_MTA_BUILD

Functions

static const char * nz (const char *s)
static void set_err (char *errbuf, size_t errlen, const char *msg)
int eon_mta_abi_version (void)
 ABI version for loader checks.
EonMtaPoteon_mta_pot_create (const EonMtaConfig *cfg, char *errbuf, size_t errlen)
 Create pot.
void eon_mta_pot_destroy (EonMtaPot *pot)
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.

Macro Definition Documentation

◆ EON_MTA_BUILD

#define EON_MTA_BUILD

Definition at line 8 of file MetatomicCAbi.cpp.

Function Documentation

◆ eon_mta_abi_version()

int eon_mta_abi_version ( void )

ABI version for loader checks.

Definition at line 33 of file MetatomicCAbi.cpp.

33{ return EON_MTA_ABI_VERSION; }
#define EON_MTA_ABI_VERSION

◆ eon_mta_pot_create()

EonMtaPot * eon_mta_pot_create ( const EonMtaConfig * cfg,
char * errbuf,
size_t errlen )

Create pot.

On failure returns NULL and writes a message into errbuf (if non-NULL and errlen > 0).

Definition at line 35 of file MetatomicCAbi.cpp.

36 {
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}
static void set_err(char *errbuf, size_t errlen, const char *msg)
static const char * nz(const char *s)
struct eonc::Parameters::potential_options_t potential_options
struct eonc::Parameters::metatomic_options_t metatomic_options
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

◆ eon_mta_pot_destroy()

void eon_mta_pot_destroy ( EonMtaPot * pot)

Definition at line 76 of file MetatomicCAbi.cpp.

76{ delete pot; }

◆ eon_mta_pot_force()

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.

Returns 0 on success, nonzero on failure. variance may be NULL.

Definition at line 78 of file MetatomicCAbi.cpp.

80 {
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}
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.
std::unique_ptr< MetatomicPotential > impl

◆ nz()

const char * nz ( const char * s)
static

Definition at line 23 of file MetatomicCAbi.cpp.

23{ return s ? s : ""; }

◆ set_err()

void set_err ( char * errbuf,
size_t errlen,
const char * msg )
static

Definition at line 25 of file MetatomicCAbi.cpp.

25 {
26 if (!errbuf || errlen == 0)
27 return;
28 std::snprintf(errbuf, errlen, "%s", msg ? msg : "unknown error");
29}