Same as compute(matter, dir) but with an explicit mobile atom list (intersected with free flags).
Eigenvector rows outside the list are 0.
47 {
50 lowestEv.resize(matter->numberOfAtoms(), 3);
52
54 const int size = 3 * static_cast<int>(mobile.size());
55 if (size == 0) {
57 return;
58 }
59
60 const long maxIters =
params.lanczos_options.max_iterations;
61 MatrixXd T(size, maxIters), Q(size, maxIters);
62 T.setZero();
64
65 double alpha, beta = r.norm();
68 return;
69 }
70 double ew = 0, ewOld = 0, ewAbsRelErr;
71 const double dr =
params.main_options.finiteDifference;
72 VectorXd evEst, evT, evOldEst;
73
74 auto tmpMatter = std::make_unique<Matter>(*matter);
75 const long forceCallsStart = tmpMatter->getForceCalls();
76 const AtomMatrix pos0 = matter->getPositions();
77 const VectorXd force0 =
mobileForces(tmpMatter.get(), mobile);
78
79 auto applyH = [&](const VectorXd &v) -> VectorXd {
82 tmpMatter->setPositions(pos);
83 return -(
mobileForces(tmpMatter.get(), mobile) - force0) / dr;
84 };
85
86 for (int i = 0; i < size; i++) {
88 Q.col(i) = r / beta;
89
90 u = applyH(Q.col(i));
91
92 if (i == 0) {
93 r = u;
94 } else {
95 r = u - beta * Q.col(i - 1);
96 }
97 alpha = Q.col(i).dot(r);
98 r = r - alpha * Q.col(i);
99
100 T(i, i) = alpha;
101 if (i > 0) {
102 T(i - 1, i) = beta;
103 T(i, i - 1) = beta;
104 }
105
106 beta = r.norm();
107
108 if (beta <= 1e-10 * std::fabs(alpha)) {
109 if (i == 0) {
110 ew = alpha;
111 evEst = Q.col(0);
112 }
113 QUILL_LOG_ERROR(
log,
"[ILanczos] ERROR: linear dependence");
114 break;
115 }
116 if (i >= 1) {
117 Eigen::SelfAdjointEigenSolver<MatrixXd> es(T.block(0, 0, i + 1, i + 1));
118 ew = es.eigenvalues()(0);
119 evT = es.eigenvectors().col(0);
121 std::fabs(ewOld), 1.0);
122 ewOld = ew;
123
124 evEst = Q.block(0, 0, size, i + 1) * evT;
125 evEst.normalize();
129 evOldEst = evEst;
131 "[ILanczos] {:9s} {:9s} {:10s} {:14s} {:9.4f} "
132 "{:10.6f} {:7.3f} {:5} n_mobile={}",
133 "----", "----", "----", "----", ew, ewAbsRelErr,
135 if (ewAbsRelErr <
params.lanczos_options.tolerance) {
136 QUILL_LOG_INFO(
log,
"[ILanczos] Tolerance reached: {}",
137 params.lanczos_options.tolerance);
138 break;
139 }
140 } else {
141 ew = alpha;
142 ewOld = ew;
143 evEst = Q.col(0);
144 evOldEst = Q.col(0);
147 double Cnew = u.dot(Q.col(i));
149 std::fabs(Cprev), 1.0);
150 if (ewAbsRelErr <=
params.lanczos_options.tolerance) {
153 QUILL_LOG_INFO(
log,
"[ILanczos] Tolerance reached: {}",
154 params.lanczos_options.tolerance);
155 break;
156 }
157 }
158 }
159
160 if (i >=
params.lanczos_options.max_iterations - 1) {
161 QUILL_LOG_ERROR(
log,
"[ILanczos] Max iterations");
162 break;
163 }
164 }
165
168
170 if (evEst.size() == size) {
172 }
173}
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, eOnStorageOrder > MatrixXd
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
constexpr double safe_div(double num, double denom, double fallback=0.0)
double safe_acos(double x)
void unpackMobileRows(const VectorXd &packed, const VectorXi &mobile, AtomMatrix &full)
Write a 3*n_mobile vector into full AtomMatrix rows (other rows unchanged).
VectorXd packMobileRows(const AtomMatrix &full, const VectorXi &mobile)
Pack full (n_atoms,3) rows of mobile atoms into a 3*n_mobile vector.
VectorXd mobileForces(Matter *matter, const VectorXi &mobile)
Force components on mobile atoms after Matter has a valid force cache (calls getForces under the hood...