Loading...
Searching...
No Matches
PhysicalBathModel.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
10#include <fstream>
11#include <memory>
12#include <nlohmann/json.hpp>
13#include <string>
14
15namespace eonc::tunneling {
16
18 Eigen::VectorXd center;
19 Eigen::MatrixXd reactionBasis, bathBasis;
20 std::shared_ptr<PhysicalQuadraticBath> bath;
21 std::shared_ptr<const DerivativeRbfModel> potentialModel;
22 std::string qualification;
24
25 static Eigen::VectorXd vector(const nlohmann::json &value) {
26 if (!value.is_array())
27 throw std::invalid_argument("physical bath model vector is not an array");
28 Eigen::VectorXd out(value.size());
29 for (size_t i = 0; i < value.size(); ++i)
30 out[i] = value.at(i).get<double>();
31 if (!out.allFinite())
32 throw std::invalid_argument("physical bath model vector is not finite");
33 return out;
34 }
35 static Eigen::MatrixXd matrix(const nlohmann::json &value,
36 long columns = -1) {
37 if (!value.is_array())
38 throw std::invalid_argument("physical bath model matrix is not an array");
39 const long cols = value.empty() ? std::max(0L, columns)
40 : static_cast<long>(value.at(0).size());
41 Eigen::MatrixXd out(value.size(), cols);
42 for (size_t i = 0; i < value.size(); ++i) {
43 if (!value.at(i).is_array() ||
44 value.at(i).size() != static_cast<size_t>(cols))
45 throw std::invalid_argument(
46 "physical bath model matrix has inconsistent rows");
47 for (long j = 0; j < cols; ++j)
48 out(i, j) = value.at(i).at(j).get<double>();
49 }
50 if (!out.allFinite())
51 throw std::invalid_argument("physical bath model matrix is not finite");
52 return out;
53 }
54 static PhysicalBathModel read(const std::string &filename, long dimension) {
55 std::ifstream stream(filename);
56 if (!stream)
57 throw std::runtime_error("cannot read physical bath model " + filename);
58 nlohmann::json data;
59 stream >> data;
60 if (data.at("format") != "eon.physical_quadratic_bath.v1" ||
61 data.at("callback") != "frozen")
62 throw std::invalid_argument(
63 "physical bath model requires v1 and frozen callback convention");
65 out.center = vector(data.at("center"));
66 out.reactionBasis = matrix(data.at("reaction_basis"));
67 out.bathBasis = matrix(data.at("bath_basis"));
68 out.qualification = data.at("qualification").get<std::string>();
69 out.stationaryTolerance = data.at("stationary_tolerance").get<double>();
70 out.bath = std::make_shared<PhysicalQuadraticBath>();
71 out.bath->coupling = matrix(data.at("coupling"));
72 out.bath->curvatures = vector(data.at("curvatures"));
73 const long r = out.reactionBasis.cols(), b = out.bathBasis.cols();
74 if (out.center.size() != dimension ||
75 out.reactionBasis.rows() != dimension ||
76 out.bathBasis.rows() != dimension || r + b != dimension || r < 1 ||
77 out.bath->curvatures.size() != b || out.qualification.empty() ||
78 !(out.stationaryTolerance > 0) ||
79 !std::isfinite(out.stationaryTolerance))
80 throw std::invalid_argument(
81 "physical bath model dimension or qualification mismatch");
82 Eigen::MatrixXd basis(dimension, dimension);
83 basis << out.reactionBasis, out.bathBasis;
84 if (!(basis.transpose() * basis)
85 .isApprox(Eigen::MatrixXd::Identity(dimension, dimension), 1e-10))
86 throw std::invalid_argument(
87 "physical bath model basis is not complete and orthonormal");
88 // Reactant coordinates define q=0 in the physical mass-weighted input.
89 if (out.center.norm() > 1e-10)
90 throw std::invalid_argument(
91 "physical bath model center must match the input reactant");
92 out.bath->validate(r);
93 if (data.contains("curvature_model")) {
94 const auto &spec = data.at("curvature_model");
95 if (spec.at("kind") != "convex_endpoint_blend")
96 throw std::invalid_argument(
97 "unsupported physical bath curvature model");
98 const Eigen::MatrixXd endpoint = matrix(spec.at("endpoint"));
99 const Eigen::VectorXd coordinate = vector(spec.at("coordinate"));
100 if (endpoint.rows() != b || endpoint.cols() != b ||
101 coordinate.size() != r ||
102 !endpoint.isApprox(endpoint.transpose(), 1e-12))
103 throw std::invalid_argument(
104 "physical bath curvature model dimension or symmetry mismatch");
105 Eigen::LLT<Eigen::MatrixXd> positive(endpoint);
106 if (positive.info() != Eigen::Success)
107 throw std::invalid_argument(
108 "physical bath curvature endpoint is not positive definite");
109 const Eigen::MatrixXd initial = out.bath->curvatures.asDiagonal();
110 const Eigen::MatrixXd change = endpoint - initial;
111 out.bath->curvature = [initial, change,
112 coordinate](const Eigen::VectorXd &q, int order) {
114 const double p = coordinate.dot(q);
115 double weight = 0, first = 0, second = 0;
116 if (p >= 1)
117 weight = 1;
118 else if (p > 0) {
119 weight = p * p * p * (10 + p * (-15 + 6 * p));
120 first = 30 * p * p * (1 - p) * (1 - p);
121 second = 60 * p * (1 - p) * (1 - 2 * p);
122 }
123 result.value = initial + weight * change;
124 if (order >= 1) {
125 result.first.resize(coordinate.size());
126 for (long i = 0; i < coordinate.size(); ++i)
127 result.first[i] = first * coordinate[i] * change;
128 }
129 if (order >= 2) {
130 result.second.resize(coordinate.size());
131 for (long i = 0; i < coordinate.size(); ++i) {
132 result.second[i].resize(coordinate.size());
133 for (long j = 0; j < coordinate.size(); ++j)
134 result.second[i][j] =
135 second * coordinate[i] * coordinate[j] * change;
136 }
137 }
138 return result;
139 };
140 }
141 if (data.contains("source_model")) {
142 auto model = std::make_shared<const DerivativeRbfModel>(
143 DerivativeRbfModel::fromJson(data.at("source_model")));
144 if (model->dimension() != r || model->outputs() != b)
145 throw std::invalid_argument(
146 "physical bath source interpolant dimension mismatch");
147 out.bath->source = [model](const Eigen::VectorXd &q, Eigen::VectorXd &s,
148 Eigen::MatrixXd &j,
149 std::vector<Eigen::MatrixXd> &h) {
150 const auto value = model->evaluate(q);
151 s = value.value;
152 j = value.jacobian;
153 h = value.hessians;
154 };
155 }
156 if (data.contains("potential_model")) {
157 out.potentialModel = std::make_shared<const DerivativeRbfModel>(
158 DerivativeRbfModel::fromJson(data.at("potential_model")));
159 if (out.potentialModel->dimension() != r ||
160 out.potentialModel->outputs() != 1)
161 throw std::invalid_argument(
162 "physical bath potential interpolant dimension mismatch");
163 }
164 return out;
165 }
166
167 Eigen::VectorXd project(const Eigen::VectorXd &q) const {
168 return reactionBasis.transpose() * (q - center);
169 }
170 Eigen::VectorXd frozen(const Eigen::VectorXd &q) const {
171 return center + reactionBasis * q;
172 }
173 std::vector<Eigen::VectorXd>
174 reconstruct(const std::vector<Eigen::VectorXd> &q, double spring) const {
175 const auto eliminated = bath->evaluate(q, spring);
176 std::vector<Eigen::VectorXd> out(q.size());
177 for (size_t j = 0; j < q.size(); ++j)
178 out[j] = frozen(q[j]) + bathBasis * eliminated.bathPositions[j];
179 return out;
180 }
181};
182} // namespace eonc::tunneling
static DerivativeRbfModel fromJson(const nlohmann::json &data)
Eigen::VectorXd frozen(const Eigen::VectorXd &q) const
static Eigen::MatrixXd matrix(const nlohmann::json &value, long columns=-1)
static PhysicalBathModel read(const std::string &filename, long dimension)
Eigen::VectorXd project(const Eigen::VectorXd &q) const
std::vector< Eigen::VectorXd > reconstruct(const std::vector< Eigen::VectorXd > &q, double spring) const
std::shared_ptr< PhysicalQuadraticBath > bath
static Eigen::VectorXd vector(const nlohmann::json &value)
std::shared_ptr< const DerivativeRbfModel > potentialModel
std::vector< std::vector< Eigen::MatrixXd > > second