Loading...
Searching...
No Matches
RgFireEngine.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_RG_FIRE_ENGINE_H_
6#define GPR_NEB_RG_FIRE_ENGINE_H_
7
8#if defined(__has_include)
9#if !__has_include(<rgmin.h>)
10#error "gpridge requires rgmin include/rgmin.h"
11#endif
12#endif
13#include <rgmin.h>
14
15#include <Eigen/Dense>
16#include <algorithm>
17#include <cmath>
18#include <cstddef>
19
20#include "../ml/RgminAbi.h"
21#include "GPNEB.h"
22
23namespace gpr {
24namespace neb {
25namespace detail {
26
38class RgFireEngine final : public BandRelaxationEngine {
39public:
40 RgFireEngine() = default;
41 ~RgFireEngine() override = default;
42
43 RgFireEngine(const RgFireEngine&) = delete;
44 RgFireEngine& operator=(const RgFireEngine&) = delete;
45
46 void reset() override
47 {
48 iteration_ = 0;
49 // The band resets the stepper when the objective changes: a refit
50 // surrogate is a new surface, so the opening step returns with the
51 // velocity and mixing. Keeping dt (dest FireState::reset) ratchets it
52 // down here: the first step after a reset has zero velocity, so zero
53 // power, and FIRE 2.0 halves dt on it; with the band resetting every
54 // outer, dt would halve without bound until every step fell below
55 // the coordinate resolution.
56 if (vel_.size() > 0) vel_.setZero();
57 dt_ = kIstep;
58 alpha_ = kAlphaStart;
59 n_pos_ = 0;
60 }
61
62 bool step(GlobalLBFGSObjective& objective, double max_move,
63 std::optional<double> = std::nullopt) override
64 {
65 if (!objective.refresh(false)) return false;
66 const Eigen::VectorXd pos = objective.positions();
67 if (pos.size() <= 0) return false;
68 const Eigen::VectorXd force = objective.force();
69 if (force.size() != pos.size() || !pos.allFinite() ||
70 !force.allFinite()) {
71 return false;
72 }
73 ensure(static_cast<std::size_t>(pos.size()));
74
75 const FireSnap snap = snapshot();
76 Eigen::VectorXd dir;
77 if (!(max_move > 0.0) || !std::isfinite(max_move)) {
78 // Host Internal L-BFGS limitBandMotion returns Zero
79 // (GPNEB.cpp). rgmin FIRE has no rg-maxmove no-clip
80 // sentinel: a non-positive or non-finite cap must not
81 // take the unclipped dest FIRE2 displacement.
82 dir = Eigen::VectorXd::Zero(pos.size());
83 } else {
84 dir = fireDisplacement(force);
85 if (!dir.allFinite()) {
86 restore(snap);
87 return false;
88 }
89 dir = clipAtomMaxmove(dir, max_move);
90 if (!dir.allFinite()) {
91 restore(snap);
92 return false;
93 }
94 }
95
96 const Eigen::VectorXd next = objective.stepPositions(pos, dir);
97 if (!next.allFinite() || next.size() != pos.size()) {
98 restore(snap);
99 return false;
100 }
101 objective.setPositions(next);
102 if (!objective.refresh(false)) {
103 objective.setPositions(pos);
104 (void)objective.refresh(false);
105 restore(snap);
106 return false;
107 }
108 // rgmin FIRE velocity lives on the host-retracted plant.
109 const Eigen::VectorXd host_dx = objective.difference(next, pos);
110 if (host_dx.size() != pos.size() || !host_dx.allFinite() ||
111 !(dt_ > 0.0) || !std::isfinite(dt_)) {
112 objective.setPositions(pos);
113 (void)objective.refresh(false);
114 restore(snap);
115 return false;
116 }
117 vel_ = host_dx / dt_;
118 ++iteration_;
119 return true;
120 }
121
122 void rebuildSketchedH0(
123 const std::function<Eigen::VectorXd(const Eigen::VectorXd&)>&,
124 int) override
125 {
126 }
127 void setCurvatureExclusion(Eigen::Index, Eigen::Index) noexcept override { }
128 [[nodiscard]] int iteration() const override
129 {
130 return iteration_;
131 }
132 [[nodiscard]] int historySize() const override
133 {
134 return 0;
135 }
136 [[nodiscard]] double lastCurvatureDot() const override
137 {
138 return 0.0;
139 }
140 [[nodiscard]] double lastCurvatureScale() const override
141 {
142 return 0.0;
143 }
144
145private:
146 static constexpr double kIstep = 1.0;
147 static constexpr double kAlphaStart = 0.1;
148 static constexpr int kNMin = 5;
149 static constexpr double kFInc = 1.1;
150 static constexpr double kFDec = 0.5;
151 static constexpr double kFAlpha = 0.99;
152 static constexpr double kDtFloor = 1e-12;
153
154 struct FireSnap {
155 Eigen::VectorXd vel;
156 double dt = kIstep;
157 double alpha = kAlphaStart;
158 int n_pos = 0;
159 };
160
166 static Eigen::VectorXd clipAtomMaxmove(const Eigen::VectorXd& step,
167 double cap)
168 {
169 if (!(cap > 0.0) || !std::isfinite(cap)) {
170 return Eigen::VectorXd::Zero(step.size());
171 }
172 const Eigen::Index n = step.size();
173 double max_atom = 0.0;
174 Eigen::Index i = 0;
175 while (i + 3 <= n) {
176 max_atom = std::max(max_atom, step.segment<3>(i).norm());
177 i += 3;
178 }
179 if (i < n) max_atom = std::max(max_atom, step.tail(n - i).norm());
180 if (max_atom > cap && max_atom > 0.0) return step * (cap / max_atom);
181 return step;
182 }
183
184 void ensure(std::size_t dim)
185 {
186 if (vel_.size() == static_cast<Eigen::Index>(dim)) return;
187 vel_ = Eigen::VectorXd::Zero(static_cast<Eigen::Index>(dim));
188 dt_ = kIstep;
189 dt_max_ = std::max(kIstep * 2.5, kIstep);
190 alpha_ = kAlphaStart;
191 n_pos_ = 0;
192 }
193
194 [[nodiscard]] FireSnap snapshot() const
195 {
196 return FireSnap{vel_, dt_, alpha_, n_pos_};
197 }
198
199 void restore(const FireSnap& snap)
200 {
201 vel_ = snap.vel;
202 dt_ = snap.dt;
203 alpha_ = snap.alpha;
204 n_pos_ = snap.n_pos;
205 }
206
208 Eigen::VectorXd fireDisplacement(const Eigen::VectorXd& force)
209 {
210 const double power = force.dot(vel_);
211 if (power > 0.0) {
212 const double fnorm = force.norm();
213 const double vnorm = vel_.norm();
214 if (fnorm > 0.0) {
215 const double scale = alpha_ * vnorm / fnorm;
216 vel_ = (1.0 - alpha_) * vel_ + scale * force;
217 }
218 }
219 if (power > 0.0) {
220 ++n_pos_;
221 if (n_pos_ > kNMin) {
222 dt_ = std::min(dt_ * kFInc, dt_max_);
223 alpha_ *= kFAlpha;
224 }
225 } else {
226 dt_ *= kFDec;
227 if (dt_ < kDtFloor) dt_ = kDtFloor;
228 vel_.setZero();
229 alpha_ = kAlphaStart;
230 n_pos_ = 0;
231 }
232 vel_ += force * dt_;
233 return vel_ * dt_;
234 }
235
236 Eigen::VectorXd vel_;
237 double dt_ = kIstep;
238 double dt_max_ = kIstep * 2.5;
239 double alpha_ = kAlphaStart;
240 int n_pos_ = 0;
241 int iteration_ = 0;
242};
243
244} // namespace detail
245} // namespace neb
246} // namespace gpr
247
248#endif /* GPR_NEB_RG_FIRE_ENGINE_H_ */
GP-accelerated NEB: AIE and OIE variants.
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