Loading...
Searching...
No Matches
LineIntegralString.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <algorithm>
8#include <cmath>
9#include <functional>
10#include <limits>
11#include <stdexcept>
12#include <vector>
13
14#include "LineIntegralNEB.h"
15
16namespace gpr::quantum {
17
18inline std::vector<Eigen::VectorXd> reparameterizeLineIntegralString(
19 const std::vector<Eigen::VectorXd>& path)
20{
21 if (path.size() < 3 || path.front().size() < 1)
22 throw std::invalid_argument(
23 "A line-integral string requires at least three nodes");
24 std::vector<double> arc(path.size(), 0.);
25 for (std::size_t j = 0; j < path.size(); ++j) {
26 if (path[j].size() != path.front().size() || !path[j].allFinite())
27 throw std::invalid_argument(
28 "Finite matching line-integral string coordinates required");
29 if (j) {
30 const double length = (path[j] - path[j - 1]).norm();
31 if (!(length > 0.) || !std::isfinite(length))
32 throw std::invalid_argument(
33 "Distinct adjacent line-integral string nodes required");
34 arc[j] = arc[j - 1] + length;
35 if (!std::isfinite(arc[j]))
36 throw std::overflow_error(
37 "Line-integral string length overflowed");
38 }
39 }
40 const double total = arc.back();
41 if (!(total > 0.) || !std::isfinite(total))
42 throw std::invalid_argument(
43 "A finite nonzero string length is required");
44 std::vector<Eigen::VectorXd> result(path.size());
45 result.front() = path.front();
46 result.back() = path.back();
47 std::size_t segment = 0;
48 for (std::size_t j = 1; j + 1 < path.size(); ++j) {
49 const double target = total * double(j) / double(path.size() - 1);
50 while (segment + 1 < arc.size() - 1 && arc[segment + 1] < target)
51 ++segment;
52 const double fraction =
53 (target - arc[segment]) / (arc[segment + 1] - arc[segment]);
54 result[j] =
55 path[segment] + fraction * (path[segment + 1] - path[segment]);
56 }
57 return result;
58}
59
60inline std::vector<Eigen::VectorXd> lineIntegralStringActionForces(
61 const std::vector<Eigen::VectorXd>& q,
62 const std::vector<PotentialEvaluation>& values, double energy, double hbar,
63 LineIntegralAction action)
64{
65 const std::size_t n = q.size();
66 std::vector<Eigen::VectorXd> force(n,
67 Eigen::VectorXd::Zero(q.front().size()));
69 auto contourValues = values;
70 contourValues.front().energy = energy;
71 contourValues.back().energy = energy;
72 const auto exact = evaluateJacobiPath(q, contourValues, energy);
73 for (std::size_t j = 1; j + 1 < n; ++j)
74 force[j] = -exact.gradient[j] / hbar;
75 return force;
76 }
77
78 std::vector<double> xi(n, 0.), length(n - 1);
79 for (std::size_t j = 1; j + 1 < n; ++j)
80 xi[j] = std::sqrt(2. * (values[j].energy - energy)) / hbar;
81 for (std::size_t j = 1; j < n; ++j)
82 length[j - 1] = (q[j] - q[j - 1]).norm();
83 for (std::size_t j = 1; j + 1 < n; ++j) {
84 const Eigen::VectorXd left = (q[j] - q[j - 1]) / length[j - 1];
85 const Eigen::VectorXd right = (q[j + 1] - q[j]) / length[j];
86 force[j] = -.5 * (length[j - 1] + length[j]) / (hbar * hbar * xi[j]) *
87 values[j].gradient -
88 .5 * (xi[j] + xi[j - 1]) * left +
89 .5 * (xi[j + 1] + xi[j]) * right;
90 }
91 return force;
92}
93
95 const std::vector<Eigen::VectorXd>& q,
96 const std::vector<PotentialEvaluation>& values, double energy,
97 double spring = 1., double contour = 10., double hbar = 1.,
99{
100 auto state = evaluateLineIntegralNEB(q, values, energy, spring, contour,
101 hbar, action);
102 const auto actionForce =
103 lineIntegralStringActionForces(q, values, energy, hbar, action);
104 state.maximumForce = 0.;
105 for (std::size_t j = 1; j + 1 < q.size(); ++j) {
106 const Eigen::VectorXd left = (q[j] - q[j - 1]).normalized();
107 const Eigen::VectorXd right = (q[j + 1] - q[j]).normalized();
108 Eigen::VectorXd tangent = left + right;
109 const double tangentNorm = tangent.norm();
110 if (!(tangentNorm > 1e-12) || !std::isfinite(tangentNorm))
111 throw std::domain_error(
112 "Line-integral string requires a resolved geometric tangent");
113 tangent /= tangentNorm;
114 state.force[j] = actionForce[j] - actionForce[j].dot(tangent) * tangent;
115 }
116 for (const std::size_t j: {std::size_t{0}, q.size() - 1}) {
117 const auto neighbour = j == 0 ? std::size_t{1} : q.size() - 2;
118 const Eigen::VectorXd gradient = values[j].gradient;
119 const double norm = gradient.norm();
120 if (!(norm > 0.) || !std::isfinite(norm))
121 throw std::domain_error(
122 "Line-integral string turning point requires a finite normal");
123 const Eigen::VectorXd normal = gradient / norm;
124 Eigen::VectorXd tangent = q[neighbour] - q[j];
125 const double tangentNorm = tangent.norm();
126 if (!(tangentNorm > 0.) || !std::isfinite(tangentNorm))
127 throw std::domain_error(
128 "Line-integral string endpoint requires a finite tangent");
129 // A turning point meets the contour along its normal. The spring pull
130 // towards the neighbour, projected onto the contour, vanishes only
131 // there and keeps its size while the interior converges, so the
132 // stopping test sees a misaligned endpoint.
133 const Eigen::VectorXd pull = spring * (q[neighbour] - q[j]);
134 state.force[j] = pull - pull.dot(normal) * normal -
135 contour * (values[j].energy - energy) * normal;
136 }
137 for (const auto& force: state.force) {
138 if (!force.allFinite())
139 throw std::runtime_error("Nonfinite line-integral string force");
140 state.maximumForce = std::max(state.maximumForce, force.norm());
141 }
142 return state;
143}
144
146 const std::vector<Eigen::VectorXd>& path)
147{
148 if (path.size() < 3)
149 throw std::invalid_argument(
150 "A line-integral string requires at least three nodes");
151 double total = 0., largestDifference = 0., previous = 0.;
152 for (std::size_t j = 1; j < path.size(); ++j) {
153 if (path[j].size() != path.front().size() || !path[j].allFinite())
154 throw std::invalid_argument(
155 "Finite matching line-integral string coordinates required");
156 const double length = (path[j] - path[j - 1]).norm();
157 if (!(length > 0.) || !std::isfinite(length))
158 throw std::invalid_argument(
159 "Distinct adjacent line-integral string nodes required");
160 total += length;
161 if (!std::isfinite(total))
162 throw std::overflow_error("Line-integral string length overflowed");
163 if (j > 1)
164 largestDifference =
165 std::max(largestDifference, std::abs(length - previous));
166 previous = length;
167 }
168 return largestDifference > total / (10. * double(path.size() - 1));
169}
170
172 const std::vector<Eigen::VectorXd>& initial,
173 const LineIntegralNEBBatch& potential,
174 const LineIntegralNEBOptions& options = {},
175 const std::function<void(const LineIntegralNEBResult&)>& retain = {},
176 LineIntegralFIREState* fireState = nullptr)
177{
178 if (!potential || initial.size() < 3 || !(options.timeStep > 0.) ||
179 !(options.maximumTimeStep >= options.timeStep) ||
180 !(options.maximumStep > 0.) || !(options.forceTolerance > 0.) ||
181 !(options.energyTolerance > 0.) ||
182 !(options.initialMixing >= 0. && options.initialMixing <= 1.) ||
183 !(options.minimumTimeStep >= 0. &&
184 options.minimumTimeStep <= options.timeStep) ||
185 !std::isfinite(options.initialMixing) ||
186 !std::isfinite(options.minimumTimeStep) ||
187 !std::isfinite(options.maximumTimeStep) ||
188 !std::isfinite(options.maximumStep) ||
189 !std::isfinite(options.forceTolerance) ||
190 !std::isfinite(options.energyTolerance))
191 throw std::invalid_argument(
192 "Finite positive LI-String search controls required");
193 LineIntegralNEBResult result;
194 result.path = initial;
195 auto evaluate = [&](const auto& path) {
196 ++result.batchEvaluations;
197 result.evaluations += path.size();
198 return potential(path);
199 };
200 auto state = [&](const auto& path, const auto& values) {
201 return evaluateLineIntegralString(path, values, options.energy,
202 options.spring, options.contour,
203 options.hbar, options.action);
204 };
205 result.native = evaluate(result.path);
206 result.state = state(result.path, result.native);
207 auto force = result.state.force;
208 std::vector<Eigen::VectorXd> velocity(
209 initial.size(), Eigen::VectorXd::Zero(initial.front().size()));
210 double dt = options.timeStep, alpha = options.initialMixing;
211 std::size_t positive = 0;
212 if (fireState && fireState->initialized) {
213 if (fireState->velocity.size() != initial.size() ||
214 !(fireState->timeStep > 0.) ||
215 !std::isfinite(fireState->timeStep) ||
216 !(fireState->mixing >= 0. && fireState->mixing <= 1.) ||
217 !std::isfinite(fireState->mixing))
218 throw std::invalid_argument(
219 "Finite matching LI-String FIRE state required");
220 for (std::size_t j = 0; j < initial.size(); ++j)
221 if (fireState->velocity[j].size() != initial.front().size() ||
222 !fireState->velocity[j].allFinite())
223 throw std::invalid_argument(
224 "Matching finite LI-String FIRE velocities required");
225 velocity = fireState->velocity;
226 dt = std::min(fireState->timeStep, options.maximumTimeStep);
227 if (options.minimumTimeStep > 0.)
228 dt = std::max(dt, options.minimumTimeStep);
229 alpha = fireState->mixing;
230 positive = fireState->positiveSteps;
231 }
232 auto saveFireState = [&] {
233 if (!fireState) return;
234 fireState->velocity = velocity;
235 fireState->timeStep = dt;
236 fireState->mixing = alpha;
237 fireState->positiveSteps = positive;
238 fireState->initialized = true;
239 };
240 for (std::size_t iteration = 0; iteration <= options.maximumIterations;
241 ++iteration) {
242 result.iterations = iteration;
243 if (retain) retain(result);
244 if (result.state.maximumForce < options.forceTolerance &&
245 result.state.endpointEnergyError < options.energyTolerance) {
246 auto equalArc = reparameterizeLineIntegralString(result.path);
247 double redistribution = 0.;
248 double pathScale = 1.;
249 for (std::size_t j = 0; j < equalArc.size(); ++j) {
250 redistribution = std::max(
251 redistribution, (equalArc[j] - result.path[j]).norm());
252 pathScale = std::max(pathScale, result.path[j].norm());
253 }
254 if (redistribution >
255 64. * std::numeric_limits<double>::epsilon() * pathScale) {
256 result.path = std::move(equalArc);
257 result.native = evaluate(result.path);
258 result.state = state(result.path, result.native);
259 force = result.state.force;
260 positive = 0;
261 for (auto& v: velocity)
262 v.setZero();
263 continue;
264 }
265 if (!result.state.turningOrientationsQualified) {
266 result.failure =
267 "LI-String turning orientation does not enter the "
268 "forbidden region";
269 saveFireState();
270 return result;
271 }
272 result.converged = true;
273 saveFireState();
274 return result;
275 }
276 if (iteration == options.maximumIterations) break;
277 std::vector<Eigen::VectorXd> step(initial.size()),
278 trial(initial.size());
279 double largest = 0.;
280 for (std::size_t j = 0; j < trial.size(); ++j) {
281 step[j] = dt * velocity[j] + .5 * dt * dt * force[j];
282 largest = std::max(largest, step[j].norm());
283 }
284 const double scale =
285 largest > options.maximumStep ? options.maximumStep / largest : 1.;
286 double fraction = scale;
287 bool accepted = false, reparameterized = false;
288 std::vector<PotentialEvaluation> next;
289 LineIntegralNEBState nextState;
290 for (int backtrack = 0; backtrack < 24; ++backtrack) {
291 for (std::size_t j = 0; j < trial.size(); ++j)
292 trial[j] = result.path[j] + fraction * step[j];
293 try {
294 const bool needsReparameterization =
296 if (needsReparameterization)
298 next = evaluate(trial);
299 nextState = state(trial, next);
300 reparameterized = needsReparameterization;
301 accepted = true;
302 break;
303 } catch (const std::domain_error&) {
304 fraction *= .5;
305 }
306 }
307 if (!accepted) {
308 result.failure =
309 "No admissible LI-String step in the forbidden region";
310 saveFireState();
311 return result;
312 }
313 auto nextForce = nextState.force;
314 double power = 0., vv = 0., ff = 0.;
315 for (std::size_t j = 0; j < trial.size(); ++j) {
316 velocity[j] += .5 * dt * (force[j] + nextForce[j]);
317 power += velocity[j].dot(nextForce[j]);
318 vv += velocity[j].squaredNorm();
319 ff += nextForce[j].squaredNorm();
320 }
321 if (reparameterized) {
322 positive = 0;
323 alpha = .1;
324 for (auto& v: velocity)
325 v.setZero();
326 } else if (power > 0. && fraction == scale) {
327 ++positive;
328 if (ff > 0.)
329 for (std::size_t j = 0; j < trial.size(); ++j)
330 velocity[j] = (1. - alpha) * velocity[j] +
331 alpha * std::sqrt(vv / ff) * nextForce[j];
332 if (positive > 5) {
333 dt = std::min(dt * 1.1, options.maximumTimeStep);
334 alpha *= .99;
335 }
336 } else {
337 positive = 0;
338 dt = options.minimumTimeStep > 0.
339 ? std::max(options.minimumTimeStep, dt * .5)
340 : dt * .5;
341 alpha = options.initialMixing;
342 for (auto& v: velocity)
343 v.setZero();
344 }
345 result.path = std::move(trial);
346 result.native = std::move(next);
347 result.state = std::move(nextState);
348 force = std::move(nextForce);
349 if (!(dt > std::numeric_limits<double>::epsilon())) {
350 result.failure = "LI-String time step underflows";
351 saveFireState();
352 return result;
353 }
354 }
355 result.failure = "LI-String iteration limit";
356 saveFireState();
357 return result;
358}
359} // namespace gpr::quantum
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
std::function< std::vector< PotentialEvaluation >( const std::vector< Eigen::VectorXd > &)> LineIntegralNEBBatch
std::vector< Eigen::VectorXd > lineIntegralStringActionForces(const std::vector< Eigen::VectorXd > &q, const std::vector< PotentialEvaluation > &values, double energy, double hbar, LineIntegralAction action)
JacobiPathEvaluation evaluateJacobiPath(const std::vector< Eigen::VectorXd > &points, const std::vector< PotentialEvaluation > &native, double energy)
std::vector< Eigen::VectorXd > reparameterizeLineIntegralString(const std::vector< Eigen::VectorXd > &path)
LineIntegralNEBState evaluateLineIntegralString(const std::vector< Eigen::VectorXd > &q, const std::vector< PotentialEvaluation > &values, double energy, double spring=1., double contour=10., double hbar=1., LineIntegralAction action=LineIntegralAction::MomentumTrapezoid)
LineIntegralNEBResult optimizeLineIntegralString(const std::vector< Eigen::VectorXd > &initial, const LineIntegralNEBBatch &potential, const LineIntegralNEBOptions &options={}, const std::function< void(const LineIntegralNEBResult &)> &retain={}, LineIntegralFIREState *fireState=nullptr)
bool lineIntegralStringNeedsReparameterization(const std::vector< Eigen::VectorXd > &path)
LineIntegralNEBState evaluateLineIntegralNEB(const std::vector< Eigen::VectorXd > &q, const std::vector< PotentialEvaluation > &values, double energy, double spring=1., double contour=10., double hbar=1., LineIntegralAction action=LineIntegralAction::MomentumTrapezoid, bool jctcCompatible=false, double contourRight=0., double actionForceCutoff=1e-4)
VecchiaResult evaluate(const Eigen::MatrixXd &points, const Eigen::VectorXd &y, double lengthscale, double noise, int max_neighbours)
std::vector< Eigen::VectorXd > force