Loading...
Searching...
No Matches
XTBEngineLoader Class Reference

#include <XTBEngineLoader.h>

Public Member Functions

 XTBEngineLoader (const XTBEngineOptions &opt)
 ~XTBEngineLoader ()
 XTBEngineLoader (const XTBEngineLoader &)=delete
XTBEngineLoaderoperator= (const XTBEngineLoader &)=delete
bool available () const noexcept
void force (long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) const

Private Types

using create_fn = RgpotXtbPot *(*)(const RgpotXtbConfig *, char *, size_t)
using destroy_fn = void (*)(RgpotXtbPot *)
using force_fn

Private Attributes

void * m_lib {nullptr}
RgpotXtbPotm_pot {nullptr}
create_fn m_create {nullptr}
destroy_fn m_destroy {nullptr}
force_fn m_force {nullptr}

Detailed Description

Definition at line 20 of file XTBEngineLoader.h.

Member Typedef Documentation

◆ create_fn

using XTBEngineLoader::create_fn = RgpotXtbPot *(*)(const RgpotXtbConfig *, char *, size_t)
private

Definition at line 34 of file XTBEngineLoader.h.

◆ destroy_fn

using XTBEngineLoader::destroy_fn = void (*)(RgpotXtbPot *)
private

Definition at line 35 of file XTBEngineLoader.h.

◆ force_fn

Initial value:
int (*)(RgpotXtbPot *, long, const double *, const int *,
double *, double *, double *, const double *)
struct RgpotXtbPot RgpotXtbPot
Definition xtb_c_abi.h:28

Definition at line 36 of file XTBEngineLoader.h.

Constructor & Destructor Documentation

◆ XTBEngineLoader() [1/2]

XTBEngineLoader::XTBEngineLoader ( const XTBEngineOptions & opt)
explicit

Definition at line 38 of file XTBEngineLoader.cpp.

38 {
39 std::vector<std::string> paths;
40 if (!opt.engine_path.empty())
41 paths.push_back(opt.engine_path);
42 if (const char *e = std::getenv("RGPOT_XTB_ENGINE"))
43 if (e && *e)
44 paths.emplace_back(e);
45 if (const char *e = std::getenv("XTB_ENGINE"))
46 if (e && *e)
47 paths.emplace_back(e);
48 paths.emplace_back("libxtb_engine.so");
49 auto add_dirs = [&](const char *env) {
50 if (!env)
51 return;
52 std::string s(env);
53 size_t i = 0;
54 while (i < s.size()) {
55 auto j = s.find(':', i);
56 if (j == std::string::npos)
57 j = s.size();
58 if (j > i) {
59 std::string d = s.substr(i, j - i);
60 if (!d.empty() && d.back() != '/')
61 d += '/';
62 paths.push_back(d + "libxtb_engine.so");
63 }
64 i = j + 1;
65 }
66 };
67 add_dirs(std::getenv("EON_POTENTIALS_PATH"));
68 add_dirs(std::getenv("RGPOT_ENGINE_PATH"));
69
70 std::string last_dlerr;
71 for (const auto &p : paths) {
72 m_lib = open_lib(p.c_str());
73 if (m_lib)
74 break;
75#ifndef _WIN32
76 if (const char *e = dlerror())
77 last_dlerr = e;
78#endif
79 }
80 if (!m_lib) {
81 std::string msg = "RGPOT(xtb): libxtb_engine.so not found "
82 "(set RGPOT_XTB_ENGINE or [RgpotPot] engine_path)";
83 if (!last_dlerr.empty())
84 msg += std::string("; last dlerror: ") + last_dlerr;
85 throw std::runtime_error(msg);
86 }
87
88 auto abi =
89 reinterpret_cast<int (*)()>(load_sym(m_lib, "rgpot_xtb_abi_version"));
90 m_create = reinterpret_cast<create_fn>(load_sym(m_lib, "rgpot_xtb_create"));
91 m_destroy =
92 reinterpret_cast<destroy_fn>(load_sym(m_lib, "rgpot_xtb_destroy"));
93 m_force = reinterpret_cast<force_fn>(load_sym(m_lib, "rgpot_xtb_force"));
94 if (!abi || !m_create || !m_destroy || !m_force ||
95 abi() != RGPOT_XTB_ABI_VERSION) {
96 close_lib(m_lib);
97 m_lib = nullptr;
98 throw std::runtime_error("RGPOT(xtb): engine C ABI missing/mismatch");
99 }
100 char err[1024]{};
101 RgpotXtbConfig cfg{};
102 cfg.method = opt.method;
103 cfg.accuracy = opt.accuracy;
106 cfg.charge = opt.charge;
107 cfg.uhf = opt.uhf;
108 m_pot = m_create(&cfg, err, sizeof err);
109 if (!m_pot) {
110 close_lib(m_lib);
111 m_lib = nullptr;
112 throw std::runtime_error(std::string("RGPOT(xtb): create failed: ") + err);
113 }
114}
void(*)(RgpotXtbPot *) destroy_fn
destroy_fn m_destroy
RgpotXtbPot *(*)(const RgpotXtbConfig *, char *, size_t) create_fn
RgpotXtbPot * m_pot
int(*)(RgpotXtbPot *, long, const double *, const int *, double *, double *, double *, const double *) force_fn
double accuracy
Definition xtb_c_abi.h:40
int method
RGPOT_XTB_METHOD_*.
Definition xtb_c_abi.h:39
double charge
Definition xtb_c_abi.h:43
double electronic_temperature
Kelvin.
Definition xtb_c_abi.h:41
std::string engine_path
double electronic_temperature
struct RgpotXtbConfig RgpotXtbConfig
#define RGPOT_XTB_ABI_VERSION
Definition xtb_c_abi.h:47

