Loading...
Searching...
No Matches
NEBSpringForce.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "eon/NEBSpringForce.h"
13
14#include <stdexcept>
15
16namespace eonc::neb {
17
18// --- UniformSpring ---
19
21UniformSpring::compute(long i, const AtomMatrix &tangent, double distNext,
22 double distPrev, const AtomMatrix &posDiffNext,
23 const AtomMatrix &posDiffPrev,
24 const std::shared_ptr<Matter> &image) const {
25 SpringResult result;
26 result.forceSpringPar = ksp * (distNext - distPrev) * tangent;
27 result.forceSpring = ksp * image->pbc((posDiffNext) - (posDiffPrev));
28 return result;
29}
30
31// --- GeometricSpring ---
32
34 double distNext, double distPrev) const {
35 static_cast<void>(i);
36 SpringResult result;
37 result.forceSpringPar =
38 ksp * (distNext * distNext - distPrev * distPrev) * tangent;
39 result.forceSpring = AtomMatrix::Zero(tangent.rows(), tangent.cols());
40 return result;
41}
42
43// --- WeightedSpring ---
44
46 double distNext, double distPrev) const {
47 if (i < 1 || static_cast<size_t>(i) >= springConstants.size()) {
48 throw std::invalid_argument(
49 "WeightedSpring::compute: image index out of range");
50 }
51 double kspNext = springConstants[i];
52 double kspPrev = springConstants[i - 1];
53
54 SpringResult result;
55 result.forceSpringPar =
56 ((kspNext * distNext) - (kspPrev * distPrev)) * tangent;
57 result.forceSpring = AtomMatrix::Zero(tangent.rows(), tangent.cols());
58 return result;
59}
60
61// --- OnsagerMachlupSpring ---
62
65 const AtomMatrix &posNext,
66 const AtomMatrix &posPrev, const AtomMatrix &pos,
67 const std::shared_ptr<Matter> &image) const {
68 // Mandelli Eq. 13: k * ( R(i+1) + R(i-1) - 2R(i) + L(i+1) - L(i) )
69 AtomMatrix diff =
70 image->pbc(posNext + posPrev - 2.0 * pos + L_vecs[i + 1] - L_vecs[i]);
71 AtomMatrix f_om_vec = base_k * diff;
72
73 // Mandelli Eq. 15: Project onto tangent
74 SpringResult result;
75 result.forceSpringPar = matDot(f_om_vec, tangent) * tangent;
76 result.forceSpring = AtomMatrix::Zero(tangent.rows(), tangent.cols());
77 return result;
78}
79
80// --- Factory ---
81
84 const std::vector<std::shared_ptr<Matter>> &path,
85 long numImages, int atoms, double maxEnergy, double E_ref) {
86
87 if (params.neb_options().spring.geometric) {
89 }
90
91 if (params.neb_options().spring.om.enabled) {
92 double base_k = params.neb_options().spring.constant;
93
94 if (params.neb_options().spring.om.optimize_k) {
95 double avgPotForce = 0.0;
96 double avgPathCurvature = 0.0;
97 int count = 0;
98 for (long j = 1; j <= numImages; j++) {
99 avgPotForce += path[j]->getForces().norm();
100 AtomMatrix next = path[j + 1]->getPositions();
101 AtomMatrix prev = path[j - 1]->getPositions();
102 AtomMatrix curr = path[j]->getPositions();
103 AtomMatrix curvVec = path[j]->pbc(next + prev - 2.0 * curr);
104 avgPathCurvature += curvVec.norm();
105 count++;
106 }
107 if (count > 0 && avgPathCurvature > 1e-6) {
108 double scale = params.neb_options().spring.om.k_scale;
109 base_k = scale * (avgPotForce / avgPathCurvature);
110 base_k = std::max(base_k, params.neb_options().spring.om.k_min);
111 base_k = std::min(base_k, params.neb_options().spring.om.k_max);
112 }
113 }
114
115 // Pre-calculate L vectors for all images
116 std::vector<AtomMatrix> L_vecs(numImages + 2);
117 for (long j = 0; j <= numImages + 1; j++) {
118 L_vecs[j].resize(atoms, 3);
119 if (j == 0 || j == numImages + 1) {
120 L_vecs[j].setZero();
121 } else {
122 const AtomMatrix &forces = path[j]->getForces();
123 double alpha_k = eonc::safemath::safe_recip(2.0 * base_k, 0.0);
124 for (int k = 0; k < atoms; k++) {
125 L_vecs[j].row(k) = alpha_k * forces.row(k);
126 }
127 }
128 }
129
130 return OnsagerMachlupSpring{base_k, std::move(L_vecs)};
131
132 } else if (params.neb_options().spring.weighting.enabled) {
133 double k_l = params.neb_options().spring.weighting.k_min;
134 double k_u = params.neb_options().spring.weighting.k_max;
135 std::vector<double> springConstants(numImages + 2, k_l);
136
137 double energyRange = maxEnergy - E_ref;
138 if (energyRange < 1e-10) {
139 std::fill(springConstants.begin(), springConstants.end(), k_l);
140 } else {
141 for (int idx = 1; idx <= numImages + 1; idx++) {
142 double Ei = std::max(path[idx]->getPotentialEnergy(),
143 path[idx - 1]->getPotentialEnergy());
144 if (Ei > E_ref) {
145 double alpha_i = (maxEnergy - Ei) / energyRange;
146 alpha_i = std::max(0.0, std::min(1.0, alpha_i));
147 springConstants[idx - 1] = (1.0 - alpha_i) * k_u + alpha_i * k_l;
148 } else {
149 springConstants[idx - 1] = k_l;
150 }
151 }
152 }
153
154 return WeightedSpring{std::move(springConstants)};
155
156 } else {
157 return UniformSpring{params.neb_options().spring.constant};
158 }
159}
160
161} // namespace eonc::neb
double matDot(const AtomMatrix &a, const AtomMatrix &b)
SIMD-optimized dot product for contiguous Eigen matrices.
Definition Eigen.h:50
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
const neb_options_t & neb_options() const
SpringStrategy buildSpringStrategy(const Parameters &params, const std::vector< std::shared_ptr< Matter > > &path, long numImages, int atoms, double maxEnergy, double E_ref)
Build the appropriate spring strategy from parameters and current path state.
std::variant< UniformSpring, WeightedSpring, GeometricSpring, OnsagerMachlupSpring > SpringStrategy
constexpr double safe_recip(double x, double fallback=0.0)
Definition SafeMath.h:29
Cubic-potential spring.
SpringResult compute(long i, const AtomMatrix &tangent, double distNext, double distPrev) const
Onsager-Machlup action-based springs (Mandelli & Parrinello 2021).
SpringResult compute(long i, const AtomMatrix &tangent, const AtomMatrix &posNext, const AtomMatrix &posPrev, const AtomMatrix &pos, const std::shared_ptr< Matter > &image) const
std::vector< AtomMatrix > L_vecs
Result of spring force computation for a single image.
Uniform spring constant for all images.
SpringResult compute(long i, const AtomMatrix &tangent, double distNext, double distPrev, const AtomMatrix &posDiffNext, const AtomMatrix &posDiffPrev, const std::shared_ptr< Matter > &image) const
Energy-weighted spring constants (variable per segment).
std::vector< double > springConstants
SpringResult compute(long i, const AtomMatrix &tangent, double distNext, double distPrev) const
struct eonc::neb_options_t::spring_options_t::energy_weighting_t weighting
bool geometric
Parallel force k (d_next^2 - d_prev^2).
struct eonc::neb_options_t::spring_options_t::onsager_machlup_t om
struct eonc::neb_options_t::spring_options_t spring