5#ifndef GPR_NEB_RGSADDLE_BAND_ENGINE_H_
6#define GPR_NEB_RGSADDLE_BAND_ENGINE_H_
28class RgsaddleBandEngine final :
public BandRelaxationEngine {
43 RgsaddleBandEngine() =
default;
44 ~RgsaddleBandEngine()
override
49 RgsaddleBandEngine(
const RgsaddleBandEngine&) =
delete;
50 RgsaddleBandEngine& operator=(
const RgsaddleBandEngine&) =
delete;
55 bool bind(RgsaddleClient& client, int64_t n_images, int64_t n_atoms,
56 const double* positions, Method method = Method::Fire)
58 if (!client.loaded() || n_images < 3 || n_atoms < 1 ||
59 positions ==
nullptr) {
63 rgsaddle_band_config_t cfg{};
64 cfg.version.major = RGSADDLE_ABI_MAJOR;
65 cfg.version.minor = 0;
77 case Method::Ode12rExp:
85 cfg.ci_trigger_factor = 0.0;
89 band_ = client.bandCreate(&cfg, n_images, n_atoms, positions);
90 if (band_ ==
nullptr)
return false;
91 if (client.bandSetForceGate(band_, RGSADDLE_FORCE_MAX_ATOM) !=
93 client.bandFree(band_);
101 if (method == Method::Ode12rExp &&
102 client.bandSetExpPrecon(band_, 0.0, 0.0, 0.0, 0.0) != RGSADDLE_OK) {
103 client.bandFree(band_);
109 report_at_positions_ =
false;
110 n_images_ = n_images;
112 interior_dof_ = (n_images - 2) * 3 * n_atoms;
113 full_.assign(
static_cast<std::size_t
>(n_images * 3 * n_atoms), 0.0);
114 for (std::size_t i = 0; i < full_.size(); ++i)
115 full_[i] = positions[i];
120 void setSurface(rgsaddle_surface_fn
surface,
void*
user)
126 void reset()
override
129 report_at_positions_ =
false;
130 if (client_ !=
nullptr && band_ !=
nullptr) client_->bandReset(band_);
133 bool step(GlobalLBFGSObjective& objective,
double max_move,
134 std::optional<double> = std::nullopt)
override
136 if (client_ ==
nullptr || band_ ==
nullptr || surface_ ==
nullptr)
138 const Eigen::VectorXd before = objective.
positions();
139 if (before.size() != interior_dof_)
return false;
140 report_at_positions_ =
false;
141 packInteriors(before);
142 if (client_->bandSetPositions(band_, full_.data()) != 0)
return false;
143 rgsaddle_report_t report{};
144 const int rc = client_->bandStep(band_, surface_, user_, &report);
145 if (rc != 0)
return false;
146 if (client_->bandPositions(band_, full_.data()) != 0)
return false;
147 Eigen::VectorXd after = unpackInteriors();
148 if (!after.allFinite() || after.size() != before.size()) {
149 packInteriors(before);
150 client_->bandReset(band_);
153 bool clipped =
false;
154 if (max_move > 0.0) {
155 for (Eigen::Index a = 0; a < after.size(); a += 3) {
156 const Eigen::Index n =
157 std::min<Eigen::Index>(3, after.size() - a);
158 Eigen::VectorXd d = after.segment(a, n) - before.segment(a, n);
159 const double nn = d.norm();
161 after.segment(a, n) =
162 before.segment(a, n) + d * (max_move / nn);
167 const Eigen::VectorXd next =
168 objective.stepPositions(before, after - before);
169 if (!next.allFinite() || next.size() != before.size()) {
170 packInteriors(before);
171 client_->bandReset(band_);
175 if (clipped || (next - after).cwiseAbs().maxCoeff() > 0.0) {
176 client_->bandReset(band_);
178 if (client_->bandSetPositions(band_, full_.data()) != 0) {
179 packInteriors(before);
180 client_->bandReset(band_);
183 objective.setPositions(next);
184 if (!objective.refresh(
false)) {
185 packInteriors(before);
186 objective.setPositions(before);
187 client_->bandReset(band_);
191 last_max_force_ = report.max_force;
194 report_at_positions_ =
195 !clipped && (next - after).cwiseAbs().maxCoeff() == 0.0;
207 [[nodiscard]] std::optional<double> metricResidual() const noexcept
209 if (method_ != Method::Ode12rExp || !report_at_positions_ ||
210 !std::isfinite(last_max_force_)) {
213 return last_max_force_;
216 void rebuildSketchedH0(
217 const std::function<Eigen::VectorXd(
const Eigen::VectorXd&)>&,
221 void setCurvatureExclusion(Eigen::Index, Eigen::Index)
noexcept override { }
222 [[nodiscard]]
int iteration()
const override
226 [[nodiscard]]
int historySize()
const override
230 [[nodiscard]]
double lastCurvatureDot()
const override
234 [[nodiscard]]
double lastCurvatureScale()
const override
238 [[nodiscard]]
double lastMaxForce() const noexcept
240 return last_max_force_;
246 if (client_ !=
nullptr && band_ !=
nullptr) client_->bandFree(band_);
251 void packInteriors(
const Eigen::VectorXd& interiors)
253 const Eigen::Index stride = 3 * n_atoms_;
254 Eigen::Map<Eigen::VectorXd>(full_.data() + stride, interior_dof_) =
258 [[nodiscard]] Eigen::VectorXd unpackInteriors()
const
260 const Eigen::Index stride = 3 * n_atoms_;
261 return Eigen::Map<const Eigen::VectorXd>(full_.data() + stride,
265 RgsaddleClient* client_ =
nullptr;
267 rgsaddle_surface_fn surface_ =
nullptr;
268 void* user_ =
nullptr;
269 int64_t n_images_ = 0;
270 int64_t n_atoms_ = 0;
271 Eigen::Index interior_dof_ = 0;
272 std::vector<double> full_;
274 double last_max_force_ = 0.0;
275 Method method_ = Method::Fire;
276 bool report_at_positions_ =
false;
GP-accelerated NEB: AIE and OIE variants.
eon_relax_band_t eon_relax_surface_fn surface
eon_relax_band_t eon_relax_surface_fn void * user
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.