46 {
49 lowestEv.resize(matter->numberOfAtoms(), 3);
51
53 const int size = 3 * static_cast<int>(mobile.size());
54 if (size == 0) {
56 return;
57 }
58
59 const long maxIter =
params.davidson_options.max_iterations;
60 const double tol =
params.davidson_options.tolerance;
61 const double dr =
params.main_options.finiteDifference;
62 const bool useDiagPrec =
params.davidson_options.diagonal_preconditioner;
63
66 V.setZero();
67 HV.setZero();
68
70 double beta = r.norm();
73 return;
74 }
75 r /= beta;
76
77 auto tmpMatter = std::make_unique<Matter>(*matter);
78 const long forceCallsStart = tmpMatter->getForceCalls();
79 const AtomMatrix pos0 = matter->getPositions();
80 const VectorXd force0 =
mobileForces(tmpMatter.get(), mobile);
81
82 auto applyH = [&](const VectorXd &v) -> VectorXd {
85 tmpMatter->setPositions(pos);
86 return -(
mobileForces(tmpMatter.get(), mobile) - force0) / dr;
87 };
88
89 VectorXd diagH = VectorXd::Ones(size);
90
91 V.col(0) = r;
92 HV.col(0) = applyH(V.col(0));
93 if (useDiagPrec) {
94 for (int k = 0; k < size; ++k) {
95 const double vk = V(k, 0);
96 if (std::fabs(vk) > 1e-8) {
97 diagH(k) = std::max(std::fabs(HV(k, 0) / vk), 1e-3);
98 }
99 }
100 }
101
102 double ew = 0.0, ewOld = 0.0;
103 VectorXd evEst = V.col(0);
104 VectorXd evOldEst = evEst;
105 int subspace = 1;
106
107 for (int iter = 0; iter < maxIter; ++iter) {
110
111 MatrixXd G = V.leftCols(subspace).transpose() * HV.leftCols(subspace);
112 G = 0.5 * (G + G.transpose());
113
114 Eigen::SelfAdjointEigenSolver<MatrixXd> es(G);
115 ew = es.eigenvalues()(0);
116 VectorXd y = es.eigenvectors().col(0);
117 evEst = V.leftCols(subspace) * y;
118 evEst.normalize();
119
120 VectorXd Hx = HV.leftCols(subspace) * y;
121 VectorXd resid = Hx - ew * evEst;
122 const double residNorm = resid.norm();
123 const double ewAbsRelErr =
124 (iter == 0) ? 1.0
125 : eonc::safemath::
safe_div(std::fabs(ew - ewOld),
126 std::fabs(ewOld), 1.0);
127 ewOld = ew;
128 statsTorque = std::max(ewAbsRelErr, residNorm / (std::fabs(ew) + 1e-12));
131 evOldEst = evEst;
132
134 "[Davidson] ew={:10.6f} rel_err={:10.6f} |r|={:10.6f} "
135 "angle={:7.3f} dim={:3d} iter={:3d} n_mobile={}",
136 ew, ewAbsRelErr, residNorm,
statsAngle, subspace, iter,
137 mobile.size());
138
139 if (ewAbsRelErr < tol && residNorm < tol * (std::fabs(ew) + 1.0)) {
140 QUILL_LOG_INFO(
log,
"[Davidson] Tolerance reached: {}", tol);
141 break;
142 }
143 if (subspace >= maxIter) {
144 QUILL_LOG_ERROR(
log,
"[Davidson] Max subspace dimension");
145 break;
146 }
147
148 VectorXd t(size);
149 if (useDiagPrec) {
150 for (int k = 0; k < size; ++k) {
151 const double denom = diagH(k) - ew;
152 t(k) = resid(k) / (std::fabs(denom) > 1e-12 ? denom : 1e-12);
153 }
154 } else {
155 t = resid;
156 }
157
158 for (int c = 0; c < subspace; ++c) {
159 t -= V.col(c).dot(t) * V.col(c);
160 }
161 const double tnorm = t.norm();
162 if (tnorm < 1e-14) {
163 QUILL_LOG_ERROR(
log,
"[Davidson] Linear dependence in residual");
164 break;
165 }
166 t /= tnorm;
167
168 V.col(subspace) = t;
169 HV.col(subspace) = applyH(t);
170 if (useDiagPrec) {
171 for (int k = 0; k < size; ++k) {
172 const double vk = t(k);
173 if (std::fabs(vk) > 1e-8) {
174 const double d = std::fabs(HV(k, subspace) / vk);
175 diagH(k) = std::max(diagH(k), std::max(d, 1e-3));
176 }
177 }
178 }
179 ++subspace;
180
181 if (iter >= maxIter - 1) {
182 QUILL_LOG_ERROR(
log,
"[Davidson] Max iterations");
183 break;
184 }
185 }
186
190 if (evEst.size() == size) {
192 }
193}
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...