Loading...
Searching...
No Matches
pot::GprMeanPotential Class Reference

#include <gpr/potentials/GprMeanPotential.h>

Inheritance diagram for pot::GprMeanPotential:

Public Member Functions

 GprMeanPotential (gpr::GaussianProcessRegression &model)
 GprMeanPotential (std::shared_ptr< gpr::GaussianProcessRegression > model)
 GprMeanPotential (gpr::neb::ActiveLearningSession &session)
void resetModel (std::shared_ptr< gpr::GaussianProcessRegression > model)
std::uint64_t surfaceEpoch () const noexcept
std::uint64_t paramsKey () const noexcept override
rgpot::PotCaps caps () const noexcept override
void forceImpl (const rgpot::ForceInput &in, rgpot::ForceOut *out) const override
void hessianVectorImpl (const rgpot::ForceInput &in, const double *direction, double *action) const
void force (long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box)
void forceBatch (long N, std::size_t B, const double *R, const int *atomicNrs, double *F, double *U, const double *box)
PotentialWrapper asWrapper ()

Detailed Description

Frozen-or-live GP surface as a PotentialWrapper.

Mean energy and gradient come from the cached factorization. Energy variance uses tryGateEnergyVariance (cheap Pmat) and falls back to calculateVariance. surfaceEpoch() is epoch_base_ plus the factor delta (factorEpoch() - base_factor_epoch_), or the session surface epoch when this surface follows one, so a host cache key cannot reuse a stale PES.

Definition at line 31 of file GprMeanPotential.h.

Constructor & Destructor Documentation

◆ GprMeanPotential() [1/3]

pot::GprMeanPotential::GprMeanPotential ( gpr::GaussianProcessRegression & model)
explicit

Definition at line 20 of file GprMeanPotential.cpp.

21 : rgpot::Potential<GprMeanPotential>(rgpot::PotType::UNKNOWN),
22 model_(&model)
23{
24}

◆ GprMeanPotential() [2/3]

pot::GprMeanPotential::GprMeanPotential ( std::shared_ptr< gpr::GaussianProcessRegression > model)
explicit

Definition at line 26 of file GprMeanPotential.cpp.

28 : rgpot::Potential<GprMeanPotential>(rgpot::PotType::UNKNOWN)
29{
30 resetModel(std::move(model));
31}
void resetModel(std::shared_ptr< gpr::GaussianProcessRegression > model)

◆ GprMeanPotential() [3/3]

pot::GprMeanPotential::GprMeanPotential ( gpr::neb::ActiveLearningSession & session)
explicit

Definition at line 33 of file GprMeanPotential.cpp.

34 : rgpot::Potential<GprMeanPotential>(rgpot::PotType::UNKNOWN),
35 model_(&session.gp),
36 session_(&session)
37{
38}
gpr::GaussianProcessRegression gp
Definition Session.h:471

Member Function Documentation

◆ asWrapper()

PotentialWrapper pot::GprMeanPotential::asWrapper ( )
nodiscard

Definition at line 264 of file GprMeanPotential.cpp.

265{
266 GprMeanPotential* self = this;
267 auto wrapper = PotentialWrapper(
268 [self](long N, const double* R, const int* atomicNrs, double* F,
269 double* U, double* energy_noise_rms, const double* box) {
270 // force() stores energy and forces as it goes. Hold them until
271 // the variance converts, so a throw leaves the caller buffers
272 // untouched. N <= 0 or a null output still hits force()'s own
273 // rejection, which writes nothing.
274 const bool scratch = N > 0 && F != nullptr && U != nullptr;
275 const std::size_t n_coord =
276 scratch ? static_cast<std::size_t>(N) * 3 : 0;
277 std::vector<double> scratch_forces(n_coord);
278 double scratch_energy = 0.0;
279 double variance = 0.0;
280 self->force(N, R, atomicNrs, scratch ? scratch_forces.data() : F,
281 scratch ? &scratch_energy : U,
282 energy_noise_rms != nullptr ? &variance : nullptr, box);
283 const double energy_rms = energy_noise_rms != nullptr
284 ? energyVarianceToRms(variance)
285 : 0.0;
286 if (scratch) {
287 std::copy_n(scratch_forces.data(), n_coord, F);
288 *U = scratch_energy;
289 }
290 if (energy_noise_rms != nullptr) *energy_noise_rms = energy_rms;
291 });
292 wrapper.setForceBatch([self](long N, std::size_t B, const double* R,
293 const int* atomicNrs, double* F, double* U,
294 const double* box) {
295 // The batch predicts means only; the single call carries the
296 // gate variance.
297 self->forceBatch(N, B, R, atomicNrs, F, U, box);
298 });
299 return wrapper;
300}
GprMeanPotential(gpr::GaussianProcessRegression &model)
double variance(const Weights &w, const Matrix &covariance)
double energyVarianceToRms(const double variance)

