64 const Eigen::VectorXd& reactant_positions,
65 const Eigen::VectorXd& product_positions,
66 const std::vector<int>& reactant_types,
67 const std::vector<int>& product_types,
double kmax_factor)
72 const int nat =
static_cast<int>(reactant_types.size());
73 if (
static_cast<int>(product_types.size()) != nat ||
74 reactant_positions.size() != 3 * nat ||
75 product_positions.size() != 3 * nat || nat == 0) {
82 std::vector<int> typ1(reactant_types), typ2(product_types);
83 std::vector<double> c1(reactant_positions.data(),
84 reactant_positions.data() + 3 * nat);
85 std::vector<double> c2(product_positions.data(),
86 product_positions.data() + 3 * nat);
91 std::vector<int> cand1(
static_cast<size_t>(nat), 0);
92 std::vector<int> cand2(
static_cast<size_t>(nat), 0);
96 std::vector<double> rmat_buf(9, 0.0);
97 std::vector<double> tr_buf(3, 0.0);
98 std::vector<int> perm_buf(
static_cast<size_t>(nat), 0);
101 double* rmat = rmat_buf.data();
102 double* tr = tr_buf.data();
103 int* perm = perm_buf.data();
105 libira_match(nat, typ1.data(), c1.data(), cand1.data(), nat, typ2.data(),
106 c2.data(), cand2.data(), kmax_factor, &rmat, &tr, &perm, &hd,
115 std::vector<double> dists_buf(
static_cast<size_t>(nat), 0.0);
116 double* dists = dists_buf.data();
118 libira_cshda_svd(nat, typ1.data(), c1.data(), nat, typ2.data(),
119 c2.data(), 999.9, 1, &perm,
120 &dists, &rmat, &tr, &svd_err);
124 for (
int i = 0; i < nat; ++i)
125 hd = std::max(hd, dists[i]);
128 if (dists != dists_buf.data()) std::free(dists);
137 for (
int i = 0; i < nat; ++i) {
138 const int p = res.
permutation[
static_cast<size_t>(i)];
139 if (p < 0 || p >= nat ||
140 typ2[
static_cast<size_t>(p)] != typ1[
static_cast<size_t>(i)]) {
147 if (res.
error == 0) {
149 for (
int k = 0; k < 9; ++k)
150 res.
rotation[
static_cast<size_t>(k)] = rmat[k];
151 for (
int d = 0; d < 3; ++d)
160 bool perm_identity =
true;
161 for (
int i = 0; i < nat; ++i) {
162 if (res.
permutation[
static_cast<size_t>(i)] != i) {
163 perm_identity =
false;
168 for (
int i = 0; i < 3; ++i) {
169 for (
int j = 0; j < 3; ++j) {
170 const double ident = (i == j) ? 1.0 : 0.0;
171 dev = std::max(dev, std::fabs(rmat[3 * i + j] - ident));
173 dev = std::max(dev, std::fabs(tr[i]));
176 if (perm_identity && dev < 1e-9) {
182 for (
int i = 0; i < nat; ++i) {
183 const int p = res.
permutation[
static_cast<size_t>(i)];
184 for (
int d = 0; d < 3; ++d) {
185 res.
aligned[3 * i + d] = rmat[3 * d + 0] * c2[3 * p + 0] +
186 rmat[3 * d + 1] * c2[3 * p + 1] +
187 rmat[3 * d + 2] * c2[3 * p + 2] +
197 if (rmat != rmat_buf.data()) std::free(rmat);
198 if (tr != tr_buf.data()) std::free(tr);
199 if (perm != perm_buf.data()) std::free(perm);
201 (void)reactant_positions;
202 (void)product_positions;
203 (void)reactant_types;
212 const Eigen::VectorXd& reactant_positions,
213 const Eigen::VectorXd& product_positions,
214 const std::vector<int>& reactant_types,
215 const std::vector<int>& product_types)
218 const Eigen::Index n = reactant_positions.size();
219 const int nat =
static_cast<int>(n / 3);
220 if (n == 0 || n != product_positions.size() || n % 3 != 0 ||
221 static_cast<int>(reactant_types.size()) != nat ||
222 static_cast<int>(product_types.size()) != nat) {
236 const Eigen::VectorXd aligned_id =
239 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> react(
240 reactant_positions.data(), 3, nat);
241 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> prod_id(
242 aligned_id.data(), 3, nat);
247 Eigen::MatrixXd cost(nat, nat);
248 double max_dist = 0.0;
249 for (
int i = 0; i < nat; ++i)
250 for (
int j = 0; j < nat; ++j)
252 std::max(max_dist, (react.col(i) - prod_id.col(j)).norm());
253 const double type_penalty = (max_dist + 1.0) * (nat + 1);
254 for (
int i = 0; i < nat; ++i) {
255 for (
int j = 0; j < nat; ++j) {
256 double d = (react.col(i) - prod_id.col(j)).norm();
257 if (reactant_types[i] != product_types[j]) d += type_penalty;
263 if (
static_cast<int>(perm.size()) != nat) {
267 for (
int i = 0; i < nat; ++i) {
268 if (perm[
static_cast<size_t>(i)] < 0 ||
269 perm[
static_cast<size_t>(i)] >= nat ||
270 product_types[
static_cast<size_t>(perm[
static_cast<size_t>(i)])] !=
271 reactant_types[
static_cast<size_t>(i)]) {
279 bool perm_identity =
true;
280 for (
int i = 0; i < nat; ++i)
281 if (perm[
static_cast<size_t>(i)] != i) {
282 perm_identity =
false;
285 if (!perm_identity) {
300 Eigen::VectorXd prod_swapped(n);
301 for (
int i = 0; i < nat; ++i)
302 for (
int d = 0; d < 3; ++d)
303 prod_swapped[3 * i + d] =
304 product_positions[3 * perm[
static_cast<size_t>(i)] + d];
305 const BondSet react_bonds = covalentBonds(react, reactant_types);
306 const BondSet identity_bonds = covalentBonds(
307 PosMap(product_positions.data(), 3, nat), product_types);
308 const BondSet assigned_bonds =
309 covalentBonds(PosMap(prod_swapped.data(), 3, nat), reactant_types);
311 bondChangeCount(react_bonds, identity_bonds);
313 bondChangeCount(react_bonds, assigned_bonds);
314 BondSet permuted_react_bonds;
315 for (
const auto& bond: react_bonds) {
316 const int a = perm[
static_cast<size_t>(bond.first)];
317 const int b = perm[
static_cast<size_t>(bond.second)];
318 permuted_react_bonds.insert({std::min(a, b), std::max(a, b)});
320 const bool endpoint_symmetry = assigned_bonds == identity_bonds ||
321 permuted_react_bonds == react_bonds;
322 if (product_types == reactant_types && !endpoint_symmetry) {
324 perm_identity =
true;
332 res.
method =
"kabsch_identity";
334 (aligned_id - product_positions).cwiseAbs().maxCoeff();
341 for (
int i = 0; i < nat; ++i)
342 hd = std::max(hd, (react.col(i) - prod_id.col(i)).norm());
349 Eigen::VectorXd prod_perm(n);
350 for (
int i = 0; i < nat; ++i)
351 for (
int d = 0; d < 3; ++d)
352 prod_perm[3 * i + d] =
353 product_positions[3 * perm[
static_cast<size_t>(i)] + d];
356 res.
method =
"kabsch_hungarian";
357 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> pa(
360 for (
int i = 0; i < nat; ++i)
361 hd = std::max(hd, (react.col(i) - pa.col(i)).norm());
368 const Eigen::VectorXd& reactant_positions,
369 const Eigen::VectorXd& product_positions,
const std::array<double, 9>& cell)
371 const Eigen::Index n = reactant_positions.size();
372 if (n == 0 || n != product_positions.size() || n % 3 != 0 ||
374 return product_positions;
376 const int nat =
static_cast<int>(n / 3);
378 Eigen::VectorXd unwrapped(n);
379 for (
int i = 0; i < nat; ++i) {
380 const Eigen::Vector3d from = reactant_positions.segment<3>(3 * i);
381 const Eigen::Vector3d to = product_positions.segment<3>(3 * i);
382 unwrapped.segment<3>(3 * i) = from + box.
displacement(from, to);
388 const Eigen::VectorXd& reactant_positions,
389 const Eigen::VectorXd& product_positions,
390 const std::vector<int>& reactant_types,
391 const std::vector<int>& product_types,
const std::array<double, 9>& cell)
394 const Eigen::Index n = reactant_positions.size();
395 const int nat =
static_cast<int>(n / 3);
396 if (n == 0 || n != product_positions.size() || n % 3 != 0 ||
397 static_cast<int>(reactant_types.size()) != nat ||
398 static_cast<int>(product_types.size()) != nat ||
405 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> react(
406 reactant_positions.data(), 3, nat);
407 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> prod(
408 product_positions.data(), 3, nat);
410 auto mic_norm = [&](
int i,
int j) {
411 return box.
displacement(react.col(i), prod.col(j)).norm();
413 Eigen::MatrixXd cost(nat, nat);
414 double max_dist = 0.0;
415 for (
int i = 0; i < nat; ++i)
416 for (
int j = 0; j < nat; ++j)
417 max_dist = std::max(max_dist, mic_norm(i, j));
418 const double type_penalty = (max_dist + 1.0) * (nat + 1);
419 for (
int i = 0; i < nat; ++i) {
420 for (
int j = 0; j < nat; ++j) {
421 double d = mic_norm(i, j);
422 if (reactant_types[
static_cast<std::size_t
>(i)] !=
423 product_types[
static_cast<std::size_t
>(j)])
429 if (
static_cast<int>(perm.size()) != nat) {
433 for (
int i = 0; i < nat; ++i) {
434 const int p = perm[
static_cast<std::size_t
>(i)];
435 if (p < 0 || p >= nat ||
436 product_types[
static_cast<std::size_t
>(p)] !=
437 reactant_types[
static_cast<std::size_t
>(i)]) {
443 res.
method =
"periodic_min_image";
446 bool perm_identity =
true;
447 for (
int i = 0; i < nat; ++i)
448 if (perm[
static_cast<std::size_t
>(i)] != i) {
449 perm_identity =
false;
453 auto bonds_of = [&](
const Eigen::VectorXd& flat,
454 const std::vector<int>& types) {
456 Eigen::Map<const Eigen::Matrix<double, 3, Eigen::Dynamic>> P(
457 flat.data(), 3, nat);
458 for (
int i = 0; i < nat; ++i) {
459 for (
int j = i + 1; j < nat; ++j) {
460 const double cutoff = kCovalentBondTolerance *
462 types[
static_cast<std::size_t
>(i)]) +
464 types[
static_cast<std::size_t
>(j)]));
465 if (box.
displacement(P.col(i), P.col(j)).norm() < cutoff)
472 if (!perm_identity) {
473 Eigen::VectorXd prod_swapped(n);
474 for (
int i = 0; i < nat; ++i)
475 prod_swapped.segment<3>(3 * i) = product_positions.segment<3>(
476 3 * perm[
static_cast<std::size_t
>(i)]);
477 const BondSet react_bonds =
478 bonds_of(reactant_positions, reactant_types);
479 const BondSet identity_bonds =
480 bonds_of(product_positions, product_types);
481 const BondSet assigned_bonds = bonds_of(prod_swapped, reactant_types);
483 bondChangeCount(react_bonds, identity_bonds);
485 bondChangeCount(react_bonds, assigned_bonds);
486 BondSet permuted_react_bonds;
487 for (
const auto& bond: react_bonds) {
488 const int a = perm[
static_cast<std::size_t
>(bond.first)];
489 const int b = perm[
static_cast<std::size_t
>(bond.second)];
490 permuted_react_bonds.insert({std::min(a, b), std::max(a, b)});
492 const bool endpoint_symmetry = assigned_bonds == identity_bonds ||
493 permuted_react_bonds == react_bonds;
494 if (product_types == reactant_types && !endpoint_symmetry) {
496 perm_identity =
true;
501 Eigen::VectorXd chosen = product_positions;
502 if (!perm_identity) {
503 for (
int i = 0; i < nat; ++i)
504 chosen.segment<3>(3 * i) = product_positions.segment<3>(
507 const Eigen::VectorXd unwrapped =
510 for (
int i = 0; i < nat; ++i)
511 hd = std::max(hd, (react.col(i) - unwrapped.segment<3>(3 * i)).norm());
514 (unwrapped - product_positions).cwiseAbs().maxCoeff() < 1e-9) {