52 : frame_(atoms_config)
54 active_ = hasActivePair();
57 [[nodiscard]]
bool active() const noexcept
65 if (Z < 1 || Z > kLastCorderoElement)
return kDefaultCovalentRadius;
77 if (Zi <= 0 || Zj <= 0)
return 0.0;
81 template <
typename Derived>
83 const Eigen::MatrixBase<Derived>& positions)
const
85 if (!dimensionMatches(positions.size()))
return 0.0;
88 positions, [&](
double pair_energy) {
out += pair_energy; },
89 [](Eigen::Index, double) {});
93 template <
typename Derived>
95 const Eigen::MatrixBase<Derived>& positions)
const
98 out.gradient = Eigen::RowVectorXd::Zero(positions.size());
99 if (!dimensionMatches(positions.size()))
return out;
101 positions, [&](
double pair_energy) {
out.energy += pair_energy; },
102 [&](Eigen::Index component,
double value) {
103 out.gradient(component) += value;
109 template <
typename Derived>
111 const Eigen::MatrixBase<Derived>& positions)
const
113 std::vector<HessianPair> pairs;
114 if (!dimensionMatches(positions.size()))
return pairs;
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});
125 template <
typename Derived>
127 const std::vector<HessianPair>& pairs,
128 const Eigen::MatrixBase<Derived>& directions)
134 Eigen::VectorXd& targets)
const
136 if (!active_ || positions.rows() != energy_count ||
137 !dimensionMatches(positions.cols())) {
140 const Eigen::Index required =
141 energy_count + positions.rows() * positions.cols();
142 if (targets.size() < required)
return;
144 for (Eigen::Index row = 0; row < positions.rows(); ++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;
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())) {
166 for (Eigen::Index row = 0; row < observation.
R.rows(); ++row) {
168 observation.
R.row(row),
169 [&](
double pair_energy) {
170 observation.E(row, 0) += pair_energy;
172 [&](Eigen::Index component,
double value) {
173 observation.G(row, component) += value;
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};
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;
193 [[nodiscard]]
bool hasActivePair() const noexcept
198 [[nodiscard]]
bool dimensionMatches(Eigen::Index size)
const noexcept
200 return active_ && size == frame_.coordinates();
203 [[nodiscard]]
static double screeningLength(
int Zi,
int Zj)
noexcept
205 return kScreeningLengthPrefactor * kBohrAngstrom /
206 (std::pow(
static_cast<double>(Zi), kZExponent) +
207 std::pow(
static_cast<double>(Zj), kZExponent));
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
218 constexpr bool need_hessian =
219 !std::is_same_v<std::decay_t<HessianConsumer>, std::nullptr_t>;
221 frame_.forEachPair(positions, [&](Eigen::Index ix, Eigen::Index jx,
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;
229 energy_consumer(std::numeric_limits<double>::infinity());
230 if constexpr (need_hessian)
233 Eigen::Matrix3d::Constant(
234 std::numeric_limits<double>::quiet_NaN()));
237 const double r = std::sqrt(r2);
239 const double r_off = kSwitchOffFactor * r_cov;
240 if (r >= r_off)
return;
241 const double r_on = kSwitchOnFactor * r_cov;
244 double d2sw_dr2 = 0.0;
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));
254 const double a = screeningLength(Zi, Zj);
255 const double x = r / a;
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);
263 dphi_dx -= kScreeningExponents[k] * term;
264 if constexpr (need_hessian)
266 kScreeningExponents[k] * kScreeningExponents[k] * term;
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);
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);
284 gradient_consumer(jx, -gx);
285 gradient_consumer(jx + 1, -gy);
286 gradient_consumer(jx + 2, -gz);
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);
305 PriorPairFrame frame_;
306 bool active_ =
false;