Loading...
Searching...
No Matches
RgsaddleClient.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "RgsaddleClient.h"
6
7#include <string>
8#include <vector>
9
13
14namespace gpr {
15namespace neb {
16
18{
19 rgsaddle_version_t ver{};
20 if (rgsaddle_abi_stamp(&ver) == RGSADDLE_OK) {
21 major_ = ver.major;
22 minor_ = ver.minor;
23 }
24}
25
26bool RgsaddleClient::load(const std::string&)
27{
28 rgsaddle_version_t ver{};
29 if (rgsaddle_abi_stamp(&ver) != RGSADDLE_OK) {
30 message_ = "rgsaddle_abi_stamp failed";
31 return false;
32 }
33 major_ = ver.major;
34 minor_ = ver.minor;
35 if (ver.major != RGSADDLE_ABI_MAJOR || ver.minor < kMinimumAbiMinor) {
36 message_ = "rgsaddle requires ABI major 1 and minor 71 or later";
37 return false;
38 }
39 message_.clear();
40 return true;
41}
42
43int RgsaddleClient::idppFixedPath(int64_t n_images, int64_t n_atoms,
44 const double* reactant, const double* product,
45 const double* cell, const uint8_t* mask,
46 int64_t max_iterations, double grad_tol,
47 double max_move, double* out)
48{
49 return rgsaddle_idpp_fixed_path(n_images, n_atoms, reactant, product, cell,
50 mask, max_iterations, grad_tol, max_move,
51 out);
52}
53
55 bool inverse_distance, int64_t n_images, int64_t n_atoms,
56 const double* reactant, const double* product, const double* cell,
57 const uint8_t* mask, double alpha, int64_t short_iterations,
58 int64_t final_iterations, double force_tol, double max_move, double spring,
59 double* out)
60{
61 if (inverse_distance) {
62 return rgsaddle_sidpp_alternating_inverse_distance_path(
63 n_images, n_atoms, reactant, product, cell, mask, alpha,
64 short_iterations, final_iterations, force_tol, max_move, spring,
65 out);
66 }
67 return rgsaddle_sidpp_alternating_path(
68 n_images, n_atoms, reactant, product, cell, mask, alpha,
69 short_iterations, final_iterations, force_tol, max_move, spring, out);
70}
71
72int RgsaddleClient::kappaDimerForce(const rgsaddle_kappa_config_t* config,
73 int64_t dof, const double* gradient,
74 const double* mode, int64_t n_excluded,
75 const double* excluded,
76 rgsaddle_hessian_fn hessian, void* user,
77 double* force,
78 rgsaddle_kappa_report_t* report)
79{
80 return rgsaddle_kappa_dimer_force(config, dof, gradient, mode, n_excluded,
81 excluded, hessian, user, force, report);
82}
83
84int RgsaddleClient::hessianBofill(int64_t n, const double* step,
85 const double* dgradient, double* hessian)
86{
87 return rgsaddle_hessian_bofill(n, step, dgradient, hessian);
88}
89
91 const rgsaddle_band_config_t* cfg, int64_t n_images, int64_t n_atoms,
92 const double* positions)
93{
94 if (cfg == nullptr) return nullptr;
95 return rgsaddle_band_create(cfg, n_images, n_atoms, positions);
96}
97
98int RgsaddleClient::bandStep(Band band, rgsaddle_surface_fn surface, void* user,
99 rgsaddle_report_t* out)
100{
101 if (band == nullptr) return -1;
102 return rgsaddle_band_step(band, surface, user, out);
103}
104
106{
107 if (band == nullptr) return -1;
108 return rgsaddle_band_positions(band, out);
109}
110
111int RgsaddleClient::bandSetPositions(Band band, const double* positions)
112{
113 if (band == nullptr) return -1;
114 return rgsaddle_band_set_positions(band, positions);
115}
116
118{
119 if (band == nullptr) return -1;
120 return rgsaddle_band_reset(band);
121}
122
123int RgsaddleClient::bandSetForceGate(Band band, rgsaddle_force_gate_t gate)
124{
125 if (band == nullptr) return RGSADDLE_NULL_BAND;
126 return rgsaddle_band_set_force_gate(band, gate);
127}
128
129int RgsaddleClient::bandSetExpPrecon(Band band, double a, double c_stab,
130 double mu, double r_cut_factor)
131{
132 if (band == nullptr) return RGSADDLE_NULL_BAND;
133 return rgsaddle_band_set_exp_precon(band, a, c_stab, mu, r_cut_factor);
134}
135
137{
138 if (band != nullptr) rgsaddle_band_free(band);
139}
140
142 const rgsaddle_sella_min_config_t* cfg, int64_t n_atoms,
143 const double* position, const double* masses)
144{
145 if (cfg == nullptr) return nullptr;
146 return rgsaddle_sella_min_create(cfg, n_atoms, position, masses);
147}
148
149int RgsaddleClient::sellaMinStep(SellaMin sess, rgsaddle_surface_fn surface,
150 void* user, rgsaddle_report_t* out)
151{
152 if (sess == nullptr) return -1;
153 return rgsaddle_sella_min_step(sess, surface, user, out);
154}
155
157{
158 if (sess == nullptr) return -1;
159 return rgsaddle_sella_min_position(sess, out);
160}
161
163{
164 if (sess != nullptr) rgsaddle_sella_min_free(sess);
165}
166
168 const rgsaddle_sella_saddle_config_t* cfg, int64_t n_atoms,
169 const double* position, const double* masses)
170{
171 if (cfg == nullptr) return nullptr;
172 auto* session =
173 rgsaddle_sella_saddle_create(cfg, n_atoms, position, masses);
174 if (session != nullptr) order_one_kind_ = 1;
175 return session;
176}
177
179 rgsaddle_surface_fn surface, void* user,
180 rgsaddle_report_t* out)
181{
182 if (sess == nullptr) return -1;
183 return rgsaddle_sella_saddle_step(sess, surface, user, out);
184}
185
187{
188 if (sess == nullptr) return -1;
189 return rgsaddle_sella_saddle_position(sess, out);
190}
191
193{
194 if (sess != nullptr) rgsaddle_sella_saddle_free(sess);
195}
196
198 const rgsaddle_index1_config_t* cfg, int64_t n_atoms,
199 const double* position, const double* hessian, const double* masses)
200{
201 if (cfg == nullptr) return nullptr;
202 auto* session =
203 rgsaddle_index1_create(cfg, n_atoms, position, hessian, masses);
204 if (session != nullptr) order_one_kind_ = 2;
205 return session;
206}
207
208int RgsaddleClient::index1Step(Index1 sess, rgsaddle_surface_fn surface,
209 void* user, rgsaddle_report_t* out)
210{
211 if (sess == nullptr) return -1;
212 return rgsaddle_index1_step(sess, surface, user, out);
213}
214
216{
217 if (sess == nullptr) return -1;
218 return rgsaddle_index1_position(sess, out);
219}
220
222{
223 if (sess != nullptr) rgsaddle_index1_free(sess);
224}
225
227 const rgsaddle_minmode_config_t* cfg, int64_t n_atoms,
228 const double* position, const double* mode)
229{
230 if (cfg == nullptr) return nullptr;
231 return rgsaddle_minmode_create(cfg, n_atoms, position, mode);
232}
233
234int RgsaddleClient::minModeStep(MinMode sess, rgsaddle_surface_fn surface,
235 void* user, rgsaddle_report_t* out)
236{
237 if (sess == nullptr) return -1;
238 return rgsaddle_minmode_step(sess, surface, user, out);
239}
240
242{
243 if (sess == nullptr) return -1;
244 return rgsaddle_minmode_position(sess, out);
245}
246
248 rgsaddle_force_gate_t gate)
249{
250 if (sess == nullptr) return RGSADDLE_NULL_SESSION;
251 return rgsaddle_minmode_set_force_gate(sess, gate);
252}
253
255{
256 if (sess != nullptr) rgsaddle_minmode_free(sess);
257}
258
259RgsaddleClient::Irc RgsaddleClient::ircCreate(const rgsaddle_irc_config_t* cfg,
260 int64_t n_atoms,
261 const double* saddle,
262 const double* masses,
263 const double* mode)
264{
265 if (cfg == nullptr) return nullptr;
266 return rgsaddle_irc_create(cfg, n_atoms, saddle, masses, mode);
267}
268
270 const rgsaddle_irc_config_t* cfg, int64_t n_atoms, const double* saddle,
271 const double* masses, const double* seed, rgsaddle_surface_fn surface,
272 void* user)
273{
274 if (cfg == nullptr) return nullptr;
275 return rgsaddle_irc_create_from_surface(cfg, n_atoms, saddle, masses, seed,
276 surface, user);
277}
278
279int RgsaddleClient::ircStep(Irc session, rgsaddle_surface_fn surface,
280 void* user, rgsaddle_report_t* out)
281{
282 if (session == nullptr) return RGSADDLE_NULL_SESSION;
283 return rgsaddle_irc_step(session, surface, user, out);
284}
285
286int RgsaddleClient::ircPosition(const Irc session, double* out)
287{
288 if (session == nullptr) return RGSADDLE_NULL_SESSION;
289 return rgsaddle_irc_position(session, out);
290}
291
292int RgsaddleClient::ircSetDirection(Irc session, int32_t direction)
293{
294 if (session == nullptr) return RGSADDLE_NULL_SESSION;
295 return rgsaddle_irc_set_direction(session, direction);
296}
297
299{
300 if (session == nullptr) return RGSADDLE_NULL_SESSION;
301 return rgsaddle_irc_reset(session);
302}
303
305{
306 if (session != nullptr) rgsaddle_irc_free(session);
307}
308
309int RgsaddleClient::ircSetLengths(Irc session, double kick, double max_step,
310 double min_step, int adaptive)
311{
312 if (session == nullptr) return RGSADDLE_NULL_SESSION;
313 return rgsaddle_irc_set_lengths(session, kick, max_step, min_step,
314 adaptive);
315}
316
317rgsaddle_status_t activeGpRgsaddleSurface(void* user,
318 rgsaddle_surface_request_t* req)
319{
320 if (user == nullptr || req == nullptr || req->positions == nullptr ||
321 req->energies == nullptr || req->gradients == nullptr ||
322 req->n_images <= 0 || req->n_atoms <= 0) {
323 return RGSADDLE_NULL_SESSION;
324 }
325 if (req->version.major != RGSADDLE_ABI_MAJOR) return RGSADDLE_NULL_SESSION;
326 auto* ctx = static_cast<ActiveGpRgsaddleUser*>(user);
327 if (ctx->gp == nullptr || ctx->atomic_nrs == nullptr)
328 return RGSADDLE_NULL_SESSION;
329 const long n_images = static_cast<long>(req->n_images);
330 const long n_atoms = static_cast<long>(req->n_atoms);
331 const long dof = 3 * n_atoms;
332 std::vector<double> forces(static_cast<std::size_t>(dof));
333 for (long im = 0; im < n_images; ++im) {
334 const double* p = req->positions + im * dof;
335 double* g = req->gradients + im * dof;
336 double e = 0.0;
337 double var = 0.0;
338 const int rc = ctx->gp->force(n_atoms, p, ctx->atomic_nrs,
339 forces.data(), &e, &var, ctx->box);
340 if (rc != 0) return RGSADDLE_SURFACE_FAILED;
341 req->energies[im] = e;
342 for (long k = 0; k < dof; ++k)
343 g[k] = -forces[static_cast<std::size_t>(k)];
344 }
345 return RGSADDLE_OK;
346}
347
348rgsaddle_status_t gprRgsaddleSurface(void* user,
349 rgsaddle_surface_request_t* req)
350{
351 if (user == nullptr || req == nullptr || req->positions == nullptr ||
352 req->energies == nullptr || req->gradients == nullptr ||
353 req->n_images <= 0 || req->n_atoms <= 0) {
354 return RGSADDLE_NULL_SESSION;
355 }
356 if (req->version.major != RGSADDLE_ABI_MAJOR) return RGSADDLE_NULL_SESSION;
357 auto* ctx = static_cast<GprRgsaddleUser*>(user);
358 if (ctx->gp == nullptr) return RGSADDLE_NULL_SESSION;
359 const long n_images = static_cast<long>(req->n_images);
360 const long n_atoms = static_cast<long>(req->n_atoms);
361 const long dof = 3 * n_atoms;
362 for (long im = 0; im < n_images; ++im) {
363 const double* p = req->positions + im * dof;
364 Observation probe;
365 probe.R.resize(1, dof);
366 for (long k = 0; k < dof; ++k)
367 probe.R(0, k) = p[k];
368 ctx->gp->calculatePotentialDispatched(probe);
369 if (probe.E.size() < 1 || probe.G.cols() < dof)
370 return RGSADDLE_NULL_SESSION;
371 req->energies[im] = probe.E(0, 0);
372 double* g = req->gradients + im * dof;
373 for (long k = 0; k < dof; ++k)
374 g[k] = probe.G(0, k);
375 }
376 return RGSADDLE_OK;
377}
378
379} // namespace neb
380
381namespace {
382
383std::string& bofillLibraryOverride()
384{
385 static std::string path;
386 return path;
387}
388
389// rgsaddle_hessian_bofill shipped when the header's ABI minor became 3.
390// The pinned rgsaddle.h still declares that signature.
391constexpr std::uint32_t kHessianBofillAbiMinor = 3;
392
393std::string& bofillAbiRefusalSlot()
394{
395 static std::string text;
396 return text;
397}
398
399std::string formatBofillAbiRefusal(int stamp, const rgsaddle_version_t& ver)
400{
401 std::string text = "rgsaddle_hessian_bofill requires rgsaddle ABI major " +
402 std::to_string(RGSADDLE_ABI_MAJOR) + " and minor " +
403 std::to_string(kHessianBofillAbiMinor) + "; found ";
404 if (stamp != RGSADDLE_OK) {
405 text += "rgsaddle_abi_stamp status " + std::to_string(stamp);
406 return text;
407 }
408 text += "major " + std::to_string(ver.major) + " and minor " +
409 std::to_string(ver.minor);
410 return text;
411}
412
413} // namespace
414
416{
417 if (!bofillAbiRefusalSlot().empty()) return bofillAbiRefusalSlot();
418 rgsaddle_version_t ver{};
419 const int stamp = rgsaddle_abi_stamp(&ver);
420 return formatBofillAbiRefusal(stamp, ver);
421}
422
424{
425 bofillLibraryOverride() = path == nullptr ? std::string() : path;
426}
427
428int optim::bofillThroughLoadedRgsaddle(std::int64_t n, double* hessian,
429 const double* step,
430 const double* gradient_change)
431{
432 if (n < 1 || hessian == nullptr || step == nullptr ||
433 gradient_change == nullptr)
434 return 0;
435 // The recorded path is the index-test choice. The Hessian update is
436 // the linked symbol, present from ABI minor 3.
437 (void)bofillLibraryOverride();
438 rgsaddle_version_t ver{};
439 const int stamp = rgsaddle_abi_stamp(&ver);
440 if (stamp != RGSADDLE_OK || ver.major != RGSADDLE_ABI_MAJOR ||
441 ver.minor < kHessianBofillAbiMinor) {
442 bofillAbiRefusalSlot() = formatBofillAbiRefusal(stamp, ver);
443 return -1;
444 }
445 bofillAbiRefusalSlot().clear();
446 const int status =
447 rgsaddle_hessian_bofill(n, step, gradient_change, hessian);
448 return status == RGSADDLE_OK ? 1 : 0;
449}
450
451} // namespace gpr
void sellaSaddleFree(SellaSaddle sess)
Index1 index1Create(const rgsaddle_index1_config_t *cfg, int64_t n_atoms, const double *position, const double *hessian, const double *masses)
Band bandCreate(const rgsaddle_band_config_t *cfg, int64_t n_images, int64_t n_atoms, const double *positions)
int sellaSaddleStep(SellaSaddle sess, rgsaddle_surface_fn surface, void *user, rgsaddle_report_t *out)
RgsaddleMinMode * MinMode
RgsaddleSellaMin * SellaMin
int ircStep(Irc session, rgsaddle_surface_fn surface, void *user, rgsaddle_report_t *out)
RgsaddleSellaSaddle * SellaSaddle
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)
int minModeStep(MinMode sess, rgsaddle_surface_fn surface, void *user, rgsaddle_report_t *out)
int bandStep(Band band, rgsaddle_surface_fn surface, void *user, rgsaddle_report_t *out)
int idppFixedPath(int64_t n_images, int64_t n_atoms, const double *reactant, const double *product, const double *cell, const uint8_t *mask, int64_t max_iterations, double grad_tol, double max_move, double *out)
int index1Step(Index1 sess, rgsaddle_surface_fn surface, void *user, rgsaddle_report_t *out)
int hessianBofill(int64_t n, const double *step, const double *dgradient, double *hessian)
Row-major n by n Hessian, updated only when the runtime accepts.
Irc ircCreate(const rgsaddle_irc_config_t *cfg, int64_t n_atoms, const double *saddle, const double *masses, const double *mode)
int index1Position(Index1 sess, double *out)
int ircPosition(const Irc session, double *out)
int ircSetLengths(Irc session, double kick, double max_step, double min_step, int adaptive)
int bandPositions(Band band, double *out)
int bandSetForceGate(Band band, rgsaddle_force_gate_t gate)
SellaMin sellaMinCreate(const rgsaddle_sella_min_config_t *cfg, int64_t n_atoms, const double *position, const double *masses)
RgsaddleIndex1 * Index1
int minModeSetForceGate(MinMode sess, rgsaddle_force_gate_t gate)
MinMode minModeCreate(const rgsaddle_minmode_config_t *cfg, int64_t n_atoms, const double *position, const double *mode)
static constexpr uint32_t kMinimumAbiMinor
int bandSetPositions(Band band, const double *positions)
int sellaSaddlePosition(SellaSaddle sess, double *out)
bool load(const std::string &path={})
int sidppAlternatingPath(bool inverse_distance, int64_t n_images, int64_t n_atoms, const double *reactant, const double *product, const double *cell, const uint8_t *mask, double alpha, int64_t short_iterations, int64_t final_iterations, double force_tol, double max_move, double spring, double *out)
rgsaddle_sidpp_alternating_path, or its inverse-distance variant.
int sellaMinStep(SellaMin sess, rgsaddle_surface_fn surface, void *user, rgsaddle_report_t *out)
void index1Free(Index1 sess)
void minModeFree(MinMode sess)
int minModePosition(MinMode sess, double *out)
int kappaDimerForce(const rgsaddle_kappa_config_t *config, int64_t dof, const double *gradient, const double *mode, int64_t n_excluded, const double *excluded, rgsaddle_hessian_fn hessian, void *user, double *force, rgsaddle_kappa_report_t *report)
int bandSetExpPrecon(Band band, double a, double c_stab, double mu, double r_cut_factor)
SellaSaddle sellaSaddleCreate(const rgsaddle_sella_saddle_config_t *cfg, int64_t n_atoms, const double *position, const double *masses)
void sellaMinFree(SellaMin sess)
int ircSetDirection(Irc session, int32_t direction)
int sellaMinPosition(SellaMin sess, double *out)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
eon_relax_band_t eon_relax_surface_fn surface
int status
eon_relax_band_t * band
eon_relax_band_t eon_relax_surface_fn void * user
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
rgsaddle_status_t gprRgsaddleSurface(void *user, rgsaddle_surface_request_t *req)
rgsaddle_status_t activeGpRgsaddleSurface(void *user, rgsaddle_surface_request_t *req)
std::string bofillRgsaddleAbiRefusal()
int bofillThroughLoadedRgsaddle(std::int64_t n, double *hessian_row_major, const double *step, const double *gradient_change)
void setBofillRgsaddleLibrary(const char *path)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Observation data: energy, gradient and positions.
FieldMatrixd E
GP-NEB GaussianProcessRegression as dest PointSurface.