Loading...
Searching...
No Matches
MorsePriorMean.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// Morse pair prior mean: Teng/Bao first-level force-field prior
6// (JCTC 2022 DOI 10.1021/acs.jctc.2c00546; CI-NEB JCTC 2024
7// DOI 10.1021/acs.jctc.4c00291). The GP trains on y - m(X) and adds
8// m(x*) back at prediction, same residual hook as ZBLPriorMean.
9//
10// Pair term: V(r) = D_e (1 - exp(-a (r - r_e)))^2, zero at r_e.
11// r_e is the Cordero 2008 single-bond radius sum (Dalton Trans. 2832,
12// DOI 10.1039/b801115j), the same table CovalentBondPrior already
13// ships. D_e = 4 eV and a = 2 /A are a frozen chemical-scale well,
14// not a fitted production force field: typical single-bond well and
15// Morse range, constructor-overridable, default off.
16//
17// A C^2 quintic switch kills the pair for r >= 1.5 r_e so non-bonded
18// contacts are inert (an all-pairs Morse would pull every contact to
19// r_e and collapse the molecule). Compression r < r_e stays on the
20// Morse wall at eV scale. The single-bond radius model is experimental;
21// multiple-bond environments require a calibrated equilibrium distance.
22
23#ifndef GPR_PRIOR_MORSE_PRIOR_MEAN_H_
24#define GPR_PRIOR_MORSE_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
35#include "CovalentBondPrior.h"
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 static constexpr double kDefaultWellDepthEv = 4.0;
52 static constexpr double kDefaultRangePerAngstrom = 2.0;
53 static constexpr double kSwitchOnFactor = 1.20;
54 static constexpr double kSwitchOffFactor = 1.50;
55
57 const AtomsConfiguration& atoms_config,
58 double well_depth_ev = kDefaultWellDepthEv,
59 double range_per_angstrom = kDefaultRangePerAngstrom)
60 : frame_(atoms_config),
61 well_depth_ev_(well_depth_ev),
62 range_per_angstrom_(range_per_angstrom)
63 {
64 active_ = well_depth_ev_ > 0.0 && range_per_angstrom_ > 0.0 &&
65 hasActivePair();
66 }
67
68 [[nodiscard]] bool active() const noexcept
69 {
70 return active_;
71 }
72
73 template <typename Derived>
74 [[nodiscard]] double energy(
75 const Eigen::MatrixBase<Derived>& positions) const
76 {
77 if (!dimensionMatches(positions.size())) return 0.0;
78 double out = 0.0;
79 visitPairs(
80 positions, [&](double pair_energy) { out += pair_energy; },
81 [](Eigen::Index, double) {});
82 return out;
83 }
84
85 template <typename Derived>
87 const Eigen::MatrixBase<Derived>& positions) const
88 {
90 out.gradient = Eigen::RowVectorXd::Zero(positions.size());
91 if (!dimensionMatches(positions.size())) return out;
92 visitPairs(
93 positions, [&](double pair_energy) { out.energy += pair_energy; },
94 [&](Eigen::Index component, double value) {
95 out.gradient(component) += value;
96 });
97 return out;
98 }
99
100 template <typename Derived>
101 [[nodiscard]] std::vector<HessianPair> hessianPairs(
102 const Eigen::MatrixBase<Derived>& positions) const
103 {
104 std::vector<HessianPair> pairs;
105 if (!dimensionMatches(positions.size())) return pairs;
106 visitPairs(
107 positions, [](double) {}, [](Eigen::Index, double) {},
108 [&](Eigen::Index first, Eigen::Index second,
109 const Eigen::Matrix3d& block) {
110 pairs.push_back({first, second, block});
111 });
112 return pairs;
113 }
114
115 [[nodiscard]] static Eigen::MatrixXd applyHessianPairs(
116 const std::vector<HessianPair>& pairs,
117 const Eigen::MatrixBase<Eigen::MatrixXd>& directions)
118 {
119 return applyPriorHessianPairs(pairs, directions);
120 }
121
122 template <typename Derived>
123 [[nodiscard]] static Eigen::MatrixXd applyHessianPairs(
124 const std::vector<HessianPair>& pairs,
125 const Eigen::MatrixBase<Derived>& directions)
126 {
127 return applyPriorHessianPairs(pairs, directions);
128 }
129
130 void subtractFromTargets(const Coord& positions, Eigen::Index energy_count,
131 Eigen::VectorXd& targets) const
132 {
133 if (!active_ || positions.rows() != energy_count ||
134 !dimensionMatches(positions.cols())) {
135 return;
136 }
137 const Eigen::Index required =
138 energy_count + positions.rows() * positions.cols();
139 if (targets.size() < required) return;
140
141 for (Eigen::Index row = 0; row < positions.rows(); ++row) {
142 visitPairs(
143 positions.row(row),
144 [&](double pair_energy) { targets(row) -= pair_energy; },
145 [&](Eigen::Index component, double value) {
146 const Eigen::Index target_index =
147 energy_count + row + component * positions.rows();
148 targets(target_index) -= value;
149 });
150 }
151 }
152
153 void addToObservation(Observation& observation) const
154 {
155 if (!active_ || observation.E.rows() != observation.R.rows() ||
156 observation.G.rows() != observation.R.rows() ||
157 observation.G.cols() != observation.R.cols() ||
158 observation.E.cols() < 1 ||
159 !dimensionMatches(observation.R.cols())) {
160 return;
161 }
162
163 for (Eigen::Index row = 0; row < observation.R.rows(); ++row) {
164 visitPairs(
165 observation.R.row(row),
166 [&](double pair_energy) {
167 observation.E(row, 0) += pair_energy;
168 },
169 [&](Eigen::Index component, double value) {
170 observation.G(row, component) += value;
171 });
172 }
173 }
174
175 [[nodiscard]] static double equilibriumBondLength(int Zi, int Zj) noexcept
176 {
177 return bond_length_angstrom(Zi, Zj, 1.0);
178 }
179
180private:
181 [[nodiscard]] bool hasActivePair() const noexcept
182 {
183 const auto& moving = frame_.moving_numbers;
184 const auto bonded = [](int Zi, int Zj) {
185 return Zi > 0 && Zj > 0 && equilibriumBondLength(Zi, Zj) > 0.0;
186 };
187 for (std::size_t i = 0; i < moving.size(); ++i) {
188 for (std::size_t j = i + 1; j < moving.size(); ++j)
189 if (bonded(moving[i], moving[j])) return true;
190 for (int Zk: frame_.fixed_numbers)
191 if (bonded(moving[i], Zk)) return true;
192 }
193 return false;
194 }
195
196 [[nodiscard]] bool dimensionMatches(Eigen::Index size) const noexcept
197 {
198 return active_ && size == frame_.coordinates();
199 }
200
201 template <typename Positions, typename EnergyConsumer,
202 typename GradientConsumer,
203 typename HessianConsumer = std::nullptr_t>
204 void visitPairs(const Positions& positions,
205 EnergyConsumer&& energy_consumer,
206 GradientConsumer&& gradient_consumer,
207 HessianConsumer&& hessian_consumer = nullptr) const
208 {
209 constexpr bool need_hessian =
210 !std::is_same_v<std::decay_t<HessianConsumer>, std::nullptr_t>;
211 // jx < 0 is a fixed partner: the pair moves atom ix alone.
212 frame_.forEachPair(
213 positions, [&](Eigen::Index ix, Eigen::Index jx, int Zi, int Zj,
214 const Eigen::Vector3d& delta) {
215 const double r_e = equilibriumBondLength(Zi, Zj);
216 if (!(r_e > 0.0)) return;
217
218 const double dx = delta.x();
219 const double dy = delta.y();
220 const double dz = delta.z();
221 const double r2 = dx * dx + dy * dy + dz * dz;
222 if (!(r2 > 0.0)) {
223 energy_consumer(std::numeric_limits<double>::infinity());
224 if constexpr (need_hessian)
225 hessian_consumer(
226 ix, jx,
227 Eigen::Matrix3d::Constant(
228 std::numeric_limits<double>::quiet_NaN()));
229 return;
230 }
231 const double r = std::sqrt(r2);
232 const double r_off = kSwitchOffFactor * r_e;
233 if (r >= r_off) return;
234 const double r_on = kSwitchOnFactor * r_e;
235 double sw = 1.0;
236 double dsw_dr = 0.0;
237 double d2sw_dr2 = 0.0;
238 if (r > r_on) {
239 const double t = (r - r_on) / (r_off - r_on);
240 const double u = 1.0 - t;
241 sw = u * u * u * (1.0 + 3.0 * t + 6.0 * t * t);
242 dsw_dr = -30.0 * t * t * u * u / (r_off - r_on);
243 if constexpr (need_hessian)
244 d2sw_dr2 = -60.0 * t * u * (1.0 - 2.0 * t) /
245 ((r_off - r_on) * (r_off - r_on));
246 }
247 const double expo = std::exp(-range_per_angstrom_ * (r - r_e));
248 const double d = 1.0 - expo;
249 const double pair_energy = well_depth_ev_ * d * d;
250 energy_consumer(sw * pair_energy);
251
252 // dV/dr = 2 De a d (1 - d); V = De d^2, d = 1 - exp(-a(r-re)).
253 const double dV_dr =
254 2.0 * well_depth_ev_ * range_per_angstrom_ * d * expo;
255 const double scale = (sw * dV_dr + dsw_dr * pair_energy) / r;
256 const double gx = scale * dx;
257 const double gy = scale * dy;
258 const double gz = scale * dz;
259 gradient_consumer(ix, gx);
260 gradient_consumer(ix + 1, gy);
261 gradient_consumer(ix + 2, gz);
262 if (jx >= 0) {
263 gradient_consumer(jx, -gx);
264 gradient_consumer(jx + 1, -gy);
265 gradient_consumer(jx + 2, -gz);
266 }
267 if constexpr (need_hessian) {
268 // d2V/dr2 = 2 De a^2 (1-d)(1-2d) = 2 De a^2 expo (2 expo -
269 // 1).
270 const double d2V_dr2 =
271 2.0 * well_depth_ev_ * range_per_angstrom_ *
272 range_per_angstrom_ * expo * (2.0 * expo - 1.0);
273 const double radial_curvature = sw * d2V_dr2 +
274 2.0 * dsw_dr * dV_dr +
275 d2sw_dr2 * pair_energy;
276 const Eigen::Vector3d direction(dx / r, dy / r, dz / r);
277 const Eigen::Matrix3d block =
278 scale * Eigen::Matrix3d::Identity() +
279 (radial_curvature - scale) * direction *
280 direction.transpose();
281 hessian_consumer(ix, jx, block);
282 }
283 });
284 }
285
286 PriorPairFrame frame_;
287 double well_depth_ev_ = kDefaultWellDepthEv;
288 double range_per_angstrom_ = kDefaultRangePerAngstrom;
289 bool active_ = false;
290};
291
292} // namespace priors
293} // namespace gpr
294
295#endif // GPR_PRIOR_MORSE_PRIOR_MEAN_H_
std::vector< HessianPair > hessianPairs(const Eigen::MatrixBase< Derived > &positions) const
static constexpr double kDefaultRangePerAngstrom
bool active() const noexcept
double energy(const Eigen::MatrixBase< Derived > &positions) const
MorsePriorMeanEvaluation evaluate(const Eigen::MatrixBase< Derived > &positions) const
static double equilibriumBondLength(int Zi, int Zj) noexcept
void subtractFromTargets(const Coord &positions, Eigen::Index energy_count, Eigen::VectorXd &targets) const
MorsePriorMean(const AtomsConfiguration &atoms_config, double well_depth_ev=kDefaultWellDepthEv, double range_per_angstrom=kDefaultRangePerAngstrom)
static constexpr double kSwitchOnFactor
static Eigen::MatrixXd applyHessianPairs(const std::vector< HessianPair > &pairs, const Eigen::MatrixBase< Eigen::MatrixXd > &directions)
static constexpr double kDefaultWellDepthEv
void addToObservation(Observation &observation) const
static constexpr double kSwitchOffFactor
static Eigen::MatrixXd applyHessianPairs(const std::vector< HessianPair > &pairs, const Eigen::MatrixBase< Derived > &directions)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Prior distributions and prior-mean functions for the GP.
double bond_length_angstrom(int Za, int Zb, double alpha=1.0) noexcept
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,...
std::vector< int > moving_numbers
Atomic numbers of the atoms the band moves, in band order.
std::vector< int > fixed_numbers