Loading...
Searching...
No Matches
CyclicBlockBath.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 <cmath>
10#include <limits>
11#include <stdexcept>
12#include <vector>
13
14namespace eonc::tunneling {
15
19public:
20 CyclicBlockBath(const std::vector<Eigen::MatrixXd> &blocks, double spring)
21 : count_(blocks.size()),
22 width_(blocks.empty() ? 0 : blocks.front().rows()) {
23 if (count_ == 0 || width_ == 0 || !std::isfinite(spring) || spring < 0)
24 throw std::invalid_argument("invalid cyclic bath dimensions or spring");
25 for (const auto &block : blocks) {
26 if (block.rows() != width_ || block.cols() != width_ ||
27 !block.allFinite() ||
28 (block - block.transpose()).norm() >
29 64 * std::numeric_limits<double>::epsilon() *
30 std::max(1.0, block.norm()))
31 throw std::invalid_argument(
32 "invalid or nonsymmetric physical bath block");
33 Eigen::LLT<Eigen::MatrixXd> physical(block);
34 if (physical.info() != Eigen::Success)
35 throw std::invalid_argument(
36 "physical bath block is not positive definite");
37 }
38 factors_.resize(count_);
39 next_.resize(count_ - 1);
40 boundary_.resize(count_ - 1);
41 const Eigen::MatrixXd identity = Eigen::MatrixXd::Identity(width_, width_);
42 std::vector<Eigen::MatrixXd> diagonal = blocks;
43 if (count_ > 1)
44 for (auto &block : diagonal)
45 block += 2 * spring * identity;
46 Eigen::MatrixXd coupling = -(count_ == 2 ? 2 : 1) * spring * identity;
47 for (std::size_t j = 0; j + 1 < count_; ++j) {
48 factor(j, diagonal[j]);
49 boundary_[j] = factors_[j].solve(coupling);
50 diagonal.back() -= coupling.transpose() * boundary_[j];
51 if (j + 2 < count_) {
52 const Eigen::MatrixXd adjacent = -spring * identity;
53 next_[j] = factors_[j].solve(adjacent);
54 diagonal[j + 1] -= adjacent.transpose() * next_[j];
55 Eigen::MatrixXd direct = j + 3 == count_
56 ? Eigen::MatrixXd(-spring * identity)
57 : Eigen::MatrixXd::Zero(width_, width_);
58 coupling = direct - adjacent.transpose() * boundary_[j];
59 } else {
60 next_[j] = Eigen::MatrixXd::Zero(width_, width_);
61 }
62 }
63 factor(count_ - 1, diagonal.back());
64 }
65
66 Eigen::MatrixXd solve(const Eigen::MatrixXd &rhs) const {
67 if (rhs.rows() != static_cast<Eigen::Index>(count_) * width_ ||
68 !rhs.allFinite())
69 throw std::invalid_argument("invalid cyclic bath right hand side");
70 Eigen::MatrixXd result = rhs;
71 const Eigen::Index last = static_cast<Eigen::Index>(count_ - 1) * width_;
72 for (std::size_t j = 0; j + 1 < count_; ++j) {
73 const Eigen::Index row = static_cast<Eigen::Index>(j) * width_;
74 const Eigen::MatrixXd current = result.middleRows(row, width_);
75 if (j + 2 < count_)
76 result.middleRows(row + width_, width_) -=
77 next_[j].transpose() * current;
78 result.middleRows(last, width_) -= boundary_[j].transpose() * current;
79 result.middleRows(row, width_) = factors_[j].solve(current);
80 }
81 result.middleRows(last, width_) =
82 factors_.back().solve(result.middleRows(last, width_).eval());
83 for (std::size_t j = count_ - 1; j-- > 0;) {
84 const Eigen::Index row = static_cast<Eigen::Index>(j) * width_;
85 result.middleRows(row, width_) -=
86 boundary_[j] * result.middleRows(last, width_);
87 if (j + 2 < count_)
88 result.middleRows(row, width_) -=
89 next_[j] * result.middleRows(row + width_, width_);
90 }
91 return result;
92 }
93
94 double logDeterminant() const { return logDeterminant_; }
95
96private:
97 void factor(std::size_t index, const Eigen::MatrixXd &matrix) {
98 factors_[index].compute((matrix + matrix.transpose()) * 0.5);
99 if (factors_[index].info() != Eigen::Success)
100 throw std::runtime_error(
101 "cyclic bath Schur block is not positive definite");
102 const Eigen::MatrixXd lower = factors_[index].matrixL();
103 logDeterminant_ += 2 * lower.diagonal().array().log().sum();
104 if (!std::isfinite(logDeterminant_))
105 throw std::runtime_error("nonfinite cyclic bath determinant");
106 }
107
108 std::size_t count_;
109 Eigen::Index width_;
110 std::vector<Eigen::LLT<Eigen::MatrixXd>> factors_;
111 std::vector<Eigen::MatrixXd> next_, boundary_;
112 double logDeterminant_ = 0;
113};
114
115} // namespace eonc::tunneling
std::vector< Eigen::MatrixXd > boundary_
std::vector< Eigen::MatrixXd > next_
void factor(std::size_t index, const Eigen::MatrixXd &matrix)
std::vector< Eigen::LLT< Eigen::MatrixXd > > factors_
Eigen::MatrixXd solve(const Eigen::MatrixXd &rhs) const
CyclicBlockBath(const std::vector< Eigen::MatrixXd > &blocks, double spring)