28 const Eigen::VectorXd& left,
const Eigen::VectorXd& right,
double energy,
30 double energyTolerance = 1e-9, std::size_t maximumEvaluations = 64)
32 if (left.size() < 1 || right.size() != left.size() || !left.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 "
46 double t{}, f{}, slope{};
49 auto measure = [&](
double t) {
50 const Eigen::VectorXd q = left + t * direction;
51 auto native = evaluate(q);
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 "
59 return Point{t, native.energy - energy, native.gradient.dot(direction),
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)) {
80 "Turning-energy bracket is unresolved at floating-point "
85 auto point = measure(next);
86 if (std::signbit(point.f) == std::signbit(a.f))
90 best = std::abs(a.f) <= std::abs(b.f) ? a : b;
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";
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)