GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
EonRelaxClient.cpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
// See LICENSE for details.
4
5
#include "
EonRelaxClient.h
"
6
7
#include <dlfcn.h>
8
9
#include <algorithm>
10
#include <cstdlib>
11
#include <vector>
12
13
#include "
../potentials/GprMeanPotential.h
"
14
15
namespace
gpr
{
16
namespace
neb
{
17
18
EonRelaxClient::~EonRelaxClient
()
19
{
20
destroy
();
21
if
(lib_ !=
nullptr
) {
22
dlclose(lib_);
23
lib_ =
nullptr
;
24
}
25
}
26
27
bool
EonRelaxClient::load
(
const
std::string& path)
28
{
29
std::string resolved = path;
30
if
(resolved.empty()) {
31
const
char
* env = std::getenv(
"GPR_EON_RELAX_ENGINE"
);
32
if
(env !=
nullptr
) resolved = env;
33
}
34
if
(resolved.empty()) {
35
message_ =
"no engine path: pass one or set GPR_EON_RELAX_ENGINE"
;
36
return
false
;
37
}
38
destroy
();
39
if
(lib_ !=
nullptr
) {
40
dlclose(lib_);
41
lib_ =
nullptr
;
42
}
43
run_ =
nullptr
;
44
step_ =
nullptr
;
45
reset_ =
nullptr
;
46
set_epoch_ =
nullptr
;
47
last_error_ =
nullptr
;
48
destroy_ =
nullptr
;
49
create_ =
nullptr
;
50
lib_ = dlopen(resolved.c_str(), RTLD_NOW | RTLD_LOCAL);
51
if
(lib_ ==
nullptr
) {
52
const
char
* err = dlerror();
53
message_ = err !=
nullptr
? err :
"dlopen failed"
;
54
return
false
;
55
}
56
auto
*
stamp
=
57
reinterpret_cast<
abi_stamp_fn
>
(dlsym(lib_,
"eon_relax_abi_stamp"
));
58
auto
* available =
59
reinterpret_cast<
available_fn
>
(dlsym(lib_,
"eon_relax_available"
));
60
create_ =
reinterpret_cast<
create_fn
>
(dlsym(lib_,
"eon_relax_create"
));
61
run_ =
reinterpret_cast<
run_fn
>
(dlsym(lib_,
"eon_relax_run"
));
62
step_ =
reinterpret_cast<
step_fn
>
(dlsym(lib_,
"eon_relax_step"
));
63
reset_ =
reinterpret_cast<
reset_fn
>
(dlsym(lib_,
"eon_relax_reset"
));
64
set_epoch_ =
reinterpret_cast<
set_epoch_fn
>
(
65
dlsym(lib_,
"eon_relax_set_surface_epoch"
));
66
last_error_ =
67
reinterpret_cast<
last_error_fn
>
(dlsym(lib_,
"eon_relax_last_error"
));
68
destroy_ =
reinterpret_cast<
destroy_fn
>
(dlsym(lib_,
"eon_relax_destroy"
));
69
if
(
stamp
==
nullptr
|| available ==
nullptr
|| create_ ==
nullptr
||
70
run_ ==
nullptr
|| step_ ==
nullptr
|| reset_ ==
nullptr
||
71
set_epoch_ ==
nullptr
|| last_error_ ==
nullptr
||
72
destroy_ ==
nullptr
) {
73
message_ =
"mandatory eon_relax symbols missing"
;
74
dlclose(lib_);
75
lib_ =
nullptr
;
76
run_ =
nullptr
;
77
return
false
;
78
}
79
if
(
stamp
(&stamp_) != 0 || stamp_.
major
!=
EON_RELAX_ABI_MAJOR
||
80
stamp_.minor <
EON_RELAX_ABI_MINOR
) {
81
message_ =
"eon_relax ABI stamp mismatch"
;
82
dlclose(lib_);
83
lib_ =
nullptr
;
84
run_ =
nullptr
;
85
return
false
;
86
}
87
if
(available() == 0) {
88
message_ =
"eon_relax reports unavailable"
;
89
dlclose(lib_);
90
lib_ =
nullptr
;
91
run_ =
nullptr
;
92
return
false
;
93
}
94
message_.clear();
95
return
true
;
96
}
97
98
bool
EonRelaxClient::create
(
const
void
* config, std::size_t
config_len
)
99
{
100
if
(!
loaded
()) {
101
message_ =
"engine library not loaded"
;
102
return
false
;
103
}
104
destroy
();
105
char
errbuf
[1024] = {0};
106
engine_ = create_(config,
config_len
,
errbuf
,
sizeof
errbuf
);
107
if
(engine_ ==
nullptr
) {
108
message_ =
errbuf
[0] !=
'\0'
?
errbuf
:
"eon_relax_create failed"
;
109
return
false
;
110
}
111
message_.clear();
112
return
true
;
113
}
114
115
int
EonRelaxClient::run
(
eon_relax_band_t
&
band
,
eon_relax_surface_fn
surface
,
116
void
*
user
,
eon_relax_outcome_t
& outcome)
117
{
118
if
(!
created
()) {
119
message_ =
"engine instance not created"
;
120
return
EON_RELAX_NULL_ENGINE
;
121
}
122
return
run_(engine_, &
band
,
surface
,
user
, &outcome);
123
}
124
125
int
EonRelaxClient::step
(
eon_relax_band_t
&
band
,
eon_relax_surface_fn
surface
,
126
void
*
user
,
eon_relax_outcome_t
& outcome)
127
{
128
if
(!
created
()) {
129
message_ =
"engine instance not created"
;
130
return
EON_RELAX_NULL_ENGINE
;
131
}
132
return
step_(engine_, &
band
,
surface
,
user
, &outcome);
133
}
134
135
int
EonRelaxClient::reset
()
136
{
137
if
(!
created
()) {
138
message_ =
"engine instance not created"
;
139
return
EON_RELAX_NULL_ENGINE
;
140
}
141
return
reset_(engine_);
142
}
143
144
int
EonRelaxClient::setSurfaceEpoch
(std::uint64_t
epoch
)
145
{
146
if
(!
created
()) {
147
message_ =
"engine instance not created"
;
148
return
EON_RELAX_NULL_ENGINE
;
149
}
150
return
set_epoch_(engine_,
epoch
);
151
}
152
153
int
EonRelaxClient::lastError
()
const
154
{
155
if
(!
created
() || last_error_ ==
nullptr
)
return
EON_RELAX_NULL_ENGINE
;
156
return
last_error_(engine_);
157
}
158
159
void
EonRelaxClient::destroy
()
160
{
161
if
(engine_ !=
nullptr
&& destroy_ !=
nullptr
) {
162
destroy_(engine_);
163
}
164
engine_ =
nullptr
;
165
}
166
167
int
gprMeanSurfaceCallback
(
void
*
user
,
168
eon_relax_surface_request_t
* req)
noexcept
169
{
170
if
(
user
==
nullptr
|| req ==
nullptr
|| req->positions ==
nullptr
||
171
req->atomic_nrs ==
nullptr
|| req->boxes ==
nullptr
||
172
req->energies ==
nullptr
|| req->forces ==
nullptr
||
173
req->n_images <= 0 || req->n_atoms <= 0) {
174
return
-1;
175
}
176
if
(req->version.major !=
EON_RELAX_ABI_MAJOR
) {
177
return
-1;
178
}
179
try
{
180
auto
*
surface
=
static_cast<
pot::GprMeanPotential
*
>
(
user
);
181
const
long
n_images =
static_cast<
long
>
(req->n_images);
182
const
long
n_atoms =
static_cast<
long
>
(req->n_atoms);
183
const
long
dof = 3 * n_atoms;
184
std::vector<int> atomic_nrs(req->atomic_nrs, req->atomic_nrs + n_atoms);
185
// Publish nothing until every image succeeds. A later failure
186
// must leave the caller buffers as they were.
187
std::vector<double> scratch_energies(
188
static_cast<
std::size_t
>
(n_images));
189
std::vector<double> scratch_forces(
static_cast<
std::size_t
>
(n_images) *
190
static_cast<
std::size_t
>
(dof));
191
std::vector<double> scratch_variances;
192
if
(req->variances !=
nullptr
)
193
scratch_variances.resize(
static_cast<
std::size_t
>
(n_images));
194
for
(
long
i = 0; i < n_images; ++i) {
195
double
variance = 0.0;
196
surface
->force(n_atoms, req->positions + i * dof, atomic_nrs.data(),
197
scratch_forces.data() + i * dof,
198
&scratch_energies[
static_cast<
std::size_t
>
(i)],
199
req->variances !=
nullptr
? &variance :
nullptr
,
200
req->boxes + i * 9);
201
// Same acceptance rule as energyVarianceToRms. A rejected
202
// raw variance must fail before any caller buffer or the
203
// epoch is written. A tiny negative value may stay raw.
204
if
(req->variances !=
nullptr
&&
205
!
pot::energyVarianceAccepted
(variance))
206
return
-1;
207
if
(req->variances !=
nullptr
)
208
scratch_variances[
static_cast<
std::size_t
>
(i)] = variance;
209
}
210
std::copy_n(scratch_energies.data(), n_images, req->energies);
211
std::copy_n(
212
scratch_forces.data(),
213
static_cast<
std::size_t
>
(n_images) *
static_cast<
std::size_t
>
(dof),
214
req->forces);
215
if
(req->variances !=
nullptr
)
216
std::copy_n(scratch_variances.data(), n_images, req->variances);
217
if
(req->epoch_out !=
nullptr
)
218
*req->epoch_out =
surface
->surfaceEpoch();
219
return
0;
220
}
catch
(...) {
221
return
-1;
222
}
223
}
224
225
}
// namespace neb
226
}
// namespace gpr
EonRelaxClient.h
GprMeanPotential.h
gpr::neb::EonRelaxClient::destroy
void destroy()
Definition
EonRelaxClient.cpp:159
gpr::neb::EonRelaxClient::loaded
bool loaded() const noexcept
Definition
EonRelaxClient.h:40
gpr::neb::EonRelaxClient::stamp
eon_relax_version_t stamp() const noexcept
Definition
EonRelaxClient.h:75
gpr::neb::EonRelaxClient::created
bool created() const noexcept
Definition
EonRelaxClient.h:49
gpr::neb::EonRelaxClient::setSurfaceEpoch
int setSurfaceEpoch(std::uint64_t epoch)
Definition
EonRelaxClient.cpp:144
gpr::neb::EonRelaxClient::reset
int reset()
Definition
EonRelaxClient.cpp:135
gpr::neb::EonRelaxClient::lastError
int lastError() const
Definition
EonRelaxClient.cpp:153
gpr::neb::EonRelaxClient::run
int run(eon_relax_band_t &band, eon_relax_surface_fn surface, void *user, eon_relax_outcome_t &outcome)
Definition
EonRelaxClient.cpp:115
gpr::neb::EonRelaxClient::~EonRelaxClient
~EonRelaxClient()
Definition
EonRelaxClient.cpp:18
gpr::neb::EonRelaxClient::step
int step(eon_relax_band_t &band, eon_relax_surface_fn surface, void *user, eon_relax_outcome_t &outcome)
Definition
EonRelaxClient.cpp:125
gpr::neb::EonRelaxClient::create
bool create(const void *config=nullptr, std::size_t config_len=0)
Definition
EonRelaxClient.cpp:98
gpr::neb::EonRelaxClient::load
bool load(const std::string &path={})
Definition
EonRelaxClient.cpp:27
pot::GprMeanPotential
Definition
GprMeanPotential.h:31
EON_RELAX_ABI_MAJOR
#define EON_RELAX_ABI_MAJOR
Definition
eon_relax_engine.h:70
EON_RELAX_ABI_MINOR
#define EON_RELAX_ABI_MINOR
Definition
eon_relax_engine.h:71
surface
eon_relax_band_t eon_relax_surface_fn surface
Definition
eon_relax_engine.h:239
band
eon_relax_band_t * band
Definition
eon_relax_engine.h:238
epoch
uint64_t epoch
Definition
eon_relax_engine.h:225
errbuf
size_t char * errbuf
Definition
eon_relax_engine.h:221
config_len
size_t config_len
Definition
eon_relax_engine.h:221
EON_RELAX_NULL_ENGINE
@ EON_RELAX_NULL_ENGINE
Definition
eon_relax_engine.h:98
eon_relax_surface_fn
int(* eon_relax_surface_fn)(void *user, eon_relax_surface_request_t *req)
Definition
eon_relax_engine.h:199
user
eon_relax_band_t eon_relax_surface_fn void * user
Definition
eon_relax_engine.h:239
gpr::neb
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
Definition
ChainRealign.h:29
gpr::neb::gprMeanSurfaceCallback
int gprMeanSurfaceCallback(void *user, eon_relax_surface_request_t *req) noexcept
Definition
EonRelaxClient.cpp:167
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
pot::energyVarianceAccepted
bool energyVarianceAccepted(const double variance) noexcept
Definition
PotentialWrapper.h:45
eon_relax_band_t
Definition
eon_relax_engine.h:147
eon_relax_outcome_t
Definition
eon_relax_engine.h:162
eon_relax_surface_request_t
Definition
eon_relax_engine.h:183
eon_relax_version_t::major
uint32_t major
Definition
eon_relax_engine.h:75
gpr
neb
EonRelaxClient.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf