292 {
296
297 QUILL_LOG_DEBUG(
log,
"Nudged elastic band calculation started.");
298
299
300 E_ref = std::max(
path[0]->getPotentialEnergy(),
302
304
305 auto objf = std::make_shared<NEBObjectiveFunction>(
this,
params);
306
307 bool switched{false};
308#ifdef WITH_RGSADDLE
309 std::unique_ptr<XtsciBand> rustBand;
310#endif
311 std::unique_ptr<Optimizer> optim;
312 std::unique_ptr<Optimizer> refine_optim;
313#ifdef WITH_RGSADDLE
314 const auto bandMethod =
params.neb_options().opt_method;
315 if (!
params.neb_options().spring.geometric &&
318 rustBand = std::make_unique<XtsciBand>(*
this,
params);
319 } else
320#endif
321 {
322 const auto &band =
params.neb_options().band;
323 if (band.precon != "none" || band.stepper != "auto") {
324 throw std::invalid_argument(
325 "band_precon and band_stepper act on the rgsaddle band: a build "
326 "with rgsaddle, opt_method lbfgs, fire or xtsci, and no "
327 "geometric_spring");
328 }
333 objf,
params.optimizer_options().refine.method,
params);
334 }
335 }
336
339 bool zoomDone{false};
340 long zoomStable{0};
341 long zoomPrevCI{-1};
342 long zoomAt{-1};
343
344 double activation_force{0.0};
345
346
347
348
349 const double moveBase =
params.optimizer_options().max_move;
351 double moveNow = moveBase;
352 double bestForce = std::numeric_limits<double>::infinity();
353 int stall = 0;
354
357 path.front()->pollCancel(
"neb");
358 }
359 if (
params.debug_options().write_movies &&
360 (iteration %
params.debug_options().write_movies_interval == 0)) {
361 bool append = (iteration != 0);
364 params.debug_options().estimate_neb_eigenvalues,
365 std::format("neb_path_{:03d}.con", iteration), iteration,
367 QUILL_LOG_ERROR(
log,
"Failed to write NEB path movie for iteration {}",
368 iteration);
369 }
370
373 maxTang =
374 path[0]->pbc(
path[1]->getPositions() -
path[0]->getPositions());
378 } else {
380 }
381 eonc::safemath::safe_normalize_inplace(maxTang);
382 auto maxImageMetadata = eonc::io::ConFrameMetadata{};
383 maxImageMetadata.frame_index =
static_cast<uint64_t
>(
maxEnergyImage);
385 maxImageMetadata.neb_bead =
static_cast<uint64_t
>(
maxEnergyImage);
386 maxImageMetadata.neb_band = static_cast<uint64_t>(iteration);
387 maxImageMetadata.scalars.push_back(
388 {"relative_energy",
390 maxImageMetadata.scalars.push_back(
391 {"parallel_force",
393 maxImageMetadata.strings.push_back({"movie_kind", "neb_maximage"});
395 "neb_maximage.con", append, &maxImageMetadata))) {
397 }
399 }
400
401 VectorXd pos = objf->getPositions();
403
405
406 if (iteration == 0) {
408 activation_force = convForce;
409 ocineb.initBaseline(convForce);
410
411
412 auto &ci_opt =
params.neb_options().climbing_image;
413 auto &mmf_opt = ci_opt.ocineb;
414 auto fmt_trigger = [](double val) -> std::string {
415 if (val > 1e100)
416 return "INF";
417 return std::format("{:.4f}", val);
418 };
419
420 QUILL_LOG_INFO(
422 "===============================================================");
423 QUILL_LOG_INFO(
log,
" NEB Optimization Configuration");
424 QUILL_LOG_INFO(
426 "===============================================================");
428
429 std::string ci_status = ci_opt.enabled ? "ENABLED" : "DISABLED";
430 QUILL_LOG_INFO(
log,
" {:<25} : {}",
"Climbing Image (CI)", ci_status);
431 if (ci_opt.enabled) {
433 QUILL_LOG_INFO(
log,
" - {:<21} : {} (Factor: {:.2f})",
434 "Relative Trigger", fmt_trigger(ci_rel_val),
435 ci_opt.trigger_factor);
436 QUILL_LOG_INFO(
log,
" - {:<21} : {}",
"Absolute Trigger",
437 fmt_trigger(ci_opt.trigger_force));
438 QUILL_LOG_INFO(
log,
" - {:<21} : {}",
"Converged Only",
439 ci_opt.converged_only);
440 }
441
442 std::string mmf_status =
443 (ci_opt.enabled && mmf_opt.use_mmf) ? "ENABLED" : "DISABLED";
444 QUILL_LOG_INFO(
log,
" {:<25} : {}",
"Hybrid MMF (OCINEB)", mmf_status);
445 if (ci_opt.enabled && mmf_opt.use_mmf) {
446 QUILL_LOG_INFO(
log,
" - {:<21} : {:.4f} (Factor: {:.2f})",
447 "Initial Threshold", ocineb.threshold(),
448 mmf_opt.trigger_factor);
449 QUILL_LOG_INFO(
log,
" - {:<21} : {:.4f}",
"Absolute Floor",
450 mmf_opt.trigger_force);
451 QUILL_LOG_INFO(
log,
" - {:<21} : {:.4f}",
"Angle Tolerance",
452 mmf_opt.angle_tol);
453 }
454 QUILL_LOG_INFO(
456 "---------------------------------------------------------------");
457
458 EONC_LOG_DEBUG(
"{:>10s} {:>12s} {:>14s} {:>11s} {:>12s}",
"iteration",
459 "step size",
460 params.optimizer_options().convergence_metric_label,
461 "max image", "max energy");
462 QUILL_LOG_DEBUG(
464 "---------------------------------------------------------------\n");
465 }
466
467
468 bool ci_active =
469 params.neb_options().climbing_image.enabled &&
470 (activation_force <
472 params.neb_options().climbing_image.trigger_factor ||
473 activation_force <
params.neb_options().climbing_image.trigger_force);
474
475 bool zoomedThisStep = false;
476 if (iteration && !zoomDone &&
params.neb_options().zoom.enabled &&
479 ++zoomStable;
480 } else {
482 zoomStable = 1;
483 }
484 const auto &zoom =
params.neb_options().zoom;
485 const double zoomForce =
486 zoom.activation_threshold > 0.0
487 ? zoom.activation_threshold
488 : 10.0 *
params.neb_options().force_tolerance;
489 if (zoomStable >= zoom.stability_count && convForce < zoomForce) {
490 std::vector<double> energy;
491 energy.reserve(
path.size());
492 for (
const auto &image :
path) {
493 energy.push_back(image->getPotentialEnergy());
494 }
495 const auto window =
498 zoom.interpolation)) {
502 zoomDone = true;
503 zoomedThisStep = true;
504 zoomAt = iteration;
505 QUILL_LOG_INFO(
log,
"Zoom-NEB: packed the band onto images [{}, {}]",
506 window.lo, window.hi);
507 }
508 }
509 }
510
511 if (iteration) {
512
513
514 double mmfForce = convForce;
518 params.optimizer_options().convergence_metric);
519 }
520
521
522 if (!zoomedThisStep &&
524 ocineb.stabilityCount())) {
525 auto result = ocineb.run(*this, convForce);
526
527 if (result.convergedAfterMMF) {
529 break;
530 }
531
532
533 if (result.shouldResetOptimizer) {
534#ifdef WITH_RGSADDLE
535 if (rustBand) {
536 rustBand->syncFromPath();
537 rustBand->reset();
538 } else
539#endif
540 {
543 }
544 }
545 }
546
547 long iterLimit =
params.neb_options().max_iterations;
548 if (zoomDone &&
params.neb_options().zoom.max_iterations > 0 &&
549 zoomAt >= 0) {
550 iterLimit = zoomAt +
params.neb_options().zoom.max_iterations;
551 }
552 if (iteration >= iterLimit) {
554 break;
555 }
556
557 if (zoomedThisStep) {
559 iteration++;
560 continue;
561 }
562
563
564
566
567#ifdef WITH_RGSADDLE
568 if (rustBand) {
569 rustBand->step(moveNow);
570 } else
571#endif
572 {
573 auto &activeOptim =
574 (refine_optim &&
575 convForce <=
params.optimizer_options().refine.threshold)
576 ? refine_optim
577 : optim;
578 if (refine_optim &&
579 convForce <=
params.optimizer_options().refine.threshold &&
580 !switched) {
581 switched = true;
583 magic_enum::enum_name<OptType>(
584 params.optimizer_options().refine.method));
585 }
586 activeOptim->step(moveNow);
587 }
588
590 if (adaptCg && activation_force < bestForce * 0.98) {
591 bestForce = activation_force;
592 stall = 0;
593 if (moveNow < moveBase && convForce > 0.0 &&
594 activation_force < 0.8 * convForce) {
595 moveNow = std::min(moveBase, moveNow * 1.5);
596 }
597 } else if (adaptCg) {
598 ++stall;
599 if (convForce > 0.0 && activation_force > 2.0 * convForce) {
600 moveNow = std::max(moveNow * 0.5, moveBase * 0.2);
601 }
602 if (stall >= 12) {
603 moveNow = std::max(moveBase * 0.2, moveNow * 0.5);
604 stall = 0;
605#ifdef WITH_RGSADDLE
606 if (rustBand) {
607 rustBand->reset();
608 } else
609#endif
610 {
613 if (refine_optim) {
615 objf,
params.optimizer_options().refine.method,
params);
616 }
617 }
618 QUILL_LOG_DEBUG(
log,
"Band stalled; reset optimizer, step {:.4e}",
619 moveNow);
620 }
621 }
623 }
624
625 iteration++;
626
628 double stepSize = 0.0;
630 const VectorXd delta = objf->difference(objf->getPositions(), pos);
631 const long seg = 3L *
atoms + 9L;
632 for (
long image = 0; image <
numImages; ++image) {
633 stepSize = std::max(
634 stepSize, delta.segment(image * seg, seg).cwiseAbs().maxCoeff());
635 }
636 } else {
638 path[0]->pbcV(objf->getPositions() - pos));
639 }
640 QUILL_LOG_DEBUG(
log,
"{:>10} {:>12.4e} {:>14.4e} {:>11} {:>12.4}",
642 dE);
643
645 if (objf->isUncertain()) {
646 QUILL_LOG_DEBUG(
log,
"NEB failed due to high uncertainty");
648 break;
649 } else if (objf->isConverged()) {
650 QUILL_LOG_DEBUG(
log,
"NEB converged\n");
652 break;
653 }
654 } else {
655 if (objf->isConverged()) {
656 QUILL_LOG_DEBUG(
log,
"NEB converged\n");
658 break;
659 }
660 }
661 }
663}
double matDot(const AtomMatrix &a, const AtomMatrix &b)
SIMD-optimized dot product for contiguous Eigen matrices.
#define EONC_LOG_DEBUG(...)
#define EONC_LOG_WARNING(...)
void printImageData(bool writeToFile=false, size_t idx=0)
double convergenceForce(void)
void setCIEnabled(bool enabled)
friend class eonc::neb::OCINEBController
static Config fromParams(const Parameters ¶ms)
static double forceMeasure(const AtomMatrix &freeForces, std::string_view metric)
Measure free-atom forces with the band's configured convergence metric.
double maxAtomMotionV(const VectorXd v1)
IoStatus matter2con(Matter &m, std::string filename, bool append, const ConFrameMetadata *metadata)
Append a frame to a .con, or truncate and write one frame.
constexpr bool io_ok(IoStatus s) noexcept
Window selectWindow(const std::vector< double > &energy, std::size_t climbingImage, const neb_options_t::zoom_options_t &cfg)
Auto: contiguous images around the climbing image whose energy is above E_ref + alpha * barrier.
bool redistributePath(std::vector< std::shared_ptr< Matter > > &path, Window window, neb_options_t::zoom_options_t::Interpolation how)
Place every band image on the window by equal arc length.
eonc::io::IoStatus writePathCon(const std::vector< std::shared_ptr< Matter > > &path, const std::vector< std::shared_ptr< AtomMatrix > > &tangent, const std::vector< std::shared_ptr< EigenmodeStrategy > > &eigenmode_solvers, long numImages, bool estimateEigenvalues, std::string filename, std::optional< size_t > bandIndex, double referenceEnergy, const std::vector< double > *quantumFreeEnergy, const eonc::tunneling::BandWalls *walls)
Write a NEB band as a multi-frame .con via readcon ConFrameBuilder::clone().