Loading...
Searching...
No Matches
gpr::irc::IRCDriver Class Reference

#include <gpr/irc/IRCDriver.h>

Public Member Functions

 IRCDriver (GradientFn gradient_fn, Eigen::VectorXd masses, IRCOptions options={})
IRCResult run (const Eigen::VectorXd &saddle_positions, const Eigen::MatrixXd &saddle_hessian)

Detailed Description

Definition at line 117 of file IRCDriver.h.

Constructor & Destructor Documentation

◆ IRCDriver()

gpr::irc::IRCDriver::IRCDriver ( GradientFn gradient_fn,
Eigen::VectorXd masses,
IRCOptions options = {} )

Definition at line 281 of file IRCDriver.cpp.

283 : gradient_fn_(std::move(gradient_fn)),
284 masses_(std::move(masses)),
285 options_(options)
286{
287 if (!gradient_fn_) {
288 throw std::invalid_argument(
289 "IRCDriver: gradient functor must not be null");
290 }
291 if (masses_.size() < 1) {
292 throw std::invalid_argument(
293 "IRCDriver: masses vector must have at least one atom");
294 }
295 for (Eigen::Index a = 0; a < masses_.size(); ++a) {
296 if (!(masses_(a) > 0.0) || !std::isfinite(masses_(a))) {
297 throw std::invalid_argument(
298 "IRCDriver: each mass must be finite and positive");
299 }
300 }
301 sqrt_masses_ = buildSqrtMasses(masses_);
302}

Member Function Documentation

◆ run()

IRCResult gpr::irc::IRCDriver::run ( const Eigen::VectorXd & saddle_positions,
const Eigen::MatrixXd & saddle_hessian )

Definition at line 304 of file IRCDriver.cpp.

