51 const std::vector<Eigen::VectorXd>& cartesianPath,
double contourEnergy,
52 NativeEvaluate&& evaluate, NativeEnergy&& nativeEnergy,
53 std::size_t maximumIterations = 32,
double maximumStepBohr = .1,
54 double energyTolerance = 1e-12)
56 using V = Eigen::VectorXd;
57 if (cartesianPath.size() < 3 || !std::isfinite(contourEnergy) ||
58 !maximumIterations || !(maximumStepBohr > 0) ||
59 !std::isfinite(maximumStepBohr) || !(energyTolerance > 0) ||
60 !std::isfinite(energyTolerance))
61 throw std::invalid_argument(
62 "Finite contour energy and turning-point controls required");
64 result.
path = cartesianPath;
65 for (
const auto endpoint: {std::size_t{0}, result.
path.size() - 1}) {
66 V&
x = result.
path[endpoint];
68 for (std::size_t iteration = 0; iteration < maximumIterations;
70 const double residual = nativeEnergy(
x) - contourEnergy;
74 if (std::abs(residual) <= energyTolerance)
break;
75 const auto value = evaluate(
x);
76 if (value.gradient.size() !=
x.size() ||
77 !value.gradient.allFinite())
78 throw std::runtime_error(
79 "Native turning-point gradient has the wrong shape");
80 const double norm2 = value.gradient.squaredNorm();
81 if (!(norm2 > 0) || !std::isfinite(norm2))
82 throw std::domain_error(
"Turning contour has no finite normal");
83 V step = (-residual / norm2) * value.gradient;
84 if (step.norm() > maximumStepBohr)
85 step *= maximumStepBohr / step.norm();
86 bool accepted =
false;
87 for (
double scale = 1.; scale >= 1. / 256.; scale *= .5) {
88 const V candidate =
x + scale * step;
89 const double candidateResidual =
90 nativeEnergy(candidate) - contourEnergy;
92 if (std::abs(candidateResidual) < std::abs(residual)) {
99 throw std::runtime_error(
100 "Turning-point contour projection stalled");
102 const double residual = std::abs(nativeEnergy(
x) - contourEnergy);
108 if (residual > energyTolerance)
109 throw std::runtime_error(
110 "Turning point did not reach the common energy contour");
219 const std::vector<Eigen::VectorXd>& cartesianPath,
220 const Eigen::VectorXd& masses, NativeEvaluate&& evaluate,
221 NativeEnergy&& nativeEnergy,
double timeStep = 4.,
222 double boltzmann = 3.1668152e-6, std::size_t maximumSteps = 1000000,
223 double endTolerance = 1e-3)
225 using V = Eigen::VectorXd;
226 if (masses.size() < 9 || masses.size() % 3 || !masses.allFinite() ||
227 !(masses.array() > 0).all() || !(timeStep > 0) ||
228 !std::isfinite(timeStep) || !(boltzmann > 0) ||
229 !std::isfinite(boltzmann) || !maximumSteps || !(endTolerance > 0) ||
230 !std::isfinite(endTolerance))
231 throw std::invalid_argument(
232 "Finite masses and imaginary-time integration controls required");
233 for (
const auto&
x: cartesianPath)
234 if (
x.size() != masses.size() || !
x.allFinite())
235 throw std::invalid_argument(
236 "Cartesian path and mass dimensions differ");
241 auto rhs = [&](
const State&
y) {
245 const auto value = evaluate(
x);
246 if (value.gradient.size() != masses.size() ||
247 !value.gradient.allFinite())
248 throw std::runtime_error(
249 "Native imaginary-time path gradient has the wrong shape");
250 const double metric = dx.dot(masses.cwiseProduct(dx));
251 if (!(metric > 0) || !std::isfinite(metric))
252 throw std::domain_error(
"Cartesian spline has zero mass speed");
253 const double force = dx.dot(value.gradient);
254 const double curvature = dx.dot(masses.cwiseProduct(d2x));
255 const double acceleration = (force - curvature *
y.v *
y.v) / metric;
256 if (!std::isfinite(acceleration))
257 throw std::runtime_error(
258 "Nonfinite constrained imaginary-time acceleration");
259 return State{
y.v, acceleration};
261 auto add = [](
const State& a,
const State& b,
double scale) {
262 return State{a.r + scale * b.r, a.v + scale * b.v};
264 auto advance = [&](
const State&
y,
double dt) {
265 const State k1 = rhs(
y), k2 = rhs(add(
y, k1, .5 * dt));
266 const State k3 = rhs(add(
y, k2, .5 * dt)), k4 = rhs(add(
y, k3, dt));
267 return State{
y.r + dt * (k1.r + 2 * k2.r + 2 * k3.r + k4.r) / 6.,
268 y.v + dt * (k1.v + 2 * k2.v + 2 * k3.v + k4.v) / 6.};
273 const auto [x0, dx0] = spline.
evaluate(0.);
275 const double energy0 = -nativeEnergy(x0);
277 {time, state.r, state.v, x0, V::Zero(masses.size())});
278 bool reachedTurningPoint =
false;
279 for (std::size_t step = 0; step < maximumSteps; ++step) {
280 double acceptedStep = timeStep;
281 State next = advance(state, timeStep);
282 if (state.v > 0. && next.v <= 0.) {
283 double low = 0., high = timeStep;
284 for (
int iteration = 0; iteration < 64; ++iteration) {
285 const double middle = .5 * (low + high);
286 if (advance(state, middle).v > 0.)
291 acceptedStep = .5 * (low + high);
292 next = advance(state, acceptedStep);
295 throw std::runtime_error(
296 "Imaginary-time velocity reversal lies away from the "
297 "opposite turning point");
301 reachedTurningPoint =
true;
304 time += acceptedStep;
305 const auto [
x, dx] = spline.
evaluate(state.r);
306 const double energy = nativeEnergy(
x);
307 const double kinetic =
308 .5 * state.v * state.v * dx.dot(masses.cwiseProduct(dx));
311 result.
samples.push_back({time, state.r, state.v,
x, dx * state.v});
312 if (reachedTurningPoint)
break;
314 if (!reachedTurningPoint)
315 throw std::runtime_error(
316 "Imaginary-time trajectory did not reach the opposite turning "
322 throw std::runtime_error(
323 "Imaginary-time trajectory has an invalid period");
330 if (fullBeads < 4 || fullBeads % 2 || trajectory.
samples.size() < 2 ||
331 !(trajectory.
period > 0) || !std::isfinite(trajectory.
period))
332 throw std::invalid_argument(
333 "Even full ring and complete imaginary-time trajectory required");
334 const std::size_t halfBeads = fullBeads / 2;
335 std::vector<Eigen::VectorXd> half(halfBeads);
336 std::size_t segment = 0;
337 for (std::size_t bead = 0; bead < halfBeads; ++bead) {
338 const double target =
339 trajectory.
period * (double(bead) + .5) /
double(fullBeads);
340 while (segment + 1 < trajectory.
samples.size() &&
341 target >= trajectory.
samples[segment + 1].time)
343 if (segment + 1 >= trajectory.
samples.size())
344 throw std::out_of_range(
345 "Ring time lies beyond the imaginary-time trajectory");
346 const auto& a = trajectory.
samples[segment];
347 const auto& b = trajectory.
samples[segment + 1];
348 const double dt = b.time - a.time, tau = target - a.time,
351 a.position + a.velocity * tau +
352 fraction * fraction * (b.position - a.position - a.velocity * dt);
354 std::vector<Eigen::VectorXd> ring(fullBeads);
355 for (std::size_t bead = 0; bead < halfBeads; ++bead) {
356 ring[bead] = half[bead];
357 ring[fullBeads - 1 - bead] = half[bead];
ConstrainedImaginaryTimeTrajectory integrateConstrainedImaginaryTimePath(const std::vector< Eigen::VectorXd > &cartesianPath, const Eigen::VectorXd &masses, NativeEvaluate &&evaluate, NativeEnergy &&nativeEnergy, double timeStep=4., double boltzmann=3.1668152e-6, std::size_t maximumSteps=1000000, double endTolerance=1e-3)
IsoenergyTurningPointRefinement refineIsoenergyTurningPoints(const std::vector< Eigen::VectorXd > &cartesianPath, double contourEnergy, NativeEvaluate &&evaluate, NativeEnergy &&nativeEnergy, std::size_t maximumIterations=32, double maximumStepBohr=.1, double energyTolerance=1e-12)