22#ifndef GPR_AUXILIARY_LEBEDEV_LAIKOV_H_
23#define GPR_AUXILIARY_LEBEDEV_LAIKOV_H_
45 double z,
double weight)
47 std::array<double, 3> coords{
x,
y,
z};
48 std::sort(coords.begin(), coords.end());
49 std::set<std::array<long, 3>> seen;
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;
61 }
while (std::next_permutation(coords.begin(), coords.end()));
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);
72 }
else if (order == 14) {
76 }
else if (order == 26) {
82 throw std::invalid_argument(
83 "Lebedev-Laikov order must be 6, 14, or 26");
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;
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);
126 Eigen::Matrix3d
rotation = Eigen::Matrix3d::Identity();
134inline bool permutationIsIdentity(
const std::vector<int>& permutation,
int nat)
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;
143inline OracleOrientation kabschOrientation(
const Eigen::VectorXd& reference,
144 const Eigen::VectorXd& positions)
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
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(),
175 mapped = (rotation * prod_c).colwise() + react_centroid;
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));
184 dev = std::max(dev, std::fabs(translation[i]));
186 if (dev < 1.0e-9)
return out;
188 out.rotation = rotation;
189 out.translation = translation;
190 out.passthrough =
false;
205 const Eigen::VectorXd& positions,
const Eigen::VectorXd& reference,
206 const std::vector<int>& types,
bool align,
double kmax_factor)
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()) {
220 detail::permutationIsIdentity(matched.
permutation, nat) &&
221 matched.
aligned.size() == positions.size()) {
223 out.passthrough =
false;
224 for (
int i = 0; i < 3; ++i) {
225 for (
int j = 0; j < 3; ++j) {
227 matched.
rotation[
static_cast<size_t>(3 * i + j)];
233 return detail::kabschOrientation(reference, positions);
237 const Eigen::Matrix3d& rotation)
239 if (!forces || n_atoms <= 0)
return;
240 Eigen::Map<Eigen::Matrix<double, 3, Eigen::Dynamic>> mapped(forces, 3,
242 const Eigen::Matrix<double, 3, Eigen::Dynamic> pulled =
243 rotation.transpose() * mapped;
252 Eval eval,
double* energy,
double* forces,
253 double* energy_noise,
double* force_noise)
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;
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;
272 Eigen::Map<Eigen::Matrix<double, 3, Eigen::Dynamic>>
out(
273 rotated.data(), 3, n_atoms);
275 (rotation * (cloud.colwise() - centroid)).colwise() + centroid;
276 sample = rotated.data();
278 double sample_energy = 0.0;
279 double sample_energy_noise = 0.0;
280 double sample_force_noise = 0.0;
282 eval(sample, force_i.data(), &sample_energy, &sample_energy_noise,
283 &sample_force_noise);
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;
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;
296 if (energy) *energy = energy_acc;
297 if (energy_noise) *energy_noise = energy_noise_acc;
298 if (force_noise) *force_noise = force_noise_acc;
300 Eigen::Map<Eigen::VectorXd>(forces, n3) = force_acc;
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)
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.
std::array< double, 3 > translation
std::vector< int > permutation
std::array< double, 9 > rotation
row-major R
bool applied
libira_match succeeded (error == 0)
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