120 {
121
122 VectorXd initialDirection = VectorXd::Map(initialDirectionAtomMatrix.data(),
123 3 * matter->numberOfAtoms());
124 tau = initialDirection.array() * matter->getFreeV().array();
127
128 const std::unique_ptr<Matter> forward = std::move(
forward_);
129 if (
tau.norm() > 1e-10) {
130 eonc::safemath::safe_normalize_inplace(
tau);
131 } else {
132
134 tau =
tau.array() * matter->getFreeV().array();
135 eonc::safemath::safe_normalize_inplace(
tau);
136 }
137
138
139 double bestNegativeCurvature = std::numeric_limits<double>::max();
140 VectorXd bestTau =
tau;
141 VectorXd bestX0Positions;
142 VectorXd bestG0, bestG1;
143
144
146
147 {
148
149 auto x1Pot =
x1->getPotential();
152 x1->setPotential(x1Pot);
153 }
154 VectorXd x0_r =
x0->getPositionsV();
155 bestX0Positions = x0_r;
156
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());
162 }
163
164
165
166
167 if (!
pot->supportsBatchEvaluation()) {
168 Matter *
const ends[] = {
x0.get(),
x1.get()};
170 }
171
173 (
pot->needsPerImageInstance() &&
174 x0->getPotential().get() !=
x1->getPotential().get());
175 VectorXd g0, g1;
176 auto evaluatePair = [&]() {
177
178
179
180 if (
pot->supportsBatchEvaluation()) {
181 long n =
x0->numberOfAtoms();
182 bool x0dirty =
x0->needsForceUpdate();
183 bool x1dirty =
x1->needsForceUpdate();
184
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) {
211 }
212 g0 = -
x0->getForcesV();
213 g1 = -
x1->getForcesV();
214 }
else if (
params.main_options().parallel && canParallel) {
215 std::exception_ptr t0Error;
216 std::thread t0([&] {
217 try {
218 g0 = -
x0->getForcesV();
219 } catch (...) {
220 t0Error = std::current_exception();
221 }
222 });
223 try {
224 g1 = -
x1->getForcesV();
225 } catch (...) {
226 t0.join();
227 throw;
228 }
229 t0.join();
230 if (t0Error)
231 std::rethrow_exception(t0Error);
232 } else {
233 g0 = -
x0->getForcesV();
234 g1 = -
x1->getForcesV();
235 }
236 };
237 evaluatePair();
238
239
240 if (
x1->getPotentialEnergy() -
x0->getPotentialEnergy() > 10.0 * delta) {
242 x1->setPositionsV(x0_r + delta *
tau);
243 QUILL_LOG_DEBUG(
244 log,
"[IDimer] Initial tangent flipped due to high energy wall.");
245 }
246
247
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);
258
259
260 x0_r = matter->getPositionsV();
261 x1->setPositionsV(x0_r + delta *
tau);
264 return;
265 }
266
267
268
274 }
275
276
277
278
279
280 VectorXd x1_rp, x1_r, tau_prime, tau_Old, g1_prime;
281 double phi_tol =
283 double phi_prime = 0.0;
284 double phi_min = 0.0;
285
287
288
289 auto x1p = std::make_shared<Matter>(
x1->getPotential(),
params);
290
291
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);
300 }
301
302 evaluatePair();
303
304 bestG0 = g0;
305 bestG1 = g1;
306
313
314 do {
315
316
317 F_R = -2.0 * (g1 - g0) + 2.0 * ((g1 - g0).
dot(
tau)) *
tau;
319
320
322 theta = eonc::safemath::safe_normalized(
F_R);
323
328 } else {
333 : 0.0;
334 }
335
341 }
342 eonc::safemath::safe_normalize_inplace(
theta);
344
347 VectorXd s0 =
tau - tau_Old;
349 VectorXd y0 =
353 } else {
355 }
356
357 double H0 = 1.0 / 60.0;
358 size_t loopmax =
s.size();
359 std::vector<double> alpha(loopmax);
360
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];
365 }
366 VectorXd z = H0 * q;
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);
370 }
371
372 double vd = std::clamp(-eonc::safemath::safe_normalized(z).
dot(
373 eonc::safemath::safe_normalized(
F_R)),
374 -1.0, 1.0);
375 double angle =
377
378 if (angle > 87.0) {
383 }
384
385 theta = -eonc::safemath::safe_normalized(z);
387 eonc::safemath::safe_normalize_inplace(
theta);
388
392 }
393
394
396
397
398 if (
C_tau < bestNegativeCurvature) {
399 bestNegativeCurvature =
C_tau;
401 bestX0Positions =
x0->getPositionsV();
402 bestG0 = g0;
403 bestG1 = g1;
405 }
406
407
408 double d_C_tau_d_phi =
410 phi_prime = -0.5 * atanRatioOrHalfPi(d_C_tau_d_phi, 2.0 * std::abs(
C_tau));
412
413 double alignment = std::abs(
tau.dot(referenceMode));
414
415 if (std::abs(phi_prime) > phi_tol) {
416 double b1 = 0.5 * d_C_tau_d_phi;
417
418
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;
423
425 x1p->setPositionsV(x1_rp);
426 g1_prime = -x1p->getForcesV();
427
430
431 double C_tau_prime =
433
434
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);
440
441 double C_tau_min = 0.5 * a0 + a1 * std::cos(2.0 * phi_min) +
442 b1 * std::sin(2.0 * phi_min);
443
444
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);
449 }
450
451
454 }
456
457
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;
461
462
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;
471 }
472
473 x1->setPositionsV(x1_r);
475
476
477 double sin_pp = std::sin(phi_prime);
479 0.0) +
481 g0 * (1.0 - std::cos(phi_min) -
482 std::sin(phi_min) * std::tan(phi_prime * 0.5));
483
486 QUILL_LOG_INFO(
488 "[IDimerRot] ----- --------- ---------- ------------------ "
489 "{:9.4f} {:7.3f} {:6.3f} {:4} {:5.3f}",
491 } else {
492 QUILL_LOG_INFO(
494 "[IDimerRot] ----- --------- ---------- ------------------ "
495 "{:9.4f} {:7.3f} ------ ---- {:5.3f}",
496 C_tau,
F_R.norm() / delta, alignment);
497 }
498
499
500 if (alignment <
params.neb_options().climbing_image.ocineb.angle_tol &&
501 params.neb_options().climbing_image.ocineb.use_mmf) {
502 QUILL_LOG_WARNING(
503 log,
"Terminating dimer due to lost mode (align {:.3f}).", alignment);
505
506 if (bestNegativeCurvature < 0.0) {
507
508 C_tau = bestNegativeCurvature;
510
511
512 if (
x0->getPositionsV() != bestX0Positions) {
513 x0->setPositionsV(bestX0Positions);
515 }
516 x1->setPositionsV(bestX0Positions + delta * bestTau);
517 QUILL_LOG_DEBUG(
518 log,
"Restored best negative curvature state: C_tau={:.4f}",
C_tau);
519 throw eonc::DimerModeRestoredException();
520 } else {
521 QUILL_LOG_WARNING(
522 log,
"Never found negative curvature. Final C_tau: {:.4f}",
C_tau);
523 throw eonc::DimerModeLostException();
524 }
525 }
526
527 } while (std::abs(phi_prime) > std::abs(phi_tol) &&
528 std::abs(phi_min) > std::abs(phi_tol) &&
530}
bool foundNegativeCurvature
std::unique_ptr< Matter > forward_
std::vector< VectorXd > s
VectorXd fixedReferenceMode
std::vector< VectorXd > positions
std::vector< VectorXd > gradients
std::vector< VectorXd > y
std::vector< double > rho
double dot(const double *x, const double *y, std::size_t n)
Round-to-nearest-even value of the exact sum of x[i] * y[i].
void rotationRemove(const AtomMatrix r1, std::shared_ptr< Matter > m2)
constexpr double safe_div(double num, double denom, double fallback=0.0)
double safe_acos(double x)
constexpr double safe_recip(double x, double fallback=0.0)
bool potAllowsSharedInstance(const P &p) noexcept
std::optional< DimerRotationResult > runAlternativeRotation(DimerRotationBackend backend, const std::shared_ptr< Matter > &matter, const Parameters ¶ms, const std::shared_ptr< Potential > &pot, const AtomMatrix &initialDirection, quill::Logger *log=nullptr)
Run Lanczos, Davidson, or LOR.
void evaluateTogether(Potential &pot, std::span< Matter *const > systems, bool threads, std::span< const long > owners)
Evaluates every system that needs a force update.