12#ifndef PotentialWrapper_h
13#define PotentialWrapper_h
26#include "rgpot/ForceStructs.hpp"
27#include "rgpot/types/AtomMatrix.hpp"
57 throw std::domain_error(
58 "PotentialWrapper: rgpot energy variance must be finite and "
60 if (variance < 0.0)
return 0.0;
61 return std::sqrt(variance);
95 struct RichForceTag { };
103 long N,
const double* R,
const int* atomicNrs,
double* F,
double* U,
104 double* energy_noise_rms,
const double* box)>;
106 long N,
const double* R,
const int* atomicNrs,
double* F,
double* U,
107 double* energy_noise_rms,
double* force_noise_rms,
const double* box)>;
111 long N, std::size_t B,
const double* R,
const int* atomicNrs,
double* F,
112 double* U,
const double* box)>;
115 long N, std::size_t B,
const double* R,
const int* atomicNrs,
double* F,
116 double* U,
double* V,
const double* box)>;
119 long N, std::size_t B,
const double* R,
const int* atomicNrs,
double* F,
120 double* U,
double* energy_noise_rms,
double* force_noise_rms,
126 throw std::invalid_argument(
127 "PotentialWrapper: force function must not be null");
129 m_force_with_uncertainty =
130 [fn = std::move(fn)](
long N,
const double* R,
const int* atomicNrs,
131 double* F,
double* U,
double* energy_noise_rms,
132 double* force_noise_rms,
133 const double* box)
mutable {
134 fn(N, R, atomicNrs, F, U, energy_noise_rms, box);
138 if (force_noise_rms) *force_noise_rms = 0.0;
145 void force(
long N,
const double* R,
const int* atomicNrs,
double* F,
146 double* U,
double* energy_noise_rms,
const double* box)
153 double* F,
double* U,
double* energy_noise_rms,
154 double* force_noise_rms,
const double* box)
157 m_force_with_uncertainty(N, R, atomicNrs, F, U, energy_noise_rms,
158 force_noise_rms, box);
171 return static_cast<bool>(m_force_batch) ||
177 return static_cast<bool>(m_force_batch_with_uncertainty);
182 m_force_batch = std::move(fn);
183 m_force_batch_with_uncertainty = {};
198 [fn = std::move(fn)](
long N, std::size_t B,
const double* R,
199 const int* atomicNrs,
double* F,
double* U,
200 double* energy_noise,
double* force_noise,
202 fn(N, B, R, atomicNrs, F, U, force_noise, box);
206 if (energy_noise) std::fill_n(energy_noise, B, 0.0);
212 m_force_batch_with_uncertainty = std::move(fn);
218 const int* atomicNrs,
double* F,
double* U,
222 if (m_force_batch_with_uncertainty) {
223 m_force_batch_with_uncertainty(N, B, R, atomicNrs, F, U,
nullptr,
225 }
else if (m_force_batch) {
226 m_force_batch(N, B, R, atomicNrs, F, U, box);
228 throw std::logic_error(
229 "PotentialWrapper: no batch surface on this potential");
235 const int* atomicNrs,
double* F,
double* U,
double* V,
248 const int* atomicNrs,
double* F,
double* U,
249 double* energy_noise,
double* force_noise,
253 if (m_force_batch_with_uncertainty) {
254 m_force_batch_with_uncertainty(N, B, R, atomicNrs, F, U,
255 energy_noise, force_noise, box);
264 throw std::invalid_argument(
265 "PotentialWrapper: n_atoms must be positive");
267 const auto stride =
static_cast<std::size_t
>(N) * 3;
268 const bool write_forces = F !=
nullptr && stride > 0;
269 const bool write_energy = U !=
nullptr;
270 const bool write_energy_noise = energy_noise !=
nullptr;
271 const bool write_force_noise = force_noise !=
nullptr;
272 std::vector<double> scratch_forces(write_forces ? B * stride : 0);
273 std::vector<double> scratch_energy(write_energy ? B : 0);
274 std::vector<double> scratch_energy_noise(write_energy_noise ? B : 0);
275 std::vector<double> scratch_force_noise(write_force_noise ? B : 0);
276 for (std::size_t b = 0; b < B; ++b) {
278 N, R + b * stride, atomicNrs,
279 write_forces ? scratch_forces.data() + b * stride :
nullptr,
280 write_energy ? scratch_energy.data() + b :
nullptr,
281 write_energy_noise ? scratch_energy_noise.data() + b :
nullptr,
282 write_force_noise ? scratch_force_noise.data() + b :
nullptr,
285 if (write_forces && B > 0)
286 std::copy_n(scratch_forces.data(), scratch_forces.size(), F);
287 if (write_energy && B > 0) std::copy_n(scratch_energy.data(), B, U);
288 if (write_energy_noise && B > 0)
289 std::copy_n(scratch_energy_noise.data(), B, energy_noise);
290 if (write_force_noise && B > 0)
291 std::copy_n(scratch_force_noise.data(), B, force_noise);
307 [geometry = std::move(geometry),
308 uncertainty = std::move(uncertainty)](
309 long N,
const double* R,
const int* atomicNrs,
double* F,
310 double* U,
double* energy_noise_rms,
double* force_noise_rms,
311 const double* box)
mutable {
315 double geometry_energy_noise = 0.0;
316 geometry.forceWithUncertainty(N, R, atomicNrs, F, U,
317 energy_noise_rms !=
nullptr
318 ? &geometry_energy_noise
320 force_noise_rms, box);
321 if (!energy_noise_rms)
return;
323 const std::size_t n_components =
324 N > 0 ?
static_cast<std::size_t
>(N) * 3 : 0;
325 std::vector<double> discarded_forces(n_components);
326 double discarded_energy = 0.0;
327 double source_energy_noise_rms = 0.0;
328 double discarded_force_noise_rms = 0.0;
330 N, R, atomicNrs, discarded_forces.data(), &discarded_energy,
331 &source_energy_noise_rms, &discarded_force_noise_rms, box);
332 *energy_noise_rms = source_energy_noise_rms;
346 [source = std::move(source)](
347 long N,
const double* R,
const int* atomicNrs,
double* F,
348 double* U,
double* energy_noise_rms,
double* force_noise_rms,
349 const double* box)
mutable {
350 double routed_force_noise_rms = 0.0;
351 double discarded_force_noise_rms = 0.0;
353 &routed_force_noise_rms,
354 &discarded_force_noise_rms, box);
355 if (energy_noise_rms) *energy_noise_rms = 0.0;
356 if (force_noise_rms) *force_noise_rms = routed_force_noise_rms;
366 template <
typename RgPot>
369 return from_rgpot_caller(
370 [&
pot](
const rgpot::types::AtomMatrix& positions,
371 const std::vector<int>& atmtypes,
372 const std::array<std::array<double, 3>, 3>& cell) {
373 return pot(positions, atmtypes, cell);
383 template <
typename RgPot>
387 throw std::invalid_argument(
388 "PotentialWrapper: rgpot shared pointer must not be null");
390 auto wrapper = from_rgpot_caller(
391 [
pot](
const rgpot::types::AtomMatrix& positions,
392 const std::vector<int>& atmtypes,
393 const std::array<std::array<double, 3>, 3>& cell) {
394 return (*
pot)(positions, atmtypes, cell);
402 if constexpr (
requires(RgPot& p, rgpot::ForceBatch& b) {
405 using Result = std::invoke_result_t<
406 RgPot&,
const rgpot::types::AtomMatrix&,
407 const std::vector<int>&,
408 const std::array<std::array<double, 3>, 3>&>;
409 constexpr auto arity = std::tuple_size<std::decay_t<Result>>::value;
411 [
pot = std::move(
pot)](
long N, std::size_t B,
const double* R,
412 const int* atomicNrs,
double* F,
413 double* U,
double* energy_noise,
414 double* force_noise,
const double* box) {
419 throw std::invalid_argument(
420 "PotentialWrapper: n_atoms must be positive");
426 const std::size_t stride =
static_cast<std::size_t
>(N) * 3;
427 std::vector<double> scratch_forces(B * stride);
428 std::vector<rgpot::ForceInput> in;
429 std::vector<rgpot::ForceOut>
out;
432 for (std::size_t b = 0; b < B; ++b) {
434 rgpot::ForceInput{
static_cast<std::size_t
>(N),
435 R + b * 3 * N, atomicNrs, box});
437 out.back().F = scratch_forces.data() + b * stride;
439 rgpot::ForceBatch batch{B, in.data(),
out.data()};
440 pot->forceBatch(batch);
441 std::vector<double> energy_rms;
442 if (energy_noise !=
nullptr) {
443 energy_rms.resize(B);
444 for (std::size_t b = 0; b < B; ++b) {
445 if constexpr (arity >= 3)
453 std::copy_n(scratch_forces.data(),
454 scratch_forces.size(), F);
459 for (std::size_t b = 0; b < B; ++b)
460 U[b] =
out[b].energy;
462 if (energy_noise !=
nullptr)
463 std::copy_n(energy_rms.data(), B, energy_noise);
464 if (force_noise !=
nullptr)
465 std::fill_n(force_noise, B, 0.0);
467 if constexpr (arity <= 3) {
470 wrapper.setForceBatchWithUncertainty(std::move(native_batch));
473 wrapper.setForceBatch([native_batch = std::move(native_batch)](
474 long N, std::size_t B,
475 const double* R,
const int* atomicNrs,
476 double* F,
double* U,
478 native_batch(N, B, R, atomicNrs, F, U,
nullptr,
nullptr,
488 wrapper.m_force_batch = [
pot = std::move(
pot)](
489 long N, std::size_t B,
const double* R,
490 const int* atomicNrs,
double* F,
491 double* U,
const double* box) {
496 throw std::invalid_argument(
497 "PotentialWrapper: n_atoms must be positive");
499 const auto n =
static_cast<std::size_t
>(N);
500 std::array<std::array<double, 3>, 3> cell{};
501 if (box !=
nullptr) {
502 for (std::size_t r = 0; r < 3; ++r)
503 for (std::size_t c = 0; c < 3; ++c)
504 cell[r][c] = box[r * 3 + c];
506 const std::vector<int> types(atomicNrs, atomicNrs + n);
509 const std::size_t stride = 3 * n;
510 std::vector<double> scratch_energies(B);
511 std::vector<double> scratch_forces(B * stride);
512 for (std::size_t b = 0; b < B; ++b) {
513 rgpot::types::AtomMatrix pos(n, 3);
514 for (std::size_t a = 0; a < n; ++a)
515 for (std::size_t d = 0; d < 3; ++d)
516 pos(a, d) = R[b * 3 * n + a * 3 + d];
521 const auto result = (*pot)(pos, types, cell);
522 scratch_energies[b] = std::get<0>(result);
523 const auto& forces = std::get<1>(result);
524 if (stride == 0)
continue;
525 for (std::size_t a = 0; a < n; ++a)
526 for (std::size_t d = 0; d < 3; ++d)
527 scratch_forces[b * stride + a * 3 + d] =
534 std::copy_n(scratch_energies.data(), B, U);
535 if (F !=
nullptr && stride > 0)
536 std::copy_n(scratch_forces.data(),
537 scratch_forces.size(), F);
554 std::shared_ptr<rgpot::PotentialBase>
pot);
560 template <
typename T>
561 struct dependent_false : std::false_type { };
563 template <
typename Result>
564 static std::tuple<double, rgpot::types::AtomMatrix, double, double>
565 normalize_rgpot_result(Result&& result)
567 using Decayed = std::decay_t<Result>;
568 constexpr std::size_t arity = std::tuple_size<Decayed>::value;
569 if constexpr (arity == 2) {
570 auto [energy, forces] = std::forward<Result>(result);
571 return {energy, std::move(forces), 0.0, 0.0};
572 }
else if constexpr (arity == 3) {
573 auto [energy, forces, energy_variance] =
574 std::forward<Result>(result);
575 return {energy, std::move(forces), energy_variance, 0.0};
576 }
else if constexpr (arity == 4) {
577 auto [energy, forces, energy_variance, force_noise_rms] =
578 std::forward<Result>(result);
579 return {energy, std::move(forces), energy_variance,
582 static_assert(dependent_false<Decayed>::value,
583 "rgpot potential must return (energy, forces) or "
584 "(energy, forces, energy_variance) or "
585 "(energy, forces, energy_variance, "
590 template <
typename Caller>
594 [caller = std::move(caller)](
595 long N,
const double* R,
const int* atomicNrs,
double* F,
596 double* U,
double* energy_noise_rms,
double* force_noise_rms,
597 const double* box)
mutable {
599 rgpot::types::AtomMatrix positions(
static_cast<size_t>(N), 3);
600 for (
long i = 0; i < N; ++i) {
601 positions(
static_cast<size_t>(i), 0) = R[3 * i];
602 positions(
static_cast<size_t>(i), 1) = R[3 * i + 1];
603 positions(
static_cast<size_t>(i), 2) = R[3 * i + 2];
607 std::vector<int> atmtypes(atomicNrs, atomicNrs + N);
610 std::array<std::array<double, 3>, 3> cell;
611 for (
int r = 0; r < 3; ++r) {
612 for (
int c = 0; c < 3; ++c) {
613 cell[
static_cast<size_t>(r)][
static_cast<size_t>(c)] =
619 auto [energy, forces, pot_energy_variance,
620 pot_force_noise_rms] =
621 normalize_rgpot_result(caller(positions, atmtypes, cell));
626 const bool publish_energy_noise = energy_noise_rms !=
nullptr;
627 const double energy_rms =
633 if (force_noise_rms !=
nullptr &&
634 (!std::isfinite(pot_force_noise_rms) ||
635 pot_force_noise_rms < 0.0))
636 throw std::domain_error(
637 "PotentialWrapper: rgpot force noise RMS must be "
638 "finite and non-negative");
644 for (
long i = 0; i < N; ++i) {
645 F[3 * i] = forces(
static_cast<size_t>(i), 0);
646 F[3 * i + 1] = forces(
static_cast<size_t>(i), 1);
647 F[3 * i + 2] = forces(
static_cast<size_t>(i), 2);
650 if (publish_energy_noise) *energy_noise_rms = energy_rms;
651 if (force_noise_rms) *force_noise_rms = pot_force_noise_rms;
656 : m_force_with_uncertainty(std::move(fn))
658 if (!m_force_with_uncertainty) {
659 throw std::invalid_argument(
660 "PotentialWrapper: force function must not be null");
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)
std::function< void( long N, std::size_t B, const double *R, const int *atomicNrs, double *F, double *U, double *energy_noise_rms, double *force_noise_rms, const double *box)> ForceBatchWithUncertaintyFn
Both optional noise arrays use the single-call RMS conventions.
void setForceBatch(ForceBatchWithForceNoiseFn fn)
static PotentialWrapper composeGeometryAndEnergyUncertainty(PotentialWrapper geometry, PotentialWrapper uncertainty)
Compose geometry output with an independent energy-uncertainty source.
std::function< void( long N, std::size_t B, const double *R, const int *atomicNrs, double *F, double *U, const double *box)> ForceBatchFn
void forceBatch(long N, std::size_t B, const double *R, const int *atomicNrs, double *F, double *U, double *V, const double *box)
Batched evaluation with optional force noise.
std::function< void( long N, const double *R, const int *atomicNrs, double *F, double *U, double *energy_noise_rms, double *force_noise_rms, const double *box)> ForceWithUncertaintyFn
PotentialWrapper(ForceFn fn)
void forceBatch(long N, std::size_t B, const double *R, const int *atomicNrs, double *F, double *U, const double *box)
Batched means; requires hasForceBatch().
std::function< void( long N, std::size_t B, const double *R, const int *atomicNrs, double *F, double *U, double *V, const double *box)> ForceBatchWithForceNoiseFn
A batch with force noise and zero energy noise.
bool hasForceBatchWithUncertainty() const noexcept
std::function< void( long N, const double *R, const int *atomicNrs, double *F, double *U, double *energy_noise_rms, const double *box)> ForceFn
static PotentialWrapper from_force_with_uncertainty(ForceWithUncertaintyFn fn)
static PotentialWrapper routeEnergyUncertaintyToForceChannel(PotentialWrapper source)
Route a legacy scalar uncertainty into the force-noise channel.
bool hasForceBatch() 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)
void setForceBatchWithUncertainty(ForceBatchWithUncertaintyFn fn)
static PotentialWrapper from_rgpot(std::shared_ptr< RgPot > pot)
Factory: adapt and own an rgpot PotentialBase subclass.
static PotentialWrapper from_rgpot(RgPot &pot)
Factory: adapt an rgpot PotentialBase subclass.
void setForceBatch(ForceBatchFn fn)
void setForceBatch(std::nullptr_t)
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *energy_noise_rms, const double *box)
Invoke the wrapped potential.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Potential wrappers and the batched oracle interface.
void resetOracleEvaluations() noexcept
long oracleEvaluations() noexcept
bool energyVarianceAccepted(const double variance) noexcept
double energyVarianceToRms(const double variance)
constexpr double kEnergyVarianceNegativeRtol
void pushOracleEvaluations(long evaluations) noexcept
void popOracleEvaluations() noexcept
OracleEvaluationScope & operator=(const OracleEvaluationScope &)=delete
OracleEvaluationScope(long evaluations)
OracleEvaluationScope(const OracleEvaluationScope &)=delete