36 Eigen::Matrix3d
orthogonal = Eigen::Matrix3d::Identity();
43 [[nodiscard]]
bool empty() const noexcept
56 [[nodiscard]] Eigen::VectorXd
mapPositions(
const Eigen::VectorXd&
x)
const
58 return mapVector(
x,
true);
61 [[nodiscard]] Eigen::VectorXd
mapGradient(
const Eigen::VectorXd& g)
const
63 return mapVector(g,
false);
67 [[nodiscard]] Eigen::VectorXd mapVector(
const Eigen::VectorXd& v,
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 "
75 Eigen::VectorXd
out(v.size());
76 for (Eigen::Index a = 0; a < nat; ++a) {
77 const Eigen::Index source =
79 Eigen::Vector3d mapped =
orthogonal * v.segment<3>(3 * source);
81 out.segment<3>(3 * a) = mapped;
120 const std::vector<int>& permutation,
const Eigen::VectorXd& reactant,
121 const Eigen::VectorXd& product,
const std::vector<int>& atomic_numbers)
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)] !=
141 throw std::invalid_argument(
142 "endpoint symmetry: the permutation exchanges unlike atoms");
143 identity = identity &&
static_cast<std::size_t
>(source) == a;
146 throw std::invalid_argument(
147 "endpoint symmetry: the identity permutation exchanges nothing");
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));
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();
168 target_centroid - symmetry.
orthogonal * source_centroid;
170 symmetry.
mapPositions(reactant), product, atomic_numbers);
172 symmetry.
mapPositions(product), reactant, atomic_numbers);
180 const std::vector<int>& permutation,
const Eigen::VectorXd& reactant,
181 const Eigen::VectorXd& product,
const std::vector<int>& atomic_numbers,
185 permutation, reactant, product, atomic_numbers);
188 std::ostringstream message;
189 message <<
"endpoint symmetry: the operation maps the reactant "
192 <<
" A from the other endpoint, beyond the trust radius "
194 throw std::invalid_argument(message.str());
EndpointSymmetry requireEndpointSymmetry(const std::vector< int > &permutation, const Eigen::VectorXd &reactant, const Eigen::VectorXd &product, const std::vector< int > &atomic_numbers, double radius)