35 static Eigen::MatrixXd
matrix(
const nlohmann::json &value,
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>();
51 throw std::invalid_argument(
"physical bath model matrix is not finite");
55 std::ifstream stream(filename);
57 throw std::runtime_error(
"cannot read physical bath model " + filename);
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");
68 out.
qualification = data.at(
"qualification").get<std::string>();
70 out.
bath = std::make_shared<PhysicalQuadraticBath>();
71 out.
bath->coupling =
matrix(data.at(
"coupling"));
72 out.
bath->curvatures =
vector(data.at(
"curvatures"));
74 if (out.
center.size() != dimension ||
76 out.
bathBasis.rows() != dimension || r + b != dimension || r < 1 ||
80 throw std::invalid_argument(
81 "physical bath model dimension or qualification mismatch");
82 Eigen::MatrixXd basis(dimension, dimension);
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");
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;
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);
123 result.
value = initial + weight * change;
125 result.
first.resize(coordinate.size());
126 for (
long i = 0; i < coordinate.size(); ++i)
127 result.
first[i] = first * coordinate[i] * change;
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)
135 second * coordinate[i] * coordinate[j] * change;
141 if (data.contains(
"source_model")) {
142 auto model = std::make_shared<const DerivativeRbfModel>(
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,
149 std::vector<Eigen::MatrixXd> &h) {
150 const auto value = model->evaluate(q);
156 if (data.contains(
"potential_model")) {
161 throw std::invalid_argument(
162 "physical bath potential interpolant dimension mismatch");