Loading...
Searching...
No Matches
XtsciIrc.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "eon/XtsciIrc.h"
13
14#include <stdexcept>
15
16#ifdef WITH_RGSADDLE
17#include <rgsaddle.h>
18#if RGSADDLE_ABI_MINOR >= 14
19#define EONC_RGSADDLE_IRC 1
20#endif
21#endif
22
23#ifdef EONC_RGSADDLE_IRC
24#include <algorithm>
25#include <deque>
26#include <memory>
27#include <string>
28#endif
29
30namespace eonc {
31
32#ifdef EONC_RGSADDLE_IRC
33namespace {
34
35// rgsaddle evaluates the accepted point of a step again when the next step
36// starts, and a rejected trial leaves that point a few ulps away.
37constexpr double kSamePoint = 1.0e-12;
38
39struct Evaluation {
40 Eigen::VectorXd x;
41 double energy;
42 Eigen::VectorXd gradient;
43};
44
45struct IrcUser {
46 Matter *probe;
47 std::deque<Evaluation> seen;
48};
49
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) {
54 return e;
55 }
56 }
57 ctx.probe->setPositionsFreeV(at);
58 Evaluation e{at, ctx.probe->getPotentialEnergy(),
59 -ctx.probe->getForcesFreeV()};
60 if (ctx.seen.size() >= 4) {
61 ctx.seen.pop_front();
62 }
63 ctx.seen.push_back(std::move(e));
64 return ctx.seen.back();
65}
66
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 ||
71 req->n_images != 1) {
72 return RGSADDLE_SURFACE_FAILED;
73 }
74 if (req->n_atoms != ctx->probe->numberOfFreeAtoms()) {
75 return RGSADDLE_SHAPE;
76 }
77 try {
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);
81 return RGSADDLE_OK;
82 } catch (...) {
83 return RGSADDLE_SURFACE_FAILED;
84 }
85}
86
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)));
92 }
93}
94
95IrcBranch followBranch(RgsaddleIrc *session, IrcUser &ctx, long maxSteps,
96 Eigen::Index n) {
97 IrcBranch branch;
98 Eigen::VectorXd x(n);
99 Eigen::VectorXd last;
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");
106 ++branch.steps;
107 // rgsaddle reports a minimum only after the first step and at a largest
108 // atomic force at or below config.force_tol, which followIrc sets.
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)
114 .rowwise()
115 .norm()
116 .maxCoeff());
117 ctx.probe->setPositionsFreeV(x);
118 branch.positions.push_back(ctx.probe->getPositions());
119 last = x;
120 }
121 }
122 return branch;
123}
124
125} // namespace
126#endif
127
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;
133#else
134 return false;
135#endif
136}
137
138IrcPath followIrc(const Matter &saddle, const AtomMatrix &mode,
139 const Parameters &params) {
140 if (!ircAvailable()) {
141 throw std::runtime_error("rgsaddle IRC is not available in this build");
142 }
143#ifdef EONC_RGSADDLE_IRC
144 const auto &opts = params.saddle_search_options().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));
150 rows.push_back(i);
151 }
152 }
153 if (rows.empty() || mode.rows() != saddle.numberOfAtoms()) {
154 throw std::runtime_error("IRC needs free atoms and one mode row per atom");
155 }
156 const auto nFree = static_cast<Eigen::Index>(rows.size());
157 AtomMatrix freeMode(nFree, 3);
158 for (Eigen::Index j = 0; j < nFree; ++j) {
159 freeMode.row(j) = mode.row(rows[static_cast<size_t>(j)]);
160 }
161 const Eigen::VectorXd x0 = saddle.getPositionsFreeV();
162
163 Matter probe(saddle);
164 IrcUser ctx{&probe, {}};
165 // One session per branch: a GS2 session halves dx on every rejected trial
166 // and keeps the shorter step through rgsaddle_irc_set_direction.
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;
173 config.max_move = params.optimizer_options().max_move;
174 config.max_inner = 10;
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(),
179 freeMode.data()),
180 &rgsaddle_irc_free);
181 if (!session) {
182 throw std::runtime_error("rgsaddle_irc_create failed");
183 }
184 return followBranch(session.get(), ctx, opts.max_steps, 3 * nFree);
185 };
186 IrcBranch forward = branch(RGSADDLE_IRC_FORWARD);
187 IrcBranch reverse = branch(RGSADDLE_IRC_REVERSE);
188
189 // The forward kick takes its sign from rgsaddle.
190 const bool forwardAlongMode =
191 forward.positions.empty() ||
192 saddle.pbc(forward.positions.front() - saddle.getPositions())
193 .cwiseProduct(mode)
194 .sum() > 0.0;
195 IrcPath path;
196 path.plus = forwardAlongMode ? std::move(forward) : std::move(reverse);
197 path.minus = forwardAlongMode ? std::move(reverse) : std::move(forward);
198 return path;
199#else
200 static_cast<void>(saddle);
201 static_cast<void>(mode);
202 static_cast<void>(params);
203 return {};
204#endif
205}
206
207} // namespace eonc
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
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 &params)
Follows the rgsaddle IRC from a converged saddle along both signs of its lowest mode.
Definition XtsciIrc.cpp:138
bool ircAvailable()
True when this build can run followIrc: linked to rgsaddle at C ABI minor 14 or newer,...
Definition XtsciIrc.cpp:128
Accepted points of one branch of the intrinsic reaction coordinate (IRC), in order away from the sadd...
Definition XtsciIrc.h:23
std::vector< AtomMatrix > positions
Definition XtsciIrc.h:24
Both branches of the IRC.
Definition XtsciIrc.h:35
IrcBranch minus
Definition XtsciIrc.h:36
IrcBranch plus
Definition XtsciIrc.h:37
struct eonc::saddle_search_options_t::irc_t irc