Loading...
Searching...
No Matches
JacobiTurningPoint.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <cmath>
8#include <functional>
9#include <limits>
10#include <string>
11
12#include "PotentialPath.h"
13
14namespace gpr::quantum {
16 Eigen::VectorXd point;
19 std::size_t evaluations{};
20 bool converged{};
21 std::string failure;
22};
23
24// The callback measures every requested point in one fixed mass-coordinate
25// chart. A turning-energy root along a supplied line does not certify path
26// stationarity or a physical tunnelling rate.
28 const Eigen::VectorXd& left, const Eigen::VectorXd& right, double energy,
29 const std::function<PotentialEvaluation(const Eigen::VectorXd&)>& evaluate,
30 double energyTolerance = 1e-9, std::size_t maximumEvaluations = 64)
31{
32 if (left.size() < 1 || right.size() != left.size() || !left.allFinite() ||
33 !right.allFinite())
34 throw std::invalid_argument(
35 "Matching finite turning-point coordinates required");
36 const Eigen::VectorXd direction = right - left;
37 if (!(direction.norm() > 0) || !std::isfinite(direction.norm()) ||
38 !direction.allFinite() || !std::isfinite(energy) ||
39 !(energyTolerance > 0) || !std::isfinite(energyTolerance) ||
40 maximumEvaluations < 2 || !evaluate)
41 throw std::invalid_argument(
42 "Finite distinct turning bracket, energy scale and native callback "
43 "required");
45 struct Point {
46 double t{}, f{}, slope{};
48 };
49 auto measure = [&](double t) {
50 const Eigen::VectorXd q = left + t * direction;
51 auto native = evaluate(q);
52 ++out.evaluations;
53 if (!std::isfinite(native.energy) ||
54 native.gradient.size() != left.size() ||
55 !native.gradient.allFinite())
56 throw std::runtime_error(
57 "Turning-point callback returned invalid native energy or "
58 "gradient");
59 return Point{t, native.energy - energy, native.gradient.dot(direction),
60 std::move(native)};
61 };
62 auto a = measure(0.), b = measure(1.);
63 auto best = std::abs(a.f) <= std::abs(b.f) ? a : b;
64 if (std::abs(best.f) > energyTolerance &&
65 std::signbit(a.f) == std::signbit(b.f))
66 throw std::invalid_argument(
67 "Measured endpoint energies do not bracket the turning energy");
68 double previousStep = 1.;
69 while (std::abs(best.f) > energyTolerance &&
70 out.evaluations < maximumEvaluations) {
71 const double width = b.t - a.t;
72 double next = best.t - best.f / best.slope;
73 const double margin = 16 * std::numeric_limits<double>::epsilon() *
74 std::max(1., std::abs(best.t));
75 if (!std::isfinite(next) || next <= a.t + margin ||
76 next >= b.t - margin || std::abs(next - best.t) > .5 * previousStep)
77 next = .5 * (a.t + b.t);
78 if (!(next > a.t && next < b.t)) {
79 out.failure =
80 "Turning-energy bracket is unresolved at floating-point "
81 "precision";
82 break;
83 }
84 previousStep = width;
85 auto point = measure(next);
86 if (std::signbit(point.f) == std::signbit(a.f))
87 a = point;
88 else
89 b = point;
90 best = std::abs(a.f) <= std::abs(b.f) ? a : b;
91 }
92 out.parameter = best.t;
93 out.point = left + best.t * direction;
94 out.native = best.native;
95 out.energyResidual = std::abs(best.f);
96 out.bracketWidth = (b.t - a.t) * direction.norm();
97 out.converged = out.energyResidual <= energyTolerance;
98 if (!out.converged && out.failure.empty())
99 out.failure = "Maximum native turning-point evaluations reached";
100 return out;
101}
102} // namespace gpr::quantum
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
JacobiTurningPoint findJacobiTurningPoint(const Eigen::VectorXd &left, const Eigen::VectorXd &right, double energy, const std::function< PotentialEvaluation(const Eigen::VectorXd &)> &evaluate, double energyTolerance=1e-9, std::size_t maximumEvaluations=64)