Loading...
Searching...
No Matches
HarmonicReferenceAction.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 <Eigen/Dense>
9#include <Eigen/Eigenvalues>
10#include <cmath>
11#include <limits>
12#include <stdexcept>
13
14namespace eonc::tunneling {
15
23public:
24 HarmonicReferenceAction(const Eigen::MatrixXd &h0,
25 const Eigen::VectorXd &center, double tau)
26 : h0_(h0), center_(center) {
27 if (!(tau > 0.0) || !std::isfinite(tau) || center.size() == 0 ||
28 !center.allFinite() || h0.rows() != center.size() ||
29 h0.cols() != center.size() || !h0.allFinite() ||
30 !h0.isApprox(h0.transpose(),
31 32 * std::numeric_limits<double>::epsilon())) {
32 throw std::invalid_argument(
33 "harmonic reference requires finite symmetric H0 and positive tau");
34 }
35 h0_ = 0.5 * h0 + 0.5 * h0.transpose();
36 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eig(h0_);
37 if (eig.info() != Eigen::Success || eig.eigenvalues().minCoeff() < 0.0) {
38 throw std::invalid_argument("harmonic reference requires nonnegative H0");
39 }
40 Eigen::VectorXd b(center.size()), d(center.size());
42 for (Eigen::Index i = 0; i < center.size(); ++i) {
43 const double omega = std::sqrt(eig.eigenvalues()[i]);
44 const double z = tau * omega;
45 if (!std::isfinite(z)) {
46 throw std::invalid_argument(
47 "harmonic reference frequency-time product overflows");
48 }
49 double logRatio;
50 if (z < 1e-4) {
51 const double z2 = z * z;
52 const double ratio = 1.0 - z2 / 6.0 + 7.0 * z2 * z2 / 360.0;
53 b[i] = ratio / tau / tau;
54 logRatio = -z2 / 6.0 + z2 * z2 / 180.0;
55 } else {
56 logRatio =
57 std::log(2.0) + std::log(z) - z - std::log(-std::expm1(-2 * z));
58 b[i] = std::exp(logRatio) / tau / tau;
59 }
60 d[i] = 2 * omega * std::tanh(z / 2) / tau;
61 logMeasurePerBead_ += 0.5 * logRatio;
62 }
63 b_ = eig.eigenvectors() * b.asDiagonal() * eig.eigenvectors().transpose();
64 d_ = eig.eigenvectors() * d.asDiagonal() * eig.eigenvectors().transpose();
65 if (!b_.allFinite() || !d_.allFinite() ||
66 !std::isfinite(logMeasurePerBead_)) {
67 throw std::invalid_argument("harmonic reference coefficients overflow");
68 }
69 }
70
71 const Eigen::MatrixXd &link() const { return b_; }
72 const Eigen::MatrixXd &onsite() const { return d_; }
73 double logMeasureCorrection(long beads) const {
74 if (beads < 1)
75 throw std::invalid_argument("ring requires at least one bead");
76 return beads * logMeasurePerBead_;
77 }
78
80 double correction(const Eigen::MatrixXd &q) const {
81 validate(q);
82 double value = 0.0;
83 for (Eigen::Index j = 0; j < q.rows(); ++j) {
84 const Eigen::VectorXd x = q.row(j).transpose() - center_;
85 const Eigen::VectorXd dx =
86 q.row((j + 1) % q.rows()).transpose() - q.row(j).transpose();
87 value += 0.5 * x.dot((d_ - h0_) * x) + 0.5 * dx.dot(b_ * dx);
88 }
89 return value;
90 }
91
92 Eigen::MatrixXd gradientCorrection(const Eigen::MatrixXd &q) const {
93 validate(q);
94 Eigen::MatrixXd result(q.rows(), q.cols());
95 for (Eigen::Index j = 0; j < q.rows(); ++j) {
96 const Eigen::VectorXd x = q.row(j).transpose() - center_;
97 const Eigen::VectorXd lap =
98 2 * q.row(j).transpose() - q.row((j + 1) % q.rows()).transpose() -
99 q.row((j + q.rows() - 1) % q.rows()).transpose();
100 result.row(j) = ((d_ - h0_) * x + b_ * lap).transpose();
101 }
102 return result;
103 }
104
106 Eigen::MatrixXd hessianCorrection(long beads) const {
107 if (beads < 1)
108 throw std::invalid_argument("ring requires at least one bead");
109 const Eigen::Index f = center_.size();
110 Eigen::MatrixXd result = Eigen::MatrixXd::Zero(beads * f, beads * f);
111 for (long j = 0; j < beads; ++j) {
112 const long k = (j + 1) % beads;
113 result.block(j * f, j * f, f, f) += d_ - h0_;
114 result.block(j * f, j * f, f, f) += b_;
115 result.block(k * f, k * f, f, f) += b_;
116 result.block(j * f, k * f, f, f) -= b_;
117 result.block(k * f, j * f, f, f) -= b_;
118 }
119 return result;
120 }
121
122private:
123 void validate(const Eigen::MatrixXd &q) const {
124 if (q.rows() < 1 || q.cols() != center_.size() || !q.allFinite()) {
125 throw std::invalid_argument(
126 "ring coordinates have invalid shape or values");
127 }
128 }
129 Eigen::MatrixXd h0_, b_, d_;
130 Eigen::VectorXd center_;
132};
133} // namespace eonc::tunneling
void validate(const Eigen::MatrixXd &q) const
Eigen::MatrixXd gradientCorrection(const Eigen::MatrixXd &q) const
double correction(const Eigen::MatrixXd &q) const
Rows are beads; columns are ordered mass-weighted coordinates.
Eigen::MatrixXd hessianCorrection(long beads) const
Flattening order is bead-major, including the N=1 and N=2 cycles.
HarmonicReferenceAction(const Eigen::MatrixXd &h0, const Eigen::VectorXd &center, double tau)