Build an orthonormal basis for rigid-body translations and rotations.
This function constructs a basis for the 3 translational and 3 rotational degrees of freedom. It then orthonormalizes that basis with classical Gram-Schmidt, dropping any vector whose residual norm is at most 1e-9 (linear molecules lose one rotation). Periodic systems use translations only.
31{
34
35 if (num_atoms == 0 || R.rows() != 1 || R.cols() % 3 != 0) {
36 return Eigen::MatrixXd(R.cols(), 0);
37 }
38
39
41 for (
Index_t i = 0; i < num_atoms; ++i) {
43 }
44 com /= static_cast<double>(num_atoms);
45
46
47 std::vector<Eigen::VectorXd> basis;
48 basis.reserve(6);
49
50
51 for (int i = 0; i < 3; ++i) {
52 Eigen::VectorXd t = Eigen::VectorXd::Zero(dof);
53 for (
Index_t j = 0; j < num_atoms; ++j) {
54 t(3 * j + i) = 1.0;
55 }
56 basis.push_back(t);
57 }
58
59
60 Eigen::VectorXd r_x = Eigen::VectorXd::Zero(dof);
61 Eigen::VectorXd r_y = Eigen::VectorXd::Zero(dof);
62 Eigen::VectorXd r_z = Eigen::VectorXd::Zero(dof);
63
64 for (
Index_t i = 0; i < num_atoms; ++i) {
66
67 r_x(3 * i + 1) = -pos(2);
68 r_x(3 * i + 2) = pos(1);
69
70 r_y(3 * i + 0) = pos(2);
71 r_y(3 * i + 2) = -pos(0);
72
73 r_z(3 * i + 0) = -pos(1);
74 r_z(3 * i + 1) = pos(0);
75 }
76 if (rotations) {
77 basis.push_back(r_x);
78 basis.push_back(r_y);
79 basis.push_back(r_z);
80 }
81
82
83
84
85
86
87
88
89 double extent = 0.0;
90 for (
Index_t i = 0; i < num_atoms; ++i)
92 const double resolution = std::numeric_limits<double>::epsilon() *
93 static_cast<double>(dof) *
94 std::max(std::sqrt(extent), 1.0);
95 std::vector<Eigen::VectorXd> ortho_basis;
96 ortho_basis.reserve(6);
97
98 for (auto& v: basis) {
99 Eigen::VectorXd u = v;
100 for (int pass = 0; pass < 2; ++pass)
101 for (const auto& ou: ortho_basis) {
102 u -= u.dot(ou) * ou;
103 }
104 if (u.norm() > resolution) {
105 u.normalize();
106 ortho_basis.push_back(u);
107 }
108 }
109
110 Eigen::MatrixXd result(dof, static_cast<Eigen::Index>(ortho_basis.size()));
111 for (Eigen::Index i = 0; i < result.cols(); ++i)
112 result.col(i) = ortho_basis[static_cast<std::size_t>(i)];
113 return result;
114}
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
Eigen::Vector3d vector3_reg