Loading...
Searching...
No Matches
GleThermostat.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include <fstream>
13#include <limits>
14#include <sstream>
15#include <vector>
16
17#include <unsupported/Eigen/MatrixFunctions>
18
19#include "eon/GleThermostat.h"
20
21namespace eonc {
22
23MatrixXd GleThermostat::loadDriftMatrix(const std::string &path) {
24 std::ifstream in(path);
25 if (!in) {
26 return MatrixXd();
27 }
28 std::vector<std::vector<double>> rows;
29 std::string line;
30 while (std::getline(in, line)) {
31 const auto hash = line.find('#');
32 if (hash != std::string::npos) {
33 line = line.substr(0, hash);
34 }
35 std::istringstream ss(line);
36 std::vector<double> row;
37 double v;
38 while (ss >> v) {
39 row.push_back(v);
40 }
41 if (!row.empty()) {
42 rows.push_back(std::move(row));
43 }
44 }
45 const auto n = static_cast<long>(rows.size());
46 if (n == 0) {
47 return MatrixXd();
48 }
49 MatrixXd a(n, n);
50 for (long i = 0; i < n; ++i) {
51 if (static_cast<long>(rows[i].size()) != n) {
52 return MatrixXd();
53 }
54 for (long j = 0; j < n; ++j) {
55 a(i, j) = rows[i][j];
56 }
57 }
58 return a;
59}
60
61GleThermostat::GleThermostat(const MatrixXd &a_drift, double kbt,
62 double dt_half, long n_dof) {
63 const long dim = a_drift.rows();
64 if (dim == 0 || a_drift.cols() != dim || !(kbt > 0.0) || !(dt_half > 0.0) ||
65 n_dof <= 0 || !a_drift.allFinite() || !std::isfinite(kbt) ||
66 !std::isfinite(dt_half)) {
67 return;
68 }
69 m_T = (-a_drift * dt_half).exp();
70 if (!m_T.allFinite()) {
71 return;
72 }
73 // Fluctuation-dissipation: S S^T = kbt (I - T T^T). The right-hand
74 // side is symmetric positive semi-definite; factor through its
75 // eigendecomposition with negative rounding clamped so a
76 // deterministic (dt -> 0) or overdamped block cannot poison the
77 // Cholesky.
78 const MatrixXd cov =
79 kbt * (MatrixXd::Identity(dim, dim) - m_T * m_T.transpose());
80 Eigen::SelfAdjointEigenSolver<MatrixXd> es(cov);
81 if (es.info() != Eigen::Success) {
82 return;
83 }
84 const double scale = es.eigenvalues().cwiseAbs().maxCoeff();
85 const double tolerance =
86 64.0 * std::numeric_limits<double>::epsilon() * dim * scale;
87 if (!es.eigenvalues().allFinite() ||
88 es.eigenvalues().minCoeff() < -tolerance) {
89 return;
90 }
91 m_S = es.eigenvectors() *
92 es.eigenvalues().cwiseMax(0.0).cwiseSqrt().asDiagonal();
93 m_Z = MatrixXd::Zero(dim, n_dof);
94 m_valid = true;
95}
96
97void GleThermostat::apply(VectorXd &vel, const VectorXd &masses3N,
98 const std::function<double()> &gauss) {
99 if (!m_valid || vel.size() != m_Z.cols() || masses3N.size() != vel.size()) {
100 return;
101 }
102 if ((masses3N.array() <= 0.0).any()) {
103 return;
104 }
105 const VectorXd sqrtM = masses3N.cwiseSqrt();
106 m_Z.row(0) = (vel.cwiseProduct(sqrtM)).transpose();
107 MatrixXd xi(m_Z.rows(), m_Z.cols());
108 for (long i = 0; i < xi.rows(); ++i) {
109 for (long j = 0; j < xi.cols(); ++j) {
110 xi(i, j) = gauss();
111 }
112 }
113 m_Z = m_T * m_Z + m_S * xi;
114 vel = m_Z.row(0).transpose().cwiseQuotient(sqrtM);
115}
116
117} // namespace eonc
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, eOnStorageOrder > MatrixXd
Definition Eigen.h:33
MatrixXd m_S
noise factor, S S^T = kbt (I - T T^T)
MatrixXd m_T
exp(-A dt/2)
void apply(VectorXd &vel, const VectorXd &masses3N, const std::function< double()> &gauss)
One OU half-step on the mass-scaled velocities; gauss supplies standard-normal draws from the job's d...
MatrixXd m_Z
extended state, (1 + n_aux) x n_dof
GleThermostat(const MatrixXd &a_drift, double kbt, double dt_half, long n_dof)
Build from the drift matrix, target kB*T (eV), the half-step dt (internal units), and the number of f...
static MatrixXd loadDriftMatrix(const std::string &path)
Load a drift matrix from a gle4md-layout text file ('#' starts a comment).
RAII resource manager for the ARTn C library with global synchronization.