29#include <capnp/message.h>
30#include <capnp/serialize.h>
35#include "rgpot/CPMDPot/CPMDPot.hpp"
36#include "rgpot/CalculatorGroup.hpp"
37#include "rgpot/NWChemPot/NWChemPot.hpp"
38#include "rgpot/rpc/Potentials.capnp.h"
40using rgpot::types::AtomMatrix;
46std::vector<::capnp::word> read_params_file(
const std::string &path) {
47 std::ifstream in(path, std::ios::binary | std::ios::ate);
49 throw std::runtime_error(
"RGPOT: cannot open params_path: " + path);
50 const std::streamsize bytes = in.tellg();
51 if (bytes <= 0 || (
static_cast<size_t>(bytes) %
sizeof(::capnp::word)) != 0)
52 throw std::runtime_error(
53 "RGPOT: params_path is not a capnp flat message: " + path);
54 std::vector<::capnp::word> words(
static_cast<size_t>(bytes) /
55 sizeof(::capnp::word));
57 in.read(
reinterpret_cast<char *
>(words.data()), bytes);
59 throw std::runtime_error(
"RGPOT: short read on params_path: " + path);
63std::string to_lower(std::string s) {
65 c =
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
69std::array<std::array<double, 3>, 3> box_from_row_major(
const double *box) {
70 std::array<std::array<double, 3>, 3> out{};
71 for (
int i = 0; i < 3; ++i)
72 for (
int j = 0; j < 3; ++j)
73 out[
static_cast<size_t>(i)][
static_cast<size_t>(j)] = box[i * 3 + j];
77bool looks_like_dft_xc(
const std::string &s) {
80 static const char *k[] = {
"b3lyp",
"blyp",
"pbe",
"pw91",
"bp86",
81 "hcth",
"ft97",
"hfexch",
"xperpbe",
nullptr};
82 for (
int i = 0; k[i]; ++i) {
83 if (s.size() >= std::char_traits<char>::length(k[i]) &&
84 s.compare(0, std::char_traits<char>::length(k[i]), k[i]) == 0)
91int xtb_method_from_paramset(
const std::string ¶mset) {
92 const std::string p = to_lower(paramset);
93 if (p ==
"gfnff" || p ==
"gfn-ff")
95 if (p ==
"gfn0xtb" || p ==
"gfn0" || p ==
"gfn0-xtb")
97 if (p ==
"gfn1xtb" || p ==
"gfn1" || p ==
"gfn1-xtb")
99 if (p ==
"gfn2xtb" || p ==
"gfn2" || p ==
"gfn2-xtb" || p.empty())
101 throw std::runtime_error(
102 "RGPOT(xtb): paramset must be GFNFF, GFN0xTB, GFN1xTB, or GFN2xTB "
109int mpi_world_hint() {
110 const char *s = std::getenv(
"OMPI_COMM_WORLD_SIZE");
111 if (s ==
nullptr || s[0] ==
'\0')
112 s = std::getenv(
"PMI_SIZE");
113 if (s ==
nullptr || s[0] ==
'\0')
118void pin_cpmd_library(
const std::string &path) {
122 const char *name = path.empty() ?
"libcpmdc.so" : path.c_str();
123 dlopen(name, RTLD_NOW | RTLD_NOLOAD | RTLD_GLOBAL | RTLD_NODELETE);
125 static_cast<void>(path);
134#if defined(__linux__)
136 std::vector<std::string> candidates;
137 if (
const char *e = std::getenv(
"EON_RGPOT_MPI_LIBRARY"); e && *e)
138 candidates.emplace_back(e);
140 if (dladdr(
reinterpret_cast<void *
>(&group_mpi), &info) != 0 &&
141 info.dli_fname !=
nullptr) {
142 std::string self(info.dli_fname);
143 const auto slash = self.rfind(
'/');
144 if (slash != std::string::npos)
145 candidates.push_back(self.substr(0, slash + 1) +
"librgpot_pot_mpi.so");
147 candidates.emplace_back(
"librgpot_pot_mpi.so");
148 for (
const auto &path : candidates) {
149 void *h = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
153 auto entry =
reinterpret_cast<Entry
>(dlsym(h,
"eon_rgpot_group_mpi_v1"));
168bool agree_construction(std::string &message) {
171 return message.empty();
172 std::array<char, 4096> shared{};
173 if (api->
agree(message.c_str(), shared.data(), shared.size()) != 0)
175 message.assign(shared.data());
182 const std::string &scfType,
int multiplicity,
183 const std::string &existing) {
184 if (!existing.empty()) {
187 const std::string theoryKey = to_lower(theory);
188 const std::string scfKey = to_lower(scfType);
189 if (theoryKey ==
"dft" && looks_like_dft_xc(scfKey)) {
190 return "dft\n xc " + scfType +
"\n mult " + std::to_string(multiplicity) +
193 if (looks_like_dft_xc(theoryKey)) {
194 return "dft\n xc " + theory +
"\n mult " + std::to_string(multiplicity) +
203 ::CPMDParams::Builder params = msg.initRoot<::CPMDParams>();
205 const auto words = read_params_file(opt.
params_path);
206 ::capnp::FlatArrayMessageReader reader(
207 kj::arrayPtr(words.data(), words.size()));
208 msg.setRoot(reader.getRoot<::CPMDParams>());
209 params = msg.getRoot<::CPMDParams>();
213 params.setCharge(opt.
charge);
215 if (!opt.
title.empty())
216 params.setTitle(opt.
title);
218 params.setMemoryMb(
static_cast<uint32_t
>(opt.
memory_mb));
233 if (
const char *env = std::getenv(
"RGPOT_CPMD_INPUT_BLOCK"))
236 if (!block.empty()) {
237 std::vector<std::string> kept;
239 auto existing = params.getInputBlocks();
240 kept.reserve(existing.size());
241 for (
auto text : existing)
242 kept.emplace_back(text.cStr());
244 auto blocks = params.initInputBlocks(kept.size() + 1);
245 for (std::size_t i = 0; i < kept.size(); ++i)
246 blocks.set(i, kept[i]);
247 blocks.set(kept.size(), block);
256 std::unique_ptr<rgpot::NWChemPot>
nwchem;
257 std::unique_ptr<rgpot::CPMDPot>
cpmd;
259 std::unique_ptr<GenericEngineLoader>
uma;
260 std::unique_ptr<XTBEngineLoader>
xtb;
275 ::capnp::MallocMessageBuilder msg;
276 auto params = msg.initRoot<::NWChemParams>();
277 params.setBasis(opt.
basis);
278 params.setTheory(opt.
theory);
280 params.setCharge(opt.
charge);
288 if (!opt.
title.empty())
289 params.setTitle(opt.
title);
291 params.setMemoryMb(
static_cast<uint32_t
>(opt.
memory_mb));
297 if (
const char *env = std::getenv(
"RGPOT_NWCHEM_INPUT_BLOCK"))
304 if (!block.empty()) {
305 auto blocks = params.initInputBlocks(1);
306 blocks.set(0, block);
308 impl_->nwchem = std::make_unique<rgpot::NWChemPot>(params.asReader());
309 if (!
impl_->nwchem->available())
310 throw std::runtime_error(
311 "RGPOT(nwchemc): engine not available (set NWCHEMC_LIBRARY / "
312 "RGPOT_NWCHEMC_ENGINE or [RgpotPot] engine_path)");
317 std::string local_error;
318 ::capnp::MallocMessageBuilder msg;
321 }
catch (
const std::exception &ex) {
322 local_error = ex.what();
326 if (mpi_world_hint() <= 1 && !local_error.empty())
327 throw std::runtime_error(local_error);
330 if (mpi_world_hint() > 1 && !agree_construction(local_error)) {
332 throw std::runtime_error(
335 "RGPOT(cpmdc): a rank failed before the calculator split")
338 if (!local_error.empty())
339 throw std::runtime_error(local_error);
340 if (::rgpot::calculatorsUseMpi()) {
347 const rgpot::CalculatorGroup g =
349 ::rgpot::finalizeMpiAtExit();
350 if (mpi_world_hint() > 1)
353 throw std::runtime_error(
354 "RGPOT(cpmdc): ranks_per_image=" +
356 " does not divide the MPI world into calculator groups");
357 impl_->groups = ::rgpot::calculatorCount();
358 impl_->group = g.index;
359 impl_->world = ::rgpot::calculatorWorldSize();
361 throw std::runtime_error(
362 "RGPOT(cpmdc): ranks_per_image needs rgpot built with MPI "
363 "(-Drgpot:with_mpi=enabled)");
366 impl_->cpmd = std::make_unique<rgpot::CPMDPot>(
367 msg.getRoot<::CPMDParams>().asReader());
370 if (!
impl_->cpmd->available())
371 throw std::runtime_error(
372 "RGPOT(cpmdc): engine not available (set CPMDC_LIBRARY / "
373 "RGPOT_CPMDC_ENGINE or [RgpotPot] engine_path)");
374 }
catch (
const std::exception &ex) {
375 local_error = ex.what();
379 if (mpi_world_hint() > 1 && !agree_construction(local_error))
380 throw std::runtime_error(
382 ? std::string(
"RGPOT(cpmdc): a rank failed to load the engine")
384 if (!local_error.empty())
385 throw std::runtime_error(local_error);
400 impl_->metatomic = std::make_unique<MetatomicEngineLoader>(mopt);
401 if (!
impl_->metatomic->available())
402 throw std::runtime_error(
403 "RGPOT(metatomic): engine not available (set RGPOT_METATOMIC_ENGINE "
404 "or [RgpotPot] engine_path to librgpot_metatomic_engine.so)");
409 gopt.
library =
"libuma_engine.so";
410 gopt.
env_var =
"RGPOT_UMA_ENGINE";
415 ::capnp::MallocMessageBuilder msg;
416 auto params = msg.initRoot<::UmaParams>();
419 params.setDevice(opt.
device);
420 params.setCharge(opt.
charge);
422 const auto words = ::capnp::messageToFlatArray(msg);
423 const auto bytes = words.asBytes();
424 gopt.
config.assign(bytes.begin(), bytes.end());
426 impl_->uma = std::make_unique<GenericEngineLoader>(gopt);
427 if (!
impl_->uma->available())
428 throw std::runtime_error(
429 "RGPOT(uma): engine not available (set RGPOT_UMA_ENGINE or "
430 "[RgpotPot] engine_path to libuma_engine.so)");
444 impl_->xtb = std::make_unique<XTBEngineLoader>(xopt);
445 if (!
impl_->xtb->available())
446 throw std::runtime_error(
447 "RGPOT(xtb): engine not available (set RGPOT_XTB_ENGINE or "
448 "[RgpotPot] engine_path to librgpot_xtb_engine.so)");
450 throw std::runtime_error(
451 "RGPOT: unknown backend '" + opt.
backend +
452 "' (expected nwchemc, cpmdc, metatomic, uma, or xtb)");
473 const rgpot::CalculatorGroup &g = ::rgpot::thisCalculator();
474 return g.index * g.ranks + g.rank_in_group;
479 ::rgpot::finalizeMpiAtExit();
483 return ::rgpot::mpiAbortRequested();
487#if defined(__linux__)
488 if (mpi_world_hint() <= 1)
491 if (api ==
nullptr || api->
hard_exit ==
nullptr)
498 static_cast<void>(
this);
505 impl_->module_down =
true;
508 reinterpret_cast<void (*)()
>(dlsym(RTLD_DEFAULT,
"cpmdc_finalize")))
510 pin_cpmd_library({});
515 if (!
impl_ ||
impl_->world <= 1 || bytes == 0)
518 if (::rgpot::shareFromCalculator(0, data, bytes) == 0)
519 throw std::runtime_error(
"RGPOT: could not broadcast from the driver rank");
523 std::string &error)
const {
525 static_cast<void>(error);
530 std::vector<double> buf(
static_cast<size_t>(3 * N + 2));
531 if (owner ==
impl_->group) {
532 buf[0] = ok ? 1.0 : 0.0;
534 std::copy(F, F + 3 * N, buf.begin() + 2);
536 if (::rgpot::shareFromCalculator(owner, buf.data(),
537 buf.size() *
sizeof(
double)) == 0)
538 throw std::runtime_error(
"RGPOT: could not share a calculator result");
540 std::copy(buf.begin() + 2, buf.end(), F);
543 std::array<char, 512> msg{};
544 if (owner ==
impl_->group && !error.empty()) {
545 const auto n = std::min(error.size(), msg.size() - 1);
546 std::memcpy(msg.data(), error.data(), n);
548 if (::rgpot::shareFromCalculator(owner, msg.data(), msg.size()) == 0)
549 throw std::runtime_error(
"RGPOT: could not share a calculator error");
551 error.assign(msg.data());
552 return buf[0] == 1.0;
559 return impl_->nwchem->available();
561 return impl_->cpmd->available();
563 return impl_->metatomic->available();
565 return impl_->uma->available();
567 return impl_->xtb->available();
574template <
class Pot>
void select_orbitals(Pot &pot, std::int64_t key) {
575 if constexpr (
requires { pot.selectOrbitals(key); })
576 pot.selectOrbitals(key);
579template <
class Pot>
bool keeps_orbitals_per_key(
const Pot &pot) {
580 if constexpr (
requires { pot.keepsOrbitalsPerKey(); })
581 return pot.keepsOrbitalsPerKey();
589 select_orbitals(*
impl_->cpmd, key);
594 keeps_orbitals_per_key(*
impl_->cpmd);
598 double *F,
double *U,
const double *box)
const {
604 if (
const char *fail = std::getenv(
"RGPOT_FORCE_FAIL")) {
606 throw std::runtime_error(fail);
611 double *F,
double *U,
const double *box)
const {
613 throw std::runtime_error(
"RGPotEngine::force called with N <= 0");
616 for (
long i = 0; i < N; ++i) {
617 const int ii =
static_cast<int>(i);
618 positions(ii, 0) = R[3 * i + 0];
619 positions(ii, 1) = R[3 * i + 1];
620 positions(ii, 2) = R[3 * i + 2];
622 std::vector<int> atmtypes(atomicNrs, atomicNrs + N);
623 const auto cell = box_from_row_major(box);
626 impl_->metatomic->force(N, R, atomicNrs, F, U,
nullptr, box);
632 const bool got_cell = box && box[0] > 0.0 && box[4] > 0.0 && box[8] > 0.0;
633 std::array<double, 9> cell_sub{};
634 const double *effective_box = box;
636 cell_sub[0] = cell_sub[4] = cell_sub[8] = 25.0;
637 effective_box = cell_sub.data();
639 impl_->uma->force(N, R, atomicNrs, F, U,
nullptr, effective_box);
643 impl_->xtb->force(N, R, atomicNrs, F, U,
nullptr, box);
648 std::tuple<double, AtomMatrix, double> result;
650 result = (*
impl_->nwchem)(positions, atmtypes, cell);
652 result = (*
impl_->cpmd)(positions, atmtypes, cell);
654 *U = std::get<0>(result);
655 const auto &forces = std::get<1>(result);
656 for (
long i = 0; i < N; ++i) {
657 const int ii =
static_cast<int>(i);
658 F[3 * i + 0] = forces(ii, 0);
659 F[3 * i + 1] = forces(ii, 1);
660 F[3 * i + 2] = forces(ii, 2);
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
std::string nwchemDftInputBlock(const std::string &theory, const std::string &scfType, int multiplicity, const std::string &existing)
NWChem input block for a DFT theory.
std::string nwchemDftInputBlock(const std::string &theory, const std::string &scfType, int multiplicity, const std::string &existing)
NWChem input block for a DFT theory.
#define EON_RGPOT_GROUP_MPI_VERSION
Function table of librgpot_pot_mpi, the MPI half of RgpotPot's calculator groups.
void selectOrbitals(std::int64_t key) const
Names the calculation the next force() belongs to (cpmdc: the engine keeps converged orbitals per key...
RGPotEngine(const RGPotEngineOptions &opt)
void broadcastFromDriver(void *data, std::size_t bytes) const
Collective on MPI_COMM_WORLD: every rank leaves with world rank 0's bytes.
static bool mpiAbortRequested() noexcept
True once rgpot asked for MPI_Abort at exit in this process.
bool keepsOrbitalsPerKey() const
True when the engine keeps converged orbitals per key.
void armGroupedExit() const
After MPI_Finalize, _Exit.
int calculatorGroups() const noexcept
Number of calculator groups the MPI world is split into (1 when ranks_per_image is off) and the group...
void forceEngine(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box) const
void shutdownModule() noexcept
Calls cpmdc_finalize while the engine is still mapped. MPI stays up.
int calculatorWorld() const noexcept
Ranks in the MPI world the groups were bound on (1 without MPI).
int calculatorIndex() const noexcept
int worldRank() const noexcept
This process's rank in that world (0 without MPI).
void finalizeMpiAtExit() const
Registers MPI_Finalize at exit (once per process) when the world has more than one rank.
std::unique_ptr< Impl > impl_
bool shareResult(int owner, long N, double *F, double *U, bool ok, std::string &error) const
Collective on MPI_COMM_WORLD: every rank leaves with the energy and the 3N forces computed by the fir...
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box) const
::CPMDParams::Builder fillCpmdParams(::capnp::MallocMessageBuilder &msg, const RGPotEngineOptions &opt)
int(* agree)(const char *local, char *shared, std::size_t cap)
Collective on MPI_COMM_WORLD; initialises MPI when needed.
void(* hard_exit)(int status, void *arg)
on_exit handler: MPI_Abort when rgpot asked for it, else MPI_Finalize, then _Exit.
std::string tag
Backend tag for error messages ("uma", ...).
std::string env_var
Optional env var naming an explicit .so path (checked first).
std::vector< unsigned char > config
Cap'n Proto flat-array message (the engine's params struct on the shared Potentials....
std::string engine_path
Optional explicit .so path from config.
std::string library
Library basename to search for, e.g. "libuma_engine.so".
std::string extensions_directory
std::string engine_library
double xtb_electronic_temperature
std::string permanent_dir
bool torch_determinism_strict
double uncertainty_threshold
std::unique_ptr< rgpot::NWChemPot > nwchem
std::unique_ptr< rgpot::CPMDPot > cpmd
std::unique_ptr< MetatomicEngineLoader > metatomic
std::unique_ptr< XTBEngineLoader > xtb
std::unique_ptr< GenericEngineLoader > uma
double electronic_temperature