306{
307 const Eigen::Index ndof = 3 * masses_.size();
308 if (saddle_positions.size() != ndof) {
309 throw std::invalid_argument(
310 "IRCDriver::run: positions size mismatch with masses");
311 }
312 if (saddle_hessian.rows() != ndof || saddle_hessian.cols() != ndof) {
313 throw std::invalid_argument(
314 "IRCDriver::run: hessian shape mismatch with positions");
315 }
316 if (options_.method != IRCMethod::Morokuma &&
317 options_.method != IRCMethod::MorokumaExtended) {
318 throw std::invalid_argument("IRCDriver::run: unknown IRC method");
319 }
320
321 gpr::neb::RgsaddleClient client;
322 requireRgsaddle(client);
323
324 const bool want_forward = options_.direction == IRCDirection::Forward ||
325 options_.direction == IRCDirection::Both;
326 const bool want_reverse = options_.direction == IRCDirection::Reverse ||
327 options_.direction == IRCDirection::Both;
328
329 Eigen::VectorXd mode = Eigen::VectorXd::Zero(ndof);
330 bool compensate_sign = false;
331 if (options_.hessian_sketch_rank > 0) {
332 if (options_.mode_to_follow != 1) {
333 throw std::runtime_error(
334 "IRCDriver::run: rgsaddle_irc_create_from_surface returns "
335 "only the lowest mode");
336 }
337 if (!client.hasIrcFromSurface()) {
338 throw std::runtime_error(
339 "IRCDriver::run: the linked librgsaddle has no "
340 "rgsaddle_irc_create_from_surface");
341 }
342 } else {
343 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(
344 hessianToMassWeighted(saddle_hessian, sqrt_masses_));
345 if (es.info() != Eigen::Success) {
346 throw std::runtime_error(
347 "IRCDriver::run: eigendecomposition of saddle Hessian failed");
348 }
349 const int k = std::max(1, options_.mode_to_follow) - 1;
350 if (k >= es.eigenvalues().size()) {
351 throw std::invalid_argument(
352 "IRCDriver::run: mode_to_follow exceeds available "
353 "eigenmodes");
354 }
355 if (es.eigenvalues()(k) >= 0.0) {
356 throw std::runtime_error(
357 "IRCDriver::run: requested mode is not imaginary "
358 "(eigenvalue >= 0); not a saddle along this coordinate");
359 }
360 const Eigen::VectorXd v_mw = es.eigenvectors().col(k);
361 mode = v_mw.cwiseQuotient(sqrt_masses_);
362 compensate_sign = true;
363 }
364
365 int evals = 0;
366 IrcSurface surface;
367 surface.gradient = &gradient_fn_;
368 surface.evals = &evals;
369 surface.ndof = ndof;
370
371 auto direction_code = [&](bool forward) -> int32_t {
372 if (!compensate_sign)
373 return forward ? RGSADDLE_IRC_FORWARD : RGSADDLE_IRC_REVERSE;
374 return rgsaddleDirection(forward, mode.cwiseProduct(sqrt_masses_));
375 };
376
377 const bool first_is_forward = want_forward || !want_reverse;
378 const int32_t first_direction = direction_code(first_is_forward);
379 const rgsaddle_irc_config_t cfg =
380 ircConfig(client, options_, first_direction);
381 gpr::neb::RgsaddleClient::Irc raw = nullptr;
382 if (options_.hessian_sketch_rank > 0) {
383 const Eigen::VectorXd seed = surfaceLaunchSeed(
384 options_.sketch_seed, saddle_positions, masses_, sqrt_masses_);
385 raw = client.ircCreateFromSurface(
386 &cfg, static_cast<int64_t>(masses_.size()), saddle_positions.data(),
387 masses_.data(), seed.data(), ircSurface, &surface);
388 } else {
389 raw = client.ircCreate(&cfg, static_cast<int64_t>(masses_.size()),
390 saddle_positions.data(), masses_.data(),
391 mode.data());
392 }
393 if (raw == nullptr) {
394 throw std::runtime_error(
395 options_.hessian_sketch_rank > 0
396 ? "IRCDriver::run: rgsaddle_irc_create_from_surface returned "
397 "null"
398 : "IRCDriver::run: rgsaddle_irc_create returned null");
399 }
400 IrcSessionGuard session(client, raw);
401 const int lengths = client.ircSetLengths(
402 session.get(), options_.initial_displacement, options_.max_step,
403 options_.min_step,
404 options_.method == IRCMethod::MorokumaExtended ? 1 : 0);
405 if (lengths != RGSADDLE_OK) {
406 throw std::runtime_error(
407 "IRCDriver::run: rgsaddle_irc_set_lengths failed with status " +
408 std::to_string(lengths));
409 }
410 if (options_.hessian_sketch_rank > 0) {
411 // The kick displacement is parallel to the mode Lanczos returned.
412 // Put the session back on the saddle before the branch is traced.
413 surface.capture = true;
414 surface.captured = false;
415 surface.saw_last = false;
416 rgsaddle_report_t launch{};
417 const int stepped =
418 client.ircStep(session.get(), ircSurface, &surface, &launch);
419 if (stepped != RGSADDLE_OK) {
420 throw std::runtime_error(
421 "IRCDriver::run: rgsaddle_irc_step failed with status " +
422 std::to_string(stepped));
423 }
424 if (!surface.captured) {
425 throw std::runtime_error(
426 "IRCDriver::run: rgsaddle_irc_step did not evaluate "
427 "the surface");
428 }
429 const Eigen::VectorXd kick = surface.position - saddle_positions;
430 const int restored =
431 client.ircSetDirection(session.get(), first_direction);
432 if (restored != RGSADDLE_OK) {
433 throw std::runtime_error(
434 "IRCDriver::run: rgsaddle_irc_set_direction failed "
435 "with status " +
436 std::to_string(restored));
437 }
438 const double eps =
439 options_.hv_eps > 0.0 && std::isfinite(options_.hv_eps)
440 ? options_.hv_eps
441 : 1e-3;
442 const double curvature = massWeightedCurvature(
443 saddle_positions, kick, sqrt_masses_, eps, gradient_fn_);
444 if (curvature >= 0.0) {
445 throw std::runtime_error(
446 "IRCDriver::run: requested mode is not imaginary "
447 "(eigenvalue >= 0); not a saddle along this coordinate");
448 }
449 }
450
451 auto trace = [&]() {
452 IRCBranch branch;
453 branch.path.reserve(
454 static_cast<std::size_t>(std::max(options_.max_steps, 0)) + 1);
455 const int evals_before = evals;
456 IRCStep saddle_step;
457 saddle_step.positions = saddle_positions;
458 if (options_.record_energy) {
459 const auto evaluated = gradient_fn_(saddle_positions);
460 ++evals;
461 if (evaluated.second.size() != ndof) {
462 throw std::invalid_argument(
463 "IRCDriver::run: gradient length differs from the "
464 "positions");
465 }
466 saddle_step.energy = evaluated.first;
467 saddle_step.gradient = evaluated.second;
468 saddle_step.g_norm =
469 evaluated.second.cwiseQuotient(sqrt_masses_).norm();
470 }
471 branch.path.push_back(std::move(saddle_step));
472
473 Eigen::VectorXd previous = saddle_positions;
474 double arc = 0.0;
475 for (int step = 0; step < options_.max_steps; ++step) {
476 surface.capture = true;
477 surface.captured = false;
478 surface.saw_last = false;
479 rgsaddle_report_t report{};
480 const int rc =
481 client.ircStep(session.get(), ircSurface, &surface, &report);
482 if (rc != RGSADDLE_OK) {
483 throw std::runtime_error(
484 "IRCDriver::run: rgsaddle_irc_step failed with status " +
485 std::to_string(rc));
486 }
487 if (!surface.captured) {
488 throw std::runtime_error(
489 "IRCDriver::run: rgsaddle_irc_step did not evaluate "
490 "the surface");
491 }
492 IRCStep sample;
493 sample.positions = surface.position;
494 sample.gradient = surface.gradient_value;
495 sample.energy = surface.energy;
496 sample.g_norm =
497 surface.gradient_value.cwiseQuotient(sqrt_masses_).norm();
498 arc +=
499 (surface.position - previous).cwiseProduct(sqrt_masses_).norm();
500 sample.cumulative_arc = arc;
501 previous = surface.position;
502 const bool small_gradient = sample.g_norm <= options_.conv_tol;
503 branch.path.push_back(std::move(sample));
504 // The kick keeps at_minimum false, so a small force on that
505 // first sample is the saddle neighborhood. A later step that
506 // is already inside tolerance returns at_minimum without
507 // moving; that sample is the stop. The block below does not
508 // append a point the session did not leave.
509 const bool library_done =
510 report.status == RGSADDLE_STATUS_CONVERGED;
511 if (small_gradient && library_done) {
512 branch.converged = true;
513 break;
514 }
515 // status 1 describes the session position after the step, which
516 // can be a later evaluation than the sample just stored. Stop
517 // only when that accepted geometry is inside conv_tol.
518 if (report.status == RGSADDLE_STATUS_CONVERGED) {
519 Eigen::VectorXd accepted(ndof);
520 if (client.ircPosition(session.get(), accepted.data()) !=
521 RGSADDLE_OK) {
522 throw std::runtime_error(
523 "IRCDriver::run: rgsaddle_irc_position failed");
524 }
525 if ((accepted - branch.path.back().positions).norm() > 1e-12) {
526 IRCStep arrived;
527 const bool last_is_accepted =
528 surface.saw_last &&
529 (surface.last_position - accepted).norm() <= 1e-12;
530 if (last_is_accepted) {
531 arrived.positions = surface.last_position;
532 arrived.gradient = surface.last_gradient;
533 arrived.energy = surface.last_energy;
534 } else {
535 const auto evaluated = gradient_fn_(accepted);
536 ++evals;
537 if (evaluated.second.size() != ndof) {
538 throw std::invalid_argument(
539 "IRCDriver::run: gradient length differs "
540 "from the positions");
541 }
542 arrived.positions = accepted;
543 arrived.energy = evaluated.first;
544 arrived.gradient = evaluated.second;
545 }
546 arrived.g_norm =
547 arrived.gradient.cwiseQuotient(sqrt_masses_).norm();
548 if (arrived.g_norm <= options_.conv_tol) {
549 arc += (arrived.positions - previous)
550 .cwiseProduct(sqrt_masses_)
551 .norm();
552 arrived.cumulative_arc = arc;
553 previous = arrived.positions;
554 branch.path.push_back(std::move(arrived));
555 branch.converged = true;
556 break;
557 }
558 }
559 }
560 }
561 branch.gradient_evals = evals - evals_before;
562 branch.final_energy =
563 branch.path.empty() ? 0.0 : branch.path.back().energy;
564 return branch;
565 };
566
567 IRCResult out;
568 if (want_forward) out.forward = trace();
569 if (want_reverse) {
570 if (want_forward) {
571 const int rc =
572 client.ircSetDirection(session.get(), direction_code(false));
573 if (rc != RGSADDLE_OK) {
574 throw std::runtime_error(
575 "IRCDriver::run: rgsaddle_irc_set_direction failed "
576 "with status " +
577 std::to_string(rc));
578 }
579 }
580 out.reverse = trace();
581 }
582 return out;
583}
int ircStep(Irc session, rgsaddle_surface_fn surface, void *user, rgsaddle_report_t *out)
Irc ircCreateFromSurface(const rgsaddle_irc_config_t *cfg, int64_t n_atoms, const double *saddle, const double *masses, const double *seed, rgsaddle_surface_fn surface, void *user)
Irc ircCreate(const rgsaddle_irc_config_t *cfg, int64_t n_atoms, const double *saddle, const double *masses, const double *mode)
int ircPosition(const Irc session, double *out)
int ircSetLengths(Irc session, double kick, double max_step, double min_step, int adaptive)
int ircSetDirection(Irc session, int32_t direction)
bool hasIrcFromSurface() const noexcept
rgsaddle_irc_create_from_surface. The Lanczos kick needs it.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
eon_relax_band_t eon_relax_surface_fn surface

The documentation for this class was generated from the following files: