20 [oracle](
long n_atoms,
const double* positions,
const int* atomic_nrs,
21 const double* box,
double* energy,
double* forces) ->
int {
22 if (!oracle)
return -1;
23 const auto n =
static_cast<std::size_t
>(n_atoms);
25 rgpot::types::AtomMatrix pos(n, 3);
26 for (std::size_t a = 0; a < n; ++a)
27 for (std::size_t d = 0; d < 3; ++d)
28 pos(a, d) = positions[a * 3 + d];
30 const std::vector<int> types(atomic_nrs, atomic_nrs + n_atoms);
32 std::array<std::array<double, 3>, 3> cell{};
33 for (std::size_t r = 0; r < 3; ++r)
34 for (std::size_t c = 0; c < 3; ++c)
35 cell[r][c] = box[r * 3 + c];
41 const auto [e, f, unc] = (*oracle)(pos, types, cell);
43 if (energy !=
nullptr) *energy = e;
44 if (forces !=
nullptr) {
45 for (std::size_t a = 0; a < n; ++a)
46 for (std::size_t d = 0; d < 3; ++d)
47 forces[a * 3 + d] = f(a, d);
49 }
catch (
const std::exception&) {
59 std::shared_ptr<gpr::neb::ActiveLearningSession> session,
60 std::shared_ptr<rgpot::PotentialBase> oracle,
Config config)
61 :
rgpot::PotentialBase(
rgpot::PotType::UNKNOWN),
62 oracle_(
std::move(oracle)),
63 surrogate_(
std::move(session), wrapOracle(oracle_), config)
66 throw std::invalid_argument(
67 "ActiveGPPotential needs an oracle to learn from");
71std::tuple<double, rgpot::types::AtomMatrix, double>
73 const std::vector<int>& atmtypes,
74 const std::array<std::array<double, 3>, 3>& box)
76 const std::size_t n = positions.rows();
77 if (atmtypes.size() != n) {
78 throw std::invalid_argument(
79 "ActiveGPPotential: one atomic number per row of positions");
82 std::vector<double> flat_pos(n * 3);
83 for (std::size_t a = 0; a < n; ++a)
84 for (std::size_t d = 0; d < 3; ++d)
85 flat_pos[a * 3 + d] = positions(a, d);
87 std::array<double, 9> flat_box{};
88 for (std::size_t r = 0; r < 3; ++r)
89 for (std::size_t c = 0; c < 3; ++c)
90 flat_box[r * 3 + c] = box[r][c];
92 std::vector<double> flat_forces(n * 3, 0.0);
94 double variance = 0.0;
96 const int rc = surrogate_.force(
static_cast<long>(n), flat_pos.data(),
97 atmtypes.data(), flat_forces.data(),
98 &energy, &variance, flat_box.data());
100 throw std::runtime_error(
101 "ActiveGPPotential: oracle failed and the posterior was not "
102 "resolved enough to answer without it");
105 rgpot::types::AtomMatrix forces(n, 3);
106 for (std::size_t a = 0; a < n; ++a)
107 for (std::size_t d = 0; d < 3; ++d)
108 forces(a, d) = flat_forces[a * 3 + d];
113 const double sigma = variance > 0.0 ? std::sqrt(variance) : 0.0;
114 return {energy, forces, sigma};
std::function< int(long n_atoms, const double *positions, const int *atomic_nrs, const double *box, double *energy, double *forces)> OracleFn
ActiveGPPotential(std::shared_ptr< gpr::neb::ActiveLearningSession > session, std::shared_ptr< rgpot::PotentialBase > oracle, Config config=Config())
std::tuple< double, rgpot::types::AtomMatrix, double > operator()(const rgpot::types::AtomMatrix &positions, const std::vector< int > &atmtypes, const std::array< std::array< double, 3 >, 3 > &box) override
Potential wrappers and the batched oracle interface.