28#if !defined(EONMPI) && !defined(IS_WINDOWS)
54 lammpsThr{p.potential_options().LAMMPSThreads},
58 mpiComm{
eonc::getMpiClientComm(p)}
64 static std::atomic<int> ids{0};
69#if !defined(EONMPI) && !defined(IS_WINDOWS)
70 if (!isolate_worker) {
81 static_cast<void>(isolate_worker);
88 std::unique_lock<std::mutex> lock(
maskMutex_, std::defer_lock);
92 if (nAtoms <= 0 || isFixed ==
nullptr) {
97 fixedMask_.assign(isFixed, isFixed + 3 * nAtoms);
102 std::vector<double> mask;
105 std::unique_lock<std::mutex> lock(
maskMutex_, std::defer_lock);
114 static constexpr const char *kUnfix[] = {
"unfix eon_fx",
"unfix eon_fy",
115 "unfix eon_fz",
"unfix eon_freeze"};
116 static constexpr const char *kUngroup[] = {
117 "group eon_fx delete",
"group eon_fy delete",
"group eon_fz delete",
118 "group eon_frozen delete"};
119 for (
const char *cmd : kUnfix) {
125 for (
const char *cmd : kUngroup) {
134 for (
long i = 0; i < N; ++i) {
135 for (
int ax = 0; ax < 3; ++ax) {
136 if (mask[
static_cast<size_t>(3 * i + ax)] >= 0.5) {
137 ids[ax] += std::format(
"{} ", i + 1);
141 static constexpr const char *kGroup[] = {
"eon_fx",
"eon_fy",
"eon_fz"};
142 static constexpr const char *kFix[] = {
143 "fix eon_fx eon_fx setforce 0.0 NULL NULL",
144 "fix eon_fy eon_fy setforce NULL 0.0 NULL",
145 "fix eon_fz eon_fz setforce NULL NULL 0.0"};
146 for (
int ax = 0; ax < 3; ++ax) {
147 if (ids[ax].empty()) {
151 (
"group " + std::string(kGroup[ax]) +
" id " + ids[ax]).c_str());
157#if !defined(EONMPI) && !defined(IS_WINDOWS)
170void rejectGeometry(
double *U,
double *F,
long N) {
180 for (
long i = 0; i < 3 * N; ++i) {
183 for (
long i = 0; i < N; ++i) {
184 F[3 * i] = (i % 2 == 0) ? 1.0 : -1.0;
190#if !defined(EONMPI) && !defined(IS_WINDOWS)
195bool readExact(
int fd,
void *buf,
size_t n) {
196 auto *p =
static_cast<char *
>(buf);
198 ssize_t r = read(fd, p, n);
200 if (r < 0 && errno == EINTR)
205 n -=
static_cast<size_t>(r);
209bool writeExact(
int fd,
const void *buf,
size_t n) {
210 const auto *p =
static_cast<const char *
>(buf);
212 ssize_t w = write(fd, p, n);
219 n -=
static_cast<size_t>(w);
231 if (pipe(reqPipe) != 0) {
232 throw std::runtime_error(
"LAMMPSPot: failed to create worker pipes");
234 if (pipe(resPipe) != 0) {
237 throw std::runtime_error(
"LAMMPSPot: failed to create worker pipes");
249 throw std::runtime_error(
"LAMMPSPot: fork for worker failed");
277 std::signal(SIGPIPE, SIG_IGN);
290extern "C" void __gcov_dump(
void);
294void workerExit(
int code) {
308 if (!readExact(
reqFd, &N,
sizeof(N))) {
314 std::vector<int> atomicNrs(
static_cast<size_t>(N));
315 std::vector<double> R(
static_cast<size_t>(3 * N));
317 if (!readExact(
reqFd, atomicNrs.data(),
318 sizeof(
int) *
static_cast<size_t>(N)) ||
319 !readExact(
reqFd, box,
sizeof(box)) ||
320 !readExact(
reqFd, R.data(),
321 sizeof(
double) *
static_cast<size_t>(3 * N))) {
324 std::vector<double> mask(
static_cast<size_t>(3 * N), 0.0);
325 if (!readExact(
reqFd, mask.data(),
326 sizeof(
double) *
static_cast<size_t>(3 * N))) {
331 std::vector<double> F(
static_cast<size_t>(3 * N), 0.0);
335 forceLocal(N, R.data(), atomicNrs.data(), F.data(), &U, box);
340 double stressRaw[9] = {};
342 for (
int i = 0; i < 9; ++i) {
343 stressRaw[i] =
stress_(i / 3, i % 3);
347 if (!writeExact(
resFd, &status,
sizeof(status)) ||
348 !writeExact(
resFd, &U,
sizeof(U)) ||
349 !writeExact(
resFd, F.data(),
350 sizeof(
double) *
static_cast<size_t>(3 * N)) ||
351 !writeExact(
resFd, stressRaw,
sizeof(stressRaw))) {
365 writeExact(
reqFd, &sentinel,
sizeof(sentinel));
380 for (
int i = 0; i < 100; ++i) {
381 pid_t r = waitpid(
workerPid, &st, WNOHANG);
392 if (r ==
workerPid || (r < 0 && errno != EINTR)) {
405 for (
int i = 0; i < 9; ++i) {
420 double *U,
double *variance,
const double *box) {
423#if defined(EONMPI) || defined(IS_WINDOWS)
424 std::lock_guard<std::mutex> stateLock(
workerMutex);
427 }
catch (
const std::exception &error) {
433 rejectGeometry(U, F, N);
439 std::lock_guard<std::mutex> workerLock(
workerMutex);
441 rejectGeometry(U, F, N);
448 std::vector<double> mask(
static_cast<size_t>(3 * N), 0.0);
455 if (!writeExact(
reqFd, &N,
sizeof(N)) ||
456 !writeExact(
reqFd, atomicNrs,
sizeof(
int) *
static_cast<size_t>(N)) ||
457 !writeExact(
reqFd, box,
sizeof(
double) * 9) ||
458 !writeExact(
reqFd, R,
sizeof(
double) *
static_cast<size_t>(3 * N)) ||
459 !writeExact(
reqFd, mask.data(),
460 sizeof(
double) *
static_cast<size_t>(3 * N))) {
468 rejectGeometry(U, F, N);
483 int pr = poll(&pfd, 1, 90000);
490 "[LAMMPSPot] worker force eval timed out; {} respawns left",
493 rejectGeometry(U, F, N);
498 double stressRaw[9] = {};
499 if (!readExact(
resFd, &status,
sizeof(status)) ||
500 !readExact(
resFd, U,
sizeof(
double)) ||
501 !readExact(
resFd, F,
sizeof(
double) *
static_cast<size_t>(3 * N)) ||
502 !readExact(
resFd, stressRaw,
sizeof(stressRaw))) {
505 "[LAMMPSPot] worker died during force eval; {} respawns left",
508 rejectGeometry(U, F, N);
515 for (
int row = 0; row < 3; ++row) {
516 for (
int col = 0; col < 3; ++col) {
517 stress_(row, col) = stressRaw[row * 3 + col];
525 "[LAMMPSPot] worker reported an evaluation error; {} respawns left",
528 rejectGeometry(U, F, N);
550 bool nonfinite = !std::isfinite(*U);
551 for (
long i = 0; i < 3 * N && !nonfinite; ++i) {
552 nonfinite = !std::isfinite(F[i]);
557 rejectGeometry(U, F, N);
563 double *F,
double *U,
const double *box) {
573 bool newLammps =
false;
574 for (
int i = 0; i < 9; i++) {
584 throw std::runtime_error(
"Should have a LAMMPS instance by now");
587 lmp.scatter_atoms(
LAMMPSObj,
"x", 1, 3,
const_cast<double *
>(R));
598 static_cast<double *
>(lmp.extract_variable(
LAMMPSObj,
"pe",
nullptr));
600 static_cast<double *
>(lmp.extract_variable(
LAMMPSObj,
"fx",
"all"));
602 static_cast<double *
>(lmp.extract_variable(
LAMMPSObj,
"fy",
"all"));
604 static_cast<double *
>(lmp.extract_variable(
LAMMPSObj,
"fz",
"all"));
605 if (!pe || !fx || !fy || !fz) {
614 throw std::runtime_error(
615 "LAMMPS: extract_variable returned null (pe/fx/fy/fz)");
620 for (
long i = 0; i < N; i++) {
621 F[3 * i + 0] = fx[i];
622 F[3 * i + 1] = fy[i];
623 F[3 * i + 2] = fz[i];
628 constexpr double kcalPerEv = 23.0609;
630 for (
long i = 0; i < 3 * N; i++) {
642 auto *sxx =
static_cast<double *
>(
643 lmp.extract_variable(
LAMMPSObj,
"eon_sxx",
nullptr));
644 auto *syy =
static_cast<double *
>(
645 lmp.extract_variable(
LAMMPSObj,
"eon_syy",
nullptr));
646 auto *szz =
static_cast<double *
>(
647 lmp.extract_variable(
LAMMPSObj,
"eon_szz",
nullptr));
648 auto *sxy =
static_cast<double *
>(
649 lmp.extract_variable(
LAMMPSObj,
"eon_sxy",
nullptr));
650 auto *sxz =
static_cast<double *
>(
651 lmp.extract_variable(
LAMMPSObj,
"eon_sxz",
nullptr));
652 auto *syz =
static_cast<double *
>(
653 lmp.extract_variable(
LAMMPSObj,
"eon_syz",
nullptr));
654 const bool got = sxx && syy && szz && sxy && sxz && syz;
655 auto release = [&]() {
674 constexpr double barPerEvA3 = 1.6021766208e6;
675 constexpr double barPerAtm = 1.01325;
677 realunits ? -(barPerAtm / barPerEvA3) : -(1.0 / barPerEvA3);
706 const auto fileSize =
static_cast<std::int64_t
>(sz);
716 while (std::getline(in, line)) {
717 if (!line.empty() && line.back() ==
'\r') {
725 in.seekg(0, std::ios::end);
726 const auto end = in.tellg();
746 std::memcpy(
oldBox, box, 9 *
sizeof(
double));
755 std::map<int, int> type_map;
757 for (
long i = 0; i < N; i++) {
758 if (type_map.count(atomicNrs[i]) == 0) {
759 type_map.insert({atomicNrs[i], ++ntypes});
764 const bool useMpi = lmp.open_mpi !=
nullptr;
767 MPI_Initialized(&initialized);
770 MPI_Comm_size(mpiComm, &ranks);
773 throw std::runtime_error(
"LAMMPS calculator uses multiple MPI ranks but "
774 "lammps_open is missing.\n"
775 "Install an MPI-enabled LAMMPS build.");
779 constexpr bool useMpi =
false;
781 const std::vector<std::string> argStore =
783 std::vector<char *> argPtrs;
784 argPtrs.reserve(argStore.size());
785 for (
const std::string &arg : argStore) {
786 argPtrs.push_back(
const_cast<char *
>(arg.c_str()));
788 int lmpargc =
static_cast<int>(argPtrs.size());
791 MPI_Comm inst_comm = MPI_COMM_NULL;
792 MPI_Comm_dup(mpiComm, &inst_comm);
793 LAMMPSObj = lmp.open_mpi(lmpargc, argPtrs.data(), inst_comm,
nullptr);
797 LAMMPSObj = lmp.open_no_mpi(lmpargc, argPtrs.data(),
nullptr);
803 std::string cmd = std::format(
"package omp {} force/neigh",
lammpsThr);
809 if (std::filesystem::exists(
"in.lammps")) {
810 std::ifstream infile(
"in.lammps");
812 while (std::getline(infile, line)) {
813 if (line ==
"#!units real") {
824 throw std::runtime_error(
825 "LAMMPS: in.lammps not found in working directory");
840 constexpr double kTiltCut = 1.0e-8;
841 if (std::abs(box[1]) > kTiltCut || std::abs(box[2]) > kTiltCut ||
842 std::abs(box[5]) > kTiltCut) {
843 throw std::runtime_error(
844 "LAMMPS: cell is not restricted triclinic (ay/az/bz must be ~0)");
846 std::string region_cmd =
847 std::format(
"region cell prism 0 {} 0 {} 0 {} {} {} {} units box", box[0],
848 box[4], box[8], box[3], box[6], box[7]);
851 std::string create_box_cmd = std::format(
"create_box {} cell", ntypes);
855 for (
long i = 0; i < N; i++) {
856 std::string atom_cmd =
857 std::format(
"create_atoms {} single {} {} {} units box",
858 type_map[atomicNrs[i]], 0.0, 0.0, 0.0);
#define EONC_LOG_WARNING(...)
#define EONC_LOG_INFO(...)
bool lammpsScreenRestart_
std::string lammpsScreenPath_
void setFixedMask(long nAtoms, const double *isFixed) override
Optional frozen-atom mask (nAtoms*3, 1.0 = fixed).
std::vector< double > fixedMask_
void makeNewLAMMPS(long N, const double *R, const int *atomicNrs, const double *box)
LAMMPSPot(const eonc::Parameters &p)
Production: process-default LammpsLoader and POSIX worker isolation.
void lammpsCommand(const char *cmd)
std::int64_t lammpsScreenPos_
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) override
bool noteScreenGeometry(long N, const double *box)
void forceLocal(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box)
void applySetforce(long N)
eonc::ILammpsLoader & loader_
Potential(PotType a_ptype)
Production default: construction-scope registry, else PotRegistry::get().
RAII resource manager for the ARTn C library with global synchronization.
std::int64_t lammpsScreenCursor(std::int64_t pos, std::int64_t fileSize, bool restarted)
Byte offset at which to keep copying a LAMMPS screen file.
bool lammpsWorkerReaped(long got, long child, int err)
True when waitpid has collected the child. EINTR is not a collection.
std::vector< std::string > lammpsOpenArgs(bool logging, bool with_omp, const std::string &screen)
LAMMPS argv.