◆ caps()

rgpot::PotCaps pot::GprMeanPotential::caps ( ) const
nodiscardoverridenoexcept

Definition at line 83 of file GprMeanPotential.cpp.

84{
85 return {.reentrancy = rgpot::Reentrancy::ProcessSerial, .periodic = true};
86}

◆ force()

void pot::GprMeanPotential::force ( long N,
const double * R,
const int * atomicNrs,
double * F,
double * U,
double * variance,
const double * box )

Definition at line 199 of file GprMeanPotential.cpp.

202{
203 if (N <= 0 || R == nullptr || F == nullptr || U == nullptr)
204 throw std::invalid_argument("GprMeanPotential::force: null input");
205 validateSystem(N, atomicNrs, box, "force");
206 gpr::Observation query;
207 query.R.resize(1, 3 * N);
208 for (long i = 0; i < 3 * N; ++i)
209 query.R(0, i) = R[i];
210 if (session_ != nullptr)
211 session_->predictAtImage(-1, query);
212 else
213 model().calculatePotentialDispatched(query);
214 *U = (query.E.size() > 0) ? query.E(0, 0) : 0.0;
215 if (query.G.rows() > 0 && query.G.cols() == 3 * N) {
216 for (long i = 0; i < 3 * N; ++i)
217 F[i] = -query.G(0, i);
218 } else {
219 for (long i = 0; i < 3 * N; ++i)
220 F[i] = 0.0;
221 }
222 if (variance != nullptr) {
223 Eigen::RowVectorXd gate;
224 if (model().tryGateEnergyVariance(query, gate) && gate.size() > 0)
225 *variance = gate(0);
226 else {
227 model().calculateVarianceDispatched(query);
228 *variance = (query.E.size() > 0) ? query.E(0, 0) : 0.0;
229 }
230 }
231}
FieldMatrixd E

◆ forceBatch()

void pot::GprMeanPotential::forceBatch ( long N,
std::size_t B,
const double * R,
const int * atomicNrs,
double * F,
double * U,
const double * box )

Definition at line 233 of file GprMeanPotential.cpp.

236{
237 if (N <= 0 || B == 0 || R == nullptr || F == nullptr || U == nullptr)
238 throw std::invalid_argument("GprMeanPotential::forceBatch: null input");
239 validateSystem(N, atomicNrs, box, "forceBatch");
240 std::vector<gpr::Observation> queries(B);
241 std::vector<gpr::Observation*> ptrs(B, nullptr);
242 for (std::size_t b = 0; b < B; ++b) {
243 queries[b].R.resize(1, 3 * N);
244 for (long i = 0; i < 3 * N; ++i)
245 queries[b].R(0, i) = R[b * static_cast<std::size_t>(3 * N) +
246 static_cast<std::size_t>(i)];
247 ptrs[b] = &queries[b];
248 }
249 model().calculatePotentialBatch(ptrs);
250 for (std::size_t b = 0; b < B; ++b) {
251 U[b] = (queries[b].E.size() > 0) ? queries[b].E(0, 0) : 0.0;
252 if (queries[b].G.rows() > 0 && queries[b].G.cols() == 3 * N) {
253 for (long i = 0; i < 3 * N; ++i)
254 F[b * static_cast<std::size_t>(3 * N) +
255 static_cast<std::size_t>(i)] = -queries[b].G(0, i);
256 } else {
257 for (long i = 0; i < 3 * N; ++i)
258 F[b * static_cast<std::size_t>(3 * N) +
259 static_cast<std::size_t>(i)] = 0.0;
260 }
261 }
262}

