160 const std::vector<Eigen::VectorXd>& q,
161 const std::vector<PotentialEvaluation>& values,
double energy,
162 double spring = 1.,
double contour = 10.,
double hbar = 1.,
164 bool jctcCompatible =
false,
double contourRight = 0.,
165 double actionForceCutoff = 1e-4)
169 throw std::invalid_argument(
"Unknown LI-NEB action quadrature");
171 throw std::invalid_argument(
172 "JCTC LI-NEB requires momentum-trapezoid action");
173 const std::size_t n = q.size();
174 if (n < 3 || values.size() != n || q.front().size() < 1 ||
175 !std::isfinite(energy) || !(spring > 0) || !(contour > 0) ||
176 !(hbar > 0) || !std::isfinite(spring) || !std::isfinite(contour) ||
177 !std::isfinite(hbar))
178 throw std::invalid_argument(
179 "Finite LI-NEB parameters and at least three matching images "
181 std::vector<double> xi(n), length(n - 1);
182 for (std::size_t j = 0; j < n; ++j) {
183 if (q[j].size() != q.front().size() ||
184 values[j].gradient.size() != q.front().size() ||
185 !q[j].allFinite() || !values[j].gradient.allFinite() ||
186 !std::isfinite(values[j].energy))
187 throw std::invalid_argument(
188 "Finite matching mass-coordinate energies and gradients "
190 if (j && j + 1 < n && !jctcCompatible) {
191 if (!(values[j].energy > energy))
192 throw std::domain_error(
193 "Interior LI-NEB image leaves the forbidden region");
195 xi[j] = std::sqrt(2 * std::max(0., values[j].energy - energy)) / hbar;
197 length[j - 1] = (q[j] - q[j - 1]).norm();
198 if (!(length[j - 1] > 0) || !std::isfinite(length[j - 1]))
199 throw std::domain_error(
200 "Distinct finite adjacent LI-NEB images required");
204 out.turningOrientationsQualified =
true;
206 out.actionForce.assign(n, Eigen::VectorXd::Zero(q.front().size()));
209 auto contourValues = values;
210 contourValues.front().energy = energy;
211 contourValues.back().energy = energy;
215 for (std::size_t j = 0; j + 1 < n; ++j)
216 out.abbreviatedAction +=
217 .5 * hbar * (xi[j] + xi[j + 1]) * length[j];
219 const auto tangents = jctcCompatible
220 ? detail::lineIntegralENOTangents(q, values)
221 : std::vector<Eigen::VectorXd>{};
222 for (std::size_t j = 1; j + 1 < n; ++j) {
223 const Eigen::VectorXd left = (q[j] - q[j - 1]) / length[j - 1];
224 const Eigen::VectorXd right = (q[j + 1] - q[j]) / length[j];
225 const Eigen::VectorXd tangent =
226 jctcCompatible ? tangents[j]
227 : detail::lineIntegralUnitTangent(q, values, j);
228 Eigen::VectorXd actionForce;
230 actionForce = -exact.
gradient[j] / hbar;
232 const double gap = values[j].energy - energy;
233 Eigen::VectorXd potentialForce =
234 Eigen::VectorXd::Zero(q.front().size());
235 if (!jctcCompatible || gap >= actionForceCutoff)
236 potentialForce = -.5 * (length[j - 1] + length[j]) /
237 (hbar * hbar * xi[j]) * values[j].gradient;
238 actionForce = potentialForce - .5 * (xi[j] + xi[j - 1]) * left +
239 .5 * (xi[j + 1] + xi[j]) * right;
241 out.actionForce[j] = actionForce;
243 std::vector<Eigen::VectorXd> smoothed =
out.actionForce;
244 if (jctcCompatible) {
245 std::vector<double> amplitude(n, 0.);
246 for (std::size_t j = 1; j + 1 < n; ++j)
247 amplitude[j] =
out.actionForce[j].norm();
248 for (std::size_t j = 1; j + 1 < n; ++j) {
249 const std::size_t first = j > 1 ? j - 1 : 1,
250 last = std::min(j + 1, n - 2);
252 for (std::size_t k = first; k <= last; ++k)
253 mean += amplitude[k];
254 mean /= double(last - first + 1);
255 if (amplitude[j] > 0)
256 smoothed[j] *= mean / amplitude[j];
258 smoothed[j].setZero();
259 const Eigen::VectorXd tangent = tangents[j];
260 const Eigen::VectorXd rawTransverse =
261 out.actionForce[j] -
out.actionForce[j].dot(tangent) * tangent;
262 out.transverseForceSum += rawTransverse.norm();
263 out.force[j] = smoothed[j] - smoothed[j].dot(tangent) * tangent +
264 spring * (length[j] - length[j - 1]) * tangent;
267 for (std::size_t j = 1; j + 1 < n; ++j) {
268 const Eigen::VectorXd tangent =
269 detail::lineIntegralUnitTangent(q, values, j);
270 const Eigen::VectorXd raw =
out.actionForce[j];
271 out.force[j] = raw - raw.dot(tangent) * tangent +
272 spring * (length[j] - length[j - 1]) * tangent;
273 out.transverseForceSum += (raw - raw.dot(tangent) * tangent).norm();
276 for (
const std::size_t j: {std::size_t(0), n - 1}) {
277 const double norm = values[j].gradient.norm();
279 throw std::domain_error(
280 "Turning contour requires a nonzero endpoint gradient");
281 const Eigen::VectorXd normal = values[j].gradient / norm;
282 const std::size_t adjacent = j == 0 ? 1 : n - 2;
283 const Eigen::VectorXd direction =
284 (q[adjacent] - q[j]) / (q[adjacent] - q[j]).norm();
285 Eigen::VectorXd pull;
286 if (jctcCompatible) {
287 pull = direction * smoothed[adjacent].norm();
289 pull = spring * (q[j == 0 ? 1 : n - 2] - q[j]);
290 out.turningOrientationsQualified =
291 out.turningOrientationsQualified && normal.dot(direction) > 0;
294 const double endpointContour =
295 j == 0 || contourRight == 0. ? contour : contourRight;
296 out.force[j] = pull - pull.dot(normal) * normal -
297 endpointContour * (values[j].energy - energy) * normal;
298 out.endpointEnergyError = std::max(
out.endpointEnergyError,
299 std::abs(values[j].energy - energy));
301 for (
const auto& f:
out.force) {
302 if (!f.allFinite())
throw std::runtime_error(
"Nonfinite LI-NEB force");
303 out.maximumForce = std::max(
out.maximumForce, f.norm());
457 const std::vector<Eigen::VectorXd>& initial,
460 const std::function<void(
const LineIntegralNEBResult&)>& retain = {},
461 LineIntegralFIREState* fireState =
nullptr,
462 const std::function<bool(
const std::vector<Eigen::VectorXd>&)>& earlyStop =
465 if (!potential || initial.size() < 3 || !(options.timeStep > 0) ||
466 !(options.maximumTimeStep >= options.timeStep) ||
467 !(options.maximumStep > 0) || !(options.forceTolerance > 0) ||
468 !(options.energyTolerance > 0) ||
469 !(options.initialMixing >= 0 && options.initialMixing <= 1) ||
470 !(options.minimumTimeStep >= 0 &&
471 options.minimumTimeStep <= options.timeStep) ||
472 !std::isfinite(options.initialMixing) ||
473 !std::isfinite(options.minimumTimeStep) ||
474 !std::isfinite(options.maximumTimeStep) ||
475 !std::isfinite(options.maximumStep) ||
476 !std::isfinite(options.forceTolerance) ||
477 !std::isfinite(options.energyTolerance) ||
478 !(options.actionForceCutoff >= 0) ||
479 !std::isfinite(options.actionForceCutoff) ||
480 !(options.endpointEnergyConvergeValue > 0) ||
481 !std::isfinite(options.endpointEnergyConvergeValue) ||
482 !(options.endpointGradientTolerance > 0) ||
483 !std::isfinite(options.endpointGradientTolerance) ||
484 !(options.transverseForceSumTolerance > 0) ||
485 !std::isfinite(options.transverseForceSumTolerance) ||
486 !(options.springForceFloor > 0) ||
487 !std::isfinite(options.springForceFloor) ||
488 !options.constraintUpdateInterval)
489 throw std::invalid_argument(
490 "Finite positive LI-NEB search controls required");
492 result.
path = initial;
493 auto evaluate = [&](
const auto& p) {
494 ++result.batchEvaluations;
495 result.evaluations += p.size();
499 auto state = [&](
const auto& p,
const auto& v) {
501 p, v, active.energy, active.spring, active.contour, active.hbar,
502 active.action, active.jctcCompatible, active.contourRight,
503 active.actionForceCutoff);
505 std::vector<Eigen::VectorXd> velocity(
506 initial.size(), Eigen::VectorXd::Zero(initial.front().size()));
507 double dt = options.timeStep, alpha = options.initialMixing;
508 std::size_t positive = 0, negative = 0, totalSteps = 0;
509 if (fireState && fireState->initialized) {
510 if (fireState->velocity.size() != initial.size() ||
511 !(fireState->timeStep > 0) || !std::isfinite(fireState->timeStep) ||
512 !(fireState->mixing >= 0 && fireState->mixing <= 1) ||
513 !std::isfinite(fireState->mixing))
514 throw std::invalid_argument(
515 "Finite matching LI-NEB FIRE state required");
516 for (std::size_t j = 0; j < initial.size(); ++j)
517 if (fireState->velocity[j].size() != initial.front().size() ||
518 !fireState->velocity[j].allFinite())
519 throw std::invalid_argument(
520 "Matching finite LI-NEB FIRE velocities required");
521 velocity = fireState->velocity;
522 dt = std::min(fireState->timeStep, options.maximumTimeStep);
523 if (options.minimumTimeStep > 0)
524 dt = std::max(dt, options.minimumTimeStep);
525 alpha = fireState->mixing;
526 positive = fireState->positiveSteps;
527 negative = fireState->negativeSteps;
528 totalSteps = fireState->steps;
529 if (options.jctcCompatible && fireState->constraintsInitialized) {
530 active.spring = fireState->spring;
531 active.contour = fireState->contourLeft;
532 active.contourRight = fireState->contourRight;
535 auto saveFireState = [&] {
536 if (!fireState)
return;
537 fireState->velocity = velocity;
538 fireState->timeStep = dt;
539 fireState->mixing = alpha;
540 fireState->positiveSteps = positive;
541 fireState->negativeSteps = negative;
542 fireState->steps = totalSteps;
543 fireState->spring = active.spring;
544 fireState->contourLeft = active.contour;
545 fireState->contourRight = active.contourRight;
546 fireState->constraintsInitialized = options.jctcCompatible;
547 fireState->initialized =
true;
549 result.native =
evaluate(result.path);
550 result.state = state(result.path, result.native);
551 auto adaptJctcConstraints = [&] {
552 if (!options.jctcCompatible)
return;
553 double pathLength = 0.;
554 for (std::size_t j = 1; j < result.path.size(); ++j)
555 pathLength += (result.path[j] - result.path[j - 1]).norm();
556 double averageActionForce = 0.;
557 for (std::size_t j = 1; j + 1 < result.state.actionForce.size(); ++j)
558 averageActionForce += result.state.actionForce[j].norm();
559 averageActionForce /= double(result.state.actionForce.size() - 2);
561 std::max(averageActionForce, options.springForceFloor);
562 active.spring = averageActionForce /
563 (pathLength / (10. * double(result.path.size())));
564 auto endpointKappa = [&](std::size_t j) {
565 const double maxForce =
566 std::max(1e-3, result.native[j].gradient.cwiseAbs().maxCoeff());
567 double kappa = .2 / (maxForce * dt * dt);
568 if (std::abs(result.native[j].energy - options.energy) <
569 options.endpointEnergyConvergeValue) {
570 const double cap = .2 * options.endpointGradientTolerance /
571 options.endpointEnergyConvergeValue;
572 kappa = std::min(kappa, cap);
576 active.contour = endpointKappa(0);
577 active.contourRight = endpointKappa(result.path.size() - 1);
580 result.state, active);
581 const bool storedConstraints = fireState && fireState->initialized &&
582 fireState->constraintsInitialized &&
583 options.jctcCompatible;
584 if (!storedConstraints) adaptJctcConstraints();
585 result.state = state(result.path, result.native);
587 result.state, active);
588 for (std::size_t iteration = 0; iteration <= options.maximumIterations;
590 result.iterations = iteration;
591 if (retain) retain(result);
592 const bool forceCriteria =
593 result.state.maximumForce < options.forceTolerance &&
594 (!options.jctcCompatible ||
595 result.state.transverseForceSum <
596 options.transverseForceSumTolerance);
597 const bool contourCriteria =
598 options.jctcCompatible ||
599 result.state.endpointEnergyError < options.energyTolerance;
600 if (earlyStop && earlyStop(result.path)) {
601 result.trustRegionExit =
true;
602 result.failure =
"LI-NEB native trust region exited";
606 if (forceCriteria && contourCriteria) {
607 if (!result.state.turningOrientationsQualified) {
609 "LI-NEB turning orientation does not enter the forbidden "
614 result.converged =
true;
618 if (iteration == options.maximumIterations)
break;
619 if (options.jctcCompatible && totalSteps > 0 &&
620 totalSteps % options.constraintUpdateInterval == 0) {
621 adaptJctcConstraints();
622 result.state = state(result.path, result.native);
624 result.state, active);
626 auto base = result.path;
628 std::vector<Eigen::VectorXd> step(initial.size());
630 if (options.jctcCompatible) {
631 double power = 0., velocitySquared = 0., forceSquared = 0.;
632 for (std::size_t j = 0; j < trial.size(); ++j) {
633 power += velocity[j].dot(searchForce[j]);
634 velocitySquared += velocity[j].squaredNorm();
635 forceSquared += searchForce[j].squaredNorm();
641 dt = std::min(dt * 1.1, options.maximumTimeStep);
647 if (negative > 100) {
649 "LI-NEB FIRE exceeded the uphill-step limit";
653 dt = options.minimumTimeStep > 0
654 ? std::max(dt * .5, options.minimumTimeStep)
656 alpha = options.initialMixing;
657 for (std::size_t j = 0; j < trial.size(); ++j) {
658 base[j] -= .5 * dt * velocity[j];
659 velocity[j].setZero();
662 for (std::size_t j = 0; j < trial.size(); ++j)
663 velocity[j] += dt * searchForce[j];
664 if (power > 0. && forceSquared > 0.) {
665 velocitySquared = 0.;
666 for (
const auto& v: velocity)
667 velocitySquared += v.squaredNorm();
668 const double velocityNorm = std::sqrt(velocitySquared);
669 const double forceNorm = std::sqrt(forceSquared);
670 for (std::size_t j = 0; j < trial.size(); ++j)
672 (1. - alpha) * velocity[j] +
673 alpha * velocityNorm /
forceNorm * searchForce[j];
675 double displacementSquared = 0.;
676 for (std::size_t j = 0; j < trial.size(); ++j) {
677 step[j] = dt * velocity[j];
678 displacementSquared += step[j].squaredNorm();
680 const double displacementNorm = std::sqrt(displacementSquared);
681 if (displacementNorm > options.maximumStep)
682 scale = options.maximumStep / displacementNorm;
685 for (std::size_t j = 0; j < trial.size(); ++j) {
686 step[j] = dt * velocity[j] + .5 * dt * dt * searchForce[j];
687 largest = std::max(largest, step[j].norm());
689 scale = largest > options.maximumStep
690 ? options.maximumStep / largest
693 std::vector<PotentialEvaluation> next;
695 bool accepted =
false;
696 double fraction = scale;
697 const int maximumBacktracks = options.jctcCompatible ? 1 : 24;
698 for (
int backtrack = 0; backtrack < maximumBacktracks; ++backtrack) {
699 for (std::size_t j = 0; j < trial.size(); ++j)
700 trial[j] = base[j] + fraction * step[j];
703 nextState = state(trial, next);
706 }
catch (
const std::domain_error&) {
712 "No admissible LI-NEB step in the forbidden region";
716 auto nextSearchForce =
718 if (!options.jctcCompatible) {
719 double power = 0., vv = 0., ff = 0.;
720 for (std::size_t j = 0; j < trial.size(); ++j) {
721 velocity[j] += .5 * dt * (searchForce[j] + nextSearchForce[j]);
722 power += velocity[j].dot(nextSearchForce[j]);
723 vv += velocity[j].squaredNorm();
724 ff += nextSearchForce[j].squaredNorm();
726 if (power > 0 && fraction == scale) {
729 for (std::size_t j = 0; j < trial.size(); ++j)
731 (1 - alpha) * velocity[j] +
732 alpha * std::sqrt(vv / ff) * nextSearchForce[j];
734 dt = std::min(dt * 1.1, options.maximumTimeStep);
739 dt = options.minimumTimeStep > 0
740 ? std::max(options.minimumTimeStep, dt * .5)
742 alpha = options.initialMixing;
743 for (
auto& v: velocity)
747 result.path = std::move(trial);
748 result.native = std::move(next);
749 result.state = std::move(nextState);
750 searchForce = std::move(nextSearchForce);
752 if (!(dt > std::numeric_limits<double>::epsilon())) {
753 result.failure =
"LI-NEB time step underflows";
758 result.failure =
"LI-NEB iteration limit";