6#include <Eigen/Eigenvalues>
15double lnTwoSinh(
double x) {
16 if (!(x > 0.0) || !std::isfinite(x)) {
17 throw std::invalid_argument(
"the sinh argument must be positive");
20 return x + std::log1p(-std::exp(-2.0 * x));
22 return std::log(2.0 * std::sinh(x));
25Eigen::VectorXi mobileAtoms(
const Matter &matter) {
27 Eigen::VectorXi ids(n);
29 for (
long atom = 0; atom < n; ++atom) {
34 ids.conservativeResize(count);
38Eigen::VectorXd massWeightedTangent(
const Matter &matter,
39 const Eigen::VectorXi &mobile,
41 if (cartesian.rows() != matter.
numberOfAtoms() || cartesian.cols() != 3) {
42 throw std::invalid_argument(
"the tangent is not N by 3");
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);
49 throw std::invalid_argument(
"a perpendicular mode needs a positive mass");
51 const double scale = std::sqrt(mass);
52 for (
int dim = 0; dim < 3; ++dim) {
53 tangent(3 * k + dim) = cartesian(atom, dim) * scale;
63Eigen::MatrixXd rigidGenerators(
const Matter &matter,
64 const Eigen::VectorXi &mobile) {
67 diff.rowwise() -= raw.row(0);
69 whole.rowwise() += raw.row(0);
70 Eigen::RowVector3d com = Eigen::RowVector3d::Zero();
72 for (
long k = 0; k < mobile.size(); ++k) {
73 const double mass = matter.
getMass(mobile(k));
74 com += mass * whole.row(mobile(k));
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();
86 out.block(3 * k, 3 + c, 3, 1) = scale * axis.cross(x);
92AtomMatrix imageTangent(
const std::vector<std::shared_ptr<Matter>> &path,
93 const std::vector<std::shared_ptr<AtomMatrix>> &tangent,
95 const long last =
static_cast<long>(path.size()) - 1;
97 return path[0]->pbc(path[1]->getPositions() - path[0]->getPositions());
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());
104 if (
static_cast<size_t>(index) < tangent.size() && tangent[index]) {
105 return *tangent[index];
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());
115 if (!(hbarOmegaEv > 0.0) || !std::isfinite(hbarOmegaEv)) {
116 throw std::invalid_argument(
117 "a perpendicular mode needs a positive hbar omega");
119 if (!(temperature > 0.0)) {
120 return 0.5 * hbarOmegaEv;
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");
134 const Eigen::MatrixXd sym = 0.5 * (hessian + hessian.transpose());
139 Eigen::MatrixXd unit(n, removed.cols());
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");
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;
152 Eigen::MatrixXd complement = Eigen::MatrixXd::Identity(n, n);
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);
160 if (complement.cols() == 0) {
161 return Eigen::VectorXd();
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");
169 return solver.eigenvalues();
173 const Eigen::MatrixXd &removed,
174 double temperature) {
177 for (
long i = 0; i < lambda.size(); ++i) {
178 if (!(lambda(i) > 0.0)) {
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");
193 const double norm = tangent.norm();
194 if (!(norm > 0.0) || !std::isfinite(norm)) {
195 throw std::invalid_argument(
"the tangent is zero");
201 const Eigen::VectorXd &tangent,
202 double temperature) {
205 for (
long i = 0; i < lambda.size(); ++i) {
206 if (!(lambda(i) > 0.0)) {
217 const std::vector<std::shared_ptr<AtomMatrix>> &tangent,
218 double temperature,
const Parameters ¶ms) {
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)];
227 const Eigen::VectorXi mobile = mobileAtoms(matter);
228 if (mobile.size() == 0) {
229 freeEnergy.push_back(potential);
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");
238 std::vector<Eigen::VectorXd> removed;
240 const Eigen::MatrixXd generators = rigidGenerators(matter, mobile);
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));
252 if (image > 0 && image < last) {
253 removed.push_back(massWeightedTangent(
254 matter, mobile, imageTangent(path, tangent, image)));
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];
261 weighted, columns, temperature));
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
const AtomMatrix & getPositions() const
long int numberOfAtoms() const
AtomMatrix pbc(const AtomMatrix &diff) const
double getMass(long int atom) const
int getFixed(long int atom) const
1 if every Cartesian axis of the atom is fixed, else 0.
void writeHessianFile(bool on) noexcept
Whether a finished Hessian goes to hessian.dat (on by default).
MatrixXd getHessian(Matter *matterIn, const VectorXi &atomsIn)
long int numberOfAtoms() const
double getPotentialEnergy() const
constexpr double kHbar
hbar in eV^0.5 amu^0.5 Angstrom, correctly rounded from the exact SI values (h = 6....
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.
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 ¶ms)
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