54 {
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");
64 PhysicalBathModel out;
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
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) {
113 PhysicalQuadraticBath::CurvatureDerivatives result;
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>(
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>(
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 }
static DerivativeRbfModel fromJson(const nlohmann::json &data)
static Eigen::MatrixXd matrix(const nlohmann::json &value, long columns=-1)
static Eigen::VectorXd vector(const nlohmann::json &value)