Loading...
Searching...
No Matches
SphereEnergyProjection.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <algorithm>
8#include <cmath>
9#include <limits>
10
11#include "PotentialPath.h"
12
13namespace gpr::quantum {
15 Eigen::VectorXd point;
17 std::size_t evaluations{};
18 bool converged{};
19};
20
21// The last geodesic link retains its length while its endpoint reaches V=E.
22// The tangent Newton direction has dV/dalpha = -(V-E). All earlier links,
23// their mesh ratios and the harmonic matching cut retain their coordinates.
24template <class Evaluate>
25SphereEnergyProjection projectSphereEnergy(const Eigen::VectorXd& center,
26 const Eigen::VectorXd& point,
27 const PotentialEvaluation& value,
28 double energy, double tolerance,
29 const Evaluate& evaluate,
30 std::size_t maximumIterations = 8,
31 std::size_t maximumBacktracks = 12)
32{
33 using V = Eigen::VectorXd;
34 const auto d = point.size();
35 auto valid = [&](const PotentialEvaluation& v) {
36 return std::isfinite(v.energy) && v.gradient.size() == d &&
37 v.gradient.allFinite();
38 };
39 if (d == 0 || center.size() != d || !point.allFinite() ||
40 !center.allFinite() || !valid(value) || !std::isfinite(energy) ||
41 !(tolerance > 0) || !std::isfinite(tolerance) || maximumBacktracks == 0)
42 throw std::invalid_argument(
43 "Finite sphere coordinates, native gradient and positive energy "
44 "tolerance required");
45 const double length = (point - center).norm();
46 if (!(length > 0) || !std::isfinite(length))
47 throw std::invalid_argument(
48 "A resolved final geodesic link is required");
49 SphereEnergyProjection result{point, value, 0,
50 std::abs(value.energy - energy) <= tolerance};
51 for (std::size_t iteration = 0;
52 !result.converged && iteration < maximumIterations; ++iteration) {
53 const V radius = result.point - center, tangent = radius.normalized();
54 const V normal = result.value.gradient -
55 tangent.dot(result.value.gradient) * tangent;
56 const double squared = normal.squaredNorm();
57 const double eps = std::numeric_limits<double>::epsilon();
58 if (!(squared > eps * eps * result.value.gradient.squaredNorm()))
59 return result;
60 const V step = -(result.value.energy - energy) / squared * normal;
61 if (!step.allFinite()) return result;
62 bool accepted = false;
63 double scale = 1.;
64 for (std::size_t backtrack = 0; backtrack < maximumBacktracks;
65 ++backtrack, scale *= .5) {
66 const V raw = radius + scale * step;
67 if (!(raw.norm() > 0) || !raw.allFinite()) continue;
68 const V trial = center + length * raw.normalized();
69 if ((trial.array() == result.point.array()).all()) continue;
70 PotentialEvaluation measured;
71 try {
72 ++result.evaluations;
73 measured = evaluate(trial);
74 } catch (const std::domain_error&) {
75 continue;
76 }
77 if (!valid(measured))
78 throw std::runtime_error(
79 "Sphere projection received invalid native values");
80 if (std::abs(measured.energy - energy) <
81 std::abs(result.value.energy - energy)) {
82 result.point = trial;
83 result.value = std::move(measured);
84 result.converged =
85 std::abs(result.value.energy - energy) <= tolerance;
86 accepted = true;
87 break;
88 }
89 }
90 if (!accepted) return result;
91 }
92 return result;
93}
94} // namespace gpr::quantum
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
SphereEnergyProjection projectSphereEnergy(const Eigen::VectorXd &center, const Eigen::VectorXd &point, const PotentialEvaluation &value, double energy, double tolerance, const Evaluate &evaluate, std::size_t maximumIterations=8, std::size_t maximumBacktracks=12)