87 const auto n = initial.size();
88 const Eigen::Index d = metric.
dimension(), k = constraints.
target.size();
91 !(options.
hbar > 0) || !std::isfinite(options.
hbar) ||
101 boundary.
twist.rows() != d || boundary.
twist.cols() != d ||
102 !boundary.
twist.allFinite() ||
103 (boundary.
twist.transpose() * boundary.
twist - Matrix::Identity(d, d))
105 throw std::invalid_argument(
106 "Invalid cyclic potential path configuration");
107 finiteVector(boundary.
offset, d);
108 finiteVector(constraints.
target, k);
109 std::vector<Matrix> normals = constraints.
normals;
110 if (!k && normals.empty()) normals.assign(n,
Matrix(d, 0));
111 if (normals.size() != n)
112 throw std::invalid_argument(
"One constraint block per node required");
113 Matrix gram = Matrix::Zero(k, k);
114 for (std::size_t j = 0; j < n; ++j) {
115 finiteVector(initial[j], d);
116 if (normals[j].rows() != d || normals[j].cols() != k ||
117 !normals[j].allFinite())
118 throw std::invalid_argument(
119 "Matching finite constraint blocks required");
120 gram += normals[j].transpose() * normals[j];
122 if ((gram - Matrix::Identity(k, k)).norm() >
123 1e-10 * std::max<Eigen::Index>(1, k))
124 throw std::invalid_argument(
125 "Global constraint columns must be orthonormal");
126 const double h = options.
duration / n;
127 if (!(h > 0) || !std::isfinite(h))
128 throw std::invalid_argument(
"Finite positive time step required");
130 const Matrix referenceHessian =
132 if (referenceHessian.rows() != d || referenceHessian.cols() != d)
133 throw std::invalid_argument(
134 "Harmonic reference must match the mass dimension");
138 result.
path = initial;
139 auto coordinate = [&](
const Nodes& p) {
141 for (std::size_t j = 0; j < n; ++j)
142 value += normals[j].transpose() * p[j];
145 auto enforce = [&](Nodes& p) {
146 const Vector error = coordinate(p);
147 for (std::size_t j = 0; j < n; ++j)
148 p[j] -= normals[j] * error;
150 enforce(result.
path);
152 if (!std::isfinite(value.energy))
153 throw std::runtime_error(
"Potential energy is not finite");
154 value.gradient = metric.
gradient(value.gradient);
157 auto point = [&](
const Vector& q) {
163 if (values.size() != 1)
164 throw std::runtime_error(
165 "Potential batch returned the wrong number of points");
166 return checkedPoint(values.front());
169 Nodes gradient, full, conditional;
170 std::vector<double> energies;
171 Vector normal, endpointGradient;
172 double endpointEnergy = 0;
173 double action = 0, fullForce = 0, conditionalForce = 0, constraint = 0;
175 auto evaluate = [&](
const Nodes& p) {
177 state.normal = Vector::Zero(k);
178 std::vector<PotentialEvaluation> values;
179 const Vector endpointCoordinate =
183 for (
const auto& q: p)
184 physical.push_back(metric.
physical(q));
185 physical.push_back(metric.
physical(endpointCoordinate));
189 if (values.size() != physical.size())
190 throw std::runtime_error(
191 "Potential batch returned the wrong number of points");
192 for (
auto& value: values)
193 value = checkedPoint(std::move(value));
195 for (
const auto& q: p)
196 values.push_back(point(q));
197 values.push_back(point(endpointCoordinate));
199 for (std::size_t j = 0; j < n; ++j) {
200 const auto& value = values[j];
201 state.energies.push_back(value.energy);
202 state.full.push_back(value.gradient);
204 const auto& endpoint = values.back();
205 state.endpointEnergy = endpoint.energy;
206 state.endpointGradient = endpoint.gradient;
208 const Nodes gradients = state.full;
209 state.full.assign(n, Vector::Zero(d));
210 for (std::size_t j = 0; j < n; ++j) {
211 const bool closing = j + 1 == n;
221 const auto link = reference.
evaluate(p[j], next, left, right,
223 state.action += link.action;
224 state.full[j] += link.leftGradient / h;
227 boundary.
twist.transpose() * link.rightGradient / h;
229 state.full[j + 1] += link.rightGradient / h;
231 for (std::size_t j = 0; j < n; ++j) {
232 state.gradient.push_back(h * state.full[j]);
233 state.normal += normals[j].transpose() * state.full[j];
235 std::max(state.fullForce, state.full[j].norm());
238 state.full[0] = .5 * (state.full[0] + boundary.
twist.transpose() *
240 for (std::size_t j = 0; j < n; ++j) {
244 (p.back() - boundary.
offset));
249 state.full[j] += (2 * p[j] - previous - next) / (h * h);
250 state.gradient.push_back(h * state.full[j]);
251 state.normal += normals[j].transpose() * state.full[j];
253 std::max(state.fullForce, state.full[j].norm());
254 state.action += h * (state.energies[j] - options.
energyShift) +
255 (next - p[j]).squaredNorm() / (2 * h);
257 state.action += .5 * h * (endpoint.energy - state.energies[0]);
259 for (std::size_t j = 0; j < n; ++j) {
260 state.conditional.push_back(state.full[j] -
261 normals[j] * state.normal);
262 state.conditionalForce = std::max(state.conditionalForce,
263 state.conditional.back().norm());
265 state.constraint = coordinate(p).norm();
266 if (!std::isfinite(state.action) || !std::isfinite(state.fullForce) ||
267 !std::isfinite(state.conditionalForce))
268 throw std::runtime_error(
"Path action or residual is not finite");
271 auto pointHessian = [&](
const Vector& q,
bool search) {
273 const bool approximate = search && bool(potential.
searchHessian);
278 const auto& callback =
282 const double defect = (physical - physical.transpose()).norm();
283 if (defect > 1e-10 * std::max(1., physical.norm()))
284 throw std::runtime_error(
285 "Potential curvature callback is not symmetric");
287 for (Eigen::Index axis = 0; axis < d; ++axis) {
288 Vector plus = q, minus = q;
291 const Vector gp = point(plus).gradient,
292 gm = point(minus).gradient;
293 physical.col(axis) = (gp - gm) / (2 * options.
hessianStep);
297 (physical - physical.transpose()).norm());
298 return Matrix(.5 * (physical + physical.transpose()));
300 auto segments = [&](
const Nodes& p,
bool search) {
301 std::vector<Matrix> hessians;
302 for (
const auto& q: p)
303 hessians.push_back(pointHessian(q, search));
305 pointHessian(boundary.
twist * p.front() + boundary.
offset, search));
306 std::vector<gaussian::Segment>
out;
307 for (std::size_t j = 0; j < n; ++j)
308 out.push_back(reference.
quadratic(hessians[j], hessians[j + 1]));
311 auto blocks = [&](
const Nodes& p) {
313 const auto links = segments(p,
true);
314 std::vector<Matrix>
out;
315 out.push_back(links.front().A + boundary.
twist.transpose() *
316 links.back().rightEndpoint() *
318 for (std::size_t j = 1; j < n; ++j)
319 out.push_back(links[j - 1].rightEndpoint() + links[j].A);
322 std::vector<Matrix>
out;
323 for (
const auto& q: p)
324 out.push_back(pointHessian(q,
true));
326 pointHessian(boundary.
twist * p.front() + boundary.
offset,
true);
328 boundary.
twist.transpose() * endpoint * boundary.
twist);
329 for (
auto& block:
out)
330 block = h * block + (2 / h) * Matrix::Identity(d, d);
334 bool hasState =
false;
335 auto metricResidual = [&](
const State& s) {
337 : s.conditionalForce;
346 item.
action = state.action;
356 result.
history.push_back(std::move(item));
359 state = evaluate(result.
path);
366 blocks(result.
path), state.gradient, normals,
368 if (!solved.invertible ||
373 "Constrained cyclic Newton solve fails its residual checks";
377 for (
const auto& v: solved.displacement)
378 maximum = std::max(maximum, v.norm());
382 bool accepted =
false;
384 ++trial, scale *= .5) {
385 Nodes candidate = result.
path;
386 for (std::size_t j = 0; j < n; ++j)
387 candidate[j] += scale * solved.displacement[j];
389 State next = evaluate(candidate);
390 if (metricResidual(next) < metricResidual(state) &&
392 result.
path = std::move(candidate);
393 state = std::move(next);
403 "Path residual does not decrease on the supplied affine "
408 }
catch (
const std::exception& error) {
411 for (
const auto& q: result.
path)
414 result.
action = state.action;
435 result.
failure =
"Maximum path iterations reached";
439 }
catch (
const std::exception& error) {