Loading...
Searching...
No Matches
HarmonicPathCorrection.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Cholesky>
8#include <Eigen/Core>
9#include <Eigen/Eigenvalues>
10#include <algorithm>
11#include <cmath>
12#include <limits>
13#include <stdexcept>
14#include <utility>
15#include <vector>
16
17namespace gpr::quantum {
19 std::size_t node{};
20 Eigen::VectorXd direction;
21 double strength{};
22};
24 Eigen::Index positive{}, negative{}, unresolved{};
25};
26
27// The reference is the exact periodic harmonic Green function. Point
28// curvatures include their imaginary-time quadrature weights. Only the
29// update and constraint matrix is factored; the harmonic bath stays implicit.
31 using V = Eigen::VectorXd;
32 using M = Eigen::MatrixXd;
33 V omega_;
34 M modes_, directions_, smallVectors_;
35 V smallValues_, signs_;
36 std::vector<double> times_;
37 std::vector<std::size_t> nodes_;
38 std::vector<M> normals_;
39 double period_{}, logRatio_{};
40 Eigen::Index dimension_{}, codimension_{};
41 HarmonicPointInertia inertia_;
42 std::vector<M> modalGreen_;
43 static void require(bool condition, const char* message)
44 {
45 if (!condition) throw std::invalid_argument(message);
46 }
47 V green(double distance) const
48 {
49 distance = std::abs(distance);
50 V result(omega_.size());
51 for (Eigen::Index k = 0; k < omega_.size(); ++k) {
52 const double w = omega_[k];
53 result[k] = (.5 / w) *
54 (std::exp(-w * distance) +
55 std::exp(-w * (period_ - distance))) /
56 (-std::expm1(-w * period_));
57 }
58 return result;
59 }
60 V greenNodes(std::size_t first, std::size_t second) const
61 {
62 if (modalGreen_.empty()) return green(times_[first] - times_[second]);
63 V result(omega_.size());
64 for (Eigen::Index k = 0; k < omega_.size(); ++k)
65 result[k] = modalGreen_[k](first, second);
66 return result;
67 }
68
69public:
70 // modes maps the positive normal modes into mass-weighted physical
71 // coordinates; an empty matrix denotes the identity. Constraint columns
72 // use the Euclidean nodal measure and must be jointly orthonormal.
73 HarmonicPathCorrection(V frequencies, double period,
74 std::vector<double> times,
75 const std::vector<HarmonicPointCurvature>& updates,
76 const M& normal, M modes = {},
77 double unresolvedTolerance = 1e-12,
78 std::vector<M> modalGreen = {})
79 : omega_(std::move(frequencies)),
80 modes_(std::move(modes)),
81 times_(std::move(times)),
82 period_(period),
83 modalGreen_(std::move(modalGreen))
84 {
85 const auto d = omega_.size(),
86 n = static_cast<Eigen::Index>(times_.size());
87 require(
88 d > 0 && n > 0 && omega_.allFinite() &&
89 (omega_.array() > 0).all() && period_ > 0 &&
90 std::isfinite(period_) && std::isfinite(unresolvedTolerance) &&
91 unresolvedTolerance >= 0,
92 "Positive finite harmonic frequencies and thermal period required");
93 require(n <= std::numeric_limits<Eigen::Index>::max() / d,
94 "Nodal dimension exceeds range");
95 dimension_ = n * d;
96 codimension_ = normal.cols();
97 require(codimension_ <= dimension_ && normal.rows() == dimension_ &&
98 normal.allFinite(),
99 "Matching finite nodal constraint columns required");
100 if (codimension_)
101 require((normal.transpose() * normal -
102 M::Identity(codimension_, codimension_))
103 .norm() <= 1e-10,
104 "Nodal constraints must be jointly orthonormal");
105 require(!modes_.size() ||
106 (modes_.rows() == d && modes_.cols() == d &&
107 modes_.allFinite() &&
108 (modes_.transpose() * modes_ - M::Identity(d, d)).norm() <=
109 1e-10),
110 "Orthonormal harmonic normal modes required");
111 for (Eigen::Index j = 0; j < n; ++j)
112 require(std::isfinite(times_[j]) && times_[j] >= 0 &&
113 times_[j] < period_ &&
114 (!j || times_[j] > times_[j - 1]),
115 "Distinct increasing imaginary times inside one period "
116 "required");
117 require(green(0).allFinite(),
118 "Harmonic reference response exceeds numerical range");
119 require(modalGreen_.empty() ||
120 modalGreen_.size() == static_cast<std::size_t>(d),
121 "One nodal reference Green matrix per harmonic mode required");
122 for (auto& matrix: modalGreen_) {
123 require(matrix.rows() == n && matrix.cols() == n &&
124 matrix.allFinite() &&
125 (matrix - matrix.transpose()).norm() <=
126 1e-12 * std::max(1., matrix.norm()),
127 "Matching finite symmetric modal Green matrices required");
128 matrix = (.5 * matrix + .5 * matrix.transpose()).eval();
129 const Eigen::LDLT<M> factor(matrix);
130 require(factor.info() == Eigen::Success &&
131 (factor.vectorD().array() > 0).all(),
132 "Modal reference Green matrix must be positive");
133 }
134 std::vector<V> columns;
135 std::vector<double> signs;
136 Eigen::Index negativeSigns = 0;
137 for (const auto& update: updates) {
138 require(update.node < times_.size() &&
139 update.direction.size() == d &&
140 update.direction.allFinite() &&
141 std::isfinite(update.strength),
142 "Finite matching point curvature required");
143 if (update.strength == 0) continue;
144 require(update.direction.squaredNorm() > 0,
145 "A nonzero point curvature needs a resolved direction");
146 V column = modes_.size() ? V(modes_.transpose() * update.direction)
147 : update.direction;
148 column *= std::sqrt(std::abs(update.strength));
149 require(column.allFinite(),
150 "Point curvature exceeds the harmonic coordinate range");
151 nodes_.push_back(update.node);
152 columns.push_back(std::move(column));
153 signs.push_back(update.strength < 0 ? -1. : 1.);
154 negativeSigns += update.strength < 0;
155 }
156 const auto r = static_cast<Eigen::Index>(columns.size()),
157 size = r + codimension_;
158 signs_ = Eigen::Map<const V>(signs.data(), r);
159 directions_.resize(d, r);
160 for (Eigen::Index j = 0; j < r; ++j)
161 directions_.col(j) = columns[j];
162 for (Eigen::Index j = 0; j < n; ++j) {
163 // No constraint columns: the block is empty and the matrix has
164 // no storage, so middleRows at a later node would offset null.
165 M block = codimension_ == 0 ? M(d, 0) : normal.middleRows(j * d, d);
166 if (modes_.size() && codimension_)
167 block = (modes_.transpose() * block).eval();
168 normals_.push_back(std::move(block));
169 }
170 M small = M::Zero(size, size);
171 V rowScales = V::Ones(size);
172 for (Eigen::Index i = 0; i < r; ++i) {
173 for (Eigen::Index j = 0; j < r; ++j)
174 small(i, j) =
175 (directions_.col(i).array() * directions_.col(j).array() *
176 greenNodes(nodes_[i], nodes_[j]).array())
177 .sum();
178 rowScales[i] += small(i, i);
179 small(i, i) += signs[i];
180 for (Eigen::Index j = 0; j < n && codimension_; ++j)
181 small.block(i, r, 1, codimension_).noalias() +=
182 (directions_.col(i).array() *
183 greenNodes(nodes_[i], j).array())
184 .matrix()
185 .transpose() *
186 normals_[j];
187 }
188 if (codimension_) {
189 small.bottomLeftCorner(codimension_, r) =
190 small.topRightCorner(r, codimension_).transpose();
191 for (Eigen::Index i = 0; i < n; ++i)
192 for (Eigen::Index j = 0; j < n; ++j)
193 small.bottomRightCorner(codimension_, codimension_)
194 .noalias() += normals_[i].transpose() *
195 greenNodes(i, j).asDiagonal() *
196 normals_[j];
197 }
198 require(small.allFinite(), "Reduced harmonic response is nonfinite");
199 small = (.5 * small + .5 * small.transpose()).eval();
200 for (Eigen::Index j = 0; j < codimension_; ++j)
201 rowScales[r + j] = small(r + j, r + j);
202 require(rowScales.allFinite() && (rowScales.array() > 0).all(),
203 "Resolved positive reference Gram diagonals required");
204 Eigen::Index negativeSmall = 0, zeroSmall = 0;
205 if (size) {
206 V scales = V::Ones(size);
207 for (Eigen::Index j = 0; j < size; ++j) {
208 // Unsigned Gram scales retain the arithmetic size of both
209 // terms when a physical fluctuation eigenvalue cancels.
210 scales[j] = 1 / std::sqrt(rowScales[j]);
211 logRatio_ -= 2 * std::log(scales[j]);
212 }
213 small = (scales.asDiagonal() * small).eval();
214 small = (small * scales.asDiagonal()).eval();
215 Eigen::SelfAdjointEigenSolver<M> spectrum(small);
216 if (spectrum.info() != Eigen::Success ||
217 !spectrum.eigenvalues().allFinite())
218 throw std::runtime_error(
219 "Reduced harmonic response eigensolve fails");
220 smallVectors_ = scales.asDiagonal() * spectrum.eigenvectors();
221 smallValues_ = spectrum.eigenvalues();
222 const double scale =
223 std::max(1., smallValues_.cwiseAbs().maxCoeff());
224 for (double value: smallValues_) {
225 if (std::abs(value) <= unresolvedTolerance * scale)
226 ++zeroSmall;
227 else {
228 negativeSmall += value < 0;
229 logRatio_ += std::log(std::abs(value));
230 }
231 }
232 } else {
233 smallVectors_.resize(0, 0);
234 smallValues_.resize(0);
235 }
236 // Haynsworth inertia for the signed update and the constraint border.
237 inertia_.negative = negativeSigns - negativeSmall - zeroSmall;
238 inertia_.unresolved = zeroSmall;
239 inertia_.positive =
240 dimension_ - codimension_ - inertia_.negative - zeroSmall;
241 if (inertia_.negative < 0 || inertia_.positive < 0)
242 throw std::runtime_error(
243 "Reduced harmonic inertia exceeds the nodal subspace");
244 }
246 {
247 return inertia_;
248 }
249 Eigen::Index reducedDimension() const
250 {
251 return smallValues_.size();
252 }
253 Eigen::Index nodalDimension() const
254 {
255 return dimension_;
256 }
258 {
259 if (inertia_.unresolved)
260 throw std::domain_error(
261 "An unresolved fluctuation determinant has no logarithm");
262 return logRatio_;
263 }
264 // A negative direction v defines a positive Gaussian section by
265 // (K v)^T dq=0 inside the existing constraint plane. This is a specified
266 // dividing section, rather than the Euclidean negative eigenvector.
268 {
269 require(
270 inertia_.negative == 1 && inertia_.unresolved == 0,
271 "A Gaussian saddle section needs one resolved unstable direction");
272 const auto d = omega_.size(),
273 n = static_cast<Eigen::Index>(times_.size());
274 const auto r = directions_.cols(), k = codimension_;
275 M gram = M::Zero(r, r), cross = M::Zero(r, k),
276 constraint = M::Zero(k, k);
277 for (Eigen::Index i = 0; i < r; ++i) {
278 for (Eigen::Index j = 0; j < r; ++j)
279 gram(i, j) =
280 (directions_.col(i).array() * directions_.col(j).array() *
281 greenNodes(nodes_[i], nodes_[j]).array())
282 .sum();
283 for (Eigen::Index j = 0; j < n && k; ++j)
284 cross.row(i).noalias() += (directions_.col(i).array() *
285 greenNodes(nodes_[i], j).array())
286 .matrix()
287 .transpose() *
288 normals_[j];
289 }
290 M projected = M::Zero(r, r);
291 if (k) {
292 for (Eigen::Index i = 0; i < n; ++i)
293 for (Eigen::Index j = 0; j < n; ++j)
294 constraint.noalias() += normals_[i].transpose() *
295 greenNodes(i, j).asDiagonal() *
296 normals_[j];
297 const Eigen::LDLT<M> factor(constraint);
298 require(factor.info() == Eigen::Success &&
299 (factor.vectorD().array() > 0).all(),
300 "Saddle-section reference constraint Gram matrix must be "
301 "positive");
302 projected = cross * factor.solve(cross.transpose());
303 }
304 M restricted = gram - projected;
305 restricted = (.5 * restricted + .5 * restricted.transpose()).eval();
306 const Eigen::SelfAdjointEigenSolver<M> eig(restricted);
307 require(eig.info() == Eigen::Success,
308 "Saddle-section reference Gram spectrum is unresolved");
309 const double threshold = 64 * std::numeric_limits<double>::epsilon() *
310 (gram.norm() + projected.norm());
311 require(eig.eigenvalues().minCoeff() >= -threshold,
312 "Saddle-section reference Gram matrix is indefinite");
313 const auto count = (eig.eigenvalues().array() > threshold).count();
314 require(count > 0, "Saddle section has no resolved update direction");
315 const M basis = eig.eigenvectors().rightCols(count);
316 const V roots = eig.eigenvalues().tail(count).array().sqrt();
317 const M reduced = M::Identity(count, count) +
318 roots.asDiagonal() * basis.transpose() *
319 signs_.asDiagonal() * basis * roots.asDiagonal();
320 const Eigen::SelfAdjointEigenSolver<M> unstable(reduced);
321 require(unstable.info() == Eigen::Success &&
322 unstable.eigenvalues()[0] < 0 &&
323 (count == 1 || unstable.eigenvalues()[1] > 0),
324 "Saddle section has no unique negative reference direction");
325 const V coefficients =
326 basis *
327 (unstable.eigenvectors().col(0).array() / roots.array()).matrix();
328 const V normalWeights =
329 coefficients + signs_.asDiagonal() * (restricted * coefficients);
330 V normal = V::Zero(dimension_);
331 for (Eigen::Index j = 0; j < r; ++j)
332 normal.segment(nodes_[j] * d, d) +=
333 directions_.col(j) * normalWeights[j];
334 V component = V::Zero(k);
335 for (Eigen::Index j = 0; j < n && k; ++j)
336 component += normals_[j].transpose() * normal.segment(j * d, d);
337 for (Eigen::Index j = 0; j < n && k; ++j)
338 normal.segment(j * d, d) -= normals_[j] * component;
339 require(normal.allFinite() && normal.norm() > 0,
340 "Saddle-section normal is unresolved");
341 normal /= normal.norm();
342 if (modes_.size())
343 for (Eigen::Index j = 0; j < n; ++j)
344 normal.segment(j * d, d) =
345 (modes_ * normal.segment(j * d, d)).eval();
346 return normal;
347 }
348 // Response may be indefinite at a saddle. A probability covariance is
349 // returned only after every unstable and unresolved direction is removed.
350 M marginalResponse(std::size_t node) const
351 {
352 require(node < times_.size(),
353 "Harmonic marginal node is outside the path");
354 if (inertia_.unresolved)
355 throw std::domain_error(
356 "An unresolved fluctuation response has no inverse");
357 const auto d = omega_.size(), r = directions_.cols();
358 M cross = M::Zero(d, r + codimension_);
359 for (Eigen::Index j = 0; j < r; ++j)
360 cross.col(j) =
361 greenNodes(node, nodes_[j]).cwiseProduct(directions_.col(j));
362 for (std::size_t j = 0; j < times_.size() && codimension_; ++j)
363 cross.rightCols(codimension_).noalias() +=
364 greenNodes(node, j).asDiagonal() * normals_[j];
365 M response = greenNodes(node, node).asDiagonal();
366 if (smallValues_.size()) {
367 const M projected = cross * smallVectors_;
368 response.noalias() -= projected *
369 smallValues_.cwiseInverse().asDiagonal() *
370 projected.transpose();
371 }
372 if (modes_.size())
373 response = (modes_ * response * modes_.transpose()).eval();
374 return M(.5 * response + .5 * response.transpose());
375 }
376 M marginalCovariance(std::size_t node, double hbar) const
377 {
378 require(hbar > 0 && std::isfinite(hbar),
379 "Positive finite hbar required");
380 if (inertia_.negative || inertia_.unresolved)
381 throw std::domain_error(
382 "A real Gaussian covariance requires a stable transverse "
383 "subspace");
384 return hbar * marginalResponse(node);
385 }
386};
387} // namespace gpr::quantum
M marginalCovariance(std::size_t node, double hbar) const
HarmonicPathCorrection(V frequencies, double period, std::vector< double > times, const std::vector< HarmonicPointCurvature > &updates, const M &normal, M modes={}, double unresolvedTolerance=1e-12, std::vector< M > modalGreen={})
const HarmonicPointInertia & inertia() const
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
STL namespace.