Loading...
Searching...
No Matches
PhysicalQuadraticBath.h
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3** SPDX-License-Identifier: BSD-3-Clause
4** Copyright (c) 2010--present, eOn Development Team
5*/
6#pragma once
7
8#include "CyclicBlockBath.h"
9#include <Eigen/Dense>
10#include <cmath>
11#include <functional>
12#include <memory>
13#include <numbers>
14#include <stdexcept>
15#include <vector>
16
17namespace eonc::tunneling {
18
22 Eigen::MatrixXd coupling;
23 Eigen::VectorXd curvatures;
25 bool relaxedPotential = false;
27 using Source =
28 std::function<void(const Eigen::VectorXd &, Eigen::VectorXd &,
29 Eigen::MatrixXd &, std::vector<Eigen::MatrixXd> &)>;
31
33 Eigen::MatrixXd value;
34 std::vector<Eigen::MatrixXd> first;
35 std::vector<std::vector<Eigen::MatrixXd>> second;
36 };
37
38 using Curvature =
39 std::function<CurvatureDerivatives(const Eigen::VectorXd &, int)>;
41
42 struct Evaluation {
43 double energy = 0.0;
44 std::vector<Eigen::VectorXd> gradient;
45 Eigen::MatrixXd hessian;
46 std::vector<Eigen::VectorXd> bathPositions;
47 std::vector<Eigen::VectorXd> inverseBath;
49 std::vector<Eigen::MatrixXd> sourceJacobians;
50 std::shared_ptr<const CyclicBlockBath> blockBath;
51 double bathLogDeterminant = 0.0;
52
53 Eigen::MatrixXd solveBath(const Eigen::MatrixXd &rhs) const {
54 if (blockBath)
55 return blockBath->solve(rhs);
56 if (inverseBath.empty())
57 throw std::logic_error("bath inverse requires a Hessian evaluation");
58 const long n = inverseBath.size(), b = inverseBath.front().size();
59 if (rhs.rows() != n * b || !rhs.allFinite())
60 throw std::invalid_argument("physical bath right hand side mismatch");
61 Eigen::MatrixXd result = Eigen::MatrixXd::Zero(n * b, rhs.cols());
62 for (long j = 0; j < n; ++j)
63 for (long l = 0; l < n; ++l)
64 result.middleRows(j * b, b).noalias() +=
65 inverseBath[(j - l + n) % n].asDiagonal() *
66 rhs.middleRows(l * b, b);
67 return result;
68 }
69 };
70
71 void validate(long dof) const {
72 if (dof < 1 || coupling.rows() != dof ||
73 coupling.cols() != curvatures.size() || !coupling.allFinite() ||
74 !curvatures.allFinite() || (curvatures.array() <= 0).any())
75 throw std::invalid_argument(
76 "physical bath requires finite coupling and positive bath curvature");
78 throw std::invalid_argument(
79 "variable physical bath requires a frozen potential callback");
80 }
81
82 CurvatureDerivatives curvatureAt(const Eigen::VectorXd &q, int order) const {
83 const long r = q.size(), b = curvatures.size();
84 if (!curvature || order < 0 || order > 2 || b < 1)
85 throw std::invalid_argument("invalid physical bath curvature request");
86 auto out = curvature(q, order);
87 auto valid = [&](const Eigen::MatrixXd &matrix) {
88 return matrix.rows() == b && matrix.cols() == b && matrix.allFinite() &&
89 matrix.isApprox(matrix.transpose(), 1e-12);
90 };
91 if (!valid(out.value) ||
92 (order >= 1 && out.first.size() != static_cast<size_t>(r)) ||
93 (order >= 2 && out.second.size() != static_cast<size_t>(r)))
94 throw std::invalid_argument(
95 "physical bath curvature derivative mismatch");
96 if (order >= 1)
97 for (const auto &matrix : out.first)
98 if (!valid(matrix))
99 throw std::invalid_argument(
100 "invalid physical bath curvature gradient");
101 if (order >= 2) {
102 for (long i = 0; i < r; ++i) {
103 if (out.second[i].size() != static_cast<size_t>(r))
104 throw std::invalid_argument(
105 "physical bath curvature Hessian mismatch");
106 for (long j = 0; j < r; ++j)
107 if (!valid(out.second[i][j]))
108 throw std::invalid_argument(
109 "invalid physical bath curvature Hessian");
110 }
111 for (long i = 0; i < r; ++i)
112 for (long j = 0; j < i; ++j)
113 if (!out.second[i][j].isApprox(out.second[j][i], 1e-12))
114 throw std::invalid_argument(
115 "asymmetric physical bath curvature Hessian");
116 }
117 return out;
118 }
119
122 Eigen::MatrixXd fullHessian(const Eigen::VectorXd &q,
123 const Eigen::MatrixXd &potentialHessian) const {
124 const long r = q.size(), b = curvatures.size();
125 validate(r);
126 if (potentialHessian.rows() != r || potentialHessian.cols() != r ||
127 !potentialHessian.allFinite())
128 throw std::invalid_argument("physical bath potential Hessian mismatch");
129 Eigen::VectorXd s;
130 Eigen::MatrixXd jac;
131 std::vector<Eigen::MatrixXd> second;
132 if (source)
133 source(q, s, jac, second);
134 else {
135 s = coupling.transpose() * q;
136 jac = coupling.transpose();
137 second.assign(b, Eigen::MatrixXd::Zero(r, r));
138 }
139 if (s.size() != b || jac.rows() != b || jac.cols() != r || !s.allFinite() ||
140 !jac.allFinite() || second.size() != static_cast<size_t>(b))
141 throw std::invalid_argument("physical bath source derivative mismatch");
142 for (const auto &h : second)
143 if (h.rows() != r || h.cols() != r || !h.allFinite() ||
144 !h.isApprox(h.transpose(), 1e-12))
145 throw std::invalid_argument("physical bath source Hessian mismatch");
146 Eigen::MatrixXd out = Eigen::MatrixXd::Zero(r + b, r + b);
147 out.topLeftCorner(r, r) = potentialHessian;
148 if (curvature) {
149 const auto state = curvatureAt(q, 2);
150 const CyclicBlockBath factor({state.value}, 1.0);
151 const Eigen::VectorXd y = -factor.solve(s).col(0);
152 Eigen::MatrixXd mixed = jac;
153 for (long a = 0; a < b; ++a)
154 out.topLeftCorner(r, r).noalias() += y[a] * second[a];
155 for (long i = 0; i < r; ++i) {
156 mixed.col(i).noalias() += state.first[i] * y;
157 for (long j = 0; j < r; ++j)
158 out(i, j) += 0.5 * y.dot(state.second[i][j] * y);
159 }
160 out.topRightCorner(r, b) = mixed.transpose();
161 out.bottomLeftCorner(b, r) = mixed;
162 out.bottomRightCorner(b, b) = state.value;
163 if (!out.allFinite() || !out.isApprox(out.transpose(), 1e-12))
164 throw std::invalid_argument(
165 "physical bath full Hessian is not finite and symmetric");
166 return out;
167 }
169 out.topLeftCorner(r, r).noalias() +=
170 jac.transpose() * curvatures.cwiseInverse().asDiagonal() * jac;
171 else
172 for (long k = 0; k < b; ++k)
173 out.topLeftCorner(r, r).noalias() -= (s[k] / curvatures[k]) * second[k];
174 out.topRightCorner(r, b) = jac.transpose();
175 out.bottomLeftCorner(b, r) = jac;
176 out.bottomRightCorner(b, b) = curvatures.asDiagonal();
177 if (!out.allFinite() || !out.isApprox(out.transpose(), 1e-12))
178 throw std::invalid_argument(
179 "physical bath full Hessian is not finite and symmetric");
180 return out;
181 }
182
184 Evaluation evaluate(const std::vector<Eigen::VectorXd> &q, double c,
185 bool needHessian = false) const {
186 if (q.empty() || !(c > 0) || !std::isfinite(c))
187 throw std::invalid_argument(
188 "physical bath requires a nonempty ring and positive spring");
189 const long n = q.size(), r = q.front().size(), b = curvatures.size();
190 validate(r);
191 Evaluation out;
192 out.gradient.assign(n, Eigen::VectorXd::Zero(r));
193 out.bathPositions.assign(n, Eigen::VectorXd::Zero(b));
194 if (needHessian)
195 out.hessian = Eigen::MatrixXd::Zero(n * r, n * r);
196 std::vector<Eigen::VectorXd> s(n);
197 std::vector<Eigen::MatrixXd> jac(n);
198 std::vector<std::vector<Eigen::MatrixXd>> second(n);
199 for (long j = 0; j < n; ++j) {
200 if (q[j].size() != r || !q[j].allFinite())
201 throw std::invalid_argument("physical bath ring coordinate mismatch");
202 if (source)
203 source(q[j], s[j], jac[j], second[j]);
204 else {
205 s[j] = coupling.transpose() * q[j];
206 jac[j] = coupling.transpose();
207 second[j].assign(b, Eigen::MatrixXd::Zero(r, r));
208 }
209 if (s[j].size() != b || jac[j].rows() != b || jac[j].cols() != r ||
210 !s[j].allFinite() || !jac[j].allFinite() ||
211 second[j].size() != static_cast<size_t>(b))
212 throw std::invalid_argument("physical bath source derivative mismatch");
213 for (const auto &h : second[j])
214 if (h.rows() != r || h.cols() != r || !h.allFinite() ||
215 !h.isApprox(h.transpose(), 1e-12))
216 throw std::invalid_argument("physical bath source Hessian mismatch");
217 }
218 if (curvature) {
219 std::vector<Eigen::MatrixXd> blocks;
220 blocks.reserve(n);
221 Eigen::MatrixXd flatSource(n * b, 1);
222 for (long j = 0; j < n; ++j) {
223 blocks.push_back(curvatureAt(q[j], 0).value);
224 flatSource.middleRows(j * b, b) = s[j];
225 }
226 auto factor = std::make_shared<CyclicBlockBath>(blocks, c);
227 const Eigen::MatrixXd flatY = -factor->solve(flatSource);
228 out.bathLogDeterminant = factor->logDeterminant();
229 if (needHessian) {
230 out.blockBath = factor;
231 out.sourceJacobians.resize(n);
232 }
233 for (long j = 0; j < n; ++j) {
234 const Eigen::VectorXd y = flatY.middleRows(j * b, b).col(0);
235 out.bathPositions[j] = y;
236 out.energy += 0.5 * s[j].dot(y);
237 out.gradient[j].noalias() += jac[j].transpose() * y;
238 const auto state = curvatureAt(q[j], needHessian ? 2 : 1);
239 Eigen::MatrixXd mixed = jac[j];
240 for (long i = 0; i < r; ++i) {
241 out.gradient[j][i] += 0.5 * y.dot(state.first[i] * y);
242 if (needHessian) {
243 mixed.col(i).noalias() += state.first[i] * y;
244 for (long k = 0; k < r; ++k)
245 out.hessian(j * r + i, j * r + k) +=
246 0.5 * y.dot(state.second[i][k] * y);
247 }
248 }
249 if (needHessian) {
250 for (long a = 0; a < b; ++a)
251 out.hessian.block(j * r, j * r, r, r).noalias() +=
252 y[a] * second[j][a];
253 out.sourceJacobians[j] = std::move(mixed);
254 }
255 }
256 if (needHessian) {
257 for (long l = 0; l < n; ++l) {
258 Eigen::MatrixXd rhs = Eigen::MatrixXd::Zero(n * b, r);
259 rhs.middleRows(l * b, b) = out.sourceJacobians[l];
260 const Eigen::MatrixXd response = factor->solve(rhs);
261 for (long j = 0; j < n; ++j)
262 out.hessian.block(j * r, l * r, r, r).noalias() -=
263 out.sourceJacobians[j].transpose() *
264 response.middleRows(j * b, b);
265 }
266 }
267 return out;
268 }
269 // inverse[lag] is diagonal in the fixed physical bath eigenbasis.
270 std::vector<Eigen::VectorXd> inverse(n, Eigen::VectorXd::Zero(b));
271 // The positive cyclic Green function solves (E+c*L)G=I. Its two
272 // decaying branches sum periodic images without Fourier cancellation.
273 const double rootSpring = std::sqrt(c);
274 for (long a = 0; a < b; ++a) {
275 const double frequency = std::sqrt(curvatures[a]);
276 double alpha = 2 * std::asinh(frequency / (2 * rootSpring));
277 if (!std::isfinite(alpha))
278 alpha = std::log(curvatures[a]) - std::log(c);
279 const double periodic = -std::expm1(-n * alpha);
280 const double scale =
281 1 / frequency / std::hypot(frequency, 2 * rootSpring) / periodic;
282 out.bathLogDeterminant +=
283 n * (std::log(c) + alpha) + 2 * std::log(periodic);
284 for (long lag = 0; lag < n; ++lag)
285 inverse[lag][a] =
286 scale * (std::exp(-lag * alpha) + std::exp(-(n - lag) * alpha));
287 }
288 for (long j = 0; j < n; ++j) {
289 Eigen::VectorXd y = Eigen::VectorXd::Zero(b);
290 for (long l = 0; l < n; ++l)
291 y.array() += inverse[(j - l + n) % n].array() * s[l].array();
292 out.bathPositions[j] = -y;
294 y.array() -= s[j].array() / curvatures.array();
295 out.energy -= 0.5 * s[j].dot(y);
296 out.gradient[j].noalias() -= jac[j].transpose() * y;
297 if (needHessian) {
298 for (long l = 0; l < n; ++l) {
299 Eigen::VectorXd weights = inverse[(j - l + n) % n];
300 if (relaxedPotential && j == l)
301 weights.array() -= curvatures.cwiseInverse().array();
302 out.hessian.block(j * r, l * r, r, r).noalias() -=
303 jac[j].transpose() * weights.asDiagonal() * jac[l];
304 }
305 for (long a = 0; a < b; ++a)
306 out.hessian.block(j * r, j * r, r, r).noalias() -=
307 y[a] * second[j][a];
308 }
309 }
310 if (needHessian) {
311 out.inverseBath = inverse;
312 out.sourceJacobians = jac;
313 }
314 return out;
315 }
316};
317} // namespace eonc::tunneling
Exact factorization of diag(E_j) plus the cyclic primitive spring matrix.
Eigen::MatrixXd solve(const Eigen::MatrixXd &rhs) const
std::vector< std::vector< Eigen::MatrixXd > > second
std::shared_ptr< const CyclicBlockBath > blockBath
Eigen::MatrixXd solveBath(const Eigen::MatrixXd &rhs) const
std::vector< Eigen::MatrixXd > sourceJacobians
Mixed bath/retained Hessians, including E_i*y for variable curvature.
V(q,b)=V_fixed(q)+source(q)^T b+b^T E(q)b/2.
Eigen::MatrixXd coupling
retained x bath, source=C^T q by default
std::function< void(const Eigen::VectorXd &, Eigen::VectorXd &, Eigen::MatrixXd &, std::vector< Eigen::MatrixXd > &)> Source
Source, bath x retained Jacobian, one retained Hessian per bath mode.
bool relaxedPotential
With V_relaxed=V_fixed-source^T E^-1 source/2, restore its counterterm.
Evaluation evaluate(const std::vector< Eigen::VectorXd > &q, double c, bool needHessian=false) const
Exact finite primitive-ring Gaussian elimination, with c=(beta_N*hbar)^-2.
Eigen::MatrixXd fullHessian(const Eigen::VectorXd &q, const Eigen::MatrixXd &potentialHessian) const
Physical curvature at the conditional bath equilibrium.
std::function< CurvatureDerivatives(const Eigen::VectorXd &, int)> Curvature
Full bath curvature in a fixed basis, with derivatives through order 0–2.
CurvatureDerivatives curvatureAt(const Eigen::VectorXd &q, int order) const
Eigen::VectorXd curvatures
strictly positive physical bath eigenvalues