Loading...
Searching...
No Matches
PairSplineModel.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_NEB_PAIRSPLINEMODEL_H_
6#define GPR_NEB_PAIRSPLINEMODEL_H_
7
8#include <Eigen/Dense>
9#include <array>
10#include <vector>
11
13
14namespace gpr {
15namespace neb {
16
32public:
33 PairSplineModel(Eigen::Index n_moving, Eigen::VectorXd frozen_positions,
34 const std::array<double, 9>& cell, double r_min,
35 double r_cut, double spacing);
36
37 Eigen::Index nMoving() const noexcept
38 {
39 return n_moving_;
40 }
41 Eigen::Index nBasis() const noexcept
42 {
43 return n_basis_;
44 }
45 double cutoff() const noexcept
46 {
47 return r_cut_;
48 }
49 bool ready() const noexcept
50 {
51 return ready_;
52 }
53 int observations() const noexcept
54 {
55 return n_obs_;
56 }
57
59 void setNoise(double energy_sigma, double gradient_sigma);
62 void setPrior(double weight_sigma, double curvature_sigma);
67 void setTailSplit(double split, double refresh);
69 void staleTail(int slot);
70
72 void observe(const Eigen::VectorXd& x, double energy,
73 const Eigen::VectorXd& gradient, int slot = 0);
75 bool fit();
78 double gradientResidual() const noexcept
79 {
80 return gradient_residual_;
81 }
82
86 double energy(const Eigen::VectorXd& x, Eigen::VectorXd* gradient,
87 int slot = 0) const;
92 double partialEnergy(const Eigen::VectorXd& x,
93 const std::vector<char>& atom_mask,
94 Eigen::VectorXd* gradient, int slot = 0) const;
98 double maskedEnergy(const Eigen::VectorXd& x,
99 const std::vector<char>& atom_mask,
100 Eigen::VectorXd* gradient, int slot = 0) const;
102 Eigen::MatrixXd hessian(const Eigen::VectorXd& x,
103 const std::vector<Eigen::Index>& dofs,
104 int slot = 0) const;
105
107 void radial(double r, double& phi, double& dphi, double& d2phi) const;
109 double minimumSeparation(const Eigen::VectorXd& x) const;
110
111private:
112 struct Pair {
113 int i; // moving atom
114 int j; // moving atom index, or frozen index when frozen
115 bool frozen; // j indexes the frozen atoms
116 Eigen::Vector3d shift; // lattice shift so that x_i - x_j + shift is
117 // the minimum-image separation at list build
118 };
119 struct List {
120 std::vector<Pair> pairs; // every pair within the cutoff plus skin
121 // The same pairs as flat arrays for the sum: coordinate offsets into
122 // the moving-then-frozen buffer and the lattice shift. Pairs that can
123 // enter the split radius come first; the n_near count bounds them.
124 std::vector<int> oi, oj;
125 std::vector<double> sx, sy, sz;
126 std::size_t n_near = 0;
127 // Pairs that touch an atom of masked_atoms, for maskedEnergy().
128 std::vector<int> masked;
129 std::vector<char> masked_atoms;
130 Eigen::VectorXd positions;
131 // Tail contributions evaluated at tail_positions with the weights of
132 // tail_fit; reused while no atom moved past the tail refresh.
133 Eigen::VectorXd tail_positions;
134 Eigen::VectorXd tail_gradient;
135 double tail_energy = 0.0;
136 long tail_fit = -1;
137 };
138 List& ensureList(const Eigen::VectorXd& x, int slot) const;
139 void rebuildList(const Eigen::VectorXd& x, List& list) const;
142 void sumRange(List& list, std::size_t begin, std::size_t end, double& e,
143 double* gp) const;
145 void sumIndexed(const List& list, const std::vector<int>& index, double& e,
146 double* gp) const;
148 void loadCoordinates(const Eigen::VectorXd& x) const;
149 bool separation(const Pair& p, const Eigen::VectorXd& x, Eigen::Vector3d& d,
150 double& r) const;
151 void basis(double r, int& first, double* b, double* db, double* d2b) const;
152 void features(const Eigen::VectorXd& x, Eigen::VectorXd& phi,
153 Eigen::MatrixXd& dphi, int slot) const;
154
155 Eigen::Index n_moving_;
156 Eigen::VectorXd frozen_;
158 std::array<double, 9> cell_{};
159 double r_min_;
160 double r_cut_;
161 double h_;
162 Eigen::Index n_basis_;
163 double skin_ = 1.0;
164 double split_ = 0.0;
165 double tail_refresh_ = 0.0;
166 long fit_count_ = 0;
167 mutable std::vector<List> lists_;
168 // Moving coordinates followed by the frozen ones, so a pair's partner
169 // is one offset whichever kind it is.
170 mutable std::vector<double> xall_;
171 mutable int list_builds_ = 0;
172 mutable double list_seconds_ = 0.0;
173 // 0 unknown, 1 cell list, 2 direct pairs. Chosen on the first periodic
174 // rebuild: the cell list only when it returns the same pairs and the
175 // same minimum-image shifts, and its neighbourhood is smaller than
176 // half the cell. The direct list otherwise.
177 mutable int list_method_ = 0;
178
179public:
180 int listBuilds() const noexcept
181 {
182 return list_builds_;
183 }
184 double listSeconds() const noexcept
185 {
186 return list_seconds_;
187 }
188
189private:
190 double noise_energy_ = 1e-3;
191 double noise_gradient_ = 1e-3;
192 double prior_weight_ = 100.0;
193 double prior_curvature_ = 0.5;
194 // Unweighted normal equations of the energy rows (constant first) and
195 // of the gradient rows (spline weights only).
196 Eigen::MatrixXd ete_;
197 Eigen::VectorXd ety_;
198 Eigen::MatrixXd gtg_;
199 Eigen::VectorXd gty_;
200 Eigen::VectorXd weights_; // constant first, then the spline weights
201 // Piecewise cubic of the fitted radial function per knot interval, in
202 // the fractional knot coordinate, for the evaluation paths.
203 std::vector<std::array<double, 4>> table_;
204 void tabulate();
205 inline bool lookup(double r, double& phi, double& dphi,
206 double& d2phi) const;
207 int n_obs_ = 0;
208 bool ready_ = false;
209 double gradient_residual_ = 0.0;
210 double sum_g2_ = 0.0;
211 Eigen::Index n_gradient_rows_ = 0;
212};
213
214} // namespace neb
215} // namespace gpr
216
217#endif // GPR_NEB_PAIRSPLINEMODEL_H_
@ x
Definition Enums.h:28
PairSplineModel(Eigen::Index n_moving, Eigen::VectorXd frozen_positions, const std::array< double, 9 > &cell, double r_min, double r_cut, double spacing)
void setPrior(double weight_sigma, double curvature_sigma)
void setNoise(double energy_sigma, double gradient_sigma)
Noise standard deviations of the energy and gradient rows.
double energy(const Eigen::VectorXd &x, Eigen::VectorXd *gradient, int slot=0) const
double gradientResidual() const noexcept
int listBuilds() const noexcept
Eigen::Index nMoving() const noexcept
bool fit()
Solve for the weights. Returns false without enough rows.
void observe(const Eigen::VectorXd &x, double energy, const Eigen::VectorXd &gradient, int slot=0)
Accumulate one measurement into the normal equations.
int observations() const noexcept
void radial(double r, double &phi, double &dphi, double &d2phi) const
Radial function and its first two derivatives at r.
void setTailSplit(double split, double refresh)
bool ready() const noexcept
Eigen::Index nBasis() const noexcept
double minimumSeparation(const Eigen::VectorXd &x) const
Smallest moving-moving or moving-frozen separation at x.
double listSeconds() const noexcept
Eigen::MatrixXd hessian(const Eigen::VectorXd &x, const std::vector< Eigen::Index > &dofs, int slot=0) const
Hessian block over the listed coordinates at x.
void staleTail(int slot)
Marks the slot's tail sum for recomputation at the next evaluation.
double cutoff() const noexcept
double maskedEnergy(const Eigen::VectorXd &x, const std::vector< char > &atom_mask, Eigen::VectorXd *gradient, int slot=0) const
double partialEnergy(const Eigen::VectorXd &x, const std::vector< char > &atom_mask, Eigen::VectorXd *gradient, int slot=0) const
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
A simple pair with identical datatypes.
Definition Structures.h:44