Loading...
Searching...
No Matches
InstantonRatePosterior.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Cholesky>
8#include <Eigen/Eigenvalues>
9#include <Eigen/QR>
10#include <cmath>
11#include <functional>
12#include <stdexcept>
13#include <vector>
14
15// First-order response of a ring-polymer instanton log rate to an error in
16// the potential. For a stationary ring of U(q) = springs + sum_j V(q_j),
17// replacing V by V + v gives, by the envelope theorem and implicit
18// differentiation of the stationarity condition H dq = -g,
19//
20// d ln k = d ln L_Q - tr'(H^+ dH)/2 + tr(H_R^-1 dH_R)/2
21// - beta_N (sum_j v(q_j) - N v(r)),
22//
23// where tr' and H^+ exclude the ring phase mode, L_Q is the closed-orbit
24// length and r the reactant minimum. Every term except the explicit
25// curvature error grad^2 v is linear in the energies and gradients of v at
26// the beads and the reactant, so its posterior variance is w^T Sigma w with
27// Sigma the joint Gaussian-process covariance of those channels.
29using Matrix = Eigen::MatrixXd;
30using Vector = Eigen::VectorXd;
31
32struct Ring {
33 std::vector<Vector> beads; // mass-weighted bead coordinates
34 Matrix hessian; // Hessian of U over all bead coordinates
35 Vector reactant; // mass-weighted reactant minimum
36 Matrix reactantHessian; // Hessian of U for the collapsed reactant ring
37 Matrix reactantCurvature; // grad^2 V at the reactant
38 double betaN = 0; // beta / N in inverse energy units
39 // Optional orthonormal columns of exact zero modes, such as global ring
40 // translations and rotations, excluded with the phase mode.
42 // Optional gradient over all bead coordinates of further geometric log
43 // factors of the rate, such as a rotational partition function.
45};
46
47// Returns grad_q tr(A grad^2 V(q)) for a symmetric A.
49 std::function<Vector(const Vector&, const Matrix&)>;
50
51struct Weights {
52 // Sites are the N beads followed by the reactant when one is supplied; an
53 // empty reactant Hessian drops the reactant site. Energies of all sites
54 // precede gradient component k of all sites, for k = 0..d-1.
56 // d ln k also contains sum_s tr(curvature[s] grad^2 v(site s)).
57 std::vector<Matrix> curvature;
58 Eigen::Index phaseMode =
59 -1; // index in the spectrum without excluded modes
60 Vector phaseVector; // phase eigenvector over all bead coordinates
62 double phaseEigenvalue = 0;
64 double orbitLength = 0;
65 Matrix pseudoInverse; // H^+ with the phase mode removed
66};
67
68inline Eigen::Index siteChannel(Eigen::Index sites, Eigen::Index site,
69 Eigen::Index component)
70{
71 return sites * (component + 1) + site;
72}
73
74inline Weights logRateWeights(const Ring& ring,
75 const CurvatureTraceGradient& traceGradient)
76{
77 const auto N = static_cast<Eigen::Index>(ring.beads.size());
78 if (N < 4 || !(ring.betaN > 0) || !std::isfinite(ring.betaN) ||
79 !traceGradient)
80 throw std::invalid_argument(
81 "At least four beads, positive beta_N and a curvature derivative "
82 "required");
83 const Eigen::Index d = ring.beads.front().size();
84 const bool hasReactant = ring.reactantHessian.size() != 0;
85 if (d == 0 || ring.hessian.rows() != N * d ||
86 ring.hessian.cols() != N * d ||
87 (hasReactant &&
88 (ring.reactant.size() != d || ring.reactantHessian.rows() != N * d ||
89 ring.reactantHessian.cols() != N * d ||
90 ring.reactantCurvature.rows() != d ||
91 ring.reactantCurvature.cols() != d)))
92 throw std::invalid_argument(
93 "Ring, reactant and Hessian dimensions must match");
94 if (ring.excludedModes.size() != 0 &&
95 (ring.excludedModes.rows() != N * d ||
96 ring.excludedModes.cols() >= N * d - 2 ||
97 !ring.excludedModes.allFinite() ||
98 !(ring.excludedModes.transpose() * ring.excludedModes)
99 .isApprox(Matrix::Identity(ring.excludedModes.cols(),
100 ring.excludedModes.cols()),
101 1e-10)))
102 throw std::invalid_argument(
103 "Excluded ring modes must be orthonormal columns");
104 if (ring.logFactorGradient.size() != 0 &&
105 (ring.logFactorGradient.size() != N * d ||
106 !ring.logFactorGradient.allFinite()))
107 throw std::invalid_argument(
108 "A finite log-factor gradient over all bead coordinates is "
109 "required");
110 for (const auto& q: ring.beads)
111 if (q.size() != d || !q.allFinite())
112 throw std::invalid_argument(
113 "Finite beads of one dimension required");
114 if (!ring.hessian.allFinite() || !ring.reactantHessian.allFinite() ||
115 !ring.reactantCurvature.allFinite() || !ring.reactant.allFinite())
116 throw std::invalid_argument("Finite ring data required");
117 const double scale = std::max(1., ring.hessian.cwiseAbs().maxCoeff());
118 if ((ring.hessian - ring.hessian.transpose()).cwiseAbs().maxCoeff() >
119 1e-10 * scale)
120 throw std::invalid_argument("Symmetric ring Hessian required");
121
122 Weights result;
123 // Work in the orthogonal complement Q of the excluded zero modes.
124 Matrix Q = Matrix::Identity(N * d, N * d);
125 if (ring.excludedModes.size() != 0) {
126 Eigen::HouseholderQR<Matrix> qr(ring.excludedModes);
127 const Matrix full = qr.householderQ();
128 Q = full.rightCols(N * d - ring.excludedModes.cols());
129 }
130 const Eigen::Index m = Q.cols();
131 const Matrix reduced =
132 Q.transpose() * (.5 * (ring.hessian + ring.hessian.transpose())) * Q;
133 Eigen::SelfAdjointEigenSolver<Matrix> spectrum(reduced);
134 if (spectrum.info() != Eigen::Success)
135 throw std::runtime_error("Ring Hessian spectrum failed");
136 const Vector& lambda = spectrum.eigenvalues();
137 const Matrix U = Q * spectrum.eigenvectors();
138 // The phase mode is the mode closest to the cyclic orbit tangent. Its
139 // eigenvalue is near zero at finite N and may round to either sign.
140 Vector tangent(N * d);
141 for (Eigen::Index j = 0; j < N; ++j)
142 tangent.segment(j * d, d) =
143 ring.beads[(j + 1) % N] - ring.beads[(j + N - 1) % N];
144 if (!(tangent.norm() > 0))
145 throw std::domain_error("A nonconstant orbit is required");
146 if (ring.excludedModes.size() != 0)
147 tangent -=
148 ring.excludedModes * (ring.excludedModes.transpose() * tangent);
149 if (!(tangent.norm() > 0))
150 throw std::domain_error("The orbit tangent lies in the excluded modes");
151 tangent.normalize();
152 for (Eigen::Index k = 0; k < m; ++k) {
153 const double overlap = std::abs(U.col(k).dot(tangent));
154 if (overlap > result.phaseTangentOverlap) {
155 result.phaseTangentOverlap = overlap;
156 result.phaseMode = k;
157 }
158 }
159 if (result.phaseTangentOverlap < .9)
160 throw std::domain_error("No ring mode follows the orbit tangent");
161 result.phaseEigenvalue = lambda(result.phaseMode);
162 result.phaseVector = U.col(result.phaseMode);
163 // Apart from the phase mode, exactly one mode is unstable and the rest
164 // are resolved positive curvatures.
165 const Eigen::Index unstable = result.phaseMode == 0 ? 1 : 0;
166 if (!(lambda(unstable) < 0))
167 throw std::domain_error("A ring Hessian of index one is required");
168 result.negativeEigenvalue = lambda(unstable);
169 const double spectralScale = lambda.cwiseAbs().maxCoeff();
170 for (Eigen::Index k = unstable + 1; k < m; ++k) {
171 if (k == result.phaseMode) continue;
172 if (lambda(k) < 0)
173 throw std::domain_error("A ring Hessian of index one is required");
174 if (!(lambda(k) > 1e-10 * spectralScale))
175 throw std::domain_error(
176 "An unresolved zero mode must be supplied as an excluded mode");
177 }
178
179 result.pseudoInverse = Matrix::Zero(N * d, N * d);
180 for (Eigen::Index k = 0; k < m; ++k)
181 if (k != result.phaseMode)
182 result.pseudoInverse.noalias() +=
183 U.col(k) * U.col(k).transpose() / lambda(k);
184
185 // Linear coefficient of d ln k in the bead displacement dq.
186 Vector dqCoefficient = Vector::Zero(N * d);
187 for (Eigen::Index j = 0; j < N; ++j) {
188 const Vector link = ring.beads[(j + 1) % N] - ring.beads[j];
189 const double length = link.norm();
190 if (!(length > 0)) throw std::domain_error("Coincident adjacent beads");
191 result.orbitLength += length;
192 }
193 for (Eigen::Index j = 0; j < N; ++j) {
194 const Vector link = ring.beads[(j + 1) % N] - ring.beads[j];
195 const Vector previous = ring.beads[j] - ring.beads[(j + N - 1) % N];
196 dqCoefficient.segment(j * d, d) +=
197 (previous / previous.norm() - link / link.norm()) /
198 result.orbitLength;
199 const Matrix block = result.pseudoInverse.block(j * d, j * d, d, d);
200 dqCoefficient.segment(j * d, d) -=
201 .5 * traceGradient(ring.beads[j], block);
202 }
203
204 if (ring.logFactorGradient.size() != 0)
205 dqCoefficient += ring.logFactorGradient;
206
207 // The reactant ring is collapsed at r; each bead shifts by -K^-1 grad v(r).
208 Matrix reactantBlock = Matrix::Zero(d, d);
209 Vector reactantGradient = Vector::Zero(d);
210 if (hasReactant) {
211 Eigen::LLT<Matrix> reactantRing(ring.reactantHessian);
212 Eigen::LLT<Matrix> reactantPoint(ring.reactantCurvature);
213 if (reactantRing.info() != Eigen::Success ||
214 reactantPoint.info() != Eigen::Success)
215 throw std::domain_error(
216 "A positive reactant curvature is required");
217 const Matrix reactantInverse =
218 reactantRing.solve(Matrix::Identity(N * d, N * d));
219 for (Eigen::Index j = 0; j < N; ++j)
220 reactantBlock += reactantInverse.block(j * d, j * d, d, d);
221 reactantGradient = -.5 * reactantPoint.solve(traceGradient(
222 ring.reactant, reactantBlock));
223 }
224
225 const Eigen::Index sites = hasReactant ? N + 1 : N;
226 result.channels = Vector::Zero(sites * (d + 1));
227 result.channels.head(N).setConstant(-ring.betaN);
228 if (hasReactant) result.channels(N) = ring.betaN * static_cast<double>(N);
229 const Vector beadGradient = -result.pseudoInverse * dqCoefficient;
230 for (Eigen::Index k = 0; k < d; ++k) {
231 for (Eigen::Index j = 0; j < N; ++j)
232 result.channels(siteChannel(sites, j, k)) = beadGradient(j * d + k);
233 if (hasReactant)
234 result.channels(siteChannel(sites, N, k)) = reactantGradient(k);
235 }
236 result.curvature.reserve(sites);
237 for (Eigen::Index j = 0; j < N; ++j)
238 result.curvature.push_back(
239 -.5 * result.pseudoInverse.block(j * d, j * d, d, d));
240 if (hasReactant) result.curvature.push_back(.5 * reactantBlock);
241 return result;
242}
243
244// Packs energies and gradients of a perturbation at the N beads and the
245// reactant into the channel layout of Weights::channels.
246inline Vector packChannels(const Vector& energies,
247 const std::vector<Vector>& gradients)
248{
249 const Eigen::Index sites = energies.size();
250 if (sites == 0 || gradients.size() != static_cast<std::size_t>(sites))
251 throw std::invalid_argument(
252 "One energy and gradient per site required");
253 const Eigen::Index d = gradients.front().size();
254 Vector packed(sites * (d + 1));
255 packed.head(sites) = energies;
256 for (Eigen::Index s = 0; s < sites; ++s) {
257 if (gradients[s].size() != d)
258 throw std::invalid_argument("Gradients of one dimension required");
259 for (Eigen::Index k = 0; k < d; ++k)
260 packed(siteChannel(sites, s, k)) = gradients[s](k);
261 }
262 return packed;
263}
264
265inline double firstOrder(const Weights& w, const Vector& channels,
266 const std::vector<Matrix>& curvatures = {})
267{
268 if (channels.size() != w.channels.size() || !channels.allFinite())
269 throw std::invalid_argument(
270 "Perturbation channels must match the weights");
271 double value = w.channels.dot(channels);
272 if (!curvatures.empty()) {
273 if (curvatures.size() != w.curvature.size())
274 throw std::invalid_argument("One curvature per site required");
275 for (std::size_t s = 0; s < curvatures.size(); ++s)
276 value += w.curvature[s].cwiseProduct(curvatures[s]).sum();
277 }
278 return value;
279}
280
281// Variance of the energy and gradient part of d ln k under a joint Gaussian
282// covariance in the channel layout above.
283inline double variance(const Weights& w, const Matrix& covariance)
284{
285 if (covariance.rows() != w.channels.size() ||
286 covariance.cols() != w.channels.size() || !covariance.allFinite())
287 throw std::invalid_argument(
288 "Covariance must match the weight channels");
289 return std::max(0., w.channels.dot(covariance * w.channels));
290}
291
292// Second-order change of -beta_N U* from the bead gradients g_j of v:
293// U* moves by -g^T H^+ g / 2, so ln k moves by +beta_N g^T H^+ g / 2.
294inline double secondOrderExponent(const Weights& w, double betaN,
295 const std::vector<Vector>& beadGradients)
296{
297 const Eigen::Index n = w.pseudoInverse.rows();
298 Vector g(n);
299 Eigen::Index offset = 0;
300 for (const auto& gj: beadGradients) {
301 if (offset + gj.size() > n)
302 throw std::invalid_argument("Bead gradients exceed the ring");
303 g.segment(offset, gj.size()) = gj;
304 offset += gj.size();
305 }
306 if (offset != n)
307 throw std::invalid_argument("One gradient per bead required");
308 return .5 * betaN * g.dot(w.pseudoInverse * g);
309}
310} // namespace gpr::quantum::ratepost
double secondOrderExponent(const Weights &w, double betaN, const std::vector< Vector > &beadGradients)
Eigen::Index siteChannel(Eigen::Index sites, Eigen::Index site, Eigen::Index component)
double variance(const Weights &w, const Matrix &covariance)
Weights logRateWeights(const Ring &ring, const CurvatureTraceGradient &traceGradient)
double firstOrder(const Weights &w, const Vector &channels, const std::vector< Matrix > &curvatures={})
Vector packChannels(const Vector &energies, const std::vector< Vector > &gradients)
std::function< Vector(const Vector &, const Matrix &)> CurvatureTraceGradient