Loading...
Searching...
No Matches
ZBLPriorMean.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// ZBL screened-Coulomb prior mean for residual Gaussian-process targets.
6// The constants are the standard universal screening coefficients used by
7// Ziegler, Biersack and Littmark. For a recent comparison of repulsive
8// interatomic potentials, see Nordlund, Lehtola and Hobler,
9// DOI 10.1103/physreva.111.032818. The residual-learning pattern follows
10// delta-ML, DOI 10.1021/acs.jctc.5b00099, and physically inspired GP priors,
11// DOI 10.1021/acs.jctc.2c00546.
12//
13// The pair term is multiplied by a C^2 quintic switch that is exactly zero
14// for r >= 0.75 * (R_i + R_j) (Cordero single-bond covalent radii) and one
15// for r <= 0.55 * (R_i + R_j). Bare ZBL carries eV-scale energy at bonded
16// distances (2.9 eV for H-C at 1.1 A, 3.4 eV for C-C at 1.5 A), which
17// roughens the residual targets across the whole bonding manifold and
18// biases saddles at extrapolation; triple bonds sit near 0.78 of the
19// single-bond covalent sum, so the 0.75 cutoff keeps the mean inert on all
20// real bonding and active only in the Pauli-wall compression region. The
21// switched-baseline construction matches common ML-potential ZBL envelopes.
22
23#ifndef GPR_PRIOR_ZBL_PRIOR_MEAN_H_
24#define GPR_PRIOR_ZBL_PRIOR_MEAN_H_
25
26#include <Eigen/Core>
27#include <array>
28#include <cmath>
29#include <cstddef>
30#include <limits>
31#include <type_traits>
32#include <vector>
33
36#include "PriorMeanHessian.h"
37#include "PriorPairFrame.h"
38
39namespace gpr {
40namespace priors {
41
43 double energy = 0.0;
44 Eigen::RowVectorXd gradient;
45};
46
48public:
50
51 explicit ZBLPriorMean(const AtomsConfiguration& atoms_config)
52 : frame_(atoms_config)
53 {
54 active_ = hasActivePair();
55 }
56
57 [[nodiscard]] bool active() const noexcept
58 {
59 return active_;
60 }
61
63 [[nodiscard]] static double covalentRadius(int Z) noexcept
64 {
65 if (Z < 1 || Z > kLastCorderoElement) return kDefaultCovalentRadius;
66 return geometry::kCorderoRadiusPm[static_cast<std::size_t>(Z)] / 100.0;
67 }
68
75 [[nodiscard]] static double repulsionOnsetRadius(int Zi, int Zj) noexcept
76 {
77 if (Zi <= 0 || Zj <= 0) return 0.0;
78 return kSwitchOffFactor * (covalentRadius(Zi) + covalentRadius(Zj));
79 }
80
81 template <typename Derived>
82 [[nodiscard]] double energy(
83 const Eigen::MatrixBase<Derived>& positions) const
84 {
85 if (!dimensionMatches(positions.size())) return 0.0;
86 double out = 0.0;
87 visitPairs(
88 positions, [&](double pair_energy) { out += pair_energy; },
89 [](Eigen::Index, double) {});
90 return out;
91 }
92
93 template <typename Derived>
95 const Eigen::MatrixBase<Derived>& positions) const
96 {
98 out.gradient = Eigen::RowVectorXd::Zero(positions.size());
99 if (!dimensionMatches(positions.size())) return out;
100 visitPairs(
101 positions, [&](double pair_energy) { out.energy += pair_energy; },
102 [&](Eigen::Index component, double value) {
103 out.gradient(component) += value;
104 });
105 return out;
106 }
107
109 template <typename Derived>
110 [[nodiscard]] std::vector<HessianPair> hessianPairs(
111 const Eigen::MatrixBase<Derived>& positions) const
112 {
113 std::vector<HessianPair> pairs;
114 if (!dimensionMatches(positions.size())) return pairs;
115 visitPairs(
116 positions, [](double) {}, [](Eigen::Index, double) {},
117 [&](Eigen::Index first, Eigen::Index second,
118 const Eigen::Matrix3d& block) {
119 pairs.push_back({first, second, block});
120 });
121 return pairs;
122 }
123
125 template <typename Derived>
126 [[nodiscard]] static Eigen::MatrixXd applyHessianPairs(
127 const std::vector<HessianPair>& pairs,
128 const Eigen::MatrixBase<Derived>& directions)
129 {
130 return applyPriorHessianPairs(pairs, directions);
131 }
132
133 void subtractFromTargets(const Coord& positions, Eigen::Index energy_count,
134 Eigen::VectorXd& targets) const
135 {
136 if (!active_ || positions.rows() != energy_count ||
137 !dimensionMatches(positions.cols())) {
138 return;
139 }
140 const Eigen::Index required =
141 energy_count + positions.rows() * positions.cols();
142 if (targets.size() < required) return;
143
144 for (Eigen::Index row = 0; row < positions.rows(); ++row) {
145 visitPairs(
146 positions.row(row),
147 [&](double pair_energy) { targets(row) -= pair_energy; },
148 [&](Eigen::Index component, double value) {
149 const Eigen::Index target_index =
150 energy_count + row + component * positions.rows();
151 targets(target_index) -= value;
152 });
153 }
154 }
155
156 void addToObservation(Observation& observation) const
157 {
158 if (!active_ || observation.E.rows() != observation.R.rows() ||
159 observation.G.rows() != observation.R.rows() ||
160 observation.G.cols() != observation.R.cols() ||
161 observation.E.cols() < 1 ||
162 !dimensionMatches(observation.R.cols())) {
163 return;
164 }
165
166 for (Eigen::Index row = 0; row < observation.R.rows(); ++row) {
167 visitPairs(
168 observation.R.row(row),
169 [&](double pair_energy) {
170 observation.E(row, 0) += pair_energy;
171 },
172 [&](Eigen::Index component, double value) {
173 observation.G(row, component) += value;
174 });
175 }
176 }
177
178private:
179 static constexpr double kCoulombEvAngstrom = 14.3996454784255;
180 static constexpr double kBohrAngstrom = 0.529177210903;
181 static constexpr double kScreeningLengthPrefactor = 0.88534;
182 static constexpr double kZExponent = 0.23;
183 static constexpr std::array<double, 4> kScreeningWeights = {
184 0.1818, 0.5099, 0.2802, 0.02817};
185 static constexpr std::array<double, 4> kScreeningExponents = {
186 3.2, 0.9423, 0.4029, 0.2016};
187 // Quintic switch bounds as fractions of the pair covalent-radii sum.
188 static constexpr double kSwitchOnFactor = 0.55;
189 static constexpr double kSwitchOffFactor = 0.75;
190 static constexpr int kLastCorderoElement = 36;
191 static constexpr double kDefaultCovalentRadius = 1.50;
192
193 [[nodiscard]] bool hasActivePair() const noexcept
194 {
195 return frame_.hasPair();
196 }
197
198 [[nodiscard]] bool dimensionMatches(Eigen::Index size) const noexcept
199 {
200 return active_ && size == frame_.coordinates();
201 }
202
203 [[nodiscard]] static double screeningLength(int Zi, int Zj) noexcept
204 {
205 return kScreeningLengthPrefactor * kBohrAngstrom /
206 (std::pow(static_cast<double>(Zi), kZExponent) +
207 std::pow(static_cast<double>(Zj), kZExponent));
208 }
209
210 template <typename Positions, typename EnergyConsumer,
211 typename GradientConsumer,
212 typename HessianConsumer = std::nullptr_t>
213 void visitPairs(const Positions& positions,
214 EnergyConsumer&& energy_consumer,
215 GradientConsumer&& gradient_consumer,
216 HessianConsumer&& hessian_consumer = nullptr) const
217 {
218 constexpr bool need_hessian =
219 !std::is_same_v<std::decay_t<HessianConsumer>, std::nullptr_t>;
220 // jx < 0 is a fixed partner: the pair moves atom ix alone.
221 frame_.forEachPair(positions, [&](Eigen::Index ix, Eigen::Index jx,
222 int Zi, int Zj,
223 const Eigen::Vector3d& delta) {
224 const double dx = delta.x();
225 const double dy = delta.y();
226 const double dz = delta.z();
227 const double r2 = dx * dx + dy * dy + dz * dz;
228 if (!(r2 > 0.0)) {
229 energy_consumer(std::numeric_limits<double>::infinity());
230 if constexpr (need_hessian)
231 hessian_consumer(
232 ix, jx,
233 Eigen::Matrix3d::Constant(
234 std::numeric_limits<double>::quiet_NaN()));
235 return;
236 }
237 const double r = std::sqrt(r2);
238 const double r_cov = covalentRadius(Zi) + covalentRadius(Zj);
239 const double r_off = kSwitchOffFactor * r_cov;
240 if (r >= r_off) return;
241 const double r_on = kSwitchOnFactor * r_cov;
242 double sw = 1.0;
243 double dsw_dr = 0.0;
244 double d2sw_dr2 = 0.0;
245 if (r > r_on) {
246 const double t = (r - r_on) / (r_off - r_on);
247 const double u = 1.0 - t;
248 sw = u * u * u * (1.0 + 3.0 * t + 6.0 * t * t);
249 dsw_dr = -30.0 * t * t * u * u / (r_off - r_on);
250 if constexpr (need_hessian)
251 d2sw_dr2 = -60.0 * t * u * (1.0 - 2.0 * t) /
252 ((r_off - r_on) * (r_off - r_on));
253 }
254 const double a = screeningLength(Zi, Zj);
255 const double x = r / a;
256 double phi = 0.0;
257 double dphi_dx = 0.0;
258 double d2phi_dx2 = 0.0;
259 for (std::size_t k = 0; k < kScreeningWeights.size(); ++k) {
260 const double term = kScreeningWeights[k] *
261 std::exp(-kScreeningExponents[k] * x);
262 phi += term;
263 dphi_dx -= kScreeningExponents[k] * term;
264 if constexpr (need_hessian)
265 d2phi_dx2 +=
266 kScreeningExponents[k] * kScreeningExponents[k] * term;
267 }
268 const double prefactor = kCoulombEvAngstrom *
269 static_cast<double>(Zi) *
270 static_cast<double>(Zj);
271 const double pair_energy = prefactor * phi / r;
272 energy_consumer(sw * pair_energy);
273
274 const double dV_dr =
275 prefactor * (dphi_dx / (a * r) - phi / (r * r));
276 const double scale = (sw * dV_dr + dsw_dr * pair_energy) / r;
277 const double gx = scale * dx;
278 const double gy = scale * dy;
279 const double gz = scale * dz;
280 gradient_consumer(ix, gx);
281 gradient_consumer(ix + 1, gy);
282 gradient_consumer(ix + 2, gz);
283 if (jx >= 0) {
284 gradient_consumer(jx, -gx);
285 gradient_consumer(jx + 1, -gy);
286 gradient_consumer(jx + 2, -gz);
287 }
288 if constexpr (need_hessian) {
289 const double d2V_dr2 = prefactor * (d2phi_dx2 / (a * a * r) -
290 2.0 * dphi_dx / (a * r2) +
291 2.0 * phi / (r2 * r));
292 const double radial_curvature = sw * d2V_dr2 +
293 2.0 * dsw_dr * dV_dr +
294 d2sw_dr2 * pair_energy;
295 const Eigen::Vector3d direction(dx / r, dy / r, dz / r);
296 const Eigen::Matrix3d block =
297 scale * Eigen::Matrix3d::Identity() +
298 (radial_curvature - scale) * direction *
299 direction.transpose();
300 hessian_consumer(ix, jx, block);
301 }
302 });
303 }
304
305 PriorPairFrame frame_;
306 bool active_ = false;
307};
308
309} // namespace priors
310} // namespace gpr
311
312#endif // GPR_PRIOR_ZBL_PRIOR_MEAN_H_
@ x
Definition Enums.h:28
bool active() const noexcept
PriorHessianPair HessianPair
static Eigen::MatrixXd applyHessianPairs(const std::vector< HessianPair > &pairs, const Eigen::MatrixBase< Derived > &directions)
Apply physical prior curvature without forming a dense Hessian.
void subtractFromTargets(const Coord &positions, Eigen::Index energy_count, Eigen::VectorXd &targets) const
void addToObservation(Observation &observation) const
ZBLPriorMeanEvaluation evaluate(const Eigen::MatrixBase< Derived > &positions) const
ZBLPriorMean(const AtomsConfiguration &atoms_config)
double energy(const Eigen::MatrixBase< Derived > &positions) const
static double repulsionOnsetRadius(int Zi, int Zj) noexcept
static double covalentRadius(int Z) noexcept
Cordero single-bond covalent radius (Angstrom) of atomic number Z.
std::vector< HessianPair > hessianPairs(const Eigen::MatrixBase< Derived > &positions) const
Cartesian pair blocks retain only interactions inside the cutoff.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
constexpr std::array< double, 97 > kCorderoRadiusPm
Prior distributions and prior-mean functions for the GP.
Eigen::MatrixXd applyPriorHessianPairs(const std::vector< PriorHessianPair > &pairs, const Eigen::MatrixBase< Derived > &directions)
Apply physical prior curvature without forming a dense Hessian.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
FieldMatrixd Coord
Definition Macros.h:35
Stores configurations of all atoms in the system.
Definition Structures.h:832
Observation data: energy, gradient and positions.
FieldMatrixd E
Pair term of a prior Hessian: block acts on x_first - x_second. A negative second is a fixed partner,...
bool hasPair() const noexcept