Loading...
Searching...
No Matches
SphereRotation.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_DIMER_SPHERE_ROTATION_H_
6#define GPR_DIMER_SPHERE_ROTATION_H_
7
8#include <rgmin.h>
9
10#include <Eigen/Dense>
11#include <cmath>
12#include <functional>
13#include <memory>
14
16#include "../ml/RgminAbi.h"
17
18namespace dimer {
19
24public:
25 using Evaluator = std::function<bool(gpr::Observation&)>;
26
27 bool start(const gpr::Coord& center, const Eigen::VectorXd& gradient,
28 double separation, const Eigen::MatrixXd& physical,
29 std::size_t memory, const gpr::Observation& endpoint,
30 const gpr::Observation* previous = nullptr)
31 {
32 center_ = center;
33 gradient_ = gradient;
34 physical_ = physical;
35 separation_ = separation;
36 if (!valid(endpoint) || physical.cols() == 0) return false;
37 reference_ = endpoint;
38 accepted_ = endpoint;
39 accepted_axis_.resize(0);
40 accepted_orientation_.resize(0, 0);
41 rgmin_control_t control{};
42 control.maxiter = 1;
43 control.gtol = 0.0;
44 control.istep = 1.0;
45 control.memory = memory;
47 solver_.reset(
48 rgmin_solver_create(RGMIN_LBFGS, &control, physical.cols()));
49 if (!solver_) return false;
50 rgmin_solver_set_manifold(solver_.get(), RGMIN_MANIFOLD_SPHERE);
51 rgmin_solver_set_accept(solver_.get(), RGMIN_ACCEPT_ENERGY);
52 if (previous && valid(*previous)) {
53 const Eigen::VectorXd old_axis =
54 (physical_.transpose() *
55 (previous->R - center_).row(0).transpose())
56 .normalized();
57 const Eigen::VectorXd axis =
58 (physical_.transpose() *
59 (endpoint.R - center_).row(0).transpose())
60 .normalized();
61 Eigen::VectorXd old_gradient =
62 physical_.transpose() *
63 (previous->G.row(0).transpose() - gradient_) / separation_;
64 Eigen::VectorXd new_gradient =
65 physical_.transpose() *
66 (endpoint.G.row(0).transpose() - gradient_) / separation_;
67 old_gradient -= old_axis.dot(old_gradient) * old_axis;
68 new_gradient -= axis.dot(new_gradient) * axis;
69 Eigen::VectorXd displacement = axis - old_axis;
70 displacement -= axis.dot(displacement) * axis;
71 const Eigen::VectorXd change =
72 new_gradient - old_gradient + axis.dot(old_gradient) * axis;
73 rgmin_solver_push_pair(solver_.get(), displacement.data(),
74 change.data(), axis.size());
75 }
76 return true;
77 }
78
79 bool step(const gpr::Coord& center, const gpr::Coord& orientation,
80 const Eigen::VectorXd& gradient, double separation,
81 const Eigen::MatrixXd& physical, std::size_t memory,
82 const Evaluator& evaluate,
83 const gpr::Observation* initial_endpoint, gpr::Coord& rotated,
84 gpr::Observation& endpoint)
85 {
86 if (physical.rows() != center.cols() || physical.cols() == 0 ||
87 !physical.allFinite())
88 return false;
89 const bool same_problem =
90 solver_ && separation_ == separation && equal(center_, center) &&
91 equal(gradient_, gradient) && equal(physical_, physical);
92 Eigen::VectorXd axis;
93 if (same_problem && equal(orientation, accepted_orientation_)) {
94 // The accepted reduced coordinate belongs to the solver state.
95 // A Cartesian round trip must not introduce an external secant.
96 axis = accepted_axis_;
97 } else {
98 axis = physical.transpose() * orientation.row(0).transpose();
99 const double norm = axis.norm();
100 if (!(norm > 0.0) || !std::isfinite(norm)) return false;
101 axis /= norm;
102 }
103 if (!same_problem) {
104 center_ = center;
105 gradient_ = gradient;
106 physical_ = physical;
107 separation_ = separation;
108 gpr::Observation seed;
109 seed.R = geometry(axis);
110 if (initial_endpoint && initial_endpoint->R.isApprox(seed.R)) {
111 seed = *initial_endpoint;
112 } else if (!evaluate(seed)) {
113 return false;
114 }
115 if (!start(center, gradient, separation, physical, memory, seed))
116 return false;
117 }
118
119 struct Context {
120 SphereRotation& rotation;
121 const Evaluator& evaluate;
122 gpr::Observation last;
123 bool failed = false;
124 } context{*this, evaluate, accepted_};
125 const auto callback = [](void* user, const DLManagedTensorVersioned* x,
126 double* value,
127 DLManagedTensorVersioned* g) noexcept {
128 auto& ctx = *static_cast<Context*>(user);
129 auto& state = ctx.rotation;
130 try {
131 const auto* xp = reinterpret_cast<const double*>(
132 static_cast<const uint8_t*>(x->dl_tensor.data) +
133 x->dl_tensor.byte_offset);
134 auto* gp = reinterpret_cast<double*>(
135 static_cast<uint8_t*>(g->dl_tensor.data) +
136 g->dl_tensor.byte_offset);
137 const Eigen::Map<const Eigen::VectorXd> u(
138 xp, state.physical_.cols());
139 gpr::Observation image;
140 image.R = state.geometry(u);
141 if (equal(image.R, state.accepted_.R)) {
142 image = state.accepted_;
143 } else if (!ctx.evaluate(image)) {
144 ctx.failed = true;
145 return RGMIN_INTERNAL_ERROR;
146 }
147 if (!state.valid(image)) {
148 ctx.failed = true;
149 return RGMIN_INTERNAL_ERROR;
150 }
151 const double linear =
152 state.gradient_.dot((image.R - state.reference_.R).row(0));
153 *value = (image.E(0, 0) - state.reference_.E(0, 0) - linear) /
154 (state.separation_ * state.separation_);
155 Eigen::Map<Eigen::VectorXd> output(gp, state.physical_.cols());
156 output = state.physical_.transpose() *
157 (image.G.row(0).transpose() - state.gradient_) /
158 state.separation_;
159 ctx.last = std::move(image);
160 return RGMIN_SUCCESS;
161 } catch (...) {
162 ctx.failed = true;
163 return RGMIN_INTERNAL_ERROR;
164 }
165 };
166 using Tensor = std::unique_ptr<DLManagedTensorVersioned,
167 decltype(&rgmin_tensor_free)>;
168 Tensor tensor(rgmin_tensor_borrow_cpu_f64(axis.data(), axis.size()),
169 rgmin_tensor_free);
170 rgmin_report_t report{};
171 if (!tensor ||
172 rgmin_solver_step_fg(solver_.get(), callback, &context,
173 tensor.get(), &report) != RGMIN_SUCCESS ||
174 context.failed || !axis.allFinite())
175 return false;
176 const gpr::Coord position = geometry(axis);
177 if (equal(position, context.last.R)) {
178 endpoint = std::move(context.last);
179 } else if (equal(position, accepted_.R)) {
180 endpoint = accepted_;
181 } else {
182 endpoint.R = position;
183 if (!evaluate(endpoint) || !valid(endpoint)) return false;
184 }
185 rotated = (physical_ * axis).transpose();
186 accepted_axis_ = axis;
187 accepted_orientation_ = rotated;
188 accepted_ = endpoint;
189 return true;
190 }
191
192private:
193 template <class A, class B>
194 static bool equal(const A& a, const B& b)
195 {
196 return a.rows() == b.rows() && a.cols() == b.cols() &&
197 (a.array() == b.array()).all();
198 }
199
200 gpr::Coord geometry(const Eigen::VectorXd& axis) const
201 {
202 return center_ + separation_ * (physical_ * axis).transpose();
203 }
204
205 bool valid(const gpr::Observation& image) const
206 {
207 return image.G.rows() == 1 && image.G.cols() == center_.cols() &&
208 image.G.allFinite() && image.E.rows() == 1 &&
209 image.E.cols() == 1 && image.E.allFinite();
210 }
211
212 std::unique_ptr<rgmin_solver_t, decltype(&rgmin_solver_free)> solver_{
213 nullptr, rgmin_solver_free};
214 gpr::Coord center_;
215 Eigen::VectorXd gradient_;
216 Eigen::MatrixXd physical_;
217 double separation_ = 0.0;
218 gpr::Observation reference_;
219 gpr::Observation accepted_;
220 Eigen::VectorXd accepted_axis_;
221 gpr::Coord accepted_orientation_;
222};
223
224} // namespace dimer
225#endif
@ x
Definition Enums.h:28
bool step(const gpr::Coord &center, const gpr::Coord &orientation, const Eigen::VectorXd &gradient, double separation, const Eigen::MatrixXd &physical, std::size_t memory, const Evaluator &evaluate, const gpr::Observation *initial_endpoint, gpr::Coord &rotated, gpr::Observation &endpoint)
std::function< bool(gpr::Observation &)> Evaluator
bool start(const gpr::Coord &center, const Eigen::VectorXd &gradient, double separation, const Eigen::MatrixXd &physical, std::size_t memory, const gpr::Observation &endpoint, const gpr::Observation *previous=nullptr)
eon_relax_band_t eon_relax_surface_fn void * user
void requireCompatibleLibrary()
Definition RgminAbi.h:90
FieldMatrixd Coord
Definition Macros.h:35
Observation data: energy, gradient and positions.
FieldMatrixd E