18#if RGSADDLE_ABI_MINOR >= 14
19#define EONC_RGSADDLE_IRC 1
23#ifdef EONC_RGSADDLE_IRC
32#ifdef EONC_RGSADDLE_IRC
37constexpr double kSamePoint = 1.0e-12;
42 Eigen::VectorXd gradient;
47 std::deque<Evaluation> seen;
50const Evaluation &evaluate(IrcUser &ctx,
const double *x, Eigen::Index n) {
51 const Eigen::Map<const Eigen::VectorXd> at(x, n);
52 for (
const auto &e : ctx.seen) {
53 if ((e.x - at).cwiseAbs().maxCoeff() <= kSamePoint) {
57 ctx.probe->setPositionsFreeV(at);
58 Evaluation e{at, ctx.probe->getPotentialEnergy(),
59 -ctx.probe->getForcesFreeV()};
60 if (ctx.seen.size() >= 4) {
63 ctx.seen.push_back(std::move(e));
64 return ctx.seen.back();
67rgsaddle_status_t surfaceCallback(
void *user, rgsaddle_surface_request_t *req) {
68 auto *ctx =
static_cast<IrcUser *
>(user);
69 if (ctx ==
nullptr || req ==
nullptr || req->positions ==
nullptr ||
70 req->energies ==
nullptr || req->gradients ==
nullptr ||
72 return RGSADDLE_SURFACE_FAILED;
74 if (req->n_atoms != ctx->probe->numberOfFreeAtoms()) {
75 return RGSADDLE_SHAPE;
78 const Evaluation &e = evaluate(*ctx, req->positions, 3 * req->n_atoms);
79 req->energies[0] = e.energy;
80 std::copy(e.gradient.begin(), e.gradient.end(), req->gradients);
83 return RGSADDLE_SURFACE_FAILED;
87void checkStatus(
int rc,
const char *what) {
88 if (rc != RGSADDLE_OK) {
89 throw std::runtime_error(
90 std::string(what) +
": " +
91 rgsaddle_status_name(
static_cast<rgsaddle_status_t
>(rc)));
95IrcBranch followBranch(RgsaddleIrc *session, IrcUser &ctx,
long maxSteps,
100 for (
long k = 0; k < maxSteps && !branch.reachedMinimum; ++k) {
101 rgsaddle_report_t report{};
102 checkStatus(rgsaddle_irc_step(session, surfaceCallback, &ctx, &report),
103 "rgsaddle_irc_step");
104 checkStatus(rgsaddle_irc_position(session, x.data()),
105 "rgsaddle_irc_position");
109 branch.reachedMinimum = report.status == RGSADDLE_STATUS_CONVERGED;
110 if (last.size() != n || (x - last).cwiseAbs().maxCoeff() > kSamePoint) {
111 const Evaluation &e = evaluate(ctx, x.data(), n);
112 branch.energies.push_back(e.energy);
113 branch.maxForces.push_back(AtomMatrix::Map(e.gradient.data(), n / 3, 3)
117 ctx.probe->setPositionsFreeV(x);
118 branch.positions.push_back(ctx.probe->getPositions());
129#ifdef EONC_RGSADDLE_IRC
130 rgsaddle_version_t abi{};
131 return rgsaddle_abi_stamp(&abi) == RGSADDLE_OK &&
132 abi.major == RGSADDLE_ABI_MAJOR && abi.minor >= 14;
141 throw std::runtime_error(
"rgsaddle IRC is not available in this build");
143#ifdef EONC_RGSADDLE_IRC
145 std::vector<double> masses;
146 std::vector<long> rows;
147 for (
long i = 0; i < saddle.numberOfAtoms(); ++i) {
148 if (!saddle.getFixed(i)) {
149 masses.push_back(saddle.getMass(i));
153 if (rows.empty() || mode.rows() != saddle.numberOfAtoms()) {
154 throw std::runtime_error(
"IRC needs free atoms and one mode row per atom");
156 const auto nFree =
static_cast<Eigen::Index
>(rows.size());
158 for (Eigen::Index j = 0; j < nFree; ++j) {
159 freeMode.row(j) = mode.row(rows[
static_cast<size_t>(j)]);
161 const Eigen::VectorXd x0 = saddle.getPositionsFreeV();
164 IrcUser ctx{&probe, {}};
167 auto branch = [&](int32_t direction) {
168 rgsaddle_irc_config_t
config{};
169 config.version = RGSADDLE_VERSION_INIT;
170 config.dx = opts.step_size;
171 config.force_tol = opts.converged_force;
172 config.force_gate = RGSADDLE_FORCE_MAX_ATOM;
175 config.kind = RGSADDLE_IRC_GS2;
176 config.direction = direction;
177 std::unique_ptr<RgsaddleIrc,
decltype(&rgsaddle_irc_free)> session(
178 rgsaddle_irc_create(&
config, nFree, x0.data(), masses.data(),
182 throw std::runtime_error(
"rgsaddle_irc_create failed");
184 return followBranch(session.get(), ctx, opts.max_steps, 3 * nFree);
186 IrcBranch forward = branch(RGSADDLE_IRC_FORWARD);
187 IrcBranch reverse = branch(RGSADDLE_IRC_REVERSE);
190 const bool forwardAlongMode =
192 saddle.pbc(forward.
positions.front() - saddle.getPositions())
196 path.
plus = forwardAlongMode ? std::move(forward) : std::move(reverse);
197 path.
minus = forwardAlongMode ? std::move(reverse) : std::move(forward);
200 static_cast<void>(saddle);
201 static_cast<void>(mode);
202 static_cast<void>(params);
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
const saddle_search_options_t & saddle_search_options() const
const optimizer_options_t & optimizer_options() const
RAII resource manager for the ARTn C library with global synchronization.
IrcPath followIrc(const Matter &saddle, const AtomMatrix &mode, const Parameters ¶ms)
Follows the rgsaddle IRC from a converged saddle along both signs of its lowest mode.
bool ircAvailable()
True when this build can run followIrc: linked to rgsaddle at C ABI minor 14 or newer,...
Accepted points of one branch of the intrinsic reaction coordinate (IRC), in order away from the sadd...
std::vector< AtomMatrix > positions
Both branches of the IRC.
struct eonc::saddle_search_options_t::irc_t irc