Loading...
Searching...
No Matches
EndpointSymmetry.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_ENDPOINTSYMMETRY_H_
6#define GPR_NEB_ENDPOINTSYMMETRY_H_
7
8#include <Eigen/Dense>
9#include <algorithm>
10#include <cmath>
11#include <cstddef>
12#include <limits>
13#include <map>
14#include <sstream>
15#include <stdexcept>
16#include <vector>
17
18namespace gpr::neb {
19
35 std::vector<int> permutation;
36 Eigen::Matrix3d orthogonal = Eigen::Matrix3d::Identity();
37 Eigen::Vector3d translation = Eigen::Vector3d::Zero();
39 double forward_residual = std::numeric_limits<double>::quiet_NaN();
41 double reverse_residual = std::numeric_limits<double>::quiet_NaN();
42
43 [[nodiscard]] bool empty() const noexcept
44 {
45 return permutation.empty();
46 }
47
51 [[nodiscard]] double resolution() const noexcept
52 {
53 return std::max(forward_residual, reverse_residual);
54 }
55
56 [[nodiscard]] Eigen::VectorXd mapPositions(const Eigen::VectorXd& x) const
57 {
58 return mapVector(x, true);
59 }
60
61 [[nodiscard]] Eigen::VectorXd mapGradient(const Eigen::VectorXd& g) const
62 {
63 return mapVector(g, false);
64 }
65
66private:
67 [[nodiscard]] Eigen::VectorXd mapVector(const Eigen::VectorXd& v,
68 bool translate) const
69 {
70 const auto nat = static_cast<Eigen::Index>(permutation.size());
71 if (v.size() != 3 * nat)
72 throw std::invalid_argument(
73 "EndpointSymmetry: vector length does not match the "
74 "permutation");
75 Eigen::VectorXd out(v.size());
76 for (Eigen::Index a = 0; a < nat; ++a) {
77 const Eigen::Index source =
78 permutation[static_cast<std::size_t>(a)];
79 Eigen::Vector3d mapped = orthogonal * v.segment<3>(3 * source);
80 if (translate) mapped += translation;
81 out.segment<3>(3 * a) = mapped;
82 }
83 return out;
84 }
85};
86
87namespace detail {
88
92inline double labelledElementDisplacement(
93 const Eigen::VectorXd& a, const Eigen::VectorXd& b,
94 const std::vector<int>& atomic_numbers)
95{
96 std::map<int, std::pair<double, int>> per_element;
97 for (std::size_t atom = 0; atom < atomic_numbers.size(); ++atom) {
98 const auto row = static_cast<Eigen::Index>(3 * atom);
99 auto& slot = per_element[atomic_numbers[atom]];
100 slot.first += (a.segment<3>(row) - b.segment<3>(row)).norm();
101 ++slot.second;
102 }
103 double worst = 0.0;
104 for (const auto& [element, slot]: per_element) {
105 (void)element;
106 worst = std::max(worst, slot.first / slot.second);
107 }
108 return worst;
109}
110
111} // namespace detail
112
120 const std::vector<int>& permutation, const Eigen::VectorXd& reactant,
121 const Eigen::VectorXd& product, const std::vector<int>& atomic_numbers)
122{
123 const std::size_t nat = atomic_numbers.size();
124 if (nat < 2 || permutation.size() != nat ||
125 reactant.size() != static_cast<Eigen::Index>(3 * nat) ||
126 product.size() != reactant.size())
127 throw std::invalid_argument(
128 "endpoint symmetry: the permutation, the atomic numbers and both "
129 "endpoints must describe the same atoms");
130 std::vector<char> seen(nat, 0);
131 bool identity = true;
132 for (std::size_t a = 0; a < nat; ++a) {
133 const int source = permutation[a];
134 if (source < 0 || static_cast<std::size_t>(source) >= nat ||
135 seen[static_cast<std::size_t>(source)])
136 throw std::invalid_argument(
137 "endpoint symmetry: the permutation is not a bijection");
138 seen[static_cast<std::size_t>(source)] = 1;
139 if (atomic_numbers[static_cast<std::size_t>(source)] !=
140 atomic_numbers[a])
141 throw std::invalid_argument(
142 "endpoint symmetry: the permutation exchanges unlike atoms");
143 identity = identity && static_cast<std::size_t>(source) == a;
144 }
145 if (identity)
146 throw std::invalid_argument(
147 "endpoint symmetry: the identity permutation exchanges nothing");
148
149 EndpointSymmetry symmetry;
150 symmetry.permutation = permutation;
151 Eigen::Matrix<double, 3, Eigen::Dynamic> source(3, nat);
152 Eigen::Matrix<double, 3, Eigen::Dynamic> target(3, nat);
153 for (std::size_t a = 0; a < nat; ++a) {
154 source.col(static_cast<Eigen::Index>(a)) =
155 reactant.segment<3>(3 * permutation[a]);
156 target.col(static_cast<Eigen::Index>(a)) =
157 product.segment<3>(static_cast<Eigen::Index>(3 * a));
158 }
159 const Eigen::Vector3d source_centroid = source.rowwise().mean();
160 const Eigen::Vector3d target_centroid = target.rowwise().mean();
161 const Eigen::Matrix3d covariance =
162 (source.colwise() - source_centroid) *
163 (target.colwise() - target_centroid).transpose();
164 const Eigen::JacobiSVD<Eigen::Matrix3d> svd(
165 covariance, Eigen::ComputeFullU | Eigen::ComputeFullV);
166 symmetry.orthogonal = svd.matrixV() * svd.matrixU().transpose();
167 symmetry.translation =
168 target_centroid - symmetry.orthogonal * source_centroid;
169 symmetry.forward_residual = detail::labelledElementDisplacement(
170 symmetry.mapPositions(reactant), product, atomic_numbers);
171 symmetry.reverse_residual = detail::labelledElementDisplacement(
172 symmetry.mapPositions(product), reactant, atomic_numbers);
173 return symmetry;
174}
175
180 const std::vector<int>& permutation, const Eigen::VectorXd& reactant,
181 const Eigen::VectorXd& product, const std::vector<int>& atomic_numbers,
182 double radius)
183{
185 permutation, reactant, product, atomic_numbers);
186 if (!(symmetry.forward_residual <= radius) ||
187 !(symmetry.reverse_residual <= radius)) {
188 std::ostringstream message;
189 message << "endpoint symmetry: the operation maps the reactant "
190 << symmetry.forward_residual << " A and the product "
191 << symmetry.reverse_residual
192 << " A from the other endpoint, beyond the trust radius "
193 << radius << " A";
194 throw std::invalid_argument(message.str());
195 }
196 return symmetry;
197}
198
199} // namespace gpr::neb
200
201#endif // GPR_NEB_ENDPOINTSYMMETRY_H_
@ x
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
EndpointSymmetry requireEndpointSymmetry(const std::vector< int > &permutation, const Eigen::VectorXd &reactant, const Eigen::VectorXd &product, const std::vector< int > &atomic_numbers, double radius)
EndpointSymmetry endpointSymmetryFromPermutation(const std::vector< int > &permutation, const Eigen::VectorXd &reactant, const Eigen::VectorXd &product, const std::vector< int > &atomic_numbers)
double reverse_residual
Labelled displacement between the mapped product and the reactant.
bool empty() const noexcept
Eigen::VectorXd mapGradient(const Eigen::VectorXd &g) const
double resolution() const noexcept
std::vector< int > permutation
Eigen::VectorXd mapPositions(const Eigen::VectorXd &x) const
double forward_residual
Labelled displacement between the mapped reactant and the product.