Loading...
Searching...
No Matches
GeodesicMeshProjection.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Core>
8#include <Eigen/LU>
9#include <algorithm>
10#include <cmath>
11#include <stdexcept>
12#include <utility>
13#include <vector>
14
15namespace gpr::quantum {
16namespace meshprojection_detail {
17using Vector = Eigen::VectorXd;
18using Matrix = Eigen::MatrixXd;
19struct Geometry {
20 Vector constraint;
21 std::vector<Vector> tangent, leftUnit, rightUnit;
22 std::vector<double> leftWeight, rightWeight;
23};
24inline Geometry geometry(const std::vector<Vector>& path,
25 const std::vector<double>& weights)
26{
27 if (path.size() < 3 || path.front().size() < 1 ||
28 (!weights.empty() && weights.size() + 1 != path.size()))
29 throw std::invalid_argument(
30 "Compatible geodesic mesh geometry required");
31 for (double weight: weights)
32 if (!(weight > 0.) || !std::isfinite(weight))
33 throw std::invalid_argument(
34 "Positive finite geodesic mesh weights required");
35 const auto n = path.size();
36 for (const auto& point: path)
37 if (point.size() != path.front().size() || !point.allFinite())
38 throw std::invalid_argument(
39 "Finite matching mesh coordinates required");
40 Geometry result;
41 result.constraint.resize(n - 2);
42 result.tangent.resize(n);
43 result.leftUnit.resize(n);
44 result.rightUnit.resize(n);
45 result.leftWeight.resize(n);
46 result.rightWeight.resize(n);
47 for (std::size_t j = 1; j + 1 < n; ++j) {
48 const Vector left = path[j] - path[j - 1],
49 right = path[j + 1] - path[j];
50 const double a = left.stableNorm(), b = right.stableNorm();
51 if (!(a > 0.) || !(b > 0.) || !std::isfinite(a + b))
52 throw std::domain_error(
53 "Distinct finite geodesic mesh links required");
54 double hm = weights.empty() ? 1. : weights[j - 1],
55 hp = weights.empty() ? 1. : weights[j];
56 const double scale = std::max(hm, hp);
57 hm /= scale;
58 hp /= scale;
59 if (!(hm > 0.) || !(hp > 0.))
60 throw std::domain_error(
61 "Resolved graded geodesic weights required");
62 result.leftUnit[j] = left / a;
63 result.rightUnit[j] = right / b;
64 const Vector tangent =
65 weights.empty()
66 ? Vector(left / a + right / b)
67 : Vector((hp / hm * left + hm / hp * right) / (hm + hp));
68 const double norm = tangent.stableNorm();
69 if (!(norm > 0.) || !std::isfinite(norm))
70 throw std::domain_error("Resolved geodesic mesh tangent required");
71 result.tangent[j] = tangent / norm;
72 result.leftWeight[j] = hm;
73 result.rightWeight[j] = hp;
74 result.constraint[j - 1] = 2 * (hp * a - hm * b) / (hm + hp);
75 }
76 return result;
77}
78} // namespace meshprojection_detail
79
80inline Eigen::VectorXd geodesicMeshConstraints(
81 const std::vector<Eigen::VectorXd>& path,
82 const std::vector<double>& weights = {})
83{
84 return meshprojection_detail::geometry(path, weights).constraint;
85}
86
93inline std::vector<Eigen::VectorXd> retractGeodesicMesh(
94 const std::vector<Eigen::VectorXd>& path,
95 const std::vector<double>& weights, double tolerance,
96 const Eigen::VectorXd& target = {})
97{
98 using namespace meshprojection_detail;
99 if (!(tolerance > 0.) || !std::isfinite(tolerance))
100 throw std::invalid_argument(
101 "Positive mesh projection tolerance required");
102 auto current = path;
103 auto data = geometry(current, weights);
104 const auto m = data.constraint.size();
105 Vector goal = Vector::Zero(m);
106 if (target.size()) goal = target;
107 if (goal.size() != m || !goal.allFinite())
108 throw std::invalid_argument(
109 "Finite matching mesh constraint target required");
110 for (unsigned iteration = 0; iteration < 32; ++iteration) {
111 const Vector residual = data.constraint - goal;
112 const double error = residual.cwiseAbs().maxCoeff();
113 if (error <= tolerance) return current;
114 Matrix jacobian = Matrix::Zero(m, m);
115 for (Eigen::Index row = 0; row < m; ++row) {
116 const auto j = static_cast<std::size_t>(row + 1);
117 const double hm = data.leftWeight[j], hp = data.rightWeight[j],
118 sum = hm + hp;
119 jacobian(row, row) =
120 2 *
121 (hp * data.leftUnit[j] + hm * data.rightUnit[j])
122 .dot(data.tangent[j]) /
123 sum;
124 if (row)
125 jacobian(row, row - 1) =
126 -2 * hp * data.leftUnit[j].dot(data.tangent[j - 1]) / sum;
127 if (row + 1 < m)
128 jacobian(row, row + 1) =
129 -2 * hm * data.rightUnit[j].dot(data.tangent[j + 1]) / sum;
130 }
131 Eigen::FullPivLU<Matrix> factor(jacobian);
132 if (factor.rank() != m)
133 throw std::domain_error(
134 "Geodesic tangential mesh Jacobian is singular");
135 const Vector step = factor.solve(-residual);
136 if (!step.allFinite() ||
137 (jacobian * step + residual).stableNorm() >
138 1e-10 * std::max(1., residual.stableNorm()))
139 throw std::domain_error("Unresolved geodesic mesh correction");
140 bool accepted = false;
141 double scale = 1.;
142 for (unsigned trial = 0; trial < 24; ++trial, scale *= .5) {
143 auto candidate = current;
144 for (Eigen::Index row = 0; row < m; ++row)
145 candidate[row + 1] += scale * step[row] * data.tangent[row + 1];
146 try {
147 auto next = geometry(candidate, weights);
148 if ((next.constraint - goal).cwiseAbs().maxCoeff() < error) {
149 current = std::move(candidate);
150 data = std::move(next);
151 accepted = true;
152 break;
153 }
154 } catch (const std::domain_error&) {
155 }
156 }
157 if (!accepted)
158 throw std::domain_error(
159 "Geodesic mesh correction does not decrease the constraint");
160 }
161 throw std::domain_error("Geodesic mesh projection iteration limit");
162}
163} // namespace gpr::quantum
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
std::vector< Eigen::VectorXd > retractGeodesicMesh(const std::vector< Eigen::VectorXd > &path, const std::vector< double > &weights, double tolerance, const Eigen::VectorXd &target={})
Eigen::MatrixXd Matrix
Eigen::VectorXd Vector
Eigen::VectorXd geodesicMeshConstraints(const std::vector< Eigen::VectorXd > &path, const std::vector< double > &weights={})