Loading...
Searching...
No Matches
LebedevLaikov.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// Lebedev–Laikov nodes on S^2, used here as an opt-in SO(3) averaging
6// set, plus a rigid IRA frame applied immediately before an oracle
7// energy evaluation. Both are off unless the caller asks.
8//
9// The 6, 14, and 26 point rules are the algebraic orders 3, 5, and 7
10// from V. I. Lebedev, "Quadratures on a sphere", USSR Computational
11// Mathematics and Mathematical Physics 16 (1976) 10–24. Weights below
12// are the published orbit weights divided by 4π, so they sum to 1.
13// V. I. Lebedev and D. N. Laikov, Doklady Mathematics 59 (1999) 477–481,
14// extends the same family to high order; these three rules are not
15// taken from a PET-MAD table.
16//
17// Each node n is lifted to one proper rotation taking e_z onto n
18// (Rodrigues in span(e_z, n); the south pole is a half-turn about e_x).
19// That is a section of SO(3) → S^2, not the metatomic rotation-count
20// orbit and not a Haar product rule.
21
22#ifndef GPR_AUXILIARY_LEBEDEV_LAIKOV_H_
23#define GPR_AUXILIARY_LEBEDEV_LAIKOV_H_
24
25#include <Eigen/Dense>
26#include <algorithm>
27#include <array>
28#include <cmath>
29#include <set>
30#include <stdexcept>
31#include <vector>
32
33#include "IRAAlign.h"
34
35namespace aux {
36
38 double x = 0.0;
39 double y = 0.0;
40 double z = 0.0;
41 double weight = 0.0;
42};
43
44inline void lebedevAddOrbit(std::vector<LebedevNode>& nodes, double x, double y,
45 double z, double weight)
46{
47 std::array<double, 3> coords{x, y, z};
48 std::sort(coords.begin(), coords.end());
49 std::set<std::array<long, 3>> seen;
50 do {
51 for (int signs = 0; signs < 8; ++signs) {
52 const double sx = (signs & 1) ? -coords[0] : coords[0];
53 const double sy = (signs & 2) ? -coords[1] : coords[1];
54 const double sz = (signs & 4) ? -coords[2] : coords[2];
55 const std::array<long, 3> key{std::lround(sx * 1.0e8),
56 std::lround(sy * 1.0e8),
57 std::lround(sz * 1.0e8)};
58 if (!seen.insert(key).second) continue;
59 nodes.push_back(LebedevNode{sx, sy, sz, weight});
60 }
61 } while (std::next_permutation(coords.begin(), coords.end()));
62}
63
65inline std::vector<LebedevNode> lebedevLaikovGrid(int order)
66{
67 std::vector<LebedevNode> nodes;
68 const double inv_sqrt2 = 1.0 / std::sqrt(2.0);
69 const double inv_sqrt3 = 1.0 / std::sqrt(3.0);
70 if (order == 6) {
71 lebedevAddOrbit(nodes, 1.0, 0.0, 0.0, 1.0 / 6.0);
72 } else if (order == 14) {
73 // 6·(1/15) + 8·(3/40) = 1.
74 lebedevAddOrbit(nodes, 1.0, 0.0, 0.0, 1.0 / 15.0);
75 lebedevAddOrbit(nodes, inv_sqrt3, inv_sqrt3, inv_sqrt3, 3.0 / 40.0);
76 } else if (order == 26) {
77 // 6·(1/21) + 12·(4/105) + 8·(9/280) = 1.
78 lebedevAddOrbit(nodes, 1.0, 0.0, 0.0, 1.0 / 21.0);
79 lebedevAddOrbit(nodes, inv_sqrt2, inv_sqrt2, 0.0, 4.0 / 105.0);
80 lebedevAddOrbit(nodes, inv_sqrt3, inv_sqrt3, inv_sqrt3, 9.0 / 280.0);
81 } else {
82 throw std::invalid_argument(
83 "Lebedev-Laikov order must be 6, 14, or 26");
84 }
85 return nodes;
86}
87
88inline double lebedevWeightSum(int order)
89{
90 double sum = 0.0;
91 for (const LebedevNode& node: lebedevLaikovGrid(order))
92 sum += node.weight;
93 return sum;
94}
95
97inline Eigen::Matrix3d lebedevNodeRotation(const LebedevNode& node)
98{
99 const Eigen::Vector3d n(node.x, node.y, node.z);
100 const Eigen::Vector3d ez(0.0, 0.0, 1.0);
101 const double cosine = n.dot(ez);
102 if (cosine > 1.0 - 1.0e-14) return Eigen::Matrix3d::Identity();
103 if (cosine < -1.0 + 1.0e-14) {
104 Eigen::Matrix3d half_turn = Eigen::Matrix3d::Identity();
105 half_turn(1, 1) = -1.0;
106 half_turn(2, 2) = -1.0;
107 return half_turn;
108 }
109 const Eigen::Vector3d crossed = ez.cross(n);
110 const double sine = crossed.norm();
111 const Eigen::Vector3d axis = crossed / sine;
112 Eigen::Matrix3d skew = Eigen::Matrix3d::Zero();
113 skew(0, 1) = -axis.z();
114 skew(0, 2) = axis.y();
115 skew(1, 0) = axis.z();
116 skew(1, 2) = -axis.x();
117 skew(2, 0) = -axis.y();
118 skew(2, 1) = axis.x();
119 return Eigen::Matrix3d::Identity() + skew * sine +
120 skew * skew * (1.0 - cosine);
121}
122
124 Eigen::VectorXd positions;
126 Eigen::Matrix3d rotation = Eigen::Matrix3d::Identity();
127 Eigen::Vector3d translation = Eigen::Vector3d::Zero();
129 bool passthrough = true;
130};
131
132namespace detail {
133
134inline bool permutationIsIdentity(const std::vector<int>& permutation, int nat)
135{
136 if (static_cast<int>(permutation.size()) != nat) return false;
137 for (int i = 0; i < nat; ++i) {
138 if (permutation[static_cast<size_t>(i)] != i) return false;
139 }
140 return true;
141}
142
143inline OracleOrientation kabschOrientation(const Eigen::VectorXd& reference,
144 const Eigen::VectorXd& positions)
145{
146 OracleOrientation out;
147 out.positions = positions;
148 const Eigen::Index n = positions.size();
149 if (n == 0 || n != reference.size() || n % 3 != 0) return out;
150 const int nat = static_cast<int>(n / 3);
151 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> react(
152 reference.data(), 3, nat);
153 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> prod(
154 positions.data(), 3, nat);
155 const Eigen::Vector3d react_centroid = react.rowwise().mean();
156 const Eigen::Vector3d prod_centroid = prod.rowwise().mean();
157 const Eigen::Matrix<double, 3, Eigen::Dynamic> react_c =
158 react.colwise() - react_centroid;
159 const Eigen::Matrix<double, 3, Eigen::Dynamic> prod_c =
160 prod.colwise() - prod_centroid;
161 const Eigen::Matrix3d covariance = prod_c * react_c.transpose();
162 Eigen::JacobiSVD<Eigen::Matrix3d> svd(
163 covariance, Eigen::ComputeFullU | Eigen::ComputeFullV);
164 Eigen::Matrix3d d = Eigen::Matrix3d::Identity();
165 d(2, 2) = (svd.matrixV() * svd.matrixU().transpose()).determinant() < 0.0
166 ? -1.0
167 : 1.0;
168 const Eigen::Matrix3d rotation =
169 svd.matrixV() * d * svd.matrixU().transpose();
170 const Eigen::Vector3d translation =
171 react_centroid - rotation * prod_centroid;
172 Eigen::VectorXd aligned(n);
173 Eigen::Map<Eigen::Matrix<double, 3, Eigen::Dynamic>> mapped(aligned.data(),
174 3, nat);
175 mapped = (rotation * prod_c).colwise() + react_centroid;
176 double dev = 0.0;
177 for (Eigen::Index i = 0; i < n; ++i)
178 dev = std::max(dev, std::fabs(aligned[i] - positions[i]));
179 for (int i = 0; i < 3; ++i) {
180 for (int j = 0; j < 3; ++j) {
181 const double ident = (i == j) ? 1.0 : 0.0;
182 dev = std::max(dev, std::fabs(rotation(i, j) - ident));
183 }
184 dev = std::max(dev, std::fabs(translation[i]));
185 }
186 if (dev < 1.0e-9) return out;
187 out.positions = std::move(aligned);
188 out.rotation = rotation;
189 out.translation = translation;
190 out.passthrough = false;
191 return out;
192}
193
194} // namespace detail
195
205 const Eigen::VectorXd& positions, const Eigen::VectorXd& reference,
206 const std::vector<int>& types, bool align, double kmax_factor)
207{
209 out.positions = positions;
210 if (!align) return out;
211 const int nat = static_cast<int>(types.size());
212 if (nat <= 0 || positions.size() != 3L * nat ||
213 reference.size() != positions.size()) {
214 return out;
215 }
216 const EndpointAlignResult matched =
217 alignProductToReactant(reference, positions, types, types, kmax_factor);
218 if (matched.applied && matched.identity) return out;
219 if (matched.applied &&
220 detail::permutationIsIdentity(matched.permutation, nat) &&
221 matched.aligned.size() == positions.size()) {
222 out.positions = matched.aligned;
223 out.passthrough = false;
224 for (int i = 0; i < 3; ++i) {
225 for (int j = 0; j < 3; ++j) {
226 out.rotation(i, j) =
227 matched.rotation[static_cast<size_t>(3 * i + j)];
228 }
229 out.translation[i] = matched.translation[static_cast<size_t>(i)];
230 }
231 return out;
232 }
233 return detail::kabschOrientation(reference, positions);
234}
235
236inline void pullForcesToCallerFrame(double* forces, long n_atoms,
237 const Eigen::Matrix3d& rotation)
238{
239 if (!forces || n_atoms <= 0) return;
240 Eigen::Map<Eigen::Matrix<double, 3, Eigen::Dynamic>> mapped(forces, 3,
241 n_atoms);
242 const Eigen::Matrix<double, 3, Eigen::Dynamic> pulled =
243 rotation.transpose() * mapped;
244 mapped = pulled;
245}
246
250template <class Eval>
251inline void averageOverLebedev(long n_atoms, const double* positions, int order,
252 Eval eval, double* energy, double* forces,
253 double* energy_noise, double* force_noise)
254{
255 const std::vector<LebedevNode> nodes = lebedevLaikovGrid(order);
256 const Eigen::Index n3 = 3 * n_atoms;
257 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> cloud(
258 positions, 3, n_atoms);
259 const Eigen::Vector3d centroid = cloud.rowwise().mean();
260 Eigen::VectorXd force_acc = Eigen::VectorXd::Zero(n3);
261 Eigen::VectorXd force_i(n3);
262 Eigen::VectorXd rotated(n3);
263 double energy_acc = 0.0;
264 double energy_noise_acc = 0.0;
265 double force_noise_acc = 0.0;
266 for (const LebedevNode& node: nodes) {
267 const bool north = std::fabs(node.x) < 1.0e-15 &&
268 std::fabs(node.y) < 1.0e-15 && node.z > 0.0;
269 const double* sample = positions;
270 const Eigen::Matrix3d rotation = lebedevNodeRotation(node);
271 if (!north) {
272 Eigen::Map<Eigen::Matrix<double, 3, Eigen::Dynamic>> out(
273 rotated.data(), 3, n_atoms);
274 out =
275 (rotation * (cloud.colwise() - centroid)).colwise() + centroid;
276 sample = rotated.data();
277 }
278 double sample_energy = 0.0;
279 double sample_energy_noise = 0.0;
280 double sample_force_noise = 0.0;
281 force_i.setZero();
282 eval(sample, force_i.data(), &sample_energy, &sample_energy_noise,
283 &sample_force_noise);
284 if (!north) {
285 Eigen::Map<Eigen::Matrix<double, 3, Eigen::Dynamic>> mapped(
286 force_i.data(), 3, n_atoms);
287 const Eigen::Matrix<double, 3, Eigen::Dynamic> pulled =
288 rotation.transpose() * mapped;
289 mapped = pulled;
290 }
291 force_acc += node.weight * force_i;
292 energy_acc += node.weight * sample_energy;
293 energy_noise_acc += node.weight * sample_energy_noise;
294 force_noise_acc += node.weight * sample_force_noise;
295 }
296 if (energy) *energy = energy_acc;
297 if (energy_noise) *energy_noise = energy_noise_acc;
298 if (force_noise) *force_noise = force_noise_acc;
299 if (forces) {
300 Eigen::Map<Eigen::VectorXd>(forces, n3) = force_acc;
301 }
302}
303
304} // namespace aux
305
306#endif // GPR_AUXILIARY_LEBEDEV_LAIKOV_H_
@ z
Definition Enums.h:28
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
void averageOverLebedev(long n_atoms, const double *positions, int order, Eval eval, double *energy, double *forces, double *energy_noise, double *force_noise)
void lebedevAddOrbit(std::vector< LebedevNode > &nodes, double x, double y, double z, double weight)
double lebedevWeightSum(int order)
OracleOrientation orientGeometryForOracle(const Eigen::VectorXd &positions, const Eigen::VectorXd &reference, const std::vector< int > &types, bool align, double kmax_factor)
EndpointAlignResult alignProductToReactant(const Eigen::VectorXd &reactant_positions, const Eigen::VectorXd &product_positions, const std::vector< int > &reactant_types, const std::vector< int > &product_types, double kmax_factor)
Definition IRAAlign.cpp:63
void pullForcesToCallerFrame(double *forces, long n_atoms, const Eigen::Matrix3d &rotation)
Eigen::Matrix3d lebedevNodeRotation(const LebedevNode &node)
Proper rotation taking e_z to the node. Identity for the north pole.
std::vector< LebedevNode > lebedevLaikovGrid(int order)
order is 6, 14, or 26. Any other value throws.
Eigen::VectorXd aligned
Definition IRAAlign.h:33
std::array< double, 3 > translation
Definition IRAAlign.h:37
std::vector< int > permutation
Definition IRAAlign.h:31
std::array< double, 9 > rotation
row-major R
Definition IRAAlign.h:35
bool applied
libira_match succeeded (error == 0)
Definition IRAAlign.h:25
Eigen::Matrix3d rotation
x_eval = rotation * x_caller + translation. Identity when passthrough.
bool passthrough
True when positions is the caller geometry, bit for bit.
Eigen::VectorXd positions
Eigen::Vector3d translation