Loading...
Searching...
No Matches
RgEonNebEngine.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_EON_NEB_ENGINE_H_
6#define GPR_NEB_RG_EON_NEB_ENGINE_H_
7
8#include <cstdint>
9#include <vector>
10
11#include "EonRelaxClient.h"
12#include "GPNEB.h"
13
14namespace gpr {
15namespace neb {
16namespace detail {
17
24class RgEonNebEngine final : public BandRelaxationEngine {
25public:
26 RgEonNebEngine() = default;
27 ~RgEonNebEngine() override = default;
28
29 RgEonNebEngine(const RgEonNebEngine&) = delete;
30 RgEonNebEngine& operator=(const RgEonNebEngine&) = delete;
31
35 bool bind(EonRelaxClient& client, eon_relax_band_t band,
37 {
38 if (!client.created() || band.n_images < 3 || band.n_atoms <= 0 ||
39 band.positions == nullptr || band.atomic_nrs == nullptr ||
40 band.boxes == nullptr || surface == nullptr) {
41 return false;
42 }
43 client_ = &client;
44 band_ = band;
45 surface_ = surface;
46 user_ = user;
47 interior_dof_ =
48 static_cast<Eigen::Index>((band.n_images - 2) * 3 * band.n_atoms);
49 iteration_ = 0;
50 return true;
51 }
52
53 void reset() override
54 {
55 iteration_ = 0;
56 if (client_ != nullptr) client_->reset();
57 }
58
59 bool step(GlobalLBFGSObjective& objective, double max_move,
60 std::optional<double> = std::nullopt) override
61 {
62 if (client_ == nullptr || surface_ == nullptr) return false;
63 const Eigen::VectorXd before = objective.positions();
64 if (before.size() != interior_dof_) return false;
65 packInteriors(before);
66 eon_relax_outcome_t out{};
67 const int rc = client_->step(band_, surface_, user_, out);
68 if (rc != EON_RELAX_OK) return false;
69 Eigen::VectorXd after = unpackInteriors();
70 if (!after.allFinite() || after.size() != before.size()) {
71 packInteriors(before);
72 client_->reset();
73 return false;
74 }
75 if (max_move > 0.0) {
76 const Eigen::VectorXd limited =
77 clipAtomMaxmove(before, after, max_move);
78 if ((limited - after).cwiseAbs().maxCoeff() > 0.0) {
79 after = limited;
80 client_->reset();
81 }
82 }
83 const Eigen::VectorXd next =
84 objective.stepPositions(before, after - before);
85 if (!next.allFinite() || next.size() != before.size()) {
86 packInteriors(before);
87 client_->reset();
88 return false;
89 }
90 packInteriors(next);
91 if ((next - after).cwiseAbs().maxCoeff() > 0.0) {
92 client_->reset();
93 }
94 objective.setPositions(next);
95 if (!objective.refresh(false)) {
96 packInteriors(before);
97 objective.setPositions(before);
98 client_->reset();
99 return false;
100 }
101 ++iteration_;
102 last_status_ = out.status;
103 last_max_force_ = out.max_force;
104 return true;
105 }
106
107 void rebuildSketchedH0(
108 const std::function<Eigen::VectorXd(const Eigen::VectorXd&)>&,
109 int) override
110 {
111 }
112 void setCurvatureExclusion(Eigen::Index, Eigen::Index) noexcept override { }
113 [[nodiscard]] int iteration() const override
114 {
115 return iteration_;
116 }
117 [[nodiscard]] int historySize() const override
118 {
119 return 0;
120 }
121 [[nodiscard]] double lastCurvatureDot() const override
122 {
123 return 0.0;
124 }
125 [[nodiscard]] double lastCurvatureScale() const override
126 {
127 return 0.0;
128 }
129 [[nodiscard]] int lastStatus() const noexcept
130 {
131 return last_status_;
132 }
133 [[nodiscard]] double lastMaxForce() const noexcept
134 {
135 return last_max_force_;
136 }
137
138private:
139 void packInteriors(const Eigen::VectorXd& interiors)
140 {
141 const Eigen::Index stride = 3 * band_.n_atoms;
142 Eigen::Map<Eigen::VectorXd>(band_.positions + stride, interior_dof_) =
143 interiors;
144 }
145
146 [[nodiscard]] Eigen::VectorXd unpackInteriors() const
147 {
148 const Eigen::Index stride = 3 * band_.n_atoms;
149 return Eigen::Map<const Eigen::VectorXd>(band_.positions + stride,
150 interior_dof_);
151 }
152
156 [[nodiscard]] Eigen::VectorXd clipAtomMaxmove(const Eigen::VectorXd& before,
157 const Eigen::VectorXd& after,
158 double max_move) const
159 {
160 Eigen::VectorXd clipped = after;
161 const Eigen::Index n_atoms = band_.n_atoms;
162 if (n_atoms <= 0 || max_move <= 0.0) return clipped;
163 const Eigen::Index n_images = interior_dof_ / (3 * n_atoms);
164 for (Eigen::Index im = 0; im < n_images; ++im) {
165 for (Eigen::Index a = 0; a < n_atoms; ++a) {
166 const Eigen::Index off = (im * n_atoms + a) * 3;
167 const Eigen::Vector3d d =
168 after.segment<3>(off) - before.segment<3>(off);
169 const double n = d.norm();
170 if (n > max_move)
171 clipped.segment<3>(off) =
172 before.segment<3>(off) + d * (max_move / n);
173 }
174 }
175 return clipped;
176 }
177
178 EonRelaxClient* client_ = nullptr;
179 eon_relax_band_t band_{};
180 eon_relax_surface_fn surface_ = nullptr;
181 void* user_ = nullptr;
182 Eigen::Index interior_dof_ = 0;
183 int iteration_ = 0;
184 int last_status_ = 0;
185 double last_max_force_ = 0.0;
186};
187
188} // namespace detail
189} // namespace neb
190} // namespace gpr
191
192#endif
GP-accelerated NEB: AIE and OIE variants.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
eon_relax_band_t eon_relax_surface_fn surface
eon_relax_band_t * band
@ EON_RELAX_OK
int(* eon_relax_surface_fn)(void *user, eon_relax_surface_request_t *req)
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.
Definition Macros.h:16
const int32_t * atomic_nrs
const double * boxes