GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
CbeMethaneOracle.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
#pragma once
3
#include <dlfcn.h>
4
5
#include <atomic>
6
#include <cmath>
7
#include <map>
8
#include <memory>
9
#include <mutex>
10
#include <stdexcept>
11
#include <string>
12
13
#include "
gpr/quantum/PotentialPath.h
"
14
15
namespace
gpr::potentials
{
16
// Corchado, Bravo and Espinosa-Garcia, JCP 130, 184314 (2009).
17
// The native plugin exports the unmodified i-PI prepot_ and ch4hpot_inter_
18
// routines. Coordinates are Bohr in H,C,H,H,H,H order; energy is Hartree.
19
class
CbeMethaneOracle
{
20
struct
State {
21
using
Initialize = void (*)();
22
using
Energy = void (*)(
double
*,
double
*);
23
void
* library{};
24
Energy
energy
{};
25
std::mutex mutex;
26
explicit
State(
void
* handle) : library(handle)
27
{
28
auto
initialize =
29
reinterpret_cast<
Initialize
>
(dlsym(library,
"prepot_"
));
30
energy
=
reinterpret_cast<
Energy
>
(dlsym(library,
"ch4hpot_inter_"
));
31
if
(!initialize || !
energy
)
32
throw
std::runtime_error(
"Incomplete native CBE interface"
);
33
initialize();
34
}
35
~State()
36
{
37
if
(library) dlclose(library);
38
}
39
};
40
static
std::shared_ptr<State> acquire(
const
std::string& path)
41
{
42
static
std::mutex registryMutex;
43
static
std::map<void*, std::weak_ptr<State>> registry;
44
std::lock_guard lock(registryMutex);
45
void
* handle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
46
if
(!handle)
47
throw
std::runtime_error(
48
std::string(
"Cannot load CBE potential: "
) + dlerror());
49
auto
existing = registry[handle].lock();
50
if
(existing) {
51
dlclose(handle);
52
return
existing;
53
}
54
try
{
55
auto
state = std::make_shared<State>(handle);
56
registry[handle] = state;
57
return
state;
58
}
catch
(...) {
59
dlclose(handle);
60
throw
;
61
}
62
}
63
std::shared_ptr<State> state_;
64
double
step_;
65
std::atomic<std::size_t> energies_{}, gradients_{};
66
double
energyUnlocked(
const
Eigen::VectorXd&
x
)
67
{
68
double
coordinates[18], value;
69
for
(
int
atom = 0; atom < 6; ++atom)
70
for
(
int
axis = 0; axis < 3; ++axis)
71
coordinates[axis * 6 + atom] =
x
[atom * 3 + axis];
72
state_->energy(coordinates, &value);
73
++energies_;
74
if
(!std::isfinite(value))
75
throw
std::runtime_error(
"Nonfinite native CBE energy"
);
76
return
value;
77
}
78
79
public
:
80
// The finite-difference step matches the pinned i-PI driver. Counts
81
// distinguish force requests from the 37 native energy calls per request.
82
explicit
CbeMethaneOracle
(
const
std::string& library,
double
step = 1e-5)
83
: state_(acquire(library)), step_(step)
84
{
85
if
(!(step > 0) || !std::isfinite(step))
86
throw
std::invalid_argument(
87
"Positive CBE derivative step required"
);
88
}
89
quantum::PotentialEvaluation
evaluate
(
const
Eigen::VectorXd&
x
)
90
{
91
if
(
x
.size() != 18 || !
x
.allFinite())
92
throw
std::invalid_argument(
93
"CBE requires 18 finite Cartesian coordinates"
);
94
std::lock_guard lock(state_->mutex);
95
quantum::PotentialEvaluation
out
{energyUnlocked(
x
),
96
Eigen::VectorXd(18)};
97
for
(
int
k = 0; k < 18; ++k) {
98
Eigen::VectorXd plus =
x
, minus =
x
;
99
plus[k] += step_;
100
minus[k] -= step_;
101
out
.gradient[k] =
102
(energyUnlocked(plus) - energyUnlocked(minus)) / (2 * step_);
103
}
104
if
(!
out
.gradient.allFinite())
105
throw
std::runtime_error(
"Nonfinite native CBE gradient"
);
106
++gradients_;
107
return
out
;
108
}
109
double
energy
(
const
Eigen::VectorXd&
x
)
110
{
111
if
(
x
.size() != 18 || !
x
.allFinite())
112
throw
std::invalid_argument(
113
"CBE requires 18 finite Cartesian coordinates"
);
114
std::lock_guard lock(state_->mutex);
115
return
energyUnlocked(
x
);
116
}
117
std::size_t
energyCalls
() const noexcept
118
{
119
return
energies_.load();
120
}
121
std::size_t
gradientCalls
() const noexcept
122
{
123
return
gradients_.load();
124
}
125
};
126
}
// namespace gpr::potentials
x
@ x
Definition
Enums.h:28
PotentialPath.h
gpr::potentials::CbeMethaneOracle::energyCalls
std::size_t energyCalls() const noexcept
Definition
CbeMethaneOracle.h:117
gpr::potentials::CbeMethaneOracle::CbeMethaneOracle
CbeMethaneOracle(const std::string &library, double step=1e-5)
Definition
CbeMethaneOracle.h:82
gpr::potentials::CbeMethaneOracle::evaluate
quantum::PotentialEvaluation evaluate(const Eigen::VectorXd &x)
Definition
CbeMethaneOracle.h:89
gpr::potentials::CbeMethaneOracle::energy
double energy(const Eigen::VectorXd &x)
Definition
CbeMethaneOracle.h:109
gpr::potentials::CbeMethaneOracle::gradientCalls
std::size_t gradientCalls() const noexcept
Definition
CbeMethaneOracle.h:121
out
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Definition
eon_relax_engine.h:240
gpr::potentials
Analytical test potentials and potentials built from a fitted GP.
Definition
AnalyticalPotentials.h:27
gpr::quantum::PotentialEvaluation
Definition
PotentialPath.h:16
gpr
potentials
CbeMethaneOracle.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf