122 VectorXd initialDirection = VectorXd::Map(initialDirectionAtomMatrix.data(),
123 3 * matter->numberOfAtoms());
124 tau = initialDirection.array() * matter->getFreeV().array();
128 const std::unique_ptr<Matter> forward = std::move(
forward_);
129 if (
tau.norm() > 1e-10) {
130 eonc::safemath::safe_normalize_inplace(
tau);
134 tau =
tau.array() * matter->getFreeV().array();
135 eonc::safemath::safe_normalize_inplace(
tau);
139 double bestNegativeCurvature = std::numeric_limits<double>::max();
140 VectorXd bestTau =
tau;
141 VectorXd bestX0Positions;
142 VectorXd bestG0, bestG1;
149 auto x1Pot =
x1->getPotential();
152 x1->setPotential(x1Pot);
154 VectorXd x0_r =
x0->getPositionsV();
155 bestX0Positions = x0_r;
157 double delta =
params.main_options().finiteDifference;
158 x1->setPositionsV(x0_r + delta *
tau);
159 if (forward && !forward->needsForceUpdate() &&
160 forward->getPositions() ==
x1->getPositions()) {
161 x1->setEvaluation(forward->getForcesRaw(), forward->getPotentialEnergy());
167 if (!
pot->supportsBatchEvaluation()) {
173 (
pot->needsPerImageInstance() &&
174 x0->getPotential().get() !=
x1->getPotential().get());
176 auto evaluatePair = [&]() {
180 if (
pot->supportsBatchEvaluation()) {
181 long n =
x0->numberOfAtoms();
182 bool x0dirty =
x0->needsForceUpdate();
183 bool x1dirty =
x1->needsForceUpdate();
185 if (x0dirty && x1dirty) {
186 auto nrs0 =
x0->getAtomicNrs();
187 auto nrs1 =
x1->getAtomicNrs();
188 auto box0 =
x0->getCell();
189 auto box1 =
x1->getCell();
190 const double *posVec[] = {
x0->getPositions().data(),
191 x1->getPositions().data()};
192 const int *nrsVec[] = {nrs0.data(), nrs1.data()};
193 double *frcVec[] = {
x0->forcesData(),
x1->forcesData()};
194 double energies[2], vars[2];
195 const double *boxVec[] = {box0.data(), box1.data()};
196 pot->forceBatch(2, n, posVec, nrsVec, frcVec, energies, vars, boxVec);
197 x0->setComputedPotential(energies[0], vars[0]);
198 x1->setComputedPotential(energies[1], vars[1]);
199 }
else if (x1dirty) {
200 auto nrs =
x1->getAtomicNrs();
201 auto box =
x1->getCell();
202 const double *posVec[] = {
x1->getPositions().data()};
203 const int *nrsVec[] = {nrs.data()};
204 double *frcVec[] = {
x1->forcesData()};
205 double energies[1], vars[1];
206 const double *boxVec[] = {box.data()};
207 pot->forceBatch(1, n, posVec, nrsVec, frcVec, energies, vars, boxVec);
208 x1->setComputedPotential(energies[0], vars[0]);
209 }
else if (x0dirty) {
212 g0 = -
x0->getForcesV();
213 g1 = -
x1->getForcesV();
214 }
else if (
params.main_options().parallel && canParallel) {
215 std::exception_ptr t0Error;
218 g0 = -
x0->getForcesV();
220 t0Error = std::current_exception();
224 g1 = -
x1->getForcesV();
231 std::rethrow_exception(t0Error);
233 g0 = -
x0->getForcesV();
234 g1 = -
x1->getForcesV();
240 if (
x1->getPotentialEnergy() -
x0->getPotentialEnergy() > 10.0 * delta) {
242 x1->setPositionsV(x0_r + delta *
tau);
244 log,
"[IDimer] Initial tangent flipped due to high energy wall.");
250 AtomMatrix::Map(
tau.data(), matter->numberOfAtoms(), 3),
251 static_cast<quill::Logger *
>(
log))) {
252 C_tau = alt->eigenvalue;
253 tau = VectorXd::Map(alt->eigenvector.data(), 3 * matter->numberOfAtoms());
256 tau =
tau.array() * matter->getFreeV().array();
257 eonc::safemath::safe_normalize_inplace(
tau);
260 x0_r = matter->getPositionsV();
261 x1->setPositionsV(x0_r + delta *
tau);
280 VectorXd x1_rp, x1_r, tau_prime, tau_Old, g1_prime;
283 double phi_prime = 0.0;
284 double phi_min = 0.0;
289 auto x1p = std::make_shared<Matter>(
x1->getPotential(),
params);
292 if (
params.dimer_options().remove_rotation) {
294 AtomMatrix::Map(x0_r.data(),
x0->numberOfAtoms(), 3),
x1);
295 x1_r =
x1->getPositionsV();
296 tau =
x1->pbcV(x1_r - x0_r);
297 eonc::safemath::safe_normalize_inplace(
tau);
298 x1_r = x0_r +
tau * delta;
299 x1->setPositionsV(x1_r);
317 F_R = -2.0 * (g1 - g0) + 2.0 * ((g1 - g0).dot(
tau)) *
tau;
322 theta = eonc::safemath::safe_normalized(
F_R);
342 eonc::safemath::safe_normalize_inplace(
theta);
347 VectorXd s0 =
tau - tau_Old;
357 double H0 = 1.0 / 60.0;
358 size_t loopmax =
s.size();
359 std::vector<double> alpha(loopmax);
362 for (
long i =
static_cast<long>(loopmax) - 1; i >= 0; i--) {
363 alpha[i] =
rho[i] *
s[i].dot(q);
364 q -= alpha[i] *
y[i];
367 for (
size_t i = 0; i < loopmax; i++) {
368 double bv =
rho[i] *
y[i].dot(z);
369 z +=
s[i] * (alpha[i] - bv);
372 double vd = std::clamp(-eonc::safemath::safe_normalized(z).dot(
373 eonc::safemath::safe_normalized(
F_R)),
385 theta = -eonc::safemath::safe_normalized(z);
387 eonc::safemath::safe_normalize_inplace(
theta);
398 if (
C_tau < bestNegativeCurvature) {
399 bestNegativeCurvature =
C_tau;
401 bestX0Positions =
x0->getPositionsV();
408 double d_C_tau_d_phi =
410 phi_prime = -0.5 * atanRatioOrHalfPi(d_C_tau_d_phi, 2.0 * std::abs(
C_tau));
413 double alignment = std::abs(
tau.dot(referenceMode));
415 if (std::abs(phi_prime) > phi_tol) {
416 double b1 = 0.5 * d_C_tau_d_phi;
419 x0_r =
x0->getPositionsV();
420 tau_prime =
tau * std::cos(phi_prime) +
theta * std::sin(phi_prime);
421 tau_prime = eonc::safemath::safe_normalized(tau_prime);
422 x1_rp = x0_r + tau_prime * delta;
425 x1p->setPositionsV(x1_rp);
426 g1_prime = -x1p->getForcesV();
436 C_tau - C_tau_prime + b1 * std::sin(2.0 * phi_prime),
437 1.0 - std::cos(2.0 * phi_prime), 0.0);
438 double a0 = 2.0 * (
C_tau - a1);
439 phi_min = 0.5 * atanRatioOrHalfPi(b1, a1);
441 double C_tau_min = 0.5 * a0 + a1 * std::cos(2.0 * phi_min) +
442 b1 * std::sin(2.0 * phi_min);
445 if (C_tau_min >
C_tau) {
447 C_tau_min = 0.5 * a0 + a1 * std::cos(2.0 * phi_min) +
448 b1 * std::sin(2.0 * phi_min);
458 tau =
tau * std::cos(phi_min) +
theta * std::sin(phi_min);
459 tau = eonc::safemath::safe_normalized(
tau);
460 x1_r = x0_r +
tau * delta;
463 if (
params.dimer_options().remove_rotation) {
464 x1->setPositionsV(x1_r);
466 AtomMatrix::Map(x0_r.data(),
x0->numberOfAtoms(), 3),
x1);
467 x1_r =
x1->getPositionsV();
469 eonc::safemath::safe_normalize_inplace(
tau);
470 x1_r = x0_r +
tau * delta;
473 x1->setPositionsV(x1_r);
477 double sin_pp = std::sin(phi_prime);
481 g0 * (1.0 - std::cos(phi_min) -
482 std::sin(phi_min) * std::tan(phi_prime * 0.5));
488 "[IDimerRot] ----- --------- ---------- ------------------ "
489 "{:9.4f} {:7.3f} {:6.3f} {:4} {:5.3f}",
494 "[IDimerRot] ----- --------- ---------- ------------------ "
495 "{:9.4f} {:7.3f} ------ ---- {:5.3f}",
496 C_tau,
F_R.norm() / delta, alignment);
500 if (alignment <
params.neb_options().climbing_image.ocineb.angle_tol &&
501 params.neb_options().climbing_image.ocineb.use_mmf) {
503 log,
"Terminating dimer due to lost mode (align {:.3f}).", alignment);
506 if (bestNegativeCurvature < 0.0) {
508 C_tau = bestNegativeCurvature;
512 if (
x0->getPositionsV() != bestX0Positions) {
513 x0->setPositionsV(bestX0Positions);
516 x1->setPositionsV(bestX0Positions + delta * bestTau);
518 log,
"Restored best negative curvature state: C_tau={:.4f}",
C_tau);
522 log,
"Never found negative curvature. Final C_tau: {:.4f}",
C_tau);
527 }
while (std::abs(phi_prime) > std::abs(phi_tol) &&
528 std::abs(phi_min) > std::abs(phi_tol) &&