123 const Eigen::MatrixXd &potentialHessian)
const {
124 const long r = q.size(), b =
curvatures.size();
126 if (potentialHessian.rows() != r || potentialHessian.cols() != r ||
127 !potentialHessian.allFinite())
128 throw std::invalid_argument(
"physical bath potential Hessian mismatch");
131 std::vector<Eigen::MatrixXd> second;
133 source(q, s, jac, second);
137 second.assign(b, Eigen::MatrixXd::Zero(r, r));
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;
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);
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");
169 out.topLeftCorner(r, r).noalias() +=
170 jac.transpose() *
curvatures.cwiseInverse().asDiagonal() * jac;
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");
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();
192 out.
gradient.assign(n, Eigen::VectorXd::Zero(r));
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");
203 source(q[j], s[j], jac[j], second[j]);
207 second[j].assign(b, Eigen::MatrixXd::Zero(r, r));
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");
219 std::vector<Eigen::MatrixXd> blocks;
221 Eigen::MatrixXd flatSource(n * b, 1);
222 for (
long j = 0; j < n; ++j) {
224 flatSource.middleRows(j * b, b) = s[j];
226 auto factor = std::make_shared<CyclicBlockBath>(blocks, c);
227 const Eigen::MatrixXd flatY = -factor->solve(flatSource);
233 for (
long j = 0; j < n; ++j) {
234 const Eigen::VectorXd y = flatY.middleRows(j * b, b).col(0);
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);
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);
250 for (
long a = 0; a < b; ++a)
251 out.
hessian.block(j * r, j * r, r, r).noalias() +=
257 for (
long l = 0; l < n; ++l) {
258 Eigen::MatrixXd rhs = Eigen::MatrixXd::Zero(n * b, r);
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() -=
264 response.middleRows(j * b, b);
270 std::vector<Eigen::VectorXd> inverse(n, Eigen::VectorXd::Zero(b));
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);
281 1 / frequency / std::hypot(frequency, 2 * rootSpring) / periodic;
283 n * (std::log(c) + alpha) + 2 * std::log(periodic);
284 for (
long lag = 0; lag < n; ++lag)
286 scale * (std::exp(-lag * alpha) + std::exp(-(n - lag) * alpha));
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();
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;
298 for (
long l = 0; l < n; ++l) {
299 Eigen::VectorXd weights = inverse[(j - l + n) % n];
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];
305 for (
long a = 0; a < b; ++a)
306 out.
hessian.block(j * r, j * r, r, r).noalias() -=