5#ifndef GPR_NEB_LAZYPOTENTIALORACLE_H_
6#define GPR_NEB_LAZYPOTENTIALORACLE_H_
26inline void updateMedianNoiseSample(std::vector<double>& samples,
double sample,
29 if (!median_out || !std::isfinite(sample) || sample <= 0.0)
return;
30 samples.push_back(sample);
31 std::vector<double> sorted(samples);
32 std::nth_element(sorted.begin(), sorted.begin() + sorted.size() / 2,
34 double median = sorted[sorted.size() / 2];
35 if (sorted.size() % 2 == 0) {
36 const double lo = *std::max_element(sorted.begin(),
37 sorted.begin() + sorted.size() / 2);
38 median = 0.5 * (lo + median);
48struct LazyPotentialOracleState {
49 LazyPotentialOracleState(
50 std::function<pot::PotentialWrapper()> factory_in,
51 std::vector<int> atomic_numbers_in, std::array<double, 9> box_in,
52 int& oracle_call_counter_in,
double* oracle_seconds_in =
nullptr,
53 double* oracle_energy_noise_rms_in =
nullptr,
54 const bool* oracle_variance_updates_enabled_in =
nullptr,
55 double* oracle_force_noise_rms_in =
nullptr,
56 std::shared_ptr<const gpr::WallClockLimit> wall_clock_limit_in =
58 : factory(std::move(factory_in)),
59 atomic_numbers(std::move(atomic_numbers_in)),
61 oracle_call_counter(oracle_call_counter_in),
62 oracle_seconds(oracle_seconds_in),
63 oracle_energy_noise_rms(oracle_energy_noise_rms_in),
64 oracle_variance_updates_enabled(oracle_variance_updates_enabled_in),
65 oracle_force_noise_rms(oracle_force_noise_rms_in),
66 wall_clock_limit(std::move(wall_clock_limit_in))
69 throw std::invalid_argument(
70 "lazy potential oracle requires a potential factory");
74 std::function<pot::PotentialWrapper()> factory;
75 std::unique_ptr<pot::PotentialWrapper> potential;
76 std::vector<int> atomic_numbers;
77 std::array<double, 9> box{};
78 int& oracle_call_counter;
79 double* oracle_seconds =
nullptr;
80 double* oracle_energy_noise_rms =
nullptr;
81 const bool* oracle_variance_updates_enabled =
nullptr;
82 double* oracle_force_noise_rms =
nullptr;
83 std::shared_ptr<const gpr::WallClockLimit> wall_clock_limit;
84 std::vector<double> energy_noise_samples;
85 std::vector<double> force_noise_samples;
87 pot::PotentialWrapper& wrapped()
91 potential = std::make_unique<pot::PotentialWrapper>(factory());
101 std::vector<std::pair<double, Eigen::VectorXd>> evaluate(
102 const std::vector<Eigen::VectorXd>& points)
104 const long n_atoms =
static_cast<long>(atomic_numbers.size());
105 const std::size_t B = points.size();
106 std::vector<std::pair<double, Eigen::VectorXd>>
out(B);
107 if (B == 0)
return out;
109 for (
const Eigen::VectorXd& point: points) {
110 if (point.size() != 3 * n_atoms) {
111 throw std::runtime_error(
112 "oracle: positions size mismatch with atom count");
115 pot::PotentialWrapper& wrapper = wrapped();
116 const auto stride =
static_cast<std::size_t
>(3 * n_atoms);
117 std::vector<double> energy_noise(B, 0.0);
118 std::vector<double> force_noise(B, 0.0);
121 std::chrono::steady_clock::time_point start =
122 std::chrono::steady_clock::now();
126 *seconds += std::chrono::duration<double>(
127 std::chrono::steady_clock::now() - start)
131 } timer{oracle_seconds};
135 struct CountedCalls {
144 std::vector<double> R(B * stride);
145 std::vector<double> F(R.size(), 0.0);
146 std::vector<double> U(B, 0.0);
147 for (std::size_t b = 0; b < B; ++b) {
148 std::copy(points[b].data(), points[b].data() + stride,
149 R.data() + b * stride);
156 const CountedCalls counted{oracle_call_counter};
158 n_atoms, B, R.data(), atomic_numbers.data(), F.data(),
159 U.data(), energy_noise.data(), force_noise.data(),
162 for (std::size_t b = 0; b < B; ++b) {
164 out[b].second = -Eigen::Map<const Eigen::VectorXd>(
165 F.data() + b * stride, 3 * n_atoms);
168 for (std::size_t b = 0; b < B; ++b) {
170 Eigen::VectorXd forces =
171 Eigen::VectorXd::Zero(points[b].size());
174 const CountedCalls counted{oracle_call_counter};
176 n_atoms, points[b].data(), atomic_numbers.data(),
177 forces.data(), &energy, &energy_noise[b],
178 &force_noise[b], box.data());
180 out[b] = {energy, -forces};
183 const bool record_variance = !oracle_variance_updates_enabled ||
184 *oracle_variance_updates_enabled;
185 if (record_variance) {
194 for (std::size_t b = 0; b < B; ++b) {
195 updateMedianNoiseSample(energy_noise_samples, energy_noise[b],
196 oracle_energy_noise_rms);
197 updateMedianNoiseSample(force_noise_samples, force_noise[b],
198 oracle_force_noise_rms);
213 std::vector<int> atomic_numbers, std::array<double, 9> box,
214 int& oracle_call_counter,
double* oracle_seconds =
nullptr,
215 double* oracle_energy_noise_rms =
nullptr,
216 const bool* oracle_variance_updates_enabled =
nullptr,
217 double* oracle_force_noise_rms =
nullptr,
218 std::shared_ptr<detail::LazyPotentialOracleState>* state_out =
nullptr,
219 std::shared_ptr<const gpr::WallClockLimit> wall_clock_limit =
nullptr)
221 auto state = std::make_shared<detail::LazyPotentialOracleState>(
222 std::move(potential_factory), std::move(atomic_numbers), box,
223 oracle_call_counter, oracle_seconds, oracle_energy_noise_rms,
224 oracle_variance_updates_enabled, oracle_force_noise_rms,
225 std::move(wall_clock_limit));
226 if (state_out) *state_out = state;
227 return [state](
const Eigen::VectorXd& positions)
228 -> std::pair<double, Eigen::VectorXd> {
229 return state->evaluate({positions}).front();
238 std::shared_ptr<detail::LazyPotentialOracleState> state)
241 throw std::invalid_argument(
242 "lazy potential batch oracle requires the single oracle's state");
245 [state = std::move(state)](
const std::vector<Eigen::VectorXd>& points)
246 -> std::vector<std::pair<double, Eigen::VectorXd>> {
247 return state->evaluate(points);
GP-accelerated NEB: AIE and OIE variants.
Type-erased wrapper around any potential that provides a force() interface.
void forceBatchWithUncertainty(long N, std::size_t B, const double *R, const int *atomicNrs, double *F, double *U, double *energy_noise, double *force_noise, const double *box)
bool hasForceBatchWithUncertainty() const noexcept
void forceWithUncertainty(long N, const double *R, const int *atomicNrs, double *F, double *U, double *energy_noise_rms, double *force_noise_rms, const double *box)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
std::function< std::vector< std::pair< double, Eigen::VectorXd > >( const std::vector< Eigen::VectorXd > &positions)> NEBBatchOracle
std::function< std::pair< double, Eigen::VectorXd >( const Eigen::VectorXd &positions)> NEBOracle
Oracle function: positions -> (energy, gradient).
NEBBatchOracle makeLazyPotentialBatchOracle(std::shared_ptr< detail::LazyPotentialOracleState > state)
NEBOracle makeLazyPotentialOracle(std::function< pot::PotentialWrapper()> potential_factory, std::vector< int > atomic_numbers, std::array< double, 9 > box, int &oracle_call_counter, double *oracle_seconds=nullptr, double *oracle_energy_noise_rms=nullptr, const bool *oracle_variance_updates_enabled=nullptr, double *oracle_force_noise_rms=nullptr, std::shared_ptr< detail::LazyPotentialOracleState > *state_out=nullptr, std::shared_ptr< const gpr::WallClockLimit > wall_clock_limit=nullptr)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
void requireWallClock(const std::shared_ptr< const WallClockLimit > &limit)
long oracleEvaluations() noexcept