57 if (
const char *e = std::getenv(
"RGPOT_BACKEND"))
59 if (
const char *e = std::getenv(
"RGPOT_NWCHEM_BASIS"))
61 if (
const char *e = std::getenv(
"RGPOT_NWCHEM_THEORY"))
63 if (
const char *e = std::getenv(
"RGPOT_NWCHEM_SCF_TYPE"))
65 if (
const char *e = std::getenv(
"RGPOT_PARAMS_PATH"))
69 std::string backend_lc = opt.
backend;
70 std::ranges::transform(backend_lc, backend_lc.begin(),
71 [](
unsigned char c) { return std::tolower(c); });
72 if (backend_lc.rfind(
"nwchem", 0) == 0) {
73 if (const char *e = std::getenv(
"NWCHEMC_LIBRARY"))
75 else if (const char *e = std::getenv(
"RGPOT_NWCHEMC_ENGINE"))
77 else if (const char *e = std::getenv(
"RGPOT_NWCHEM_ENGINE"))
79 }
else if (backend_lc.rfind(
"cpmd", 0) == 0) {
80 if (const char *e = std::getenv(
"CPMDC_LIBRARY"))
82 else if (const char *e = std::getenv(
"RGPOT_CPMDC_ENGINE"))
84 }
else if (backend_lc.rfind(
"meta", 0) == 0 || backend_lc ==
"mta") {
85 if (const char *e = std::getenv(
"RGPOT_METATOMIC_ENGINE"))
87 else if (const char *e = std::getenv(
"METATOMIC_ENGINE"))
89 if (const char *e = std::getenv(
"RGPOT_METATOMIC_MODEL"))
91 }
else if (backend_lc ==
"xtb" || backend_lc ==
"xtbpot" ||
92 backend_lc ==
"gfn" || backend_lc ==
"gfnxtb") {
93 if (
const char *e = std::getenv(
"RGPOT_XTB_ENGINE"))
95 else if (
const char *e = std::getenv(
"XTB_ENGINE"))
104 if ((backend_lc.rfind(
"meta", 0) == 0 || backend_lc ==
"mta") &&
107 if ((backend_lc.rfind(
"meta", 0) == 0 || backend_lc ==
"mta") &&
111 impl_ = std::make_unique<RGPotEngine>(opt);
116 <<
"RgpotPot: in-process rgpot backend=" <<
backend_
118 "libnwchemc/libcpmdc/librgpot_metatomic_engine/librgpot_xtb_engine)"
122 impl_->finalizeMpiAtExit();
123 impl_->armGroupedExit();
130 const bool grouped =
impl_ &&
impl_->calculatorWorld() > 1;
136 static_cast<void>(
impl_.release());
146enum : std::int64_t { kStop = 0, kSingle = 1, kBatch = 2, kDown = 3 };
156 std::int64_t hdr[4] = {kStop, 0, 0, 0};
157 impl_->broadcastFromDriver(hdr,
sizeof(hdr));
159 if (g_driver ==
this)
164 const int groups =
impl_->calculatorGroups();
165 use_.busy.assign(
static_cast<size_t>(groups), 0.0);
166 use_.systems.assign(
static_cast<size_t>(groups), 0.0);
167 for (
int g = 0; g < groups; g++) {
171 static_cast<void>(
impl_->shareResult(g, 1, tally, &unused,
true, error));
172 use_.busy[
static_cast<size_t>(g)] = tally[0];
173 use_.systems[
static_cast<size_t>(g)] = tally[1];
180 const bool grouped =
driver_ &&
impl_->calculatorWorld() > 1;
185 std::cout <<
"RgpotPot: " <<
use_.table() << std::flush;
186 }
catch (
const std::exception &ex) {
187 std::cerr <<
"RgpotPot: no calculator-group summary: " << ex.what()
192 impl_->shutdownModule();
195 if (
impl_->calculatorWorld() > 1)
196 impl_->armGroupedExit();
198 std::int64_t hdr[4] = {kDown, 0, 0, 0};
199 impl_->broadcastFromDriver(hdr,
sizeof(hdr));
205 if (g_driver || !
impl_ ||
impl_->calculatorWorld() <= 1)
214 g_driver->stopAndDrop();
221bool try_force(
const RGPotEngine &engine,
long N,
const double *R,
222 const int *atomicNrs,
double *F,
double *U,
const double *box,
223 std::string &error) {
225 engine.
force(N, R, atomicNrs, F, U, box);
227 }
catch (
const std::exception &ex) {
238constexpr std::int64_t kSingleKey = -1;
239std::int64_t orbital_key(
const std::int64_t *owners,
long j) {
240 if (owners && owners[j] >= 0)
242 return -2 -
static_cast<std::int64_t
>(j);
245[[noreturn]]
void raise_failure(
long system,
int owner,
246 const std::string &error) {
247 std::string msg =
"RGPOT: calculator " + std::to_string(owner) +
248 " failed on system " + std::to_string(system);
251 throw std::runtime_error(msg);
256 double *F,
double *U,
const double *box,
257 std::string &error) {
258 const auto t0 = std::chrono::steady_clock::now();
259 const bool ok = try_force(*
impl_, N, R, atomicNrs, F, U, box, error);
260 busy_ += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
267 double *F,
double *U,
const double *box,
274 if (
impl_->calculatorIndex() == group) {
275 impl_->selectOrbitals(kSingleKey);
276 ok =
evaluate(N, R, atomicNrs, F, U, box, error);
278 if (!
impl_->shareResult(group, N, F, U, ok, error))
279 raise_failure(0, group, error);
283 const double *
const *positions,
284 const int *
const *atomicNrs,
double *
const *forces,
285 double *energies,
const double *
const *boxes,
286 const std::int64_t *owners,
287 const std::int64_t *route) {
288 const int mine =
impl_->calculatorIndex();
289 auto ownerOf = [&](
long j) {
return static_cast<int>(route[j]); };
290 if (
impl_->calculatorWorld() <= 1) {
291 for (
long j = 0; j < nSystems; j++) {
292 impl_->selectOrbitals(orbital_key(owners, j));
293 impl_->force(nAtoms, positions[j], atomicNrs[j], forces[j], &energies[j],
298 std::vector<char> ok(
static_cast<size_t>(nSystems), 1);
299 std::vector<std::string> errors(
static_cast<size_t>(nSystems));
300 for (
long j = 0; j < nSystems; j++) {
301 if (ownerOf(j) == mine) {
302 impl_->selectOrbitals(orbital_key(owners, j));
303 ok[
static_cast<size_t>(j)] =
304 evaluate(nAtoms, positions[j], atomicNrs[j], forces[j], &energies[j],
305 boxes[j], errors[
static_cast<size_t>(j)]);
310 for (
long j = 0; j < nSystems; j++) {
311 const int owner = ownerOf(j);
312 if (!
impl_->shareResult(owner, nAtoms, forces[j], &energies[j],
313 ok[
static_cast<size_t>(j)] != 0,
314 errors[
static_cast<size_t>(j)]) &&
319 raise_failure(failed, ownerOf(failed), errors[
static_cast<size_t>(failed)]);
324 std::int64_t hdr[4] = {kStop, 0, 0, 0};
325 impl_->broadcastFromDriver(hdr,
sizeof(hdr));
326 if (hdr[0] == kStop) {
331 }
catch (
const std::exception &) {
334 impl_->shutdownModule();
337 std::int64_t ack[4] = {kDown, 0, 0, 0};
338 impl_->broadcastFromDriver(ack,
sizeof(ack));
339 impl_->armGroupedExit();
342 const long n =
static_cast<long>(hdr[1]);
343 const long m = hdr[0] == kBatch ?
static_cast<long>(hdr[2]) : 1;
344 std::vector<double> R(
static_cast<size_t>(3 * n * m));
345 std::vector<int> Z(
static_cast<size_t>(n * m));
346 std::vector<double> box(
static_cast<size_t>(9 * m));
347 impl_->broadcastFromDriver(R.data(), R.size() *
sizeof(
double));
348 impl_->broadcastFromDriver(Z.data(), Z.size() *
sizeof(
int));
349 impl_->broadcastFromDriver(box.data(), box.size() *
sizeof(
double));
350 std::vector<std::int64_t> ids(
static_cast<size_t>(m), -1);
351 std::vector<std::int64_t> route(
static_cast<size_t>(m), 0);
352 if (hdr[0] == kBatch) {
353 impl_->broadcastFromDriver(ids.data(), ids.size() *
sizeof(std::int64_t));
354 impl_->broadcastFromDriver(route.data(),
355 route.size() *
sizeof(std::int64_t));
357 std::vector<double> F(R.size()), U(
static_cast<size_t>(m));
360 if (hdr[0] == kSingle) {
362 computeSingle(n, R.data(), Z.data(), F.data(), U.data(), box.data(),
363 static_cast<int>(hdr[3]));
364 }
catch (
const std::runtime_error &) {
368 std::vector<const double *> pos(
static_cast<size_t>(m)), bx(pos.size());
369 std::vector<const int *> nrs(pos.size());
370 std::vector<double *> frc(pos.size());
371 for (
long j = 0; j < m; j++) {
372 pos[
static_cast<size_t>(j)] = R.data() + 3 * n * j;
373 nrs[
static_cast<size_t>(j)] = Z.data() + n * j;
374 frc[
static_cast<size_t>(j)] = F.data() + 3 * n * j;
375 bx[
static_cast<size_t>(j)] = box.data() + 9 * j;
378 computeBatch(m, n, pos.data(), nrs.data(), frc.data(), U.data(),
379 bx.data(), ids.data(), route.data());
380 }
catch (
const std::runtime_error &) {
386 double *U,
double *variance,
const double *box) {
389 if (
impl_->calculatorWorld() <= 1) {
390 impl_->selectOrbitals(kSingleKey);
391 impl_->force(N, R, atomicNrs, F, U, box);
394 const auto t0 = std::chrono::steady_clock::now();
397 std::make_unique<eonc::GroupSchedule>(
impl_->calculatorGroups());
399 const int group =
schedule_->nearest(R, 3 * N, box);
401 schedule_->record(kSingleKey, R, 3 * N, box);
402 std::int64_t hdr[4] = {kSingle, N, 1, group};
403 impl_->broadcastFromDriver(hdr,
sizeof(hdr));
404 impl_->broadcastFromDriver(
const_cast<double *
>(R), 3 * N *
sizeof(
double));
405 impl_->broadcastFromDriver(
const_cast<int *
>(atomicNrs), N *
sizeof(
int));
406 impl_->broadcastFromDriver(
const_cast<double *
>(box), 9 *
sizeof(
double));
412 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
415 use_.singleWall += dt;
424 (
impl_->calculatorGroups() > 1 ||
impl_->keepsOrbitalsPerKey());
428 const double *
const *positions,
429 const int *
const *atomicNrs,
double *
const *forces,
430 double *energies,
double *variances,
431 const double *
const *boxes) {
432 forceBatchOwned(nSystems, nAtoms, positions, atomicNrs, forces, energies,
433 variances, boxes,
nullptr);
437 const double *
const *positions,
438 const int *
const *atomicNrs,
439 double *
const *forces,
double *energies,
440 double *variances,
const double *
const *boxes,
441 const long *owners) {
442 std::vector<std::int64_t> ids(
static_cast<size_t>(nSystems), -1);
444 for (
long j = 0; j < nSystems; j++)
445 ids[
static_cast<size_t>(j)] = owners[j];
447 const bool grouped =
impl_->calculatorWorld() > 1;
448 const auto t0 = std::chrono::steady_clock::now();
449 std::vector<std::int64_t> route(
static_cast<size_t>(nSystems), 0);
453 std::make_unique<eonc::GroupSchedule>(
impl_->calculatorGroups());
454 std::vector<std::int64_t> keys(
static_cast<size_t>(nSystems));
455 for (
long j = 0; j < nSystems; j++)
456 keys[
static_cast<size_t>(j)] = orbital_key(ids.data(), j);
457 const std::vector<int> groupOf =
schedule_->assign(keys);
458 for (
long j = 0; j < nSystems; j++) {
459 route[
static_cast<size_t>(j)] = groupOf[
static_cast<size_t>(j)];
460 schedule_->record(keys[
static_cast<size_t>(j)], positions[j], 3 * nAtoms,
463 std::int64_t hdr[4] = {kBatch, nAtoms, nSystems, 0};
464 impl_->broadcastFromDriver(hdr,
sizeof(hdr));
465 std::vector<double> R(
static_cast<size_t>(3 * nAtoms * nSystems));
466 std::vector<int> Z(
static_cast<size_t>(nAtoms * nSystems));
467 std::vector<double> box(
static_cast<size_t>(9 * nSystems));
468 for (
long j = 0; j < nSystems; j++) {
469 std::copy(positions[j], positions[j] + 3 * nAtoms,
470 R.begin() + 3 * nAtoms * j);
471 std::copy(atomicNrs[j], atomicNrs[j] + nAtoms, Z.begin() + nAtoms * j);
472 std::copy(boxes[j], boxes[j] + 9, box.begin() + 9 * j);
474 impl_->broadcastFromDriver(R.data(), R.size() *
sizeof(
double));
475 impl_->broadcastFromDriver(Z.data(), Z.size() *
sizeof(
int));
476 impl_->broadcastFromDriver(box.data(), box.size() *
sizeof(
double));
477 impl_->broadcastFromDriver(ids.data(), ids.size() *
sizeof(std::int64_t));
478 impl_->broadcastFromDriver(route.data(),
479 route.size() *
sizeof(std::int64_t));
482 computeBatch(nSystems, nAtoms, positions, atomicNrs, forces, energies, boxes,
483 ids.data(), route.data());
486 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
490 for (
long j = 0; j < nSystems; j++) {
Opaque rgpot-backed engine (nwchemc / cpmdc / metatomic / uma / xtb).
static bool mpiAbortRequested() noexcept
True once rgpot asked for MPI_Abort at exit in this process.
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box) const
Potential backed by rgpot NWChemPot / CPMDPot (in-process dlopen of libnwchemc / libcpmdc).
RgpotPot(const eonc::Parameters &p)
bool engineAvailable() const
std::unique_ptr< eonc::GroupSchedule > schedule_
std::unique_ptr< RGPotEngine > impl_
void forceBatchOwned(long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, double *variances, const double *const *boxes, const long *owners) override
owners[j] is system j's identity (a NEB image, a ring bead): it names the orbitals the engine starts ...
void forceBatch(long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, double *variances, const double *const *boxes) override
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) override
bool evaluate(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box, std::string &error)
void computeBatch(long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, const double *const *boxes, const std::int64_t *owners, const std::int64_t *route)
bool supportsBatchEvaluation() const noexcept override
With cpmdc calculator groups ([RgpotPot] ranks_per_image), a batch is spread over the groups by eonc:...
eonc::CalculatorGroupUse use_
void releaseWorkersAtExit()
void computeSingle(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box, int group)
const metatomic_options_t & metatomic_options() const
const xtb_options_t & xtb_options() const
const rgpot_options_t & rgpot_options() const
void recordForceCall() noexcept
Charge one evaluated geometry to this potential and its owning registry.
Potential(PotType a_ptype)
Production default: construction-scope registry, else PotRegistry::get().
RAII resource manager for the ARTn C library with global synchronization.
std::string extensions_directory
std::string engine_library
double xtb_electronic_temperature
std::string permanent_dir
bool torch_determinism_strict
double uncertainty_threshold