Loading...
Searching...
No Matches
GeometryAnalysis.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
13#include "eon/EonLogger.h"
14#include "eon/ExactReduction.h"
15#include "eon/Matter.h"
16#include "eon/SafeMath.h"
17
18#include <cmath>
19#include <set>
20#include <vector>
21
23 const AtomMatrix r2) {
25
26 // Determine optimal rotation
27 // Horn, J. Opt. Soc. Am. A, 1987
28 Matrix3d m = r1.transpose() * r2;
29
30 double sxx = m(0, 0);
31 double sxy = m(0, 1);
32 double sxz = m(0, 2);
33 double syx = m(1, 0);
34 double syy = m(1, 1);
35 double syz = m(1, 2);
36 double szx = m(2, 0);
37 double szy = m(2, 1);
38 double szz = m(2, 2);
39
40 Matrix4d n;
41 n.setZero();
42 n(0, 1) = syz - szy;
43 n(0, 2) = szx - sxz;
44 n(0, 3) = sxy - syx;
45
46 n(1, 2) = sxy + syx;
47 n(1, 3) = szx + sxz;
48
49 n(2, 3) = syz + szy;
50
51 n += n.transpose().eval();
52
53 n(0, 0) = sxx + syy + szz;
54 n(1, 1) = sxx - syy - szz;
55 n(2, 2) = -sxx + syy - szz;
56 n(3, 3) = -sxx - syy + szz;
57
58 Eigen::SelfAdjointEigenSolver<Matrix4d> es(n);
59 Eigen::Vector4d maxv = es.eigenvectors().col(3);
60
61 double aa = maxv[0] * maxv[0];
62 double bb = maxv[1] * maxv[1];
63 double cc = maxv[2] * maxv[2];
64 double dd = maxv[3] * maxv[3];
65 double ab = maxv[0] * maxv[1];
66 double ac = maxv[0] * maxv[2];
67 double ad = maxv[0] * maxv[3];
68 double bc = maxv[1] * maxv[2];
69 double bd = maxv[1] * maxv[3];
70 double cd = maxv[2] * maxv[3];
71
72 R(0, 0) = aa + bb - cc - dd;
73 R(0, 1) = 2 * (bc - ad);
74 R(0, 2) = 2 * (bd + ac);
75 R(1, 0) = 2 * (bc + ad);
76 R(1, 1) = aa - bb + cc - dd;
77 R(1, 2) = 2 * (cd - ab);
78 R(2, 0) = 2 * (bd - ac);
79 R(2, 1) = 2 * (cd + ab);
80 R(2, 2) = aa - bb - cc + dd;
81
82 return R;
83}
84
86 const double max_diff) {
87 AtomMatrix r1 = m1.getPositions();
88 AtomMatrix r2 = m2.getPositions();
89
90 // Align centroids
91 Eigen::VectorXd c1(3);
92 Eigen::VectorXd c2(3);
93
94 c1[0] = r1.col(0).sum();
95 c1[1] = r1.col(1).sum();
96 c1[2] = r1.col(2).sum();
97 c2[0] = r2.col(0).sum();
98 c2[1] = r2.col(1).sum();
99 c2[2] = r2.col(2).sum();
100 c1 /= r1.rows();
101 c2 /= r2.rows();
102
103 for (int i = 0; i < r1.rows(); i++) {
104 r1(i, 0) -= c1[0];
105 r1(i, 1) -= c1[1];
106 r1(i, 2) -= c1[2];
107
108 r2(i, 0) -= c2[0];
109 r2(i, 1) -= c2[1];
110 r2(i, 2) -= c2[2];
111 }
112
114
115 // Eigen is transposed relative to numpy
116 r2 = r2 * R;
117
118 for (int i = 0; i < r1.rows(); i++) {
119 double diff = (r2.row(i) - r1.row(i)).norm();
120 if (diff > max_diff) {
121 return false;
122 }
123 }
124 return true;
125}
126
127// Project out rigid-body translation and rotation from a displacement vector.
128// Uses infinitesimal-rotation basis vectors and modified Gram-Schmidt
129// orthonormalization, following the approach in:
130// R. Goswami and H. Jonsson, "Adaptive Pruning for Increased Robustness and
131// Reduced Computational Overhead in Gaussian Process Accelerated Saddle Point
132// Searches," ChemPhysChem, Nov. 2025, doi: 10.1002/cphc.202500730.
133void eonc::geometry::projectOutRotTrans(Eigen::VectorXd &step,
134 const AtomMatrix &positions) {
135 long nAtoms = positions.rows();
136 long dof = nAtoms * 3;
137
138 // 1. Compute center of mass (unweighted geometric center)
139 Eigen::Vector3d com = Eigen::Vector3d::Zero();
140 for (long i = 0; i < nAtoms; ++i) {
141 com(0) += positions(i, 0);
142 com(1) += positions(i, 1);
143 com(2) += positions(i, 2);
144 }
145 com /= static_cast<double>(nAtoms);
146
147 // 2. Construct 6 basis vectors for rigid-body translation and rotation
148 std::vector<Eigen::VectorXd> basis;
149 basis.reserve(6);
150
151 // Translational basis vectors
152 for (int d = 0; d < 3; ++d) {
153 Eigen::VectorXd t = Eigen::VectorXd::Zero(dof);
154 for (long j = 0; j < nAtoms; ++j) {
155 t(3 * j + d) = 1.0;
156 }
157 basis.push_back(t);
158 }
159
160 // Rotational basis vectors (infinitesimal rotations around COM)
161 Eigen::VectorXd rx = Eigen::VectorXd::Zero(dof);
162 Eigen::VectorXd ry = Eigen::VectorXd::Zero(dof);
163 Eigen::VectorXd rz = Eigen::VectorXd::Zero(dof);
164
165 for (long i = 0; i < nAtoms; ++i) {
166 double x = positions(i, 0) - com(0);
167 double y = positions(i, 1) - com(1);
168 double z = positions(i, 2) - com(2);
169 // Rotation around x-axis: cross(xhat, r) = (0, -z, y)
170 rx(3 * i + 1) = -z;
171 rx(3 * i + 2) = y;
172 // Rotation around y-axis: cross(yhat, r) = (z, 0, -x)
173 ry(3 * i + 0) = z;
174 ry(3 * i + 2) = -x;
175 // Rotation around z-axis: cross(zhat, r) = (-y, x, 0)
176 rz(3 * i + 0) = -y;
177 rz(3 * i + 1) = x;
178 }
179 basis.push_back(rx);
180 basis.push_back(ry);
181 basis.push_back(rz);
182
183 // 3. Modified Gram-Schmidt orthonormalization
184 std::vector<Eigen::VectorXd> ortho;
185 ortho.reserve(6);
186
187 for (auto &v : basis) {
188 Eigen::VectorXd u = v;
189 for (const auto &e : ortho) {
190 u -= u.dot(e) * e;
191 }
192 // Handles linear molecules where one rotational mode is degenerate
193 if (u.norm() > 1e-9) {
194 u.normalize();
195 ortho.push_back(u);
196 }
197 }
198
199 // 4. Project out rigid-body components from step
200 for (const auto &e : ortho) {
201 step -= step.dot(e) * e;
202 }
203}
204
206 std::shared_ptr<Matter> m2) {
207 // Skip for extended systems (slabs/surfaces with frozen atoms).
208 // Rigid-body rotation and translation are not well-defined when the
209 // system is anchored by frozen atoms.
210 if (m2->numberOfFixedAtoms() > 0) {
211 return;
212 }
213
214 AtomMatrix r2 = m2->getPositions();
215 long n = r1_passed.size();
216
217 // Compute displacement as flat 3N vector
218 Eigen::VectorXd step(n);
219 Eigen::Map<const Eigen::VectorXd> r1_flat(r1_passed.data(), n);
220 Eigen::Map<const Eigen::VectorXd> r2_flat(r2.data(), n);
221 step = r2_flat - r1_flat;
222
223 // Project out rigid-body translation and rotation
224 projectOutRotTrans(step, r1_passed);
225
226 // Reconstruct positions: r1 + projected step
227 Eigen::VectorXd result = r1_flat + step;
228 AtomMatrix resultMat(r1_passed.rows(), 3);
229 Eigen::Map<Eigen::VectorXd>(resultMat.data(), n) = result;
230
231 m2->setPositions(resultMat);
232}
233
234void eonc::geometry::rotationRemove(const std::shared_ptr<Matter> m1,
235 std::shared_ptr<Matter> m2) {
236 AtomMatrix r1 = m1->getPositions();
237 rotationRemove(r1, m2);
238 return;
239}
240
242 AtomMatrix r1 = m1.getPositions();
243 AtomMatrix r2 = r2_passed;
244
245 // net displacement
246 Eigen::VectorXd disp(3);
247 AtomMatrix r12 = m1.pbc(r2 - r1);
248
249 disp[0] = r12.col(0).sum();
250 disp[1] = r12.col(1).sum();
251 disp[2] = r12.col(2).sum();
252 disp /= r1.rows();
253
254 for (int i = 0; i < r1.rows(); i++) {
255 r1(i, 0) += disp[0];
256 r1(i, 1) += disp[1];
257 r1(i, 2) += disp[2];
258 }
259
260 m1.setPositions(r1);
261 return;
262}
263
265 AtomMatrix r2 = m2.getPositions();
266 translationRemove(m1, r2);
267 return;
268}
269
271 double max = 0.0;
272 for (Eigen::Index i = 0; i < v1.rows(); ++i) {
273 max = std::max(max, eonc::exact::orderedNorm(v1.row(i)));
274 }
275 return max;
276}
277
278double eonc::geometry::maxAtomMotionV(const VectorXd v1) {
279 double max = 0.0;
280 long n = v1.rows();
281 if (n < 3) {
282 // Vector too short for 3D atom grouping; treat as single displacement
283 return eonc::exact::norm(v1);
284 }
285 for (long i = 0; i + 3 <= n; i += 3) {
286 double norm = eonc::exact::orderedNorm(v1.segment<3>(i));
287 if (max < norm) {
288 max = norm;
289 }
290 }
291 // Handle trailing elements (vector size not multiple of 3)
292 long rem = n % 3;
293 if (rem > 0) {
294 double norm = eonc::exact::orderedNorm(v1.tail(rem));
295 if (max < norm) {
296 max = norm;
297 }
298 }
299 return max;
300}
301
302long eonc::geometry::numAtomsMoved(const AtomMatrix v1, double cutoff) {
303 long num = 0;
304 for (int i = 0; i < v1.rows(); i++) {
305 double norm = v1.row(i).norm();
306 if (norm >= cutoff) {
307 num += 1;
308 }
309 }
310 return num;
311}
312
314 double maxMotion) {
315 AtomMatrix v2(v1);
316
317 double max = maxAtomMotion(v1);
318 if (max > maxMotion) {
319 v2 *= maxMotion / max;
320 }
321 return v2;
322}
323
324VectorXd eonc::geometry::maxAtomMotionAppliedV(const VectorXd v1,
325 double maxMotion) {
326 VectorXd v2(v1);
327
328 double max = maxAtomMotionV(v1);
329 if (max > maxMotion) {
330 v2 *= maxMotion / max;
331 }
332 return v2;
333}
334
336 double maxMotion) {
337 AtomMatrix v2(v1);
338
339 double max = eonc::exact::norm(v1);
340 if (max > maxMotion) {
341 v2 *= maxMotion / max;
342 }
343 return v2;
344}
345
346VectorXd eonc::geometry::maxMotionAppliedV(const VectorXd v1,
347 double maxMotion) {
348 VectorXd v2(v1);
349
350 double max = eonc::exact::norm(v1);
351 if (max > maxMotion) {
352 v2 *= maxMotion / max;
353 }
354 return v2;
355}
356
357namespace eonc::geometry {
358struct atom {
359 double r;
360 int z;
361};
362
363struct by_atom {
364 bool operator()(atom const &a, atom const &b) const {
365 if (a.z != b.z) {
366 return a.z < b.z;
367 } else {
368 return a.r < b.r;
369 }
370 }
371};
372
373namespace {
374
375bool pairInside(const Matter &m1, const Matter &m2, const AtomMatrix &r1,
376 const AtomMatrix &r2, int left, int right, double tolerance) {
377 return std::fabs((m1.pbc(r1.row(left) - r2.row(right))).norm()) < tolerance &&
378 m1.getAtomicNr(left) == m2.getAtomicNr(right);
379}
380
381// Augmenting path for one left atom. Index-aligned pairs are not reserved.
382bool identicalAugment(int left, const std::vector<std::vector<int>> &adj,
383 std::vector<int> &matchRight, std::vector<char> &seen) {
384 for (int right : adj[static_cast<size_t>(left)]) {
385 if (seen[static_cast<size_t>(right)] != 0) {
386 continue;
387 }
388 seen[static_cast<size_t>(right)] = 1;
389 const int taken = matchRight[static_cast<size_t>(right)];
390 if (taken < 0 || identicalAugment(taken, adj, matchRight, seen)) {
391 matchRight[static_cast<size_t>(right)] = left;
392 return true;
393 }
394 }
395 return false;
396}
397
398} // namespace
399
400} // namespace eonc::geometry
401
402bool eonc::geometry::identical(const Matter &m1, const Matter &m2,
403 const double distanceDifference) {
404 AtomMatrix r1 = m1.getPositions();
405 AtomMatrix r2 = m2.getPositions();
406 if (r1.rows() != r2.rows()) {
407 return false;
408 }
409 const int nAtoms = static_cast<int>(r1.rows());
410 const double tolerance = distanceDifference;
411
412 bool indexAligned = true;
413 for (int i = 0; i < nAtoms; i++) {
414 if (!pairInside(m1, m2, r1, r2, i, i, tolerance)) {
415 indexAligned = false;
416 break;
417 }
418 }
419 if (indexAligned) {
420 return true;
421 }
422
423 std::vector<std::vector<int>> adj(static_cast<size_t>(nAtoms));
424 for (int left = 0; left < nAtoms; left++) {
425 for (int right = 0; right < nAtoms; right++) {
426 if (pairInside(m1, m2, r1, r2, left, right, tolerance)) {
427 adj[static_cast<size_t>(left)].push_back(right);
428 }
429 }
430 }
431
432 std::vector<int> matchRight(static_cast<size_t>(nAtoms), -1);
433 int matched = 0;
434 for (int left = 0; left < nAtoms; left++) {
435 std::vector<char> seen(static_cast<size_t>(nAtoms), 0);
436 if (identicalAugment(left, adj, matchRight, seen)) {
437 matched++;
438 }
439 }
440 return matched == nAtoms;
441}
442
443bool eonc::geometry::sortedR(const Matter &m1, const Matter &m2,
444 const double distanceDifference) {
445 EONC_LOG_INFO("In sortedR");
446 AtomMatrix r1 = m1.getPositions();
447 AtomMatrix r2 = m2.getPositions();
448 double tolerance = distanceDifference;
449 int matches = 0;
450
451 if (r1.rows() != r2.rows()) {
452 return false;
453 }
454
455 // Allocate memory for rdf1 and rdf2
456 std::vector<std::set<atom, by_atom>> rdf1(r1.rows());
457 std::vector<std::set<atom, by_atom>> rdf2(r2.rows());
458
459 for (int i2 = 0; i2 < r2.rows(); i2++) {
460 rdf2[i2].clear();
461 for (int j2 = 0; j2 < r2.rows(); j2++) {
462 if (j2 == i2)
463 continue;
464 atom a2;
465 a2.r = m2.distance(i2, j2);
466 a2.z = m2.getAtomicNr(j2);
467 rdf2[i2].insert(a2);
468 rdf2[j2].insert(a2);
469 }
470 }
471
472 for (int i1 = 0; i1 < r1.rows(); i1++) {
473 if (matches == i1 - 2) {
474 return false;
475 }
476 for (int j1 = 0; j1 < r1.rows(); j1++) {
477 if (j1 == i1)
478 continue;
479 atom a;
480 a.r = m1.distance(i1, j1);
481 a.z = m1.getAtomicNr(j1);
482 rdf1[i1].insert(a);
483 rdf1[j1].insert(a);
484 }
485 for (int x = 0; x < r2.rows(); x++) {
486 auto it2 = rdf2[x].begin();
487 auto it = rdf1[i1].begin();
488 int counter = 0;
489 // Shell length is the neighbor set, which is not the atom count.
490 for (; it != rdf1[i1].end() && it2 != rdf2[x].end(); ++it, ++it2) {
491 atom k1 = *it;
492 atom k2 = *it2;
493 if (std::fabs(k1.r - k2.r) < tolerance && k1.z == k2.z) {
494 counter++;
495 } else {
496 EONC_LOG_INFO("No match");
497 break;
498 }
499 }
500 if (static_cast<size_t>(counter) == rdf1[i1].size() &&
501 it2 == rdf2[x].end()) {
502 matches++;
503 } else {
504 EONC_LOG_INFO("No match");
505 }
506 }
507 }
508
509 return matches >= r1.rows();
510}
511
512void eonc::geometry::pushApart(std::shared_ptr<Matter> m1, double minDistance) {
513 if (minDistance <= 0)
514 return;
515
516 AtomMatrix r1 = m1->getPositions();
517 AtomMatrix Force(r1.rows(), 3);
518 double f = 0.025;
519 double cut = minDistance;
520 double pushAparts = 500;
521 Force.setZero();
522 for (int count = 0; count < pushAparts; count++) {
523 int moved = 0;
524 for (int i = 0; i < r1.rows(); i++) {
525 for (int j = i + 1; j < r1.rows(); j++) {
526 // distance() is the minimum-image length. The step has to use that
527 // same vector: the raw Cartesian separation is the long box diagonal
528 // when the close pair straddles a periodic face.
529 AtomMatrix delta(1, 3);
530 delta.row(0) = r1.row(i) - r1.row(j);
531 delta = m1->pbc(delta);
532 const double d = eonc::exact::orderedNorm(delta);
533 if (!(d < cut)) {
534 continue;
535 }
536 moved++;
537 // A zero minimum-image vector has no direction. Take the finite
538 // step a unit separation would, along +x, instead of dividing.
539 if (!(d > eonc::safemath::eps)) {
540 Force(i, 0) += f;
541 Force(j, 0) -= f;
542 continue;
543 }
544 for (int axis = 0; axis <= 2; axis++) {
545 const double component = f * delta(0, axis) / d;
546 Force(i, axis) += component;
547 Force(j, axis) -= component;
548 }
549 }
550 }
551 if (moved == 0)
552 break;
553 r1 += Force;
554 Force.setZero();
555 m1->setPositions(r1);
556 // setPositions wraps when periodic boundaries are on. The next pass
557 // has to see those wrapped coordinates, not the pre-wrap copy.
558 r1 = m1->getPositions();
559 }
560}
Eigen::Matrix< double, 3, 3, eOnStorageOrder > RotationMatrix
Definition Eigen.h:38
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Definition Eigen.h:35
Eigen::Matrix< double, 4, 4, eOnStorageOrder > Matrix4d
Definition Eigen.h:36
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
#define EONC_LOG_INFO(...)
Definition EonLogger.h:316
const AtomMatrix & getPositions() const
Definition Matter.cpp:378
void setPositions(const AtomMatrix &pos)
Definition Matter.cpp:420
AtomMatrix pbc(const AtomMatrix &diff) const
Definition Matter.cpp:943
double distance(long index1, long index2) const
Definition Matter.cpp:518
long getAtomicNr(long int atom) const
Definition Matter.cpp:565
double norm(const Eigen::MatrixBase< A > &x)
sqrt of the correctly rounded squared norm; IEEE sqrt is itself correctly rounded,...
double orderedNorm(const Eigen::MatrixBase< A > &x)
sqrt of orderedDot(x, x): reproducible, not correctly rounded.
RotationMatrix rotationExtract(const AtomMatrix r1, const AtomMatrix r2)
AtomMatrix maxAtomMotionApplied(const AtomMatrix v1, double maxMotion)
long numAtomsMoved(const AtomMatrix v1, double cutoff)
double maxAtomMotion(const AtomMatrix v1)
void pushApart(std::shared_ptr< Matter > m1, double minDistance)
bool rotationMatch(const Matter &m1, const Matter &m2, const double max_diff)
VectorXd maxMotionAppliedV(const VectorXd v1, double maxMotion)
void projectOutRotTrans(Eigen::VectorXd &step, const AtomMatrix &positions)
bool identical(const Matter &m1, const Matter &m2, const double distanceDifference)
VectorXd maxAtomMotionAppliedV(const VectorXd v1, double maxMotion)
double maxAtomMotionV(const VectorXd v1)
bool sortedR(const Matter &m1, const Matter &m2, const double distanceDifference)
void rotationRemove(const AtomMatrix r1, std::shared_ptr< Matter > m2)
AtomMatrix maxMotionApplied(const AtomMatrix v1, double maxMotion)
void translationRemove(Matter &m1, const AtomMatrix r1)
constexpr double eps
Definition SafeMath.h:19
bool operator()(atom const &a, atom const &b) const