Loading...
Searching...
No Matches
RingCentroidSpread.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Eigenvalues>
8#include <Eigen/QR>
9#include <cmath>
10#include <stdexcept>
11#include <vector>
12
13// Centroid and per-atom spreads of a ring-polymer instanton, in the layout of
14// the readcon "spreads" section: one RMS width per atom and Cartesian axis.
15//
16// - orbit: RMS of the bead positions about their centroid, the delocalization
17// carried by the imaginary-time orbit itself;
18// - thermal: harmonic fluctuation width over the stable ring modes,
19// Cov(dq) = (1/beta_N) sum_k u_k u_k^T / lambda_k, averaged over beads; the
20// unstable mode, the phase mode and any excluded zero modes are omitted
21// because their widths are not Gaussian about this orbit;
22// - model: orbit displacement induced by a Gaussian gradient error g,
23// Cov(dq) = H^+ Sigma_g H^+, averaged over beads.
24// Inputs are mass-weighted; outputs are Cartesian, q = sqrt(m) x.
26using Matrix = Eigen::MatrixXd;
27using Vector = Eigen::VectorXd;
28
29struct Spread {
30 Matrix centroid; // atoms x 3, Cartesian
31 Matrix orbit; // atoms x 3
32 Matrix thermal; // atoms x 3, empty unless requested
33 Matrix model; // atoms x 3, empty unless requested
34 // sqrt(orbit^2 + thermal^2), the physical delocalization written as
35 // spreads.
37 {
38 if (thermal.size() == 0) return orbit;
39 return (orbit.array().square() + thermal.array().square())
40 .sqrt()
41 .matrix();
42 }
43};
44
45inline void checkRing(const std::vector<Vector>& q, const Vector& roots)
46{
47 if (q.size() < 2 || roots.size() == 0 || roots.size() % 3 != 0 ||
48 !(roots.array() > 0).all() || !roots.allFinite())
49 throw std::invalid_argument(
50 "Ring beads and positive mass roots for whole atoms required");
51 for (const auto& x: q)
52 if (x.size() != roots.size() || !x.allFinite())
53 throw std::invalid_argument(
54 "Finite beads matching the mass roots required");
55}
56
57// Per-atom RMS over beads of the diagonal of a bead-blocked covariance.
58inline Matrix beadAveragedWidth(const Matrix& covariance, Eigen::Index N,
59 const Vector& roots)
60{
61 const Eigen::Index d = roots.size();
62 Matrix width = Matrix::Zero(d / 3, 3);
63 for (Eigen::Index j = 0; j < N; ++j)
64 for (Eigen::Index k = 0; k < d; ++k) {
65 const double variance = covariance(j * d + k, j * d + k);
66 if (variance <
67 -1e-12 * std::max(1., covariance.cwiseAbs().maxCoeff()))
68 throw std::runtime_error("Negative bead variance");
69 width(k / 3, k % 3) +=
70 std::max(0., variance) / (roots(k) * roots(k));
71 }
72 return (width / static_cast<double>(N)).cwiseSqrt();
73}
74
75inline Spread orbitSpread(const std::vector<Vector>& q, const Vector& roots)
76{
77 checkRing(q, roots);
78 const auto N = static_cast<double>(q.size());
79 const Eigen::Index d = roots.size();
80 Vector mean = Vector::Zero(d);
81 for (const auto& x: q)
82 mean += x;
83 mean /= N;
84 Vector second = Vector::Zero(d);
85 for (const auto& x: q)
86 second += (x - mean).array().square().matrix();
87 Spread s;
88 s.centroid.resize(d / 3, 3);
89 s.orbit.resize(d / 3, 3);
90 for (Eigen::Index k = 0; k < d; ++k) {
91 s.centroid(k / 3, k % 3) = mean(k) / roots(k);
92 s.orbit(k / 3, k % 3) = std::sqrt(second(k) / N) / roots(k);
93 }
94 return s;
95}
96
97// Stable-mode covariance of the ring about the orbit, without phase,
98// unstable and excluded modes.
99inline Matrix thermalCovariance(const std::vector<Vector>& q, const Matrix& H,
100 double betaN, const Matrix& excludedModes = {})
101{
102 const auto N = static_cast<Eigen::Index>(q.size());
103 const Eigen::Index d = q.front().size();
104 if (H.rows() != N * d || H.cols() != N * d || !H.allFinite() ||
105 !(betaN > 0))
106 throw std::invalid_argument(
107 "A finite ring Hessian and positive beta_N required");
108 Matrix Q = Matrix::Identity(N * d, N * d);
109 if (excludedModes.size() != 0) {
110 Eigen::HouseholderQR<Matrix> qr(excludedModes);
111 const Matrix full = qr.householderQ();
112 Q = full.rightCols(N * d - excludedModes.cols());
113 }
114 Eigen::SelfAdjointEigenSolver<Matrix> s(Q.transpose() *
115 (.5 * (H + H.transpose())) * Q);
116 if (s.info() != Eigen::Success)
117 throw std::runtime_error("Ring spectrum failed");
118 const Matrix U = Q * s.eigenvectors();
119 Vector tangent(N * d);
120 for (Eigen::Index j = 0; j < N; ++j)
121 tangent.segment(j * d, d) = q[(j + 1) % N] - q[(j + N - 1) % N];
122 Eigen::Index phase = -1;
123 if (tangent.norm() > 0) {
124 tangent.normalize();
125 double best = .9;
126 for (Eigen::Index k = 0; k < U.cols(); ++k)
127 if (s.eigenvalues()(k) > 0 &&
128 std::abs(U.col(k).dot(tangent)) > best)
129 best = std::abs(U.col(k).dot(tangent)), phase = k;
130 }
131 const double scale = s.eigenvalues().cwiseAbs().maxCoeff();
132 Matrix covariance = Matrix::Zero(N * d, N * d);
133 for (Eigen::Index k = 0; k < U.cols(); ++k) {
134 if (k == phase || !(s.eigenvalues()(k) > 0)) continue;
135 if (!(s.eigenvalues()(k) > 1e-10 * scale))
136 throw std::domain_error(
137 "An unresolved zero mode must be supplied as an excluded mode");
138 covariance.noalias() +=
139 U.col(k) * U.col(k).transpose() / (betaN * s.eigenvalues()(k));
140 }
141 return covariance;
142}
143
144inline Spread instantonSpread(const std::vector<Vector>& q, const Vector& roots,
145 const Matrix& H, double betaN,
146 const Matrix& excludedModes = {})
147{
148 Spread s = orbitSpread(q, roots);
149 s.thermal = beadAveragedWidth(thermalCovariance(q, H, betaN, excludedModes),
150 static_cast<Eigen::Index>(q.size()), roots);
151 return s;
152}
153
154// Adds the orbit displacement width induced by gradient covariance Sigma_g
155// (bead-blocked, mass-weighted) through the phase-free pseudoinverse H^+.
156inline void addModelSpread(Spread& s, const Matrix& pseudoInverse,
157 const Matrix& gradientCovariance,
158 const Vector& roots)
159{
160 const Eigen::Index n = pseudoInverse.rows();
161 if (pseudoInverse.cols() != n || gradientCovariance.rows() != n ||
162 gradientCovariance.cols() != n || n % roots.size() != 0)
163 throw std::invalid_argument(
164 "Matching pseudoinverse and gradient covariance required");
166 pseudoInverse * gradientCovariance * pseudoInverse.transpose(),
167 n / roots.size(), roots);
168}
169} // namespace gpr::quantum::ringspread
@ x
Definition Enums.h:28
void checkRing(const std::vector< Vector > &q, const Vector &roots)
Matrix thermalCovariance(const std::vector< Vector > &q, const Matrix &H, double betaN, const Matrix &excludedModes={})
void addModelSpread(Spread &s, const Matrix &pseudoInverse, const Matrix &gradientCovariance, const Vector &roots)
Matrix beadAveragedWidth(const Matrix &covariance, Eigen::Index N, const Vector &roots)
Spread orbitSpread(const std::vector< Vector > &q, const Vector &roots)
Spread instantonSpread(const std::vector< Vector > &q, const Vector &roots, const Matrix &H, double betaN, const Matrix &excludedModes={})
Eigen::MatrixXd Matrix