Loading...
Searching...
No Matches
gpr::quantum::ratepost Namespace Reference

Classes

struct  Ring
struct  Weights

Typedefs

using Matrix = Eigen::MatrixXd
using Vector = Eigen::VectorXd
using CurvatureTraceGradient

Functions

Eigen::Index siteChannel (Eigen::Index sites, Eigen::Index site, Eigen::Index component)
Weights logRateWeights (const Ring &ring, const CurvatureTraceGradient &traceGradient)
Vector packChannels (const Vector &energies, const std::vector< Vector > &gradients)
double firstOrder (const Weights &w, const Vector &channels, const std::vector< Matrix > &curvatures={})
double variance (const Weights &w, const Matrix &covariance)
double secondOrderExponent (const Weights &w, double betaN, const std::vector< Vector > &beadGradients)

Typedef Documentation

◆ CurvatureTraceGradient

Initial value:
std::function<Vector(const Vector&, const Matrix&)>

Definition at line 48 of file InstantonRatePosterior.h.

◆ Matrix

using gpr::quantum::ratepost::Matrix = Eigen::MatrixXd

Definition at line 29 of file InstantonRatePosterior.h.

◆ Vector

using gpr::quantum::ratepost::Vector = Eigen::VectorXd

Definition at line 30 of file InstantonRatePosterior.h.

Function Documentation

◆ firstOrder()

double gpr::quantum::ratepost::firstOrder ( const Weights & w,
const Vector & channels,
const std::vector< Matrix > & curvatures = {} )
inline

Definition at line 265 of file InstantonRatePosterior.h.

266 {})
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}

◆ logRateWeights()

Weights gpr::quantum::ratepost::logRateWeights ( const Ring & ring,
const CurvatureTraceGradient & traceGradient )
inline

Definition at line 74 of file InstantonRatePosterior.h.

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}
Eigen::Index siteChannel(Eigen::Index sites, Eigen::Index site, Eigen::Index component)
Eigen::MatrixXd Matrix
Eigen::VectorXd Vector

◆ packChannels()

Vector gpr::quantum::ratepost::packChannels ( const Vector & energies,
const std::vector< Vector > & gradients )
inline

Definition at line 246 of file InstantonRatePosterior.h.

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}

◆ secondOrderExponent()

double gpr::quantum::ratepost::secondOrderExponent ( const Weights & w,
double betaN,
const std::vector< Vector > & beadGradients )
inline

Definition at line 294 of file InstantonRatePosterior.h.

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}

◆ siteChannel()

Eigen::Index gpr::quantum::ratepost::siteChannel ( Eigen::Index sites,
Eigen::Index site,
Eigen::Index component )
inline

Definition at line 68 of file InstantonRatePosterior.h.

70{
71 return sites * (component + 1) + site;
72}

◆ variance()

double gpr::quantum::ratepost::variance ( const Weights & w,
const Matrix & covariance )
inline

Definition at line 283 of file InstantonRatePosterior.h.

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}