◆ forceImpl()

void pot::GprMeanPotential::forceImpl ( const rgpot::ForceInput & in,
rgpot::ForceOut * out ) const
override

Definition at line 88 of file GprMeanPotential.cpp.

90{
91 if (in.nAtoms == 0 || in.pos == nullptr || out == nullptr ||
92 out->F == nullptr)
93 throw std::invalid_argument(
94 "GprMeanPotential::forceImpl: null input or output");
95 const_cast<GprMeanPotential*>(this)->force(
96 static_cast<long>(in.nAtoms), in.pos, in.atmnrs, out->F, &out->energy,
97 &out->variance, in.box);
98}
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out

◆ hessianVectorImpl()

void pot::GprMeanPotential::hessianVectorImpl ( const rgpot::ForceInput & in,
const double * direction,
double * action ) const

Definition at line 100 of file GprMeanPotential.cpp.

103{
104 if (in.nAtoms == 0 || in.pos == nullptr || direction == nullptr ||
105 action == nullptr)
106 throw std::invalid_argument(
107 "GprMeanPotential::hessianVectorImpl: null input");
108 validateSystem(static_cast<long>(in.nAtoms), in.atmnrs, in.box,
109 "hessianVectorImpl");
110 const auto n = static_cast<Eigen::Index>(3 * in.nAtoms);
111 gpr::Coord positions(1, n);
112 for (Eigen::Index i = 0; i < n; ++i)
113 positions(0, i) = in.pos[i];
114 const Eigen::Map<const Eigen::VectorXd> vector(direction, n);
115 // The prepared operator owns an exact refusal. The dispatched product
116 // would replace that refusal with a finite-difference stencil. Energy
117 // and gradient acquire the global model first; the action does the
118 // same, so a parked or stale factor is not the answer.
119 std::optional<gpr::neb::ActiveLearningSession::ModelLease> lease;
120 auto& posterior = [this, &lease]() -> gpr::GaussianProcessRegression& {
121 if (session_ == nullptr)
122 return const_cast<GprMeanPotential*>(this)->model();
123 lease.emplace(session_->acquireGlobalModel());
124 return lease->gp();
125 }();
126 const auto apply = posterior.prepareMeanHessian(positions);
127 Eigen::VectorXd result;
128 if (!apply || !apply(vector, result) || result.size() != n)
129 throw std::runtime_error("GP Hessian action is unavailable");
130 std::copy_n(result.data(), n, action);
131}
FieldMatrixd Coord
Definition Macros.h:35

◆ paramsKey()

std::uint64_t pot::GprMeanPotential::paramsKey ( ) const
nodiscardoverridenoexcept

Definition at line 78 of file GprMeanPotential.cpp.

79{
80 return surfaceEpoch();
81}
std::uint64_t surfaceEpoch() const noexcept

◆ resetModel()

void pot::GprMeanPotential::resetModel ( std::shared_ptr< gpr::GaussianProcessRegression > model)

Definition at line 40 of file GprMeanPotential.cpp.

42{
43 if (!model)
44 throw std::invalid_argument("GprMeanPotential::resetModel: null model");
45 const bool had_source = model_ != nullptr || session_ != nullptr;
46 const std::uint64_t previous = surfaceEpoch();
47 owned_model_ = std::move(model);
48 model_ = owned_model_.get();
49 session_ = nullptr;
50 base_factor_epoch_ = model_->factorEpoch();
51 epoch_base_ = had_source ? (previous + 1)
52 : std::max<std::uint64_t>(1, base_factor_epoch_);
53}

◆ surfaceEpoch()

std::uint64_t pot::GprMeanPotential::surfaceEpoch ( ) const
nodiscardnoexcept

Definition at line 67 of file GprMeanPotential.cpp.

68{
69 if (session_ != nullptr) return session_->surfaceEpoch();
70 if (model_ != nullptr) {
71 const std::uint64_t factor = model_->factorEpoch();
72 const std::uint64_t delta = factor - base_factor_epoch_;
73 return epoch_base_ + delta;
74 }
75 return 0;
76}

The documentation for this class was generated from the following files: