19 const std::vector<Eigen::VectorXd>& path)
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");
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");
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)
52 const double fraction =
53 (target - arc[segment]) / (arc[segment + 1] - arc[segment]);
55 path[segment] + fraction * (path[segment + 1] - path[segment]);
61 const std::vector<Eigen::VectorXd>& q,
62 const std::vector<PotentialEvaluation>& values,
double energy,
double hbar,
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;
73 for (std::size_t j = 1; j + 1 < n; ++j)
74 force[j] = -exact.gradient[j] / hbar;
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]) *
88 .5 * (xi[j] + xi[j - 1]) * left +
89 .5 * (xi[j + 1] + xi[j]) * right;
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.,
102 const auto actionForce =
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;
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");
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;
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());
172 const std::vector<Eigen::VectorXd>& initial,
175 const std::function<void(
const LineIntegralNEBResult&)>& retain = {},
176 LineIntegralFIREState* fireState =
nullptr)
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);
200 auto state = [&](
const auto& path,
const auto& values) {
202 options.spring, options.contour,
203 options.hbar, options.action);
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;
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;
240 for (std::size_t iteration = 0; iteration <= options.maximumIterations;
242 result.iterations = iteration;
243 if (retain) retain(result);
244 if (result.state.maximumForce < options.forceTolerance &&
245 result.state.endpointEnergyError < options.energyTolerance) {
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());
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;
261 for (
auto& v: velocity)
265 if (!result.state.turningOrientationsQualified) {
267 "LI-String turning orientation does not enter the "
272 result.converged =
true;
276 if (iteration == options.maximumIterations)
break;
277 std::vector<Eigen::VectorXd> step(initial.size()),
278 trial(initial.size());
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());
285 largest > options.maximumStep ? options.maximumStep / largest : 1.;
286 double fraction = scale;
287 bool accepted =
false, reparameterized =
false;
288 std::vector<PotentialEvaluation> next;
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];
294 const bool needsReparameterization =
296 if (needsReparameterization)
299 nextState = state(trial, next);
300 reparameterized = needsReparameterization;
303 }
catch (
const std::domain_error&) {
309 "No admissible LI-String step in the forbidden region";
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();
321 if (reparameterized) {
324 for (
auto& v: velocity)
326 }
else if (power > 0. && fraction == scale) {
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];
333 dt = std::min(dt * 1.1, options.maximumTimeStep);
338 dt = options.minimumTimeStep > 0.
339 ? std::max(options.minimumTimeStep, dt * .5)
341 alpha = options.initialMixing;
342 for (
auto& v: velocity)
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";
355 result.failure =
"LI-String iteration limit";