Loading...
Searching...
No Matches
ActiveGPPotential.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 "ActiveGPPotential.h"
6
7#include <cmath>
8#include <stdexcept>
9
10namespace pot {
11
12namespace {
13
17ActiveGP::OracleFn wrapOracle(std::shared_ptr<rgpot::PotentialBase> oracle)
18{
19 return
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);
24
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];
29
30 const std::vector<int> types(atomic_nrs, atomic_nrs + n_atoms);
31
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];
36
37 // An oracle that throws is a recoverable failure to the caller,
38 // not a crash: ActiveGP propagates a nonzero return unchanged and
39 // the host decides.
40 try {
41 const auto [e, f, unc] = (*oracle)(pos, types, cell);
42 (void)unc;
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);
48 }
49 } catch (const std::exception&) {
50 return 1;
51 }
52 return 0;
53 };
54}
55
56} // namespace
57
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)
64{
65 if (!oracle_) {
66 throw std::invalid_argument(
67 "ActiveGPPotential needs an oracle to learn from");
68 }
69}
70
71std::tuple<double, rgpot::types::AtomMatrix, double>
72ActiveGPPotential::operator()(const rgpot::types::AtomMatrix& positions,
73 const std::vector<int>& atmtypes,
74 const std::array<std::array<double, 3>, 3>& box)
75{
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");
80 }
81
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);
86
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];
91
92 std::vector<double> flat_forces(n * 3, 0.0);
93 double energy = 0.0;
94 double variance = 0.0;
95
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());
99 if (rc != 0) {
100 throw std::runtime_error(
101 "ActiveGPPotential: oracle failed and the posterior was not "
102 "resolved enough to answer without it");
103 }
104
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];
109
110 // force() gates on the energy variance and reports zero when the
111 // oracle answered; the scale operator() publishes is a standard
112 // deviation, so take the root and let the zero through as it is.
113 const double sigma = variance > 0.0 ? std::sqrt(variance) : 0.0;
114 return {energy, forces, sigma};
115}
116
117} // namespace pot
std::function< int(long n_atoms, const double *positions, const int *atomic_nrs, const double *box, double *energy, double *forces)> OracleFn
Definition ActiveGP.h:59
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.
STL namespace.