eOn 3.2.0
Long-timescale dynamics: aKMC, NEB, parallel replica
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
XTBEngineLoader.cpp
Go to the documentation of this file.
1
#include "
eon/potentials/Rgpot/XTBEngineLoader.h
"
2
#include <cstdlib>
3
#include <stdexcept>
4
#include <string>
5
#include <vector>
6
#ifndef _WIN32
7
#include <dlfcn.h>
8
#endif
9
10
namespace
{
11
void
*open_lib(
const
char
*path) {
12
#ifndef _WIN32
13
return
dlopen(path, RTLD_NOW | RTLD_LOCAL);
14
#else
15
static_cast<
void
>
(path);
16
return
nullptr
;
17
#endif
18
}
19
void
close_lib(
void
*h) {
20
#ifndef _WIN32
21
if
(h)
22
dlclose(h);
23
#else
24
static_cast<
void
>
(h);
25
#endif
26
}
27
void
*load_sym(
void
*h,
const
char
*n) {
28
#ifndef _WIN32
29
return
dlsym(h, n);
30
#else
31
static_cast<
void
>
(h);
32
static_cast<
void
>
(n);
33
return
nullptr
;
34
#endif
35
}
36
}
// namespace
37
38
XTBEngineLoader::XTBEngineLoader
(
const
XTBEngineOptions
&opt) {
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(
"librgpot_xtb_engine.so"
);
49
paths.emplace_back(
"libxtb_engine.so"
);
50
auto
add_dirs = [&](
const
char
*env) {
51
if
(!env)
52
return
;
53
std::string s(env);
54
size_t
i = 0;
55
while
(i < s.size()) {
56
auto
j = s.find(
':'
, i);
57
if
(j == std::string::npos)
58
j = s.size();
59
if
(j > i) {
60
std::string d = s.substr(i, j - i);
61
if
(!d.empty() && d.back() !=
'/'
)
62
d +=
'/'
;
63
paths.push_back(d +
"librgpot_xtb_engine.so"
);
64
paths.push_back(d +
"libxtb_engine.so"
);
65
}
66
i = j + 1;
67
}
68
};
69
add_dirs(std::getenv(
"EON_POTENTIALS_PATH"
));
70
add_dirs(std::getenv(
"RGPOT_ENGINE_PATH"
));
71
72
std::string last_dlerr;
73
for
(
const
auto
&p : paths) {
74
m_lib
= open_lib(p.c_str());
75
if
(
m_lib
)
76
break
;
77
#ifndef _WIN32
78
if
(
const
char
*e = dlerror())
79
last_dlerr = e;
80
#endif
81
}
82
if
(!
m_lib
) {
83
std::string msg =
"RGPOT(xtb): librgpot_xtb_engine.so not found "
84
"(set RGPOT_XTB_ENGINE or [RgpotPot] engine_path)"
;
85
if
(!last_dlerr.empty())
86
msg += std::string(
"; last dlerror: "
) + last_dlerr;
87
throw
std::runtime_error(msg);
88
}
89
90
auto
abi =
91
reinterpret_cast<
int
(*)()
>
(load_sym(
m_lib
,
"rgpot_xtb_abi_version"
));
92
m_create
=
reinterpret_cast<
create_fn
>
(load_sym(
m_lib
,
"rgpot_xtb_create"
));
93
m_destroy
=
94
reinterpret_cast<
destroy_fn
>
(load_sym(
m_lib
,
"rgpot_xtb_destroy"
));
95
m_force
=
reinterpret_cast<
force_fn
>
(load_sym(
m_lib
,
"rgpot_xtb_force"
));
96
if
(!abi || !
m_create
|| !
m_destroy
|| !
m_force
||
97
abi() !=
RGPOT_XTB_ABI_VERSION
) {
98
close_lib(
m_lib
);
99
m_lib
=
nullptr
;
100
throw
std::runtime_error(
"RGPOT(xtb): engine C ABI missing/mismatch"
);
101
}
102
char
err[1024]{};
103
RgpotXtbConfig
cfg{};
104
cfg.
method
= opt.
method
;
105
cfg.
accuracy
= opt.
accuracy
;
106
cfg.
electronic_temperature
= opt.
electronic_temperature
;
107
cfg.
max_iterations
= opt.
max_iterations
;
108
cfg.
charge
= opt.
charge
;
109
cfg.
uhf
= opt.
uhf
;
110
m_pot
=
m_create
(&cfg, err,
sizeof
err);
111
if
(!
m_pot
) {
112
close_lib(
m_lib
);
113
m_lib
=
nullptr
;
114
throw
std::runtime_error(std::string(
"RGPOT(xtb): create failed: "
) + err);
115
}
116
}
117
118
XTBEngineLoader::~XTBEngineLoader
() {
119
if
(
m_pot
&&
m_destroy
)
120
m_destroy
(
m_pot
);
121
m_pot
=
nullptr
;
122
// Safe to close: xtb has no torch-style static teardown hazard.
123
if
(
m_lib
)
124
close_lib(
m_lib
);
125
m_lib
=
nullptr
;
126
}
127
128
void
XTBEngineLoader::force
(
long
N,
const
double
*R,
const
int
*atomicNrs,
129
double
*F,
double
*U,
double
*variance,
130
const
double
*box)
const
{
131
if
(!
m_pot
|| !
m_force
)
132
throw
std::runtime_error(
"RGPOT(xtb): engine not available"
);
133
double
var = 0.0;
134
const
int
rc =
135
m_force
(
m_pot
, N, R, atomicNrs, F, U, variance ? variance : &var, box);
136
if
(rc != 0)
137
throw
std::runtime_error(
"RGPOT(xtb): force failed"
);
138
}
XTBEngineLoader.h
XTBEngineLoader::~XTBEngineLoader
~XTBEngineLoader()
Definition
XTBEngineLoader.cpp:118
XTBEngineLoader::destroy_fn
void(*)(RgpotXtbPot *) destroy_fn
Definition
XTBEngineLoader.h:35
XTBEngineLoader::m_destroy
destroy_fn m_destroy
Definition
XTBEngineLoader.h:39
XTBEngineLoader::m_force
force_fn m_force
Definition
XTBEngineLoader.h:40
XTBEngineLoader::force
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) const
Definition
XTBEngineLoader.cpp:128
XTBEngineLoader::m_lib
void * m_lib
Definition
XTBEngineLoader.h:32
XTBEngineLoader::create_fn
RgpotXtbPot *(*)(const RgpotXtbConfig *, char *, size_t) create_fn
Definition
XTBEngineLoader.h:34
XTBEngineLoader::m_pot
RgpotXtbPot * m_pot
Definition
XTBEngineLoader.h:33
XTBEngineLoader::m_create
create_fn m_create
Definition
XTBEngineLoader.h:38
XTBEngineLoader::XTBEngineLoader
XTBEngineLoader(const XTBEngineOptions &opt)
Definition
XTBEngineLoader.cpp:38
XTBEngineLoader::force_fn
int(*)(RgpotXtbPot *, long, const double *, const int *, double *, double *, double *, const double *) force_fn
Definition
XTBEngineLoader.h:36
RgpotXtbConfig
Definition
xtb_c_abi.h:38
RgpotXtbConfig::max_iterations
int max_iterations
Definition
xtb_c_abi.h:42
RgpotXtbConfig::accuracy
double accuracy
Definition
xtb_c_abi.h:40
RgpotXtbConfig::method
int method
RGPOT_XTB_METHOD_*.
Definition
xtb_c_abi.h:39
RgpotXtbConfig::charge
double charge
Definition
xtb_c_abi.h:43
RgpotXtbConfig::electronic_temperature
double electronic_temperature
Kelvin.
Definition
xtb_c_abi.h:41
RgpotXtbConfig::uhf
int uhf
Definition
xtb_c_abi.h:44
XTBEngineOptions
Definition
XTBEngineLoader.h:10
XTBEngineOptions::uhf
int uhf
Definition
XTBEngineLoader.h:16
XTBEngineOptions::engine_path
std::string engine_path
Definition
XTBEngineLoader.h:17
XTBEngineOptions::max_iterations
int max_iterations
Definition
XTBEngineLoader.h:14
XTBEngineOptions::charge
double charge
Definition
XTBEngineLoader.h:15
XTBEngineOptions::electronic_temperature
double electronic_temperature
Definition
XTBEngineLoader.h:13
XTBEngineOptions::method
int method
Definition
XTBEngineLoader.h:11
XTBEngineOptions::accuracy
double accuracy
Definition
XTBEngineLoader.h:12
RGPOT_XTB_ABI_VERSION
#define RGPOT_XTB_ABI_VERSION
Definition
xtb_c_abi.h:47
client
potentials
Rgpot
XTBEngineLoader.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf