Loading...
Searching...
No Matches
QuantumFreeEnergy.cpp
Go to the documentation of this file.
2
3#include "eon/Hessian.h"
4#include "eon/Tunneling.h"
5
6#include <Eigen/Eigenvalues>
7#include <Eigen/QR>
8#include <array>
9#include <cmath>
10#include <stdexcept>
11
12namespace eonc {
13namespace {
14
15double lnTwoSinh(double x) {
16 if (!(x > 0.0) || !std::isfinite(x)) {
17 throw std::invalid_argument("the sinh argument must be positive");
18 }
19 if (x > 16.0) {
20 return x + std::log1p(-std::exp(-2.0 * x));
21 }
22 return std::log(2.0 * std::sinh(x));
23}
24
25Eigen::VectorXi mobileAtoms(const Matter &matter) {
26 const long n = matter.numberOfAtoms();
27 Eigen::VectorXi ids(n);
28 long count = 0;
29 for (long atom = 0; atom < n; ++atom) {
30 if (!matter.getFixed(atom)) {
31 ids(count++) = atom;
32 }
33 }
34 ids.conservativeResize(count);
35 return ids;
36}
37
38Eigen::VectorXd massWeightedTangent(const Matter &matter,
39 const Eigen::VectorXi &mobile,
40 const AtomMatrix &cartesian) {
41 if (cartesian.rows() != matter.numberOfAtoms() || cartesian.cols() != 3) {
42 throw std::invalid_argument("the tangent is not N by 3");
43 }
44 Eigen::VectorXd tangent(3 * mobile.size());
45 for (long k = 0; k < mobile.size(); ++k) {
46 const long atom = mobile(k);
47 const double mass = matter.getMass(atom);
48 if (!(mass > 0.0)) {
49 throw std::invalid_argument("a perpendicular mode needs a positive mass");
50 }
51 const double scale = std::sqrt(mass);
52 for (int dim = 0; dim < 3; ++dim) {
53 tangent(3 * k + dim) = cartesian(atom, dim) * scale;
54 }
55 }
56 return tangent;
57}
58
59// Three translations, then three rotations about the centre of mass, over
60// the mobile atoms in mass-weighted coordinates. Positions are taken at
61// their minimum image from atom 0, so a cluster across a cell face turns as
62// one.
63Eigen::MatrixXd rigidGenerators(const Matter &matter,
64 const Eigen::VectorXi &mobile) {
65 const AtomMatrix raw = matter.getPositions();
66 AtomMatrix diff = raw;
67 diff.rowwise() -= raw.row(0);
68 AtomMatrix whole = matter.pbc(diff);
69 whole.rowwise() += raw.row(0);
70 Eigen::RowVector3d com = Eigen::RowVector3d::Zero();
71 double total = 0.0;
72 for (long k = 0; k < mobile.size(); ++k) {
73 const double mass = matter.getMass(mobile(k));
74 com += mass * whole.row(mobile(k));
75 total += mass;
76 }
77 com /= total;
78 Eigen::MatrixXd out = Eigen::MatrixXd::Zero(3 * mobile.size(), 6);
79 for (long k = 0; k < mobile.size(); ++k) {
80 const double scale = std::sqrt(matter.getMass(mobile(k)));
81 const Eigen::Vector3d x = (whole.row(mobile(k)) - com).transpose();
82 for (int c = 0; c < 3; ++c) {
83 out(3 * k + c, c) = scale;
84 Eigen::Vector3d axis = Eigen::Vector3d::Zero();
85 axis(c) = 1.0;
86 out.block(3 * k, 3 + c, 3, 1) = scale * axis.cross(x);
87 }
88 }
89 return out;
90}
91
92AtomMatrix imageTangent(const std::vector<std::shared_ptr<Matter>> &path,
93 const std::vector<std::shared_ptr<AtomMatrix>> &tangent,
94 long index) {
95 const long last = static_cast<long>(path.size()) - 1;
96 if (index <= 0) {
97 return path[0]->pbc(path[1]->getPositions() - path[0]->getPositions());
98 }
99 if (index >= last) {
100 return path[static_cast<size_t>(last - 1)]->pbc(
101 path[static_cast<size_t>(last)]->getPositions() -
102 path[static_cast<size_t>(last - 1)]->getPositions());
103 }
104 if (static_cast<size_t>(index) < tangent.size() && tangent[index]) {
105 return *tangent[index];
106 }
107 return path[static_cast<size_t>(index - 1)]->pbc(
108 path[static_cast<size_t>(index + 1)]->getPositions() -
109 path[static_cast<size_t>(index - 1)]->getPositions());
110}
111
112} // namespace
113
114double harmonicModeFreeEnergy(double hbarOmegaEv, double temperature) {
115 if (!(hbarOmegaEv > 0.0) || !std::isfinite(hbarOmegaEv)) {
116 throw std::invalid_argument(
117 "a perpendicular mode needs a positive hbar omega");
118 }
119 if (!(temperature > 0.0)) {
120 return 0.5 * hbarOmegaEv;
121 }
122 const double x = hbarOmegaEv / (2.0 * tunneling::kBoltzmann * temperature);
123 return tunneling::kBoltzmann * temperature * lnTwoSinh(x);
124}
125
126Eigen::VectorXd complementEigenvalues(const Eigen::MatrixXd &hessian,
127 const Eigen::MatrixXd &removed) {
128 const long n = hessian.rows();
129 if (hessian.cols() != n || n < 1 ||
130 (removed.cols() > 0 && removed.rows() != n)) {
131 throw std::invalid_argument(
132 "the Hessian and the removed directions differ in size");
133 }
134 const Eigen::MatrixXd sym = 0.5 * (hessian + hessian.transpose());
135 // Unit columns, so that the rank decision compares directions and not
136 // their lengths. A column at round-off of the longest (the rotation about
137 // a linear molecule's axis) removes nothing, rather than a direction of
138 // noise scaled up.
139 Eigen::MatrixXd unit(n, removed.cols());
140 long kept = 0;
141 const double largest =
142 removed.cols() > 0 ? removed.colwise().norm().maxCoeff() : 0.0;
143 if (!std::isfinite(largest)) {
144 throw std::invalid_argument("a removed direction is not finite");
145 }
146 for (long c = 0; c < removed.cols(); ++c) {
147 const double norm = removed.col(c).norm();
148 if (norm > 1e-8 * largest) {
149 unit.col(kept++) = removed.col(c) / norm;
150 }
151 }
152 Eigen::MatrixXd complement = Eigen::MatrixXd::Identity(n, n);
153 if (kept > 0) {
154 Eigen::ColPivHouseholderQR<Eigen::MatrixXd> qr(unit.leftCols(kept));
155 qr.setThreshold(1e-8);
156 const long rank = qr.rank();
157 const Eigen::MatrixXd q = qr.householderQ();
158 complement = q.rightCols(n - rank);
159 }
160 if (complement.cols() == 0) {
161 return Eigen::VectorXd();
162 }
163 const Eigen::MatrixXd reduced = complement.transpose() * sym * complement;
164 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> solver(
165 0.5 * (reduced + reduced.transpose()), Eigen::EigenvaluesOnly);
166 if (solver.info() != Eigen::Success) {
167 throw std::runtime_error("the projected Hessian did not diagonalize");
168 }
169 return solver.eigenvalues();
170}
171
172double complementHarmonicFreeEnergy(const Eigen::MatrixXd &hessian,
173 const Eigen::MatrixXd &removed,
174 double temperature) {
175 const Eigen::VectorXd lambda = complementEigenvalues(hessian, removed);
176 double sum = 0.0;
177 for (long i = 0; i < lambda.size(); ++i) {
178 if (!(lambda(i) > 0.0)) {
179 continue;
180 }
181 const double hw = tunneling::kHbar * std::sqrt(lambda(i));
182 sum += harmonicModeFreeEnergy(hw, temperature);
183 }
184 return sum;
185}
186
187Eigen::VectorXd perpendicularEigenvalues(const Eigen::MatrixXd &hessian,
188 const Eigen::VectorXd &tangent) {
189 const long n = hessian.rows();
190 if (hessian.cols() != n || tangent.size() != n || n < 1) {
191 throw std::invalid_argument("the Hessian and the tangent sizes differ");
192 }
193 const double norm = tangent.norm();
194 if (!(norm > 0.0) || !std::isfinite(norm)) {
195 throw std::invalid_argument("the tangent is zero");
196 }
197 return complementEigenvalues(hessian, tangent / norm);
198}
199
200double perpendicularHarmonicFreeEnergy(const Eigen::MatrixXd &hessian,
201 const Eigen::VectorXd &tangent,
202 double temperature) {
203 const Eigen::VectorXd lambda = perpendicularEigenvalues(hessian, tangent);
204 double sum = 0.0;
205 for (long i = 0; i < lambda.size(); ++i) {
206 if (!(lambda(i) > 0.0)) {
207 continue;
208 }
209 const double hw = tunneling::kHbar * std::sqrt(lambda(i));
210 sum += harmonicModeFreeEnergy(hw, temperature);
211 }
212 return sum;
213}
214
215std::vector<double>
216quantumFreeEnergies(const std::vector<std::shared_ptr<Matter>> &path,
217 const std::vector<std::shared_ptr<AtomMatrix>> &tangent,
218 double temperature, const Parameters &params) {
219 std::vector<double> freeEnergy;
220 freeEnergy.reserve(path.size());
221 const long last = static_cast<long>(path.size()) - 1;
222 std::array<bool, 3> rotations{{false, false, false}};
223 bool rotationsSet = false;
224 for (long image = 0; image <= last; ++image) {
225 Matter &matter = *path[static_cast<size_t>(image)];
226 const double potential = matter.getPotentialEnergy();
227 const Eigen::VectorXi mobile = mobileAtoms(matter);
228 if (mobile.size() == 0) {
229 freeEnergy.push_back(potential);
230 continue;
231 }
232 Hessian hess(params, &matter);
233 hess.writeHessianFile(false);
234 const Eigen::MatrixXd weighted = hess.getHessian(&matter, mobile);
235 if (weighted.rows() != 3 * mobile.size()) {
236 throw std::runtime_error("the image Hessian is empty");
237 }
238 std::vector<Eigen::VectorXd> removed;
239 if (mobile.size() == matter.numberOfAtoms()) {
240 const Eigen::MatrixXd generators = rigidGenerators(matter, mobile);
241 if (!rotationsSet) {
242 rotations = tunneling::rotationZeroModes(weighted, generators).zero;
243 rotationsSet = true;
244 }
245 for (int c = 0; c < 3; ++c) {
246 removed.emplace_back(generators.col(c));
247 if (rotations[static_cast<size_t>(c)]) {
248 removed.emplace_back(generators.col(3 + c));
249 }
250 }
251 }
252 if (image > 0 && image < last) {
253 removed.push_back(massWeightedTangent(
254 matter, mobile, imageTangent(path, tangent, image)));
255 }
256 Eigen::MatrixXd columns(weighted.rows(), static_cast<long>(removed.size()));
257 for (size_t k = 0; k < removed.size(); ++k) {
258 columns.col(static_cast<long>(k)) = removed[k];
259 }
260 freeEnergy.push_back(potential + complementHarmonicFreeEnergy(
261 weighted, columns, temperature));
262 }
263 return freeEnergy;
264}
265
266} // namespace eonc
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
const AtomMatrix & getPositions() const
Definition Matter.cpp:378
long int numberOfAtoms() const
Definition Matter.cpp:343
AtomMatrix pbc(const AtomMatrix &diff) const
Definition Matter.cpp:943
double getMass(long int atom) const
Definition Matter.cpp:548
int getFixed(long int atom) const
1 if every Cartesian axis of the atom is fixed, else 0.
Definition Matter.cpp:575
void writeHessianFile(bool on) noexcept
Whether a finished Hessian goes to hessian.dat (on by default).
Definition Hessian.h:61
MatrixXd getHessian(Matter *matterIn, const VectorXi &atomsIn)
Definition Hessian.cpp:146
long int numberOfAtoms() const
Definition Matter.cpp:343
double getPotentialEnergy() const
Definition Matter.cpp:624
constexpr double kHbar
hbar in eV^0.5 amu^0.5 Angstrom, correctly rounded from the exact SI values (h = 6....
Definition Tunneling.h:39
RotationZeroModes rotationZeroModes(const MatrixXd &hess, const MatrixXd &generators, double fraction)
constexpr double kBoltzmann
Boltzmann constant in eV / K, correctly rounded from the exact 1.380649e-23 J / K.
Definition Tunneling.h:43
RAII resource manager for the ARTn C library with global synchronization.
double complementHarmonicFreeEnergy(const Eigen::MatrixXd &hessian, const Eigen::MatrixXd &removed, double temperature)
Sum of harmonicModeFreeEnergy over the positive eigenvalues on the complement of removed.
double perpendicularHarmonicFreeEnergy(const Eigen::MatrixXd &hessian, const Eigen::VectorXd &tangent, double temperature)
Sum of harmonicModeFreeEnergy over the positive perpendicular eigenvalues.
Eigen::VectorXd perpendicularEigenvalues(const Eigen::MatrixXd &hessian, const Eigen::VectorXd &tangent)
Eigenvalues of the mass-weighted Hessian after the tangent direction is removed: one fewer than the H...
std::vector< double > quantumFreeEnergies(const std::vector< std::shared_ptr< Matter > > &path, const std::vector< std::shared_ptr< AtomMatrix > > &tangent, double temperature, const Parameters &params)
F(s) = V(s) + the harmonic free energy of the vibrations, one entry per image.
double harmonicModeFreeEnergy(double hbarOmegaEv, double temperature)
kT ln(2 sinh(beta hbar omega / 2)) in eV.
Eigen::VectorXd complementEigenvalues(const Eigen::MatrixXd &hessian, const Eigen::MatrixXd &removed)
Eigenvalues of the mass-weighted Hessian on the orthogonal complement of the columns of removed,...
std::array< bool, 3 > zero
Definition Tunneling.h:599