◆ ~XTBEngineLoader()

XTBEngineLoader::~XTBEngineLoader ( )

Definition at line 116 of file XTBEngineLoader.cpp.

116 {
117 if (m_pot && m_destroy)
119 m_pot = nullptr;
120 // Safe to close: xtb has no torch-style static teardown hazard.
121 if (m_lib)
122 close_lib(m_lib);
123 m_lib = nullptr;
124}

◆ XTBEngineLoader() [2/2]

XTBEngineLoader::XTBEngineLoader ( const XTBEngineLoader & )
delete

Member Function Documentation

◆ available()

bool XTBEngineLoader::available ( ) const
inlinenodiscardnoexcept

Definition at line 27 of file XTBEngineLoader.h.

27{ return m_pot != nullptr; }

◆ force()

void XTBEngineLoader::force ( long N,
const double * R,
const int * atomicNrs,
double * F,
double * U,
double * variance,
const double * box ) const

Definition at line 126 of file XTBEngineLoader.cpp.

128 {
129 if (!m_pot || !m_force)
130 throw std::runtime_error("RGPOT(xtb): engine not available");
131 double var = 0.0;
132 const int rc =
133 m_force(m_pot, N, R, atomicNrs, F, U, variance ? variance : &var, box);
134 if (rc != 0)
135 throw std::runtime_error("RGPOT(xtb): force failed");
136}

◆ operator=()

XTBEngineLoader & XTBEngineLoader::operator= ( const XTBEngineLoader & )
delete

Member Data Documentation

◆ m_create

create_fn XTBEngineLoader::m_create {nullptr}
private

Definition at line 38 of file XTBEngineLoader.h.

38{nullptr};

◆ m_destroy

destroy_fn XTBEngineLoader::m_destroy {nullptr}
private

Definition at line 39 of file XTBEngineLoader.h.

39{nullptr};

◆ m_force

force_fn XTBEngineLoader::m_force {nullptr}
private

Definition at line 40 of file XTBEngineLoader.h.

40{nullptr};

◆ m_lib

void* XTBEngineLoader::m_lib {nullptr}
private

Definition at line 32 of file XTBEngineLoader.h.

32{nullptr};

◆ m_pot

RgpotXtbPot* XTBEngineLoader::m_pot {nullptr}
private

Definition at line 33 of file XTBEngineLoader.h.

33{nullptr};

The documentation for this class was generated from the following files: