5#ifndef GPR_NEB_RG_FIRE_ENGINE_H_
6#define GPR_NEB_RG_FIRE_ENGINE_H_
8#if defined(__has_include)
9#if !__has_include(<rgmin.h>)
10#error "gpridge requires rgmin include/rgmin.h"
38class RgFireEngine final :
public BandRelaxationEngine {
40 RgFireEngine() =
default;
41 ~RgFireEngine()
override =
default;
43 RgFireEngine(
const RgFireEngine&) =
delete;
44 RgFireEngine& operator=(
const RgFireEngine&) =
delete;
56 if (vel_.size() > 0) vel_.setZero();
62 bool step(GlobalLBFGSObjective& objective,
double max_move,
63 std::optional<double> = std::nullopt)
override
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() ||
73 ensure(
static_cast<std::size_t
>(pos.size()));
75 const FireSnap snap = snapshot();
77 if (!(max_move > 0.0) || !std::isfinite(max_move)) {
82 dir = Eigen::VectorXd::Zero(pos.size());
84 dir = fireDisplacement(force);
85 if (!dir.allFinite()) {
89 dir = clipAtomMaxmove(dir, max_move);
90 if (!dir.allFinite()) {
96 const Eigen::VectorXd next = objective.stepPositions(pos, dir);
97 if (!next.allFinite() || next.size() != pos.size()) {
101 objective.setPositions(next);
102 if (!objective.refresh(
false)) {
103 objective.setPositions(pos);
104 (void)objective.refresh(
false);
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);
117 vel_ = host_dx / dt_;
122 void rebuildSketchedH0(
123 const std::function<Eigen::VectorXd(
const Eigen::VectorXd&)>&,
127 void setCurvatureExclusion(Eigen::Index, Eigen::Index)
noexcept override { }
128 [[nodiscard]]
int iteration()
const override
132 [[nodiscard]]
int historySize()
const override
136 [[nodiscard]]
double lastCurvatureDot()
const override
140 [[nodiscard]]
double lastCurvatureScale()
const override
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;
157 double alpha = kAlphaStart;
166 static Eigen::VectorXd clipAtomMaxmove(
const Eigen::VectorXd& step,
169 if (!(cap > 0.0) || !std::isfinite(cap)) {
170 return Eigen::VectorXd::Zero(step.size());
172 const Eigen::Index n = step.size();
173 double max_atom = 0.0;
176 max_atom = std::max(max_atom, step.segment<3>(i).norm());
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);
184 void ensure(std::size_t dim)
186 if (vel_.size() ==
static_cast<Eigen::Index
>(dim))
return;
187 vel_ = Eigen::VectorXd::Zero(
static_cast<Eigen::Index
>(dim));
189 dt_max_ = std::max(kIstep * 2.5, kIstep);
190 alpha_ = kAlphaStart;
194 [[nodiscard]] FireSnap snapshot()
const
196 return FireSnap{vel_, dt_, alpha_, n_pos_};
199 void restore(
const FireSnap& snap)
208 Eigen::VectorXd fireDisplacement(
const Eigen::VectorXd& force)
210 const double power = force.dot(vel_);
212 const double fnorm = force.norm();
213 const double vnorm = vel_.norm();
215 const double scale = alpha_ * vnorm / fnorm;
216 vel_ = (1.0 - alpha_) * vel_ + scale * force;
221 if (n_pos_ > kNMin) {
222 dt_ = std::min(dt_ * kFInc, dt_max_);
227 if (dt_ < kDtFloor) dt_ = kDtFloor;
229 alpha_ = kAlphaStart;
236 Eigen::VectorXd vel_;
238 double dt_max_ = kIstep * 2.5;
239 double alpha_ = kAlphaStart;
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.