Loading...
Searching...
No Matches
LAMMPSPot Class Reference

#include <LAMMPSPot.h>

Inheritance diagram for LAMMPSPot:

Public Member Functions

bool needsPerImageInstance () const noexcept override
 Whether NEB should create separate Potential instances per image for true parallel force evaluation.
 LAMMPSPot (const eonc::Parameters &p)
 Production: process-default LammpsLoader and POSIX worker isolation.
 LAMMPSPot (const eonc::Parameters &p, eonc::ILammpsLoader &loader)
 Test seam: injected loader, no worker fork, no process-default load.
 ~LAMMPSPot ()
void cleanMemory ()
void force (long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) override
void setFixedMask (long nAtoms, const double *isFixed) override
 Optional frozen-atom mask (nAtoms*3, 1.0 = fixed).
bool computesStress () const noexcept override
 True when force() leaves a Cauchy stress that cauchyStress() can read until the next force() on this instance.
Matrix3d cauchyStress () const override
 Cauchy stress in eV/Angstrom^3.
Public Member Functions inherited from eonc::Potential
void recordForceCall () noexcept
 Charge one evaluated geometry to this potential and its owning registry.
IPotRegistry & registry () const noexcept
 The registry this potential charges its force calls to.
 Potential (PotType a_ptype)
 Production default: construction-scope registry, else PotRegistry::get().
 Potential (PotType a_ptype, IPotRegistry &registry)
 Test seam: injected registry, no process-default get() counters.
 Potential (PotType a_ptype, const Parameters &p)
 Potential (const Parameters &a_params)
virtual ~Potential ()
void force (std::span< const double > positions, std::span< const int > atomicNrs, std::span< double > forces, double *energy, double *variance, std::span< const double > box)
 C++ call site: size-checked view over the raw FFI force().
std::tuple< double, AtomMatrix > get_ef (const AtomMatrix &pos, const VectorXi &atmnrs, const Matrix3d &box)
PotType getType () const
virtual unsigned long long surfaceEpoch () const noexcept
 Host-visible surface generation.
virtual double finiteCutoff () const noexcept
 Finite interaction range in position length units.
virtual bool isSurrogate () const noexcept
 Whether this is a surrogate (GP) potential.
virtual bool requiresIsolatedMoleculeLayout () const noexcept
 True for molecular QM / non-PBC backends (NWChem socket, ASE ORCA/NWChem, …).
virtual bool isThreadSafe () const noexcept
 Whether this potential's force() can be called from multiple threads on the SAME instance.
virtual unsigned layoutFlags () const noexcept
virtual bool isSharedInstanceThreadSafe () const noexcept
 Conservative gate for sharing one Potential instance across threads.
virtual std::shared_ptr< Potential > clonePotential () const
 Independent instance that does not reload from disk.
virtual bool supportsBatchEvaluation () const noexcept
 Whether this potential supports batched evaluation of N systems in a single call.
virtual void bindBand (const std::vector< std::shared_ptr< Matter > > &)
 NEB calls this with the finished band before the first force evaluation, so a host surface can name each image.
virtual bool forwardsStoredCell () const noexcept
 A host that owns the cell reads the stored matrix.
virtual 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)
 Evaluate forces for N systems in a single call.
virtual 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)
virtual double hessianRange () const noexcept
 Distance beyond which every Hessian block between two atoms is exactly zero, in position length units.

Private Member Functions

 LAMMPSPot (const eonc::Parameters &p, eonc::ILammpsLoader &loader, bool isolate_worker)
void makeNewLAMMPS (long N, const double *R, const int *atomicNrs, const double *box)
void applySetforce (long N)
void ensureWorker ()
bool noteScreenGeometry (long N, const double *box)
void runWorkerLoop ()
void stopWorker ()
void forceLocal (long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box)
void lammpsCommand (const char *cmd)
void drainLammpsScreen ()

Private Attributes

eonc::ILammpsLoader & loader_
int lammpsThr {0}
bool lammpsLogging_ {false}
int lammpsLogIndex_ {0}
std::mutex maskMutex_
std::string lammpsScreenPath_
std::int64_t lammpsScreenPos_ {0}
bool lammpsScreenRestart_ {false}
bool workerChild_ {false}
long numberOfAtoms {0}
double oldBox [9] {}
void * LAMMPSObj {nullptr}
bool realunits {false}
Matrix3d stress_ {Matrix3d::Zero()}
bool haveStress_ {false}
std::vector< double > fixedMask_
long maskN_ {0}
std::mutex workerMutex
int workerRespawnsLeft {3}
int workerPid {-1}
int reqFd {-1}
int resFd {-1}
bool workerSpawned {false}
bool screenHaveGeom_ {false}
long screenAtoms_ {0}
double screenBox_ [9] {}

Additional Inherited Members

Public Types inherited from eonc::Potential
enum class  PotLayout : unsigned { InProcess = 1u << 0 , NeedsWorkingDirectory = 1u << 1 , Subprocess = 1u << 2 }
 How the pot is executed. Combine with bitwise or. More...
Public Attributes inherited from eonc::Potential
std::atomic< size_t > forceCallCounter
Protected Attributes inherited from eonc::Potential
PotType ptype

Detailed Description

Definition at line 70 of file LAMMPSPot.h.

Constructor & Destructor Documentation

◆ LAMMPSPot() [1/3]

LAMMPSPot::LAMMPSPot ( const eonc::Parameters & p)
explicit

Production: process-default LammpsLoader and POSIX worker isolation.

Definition at line 44 of file LAMMPSPot.cpp.

LAMMPSPot(const eonc::Parameters &p)
Production: process-default LammpsLoader and POSIX worker isolation.
Definition LAMMPSPot.cpp:44
static LammpsLoader & instance()
Thread-safe singleton accessor (Meyer's pattern). Does not dlopen.

◆ LAMMPSPot() [2/3]

LAMMPSPot::LAMMPSPot ( const eonc::Parameters & p,
eonc::ILammpsLoader & loader )

Test seam: injected loader, no worker fork, no process-default load.

Definition at line 47 of file LAMMPSPot.cpp.

48 : LAMMPSPot(p, loader, false) {}

◆ ~LAMMPSPot()

LAMMPSPot::~LAMMPSPot ( )

Definition at line 85 of file LAMMPSPot.cpp.

85{ cleanMemory(); }
void cleanMemory()

◆ LAMMPSPot() [3/3]

LAMMPSPot::LAMMPSPot ( const eonc::Parameters & p,
eonc::ILammpsLoader & loader,
bool isolate_worker )
private

Definition at line 50 of file LAMMPSPot.cpp.

52 : eonc::Potential(p),
53 loader_{loader},
56#ifdef EONMPI
57 ,
58 mpiComm{eonc::getMpiClientComm(p)}
59#endif
60{
61 // Fail fast if LAMMPS library not available
62 loader_.require_loaded();
63 if (lammpsLogging_) {
64 static std::atomic<int> ids{0};
65 lammpsLogIndex_ = ids.fetch_add(1);
67 "client_lammps-" + std::to_string(lammpsLogIndex_) + ".log";
68 }
69#if !defined(EONMPI) && !defined(IS_WINDOWS)
70 if (!isolate_worker) {
71 return;
72 }
73 // Fork the worker NOW, at construction, before this process ever opens a
74 // LAMMPS instance (and thus before liblammps initialises MPI). Open MPI
75 // does not support using MPI in a process that called MPI_Init before fork,
76 // so the worker must be spawned from a still-MPI-clean parent. Every
77 // LAMMPSPot -- endpoints and per-image alike -- runs its LAMMPS in its own
78 // child process, so the parent never initialises MPI at all.
80#else
81 static_cast<void>(isolate_worker);
82#endif
83}
std::string lammpsScreenPath_
Definition LAMMPSPot.h:103
int lammpsThr
Definition LAMMPSPot.h:96
bool lammpsLogging_
Definition LAMMPSPot.h:97
int lammpsLogIndex_
Definition LAMMPSPot.h:98
void ensureWorker()
eonc::ILammpsLoader & loader_
Definition LAMMPSPot.h:95
const potential_options_t & potential_options() const

Member Function Documentation

◆ applySetforce()

void LAMMPSPot::applySetforce ( long N)
private

Definition at line 101 of file LAMMPSPot.cpp.

101 {
102 std::vector<double> mask;
103 {
104 // The worker child inherits this mutex across fork. Only the parent locks.
105 std::unique_lock<std::mutex> lock(maskMutex_, std::defer_lock);
106 if (!workerChild_) {
107 lock.lock();
108 }
109 if (LAMMPSObj == nullptr || maskN_ != N || fixedMask_.empty()) {
110 return;
111 }
112 mask = fixedMask_;
113 }
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) {
120 try {
121 lammpsCommand(cmd);
122 } catch (...) {
123 }
124 }
125 for (const char *cmd : kUngroup) {
126 try {
127 lammpsCommand(cmd);
128 } catch (...) {
129 }
130 }
131 // Matter stores per-axis isFixed. A z-only freeze must not leave LAMMPS
132 // free to move that atom in z.
133 std::string ids[3];
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);
138 }
139 }
140 }
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()) {
148 continue;
149 }
151 ("group " + std::string(kGroup[ax]) + " id " + ids[ax]).c_str());
152 lammpsCommand(kFix[ax]);
153 }
154}
std::mutex maskMutex_
Definition LAMMPSPot.h:99
std::vector< double > fixedMask_
Definition LAMMPSPot.h:121
void lammpsCommand(const char *cmd)
long maskN_
Definition LAMMPSPot.h:122
void * LAMMPSObj
Definition LAMMPSPot.h:114
bool workerChild_
Definition LAMMPSPot.h:108

◆ cauchyStress()

Matrix3d LAMMPSPot::cauchyStress ( ) const
inlinenodiscardoverridevirtual

Cauchy stress in eV/Angstrom^3.

sigma = (1/V) dE/dε for the right strain h <- h (I+ε) at fixed fractional coordinates.

Reimplemented from eonc::Potential.

Definition at line 90 of file LAMMPSPot.h.

90{ return stress_; }
Matrix3d stress_
Definition LAMMPSPot.h:119

◆ cleanMemory()

void LAMMPSPot::cleanMemory ( )

Definition at line 156 of file LAMMPSPot.cpp.

156 {
157#if !defined(EONMPI) && !defined(IS_WINDOWS)
158 stopWorker();
159#endif
160 if (LAMMPSObj != nullptr) {
162 loader_.close(LAMMPSObj);
163 LAMMPSObj = nullptr;
164 }
165}
void drainLammpsScreen()
void stopWorker()

◆ computesStress()

bool LAMMPSPot::computesStress ( ) const
inlinenodiscardoverridevirtualnoexcept

True when force() leaves a Cauchy stress that cauchyStress() can read until the next force() on this instance.

Reimplemented from eonc::Potential.

Definition at line 86 of file LAMMPSPot.h.

86 {
87 return haveStress_;
88 }
bool haveStress_
Definition LAMMPSPot.h:120

◆ drainLammpsScreen()

void LAMMPSPot::drainLammpsScreen ( )
private

Definition at line 695 of file LAMMPSPot.cpp.

695 {
696 // makeNewLAMMPS runs in the worker. The child writes the screen file and
697 // must not call the process logger; the parent copies it after the reply.
698 if (workerChild_ || !lammpsLogging_ || lammpsScreenPath_.empty()) {
699 return;
700 }
701 std::error_code ec;
702 const auto sz = std::filesystem::file_size(lammpsScreenPath_, ec);
703 if (ec) {
704 return;
705 }
706 const auto fileSize = static_cast<std::int64_t>(sz);
709 lammpsScreenRestart_ = false;
710 std::ifstream in(lammpsScreenPath_, std::ios::in | std::ios::binary);
711 if (!in) {
712 return;
713 }
714 in.seekg(static_cast<std::streamoff>(lammpsScreenPos_));
715 std::string line;
716 while (std::getline(in, line)) {
717 if (!line.empty() && line.back() == '\r') {
718 line.pop_back();
719 }
720 if (!line.empty()) {
721 EONC_LOG_INFO("{}", line);
722 }
723 }
724 in.clear();
725 in.seekg(0, std::ios::end);
726 const auto end = in.tellg();
727 if (end >= 0) {
728 lammpsScreenPos_ = static_cast<std::int64_t>(end);
729 }
730}
#define EONC_LOG_INFO(...)
Definition EonLogger.h:316
bool lammpsScreenRestart_
Definition LAMMPSPot.h:107
std::int64_t lammpsScreenPos_
Definition LAMMPSPot.h:104
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.
Definition LAMMPSPot.h:41

◆ ensureWorker()

void LAMMPSPot::ensureWorker ( )
private

Definition at line 225 of file LAMMPSPot.cpp.

225 {
226 if (workerSpawned)
227 return;
228
229 int reqPipe[2]; // parent -> child
230 int resPipe[2]; // child -> parent
231 if (pipe(reqPipe) != 0) {
232 throw std::runtime_error("LAMMPSPot: failed to create worker pipes");
233 }
234 if (pipe(resPipe) != 0) {
235 close(reqPipe[0]);
236 close(reqPipe[1]);
237 throw std::runtime_error("LAMMPSPot: failed to create worker pipes");
238 }
239
240 // Fork BEFORE opening any LAMMPS instance in this process, so MPI is first
241 // initialised inside the child. Each child is its own process with its own
242 // MPI_COMM_WORLD; concurrent children never share a communicator.
243 pid_t pid = fork();
244 if (pid < 0) {
245 close(reqPipe[0]);
246 close(reqPipe[1]);
247 close(resPipe[0]);
248 close(resPipe[1]);
249 throw std::runtime_error("LAMMPSPot: fork for worker failed");
250 }
251
252 if (pid == 0) {
253 // Child: keep reqPipe read end and resPipe write end.
254 close(reqPipe[1]);
255 close(resPipe[0]);
256 reqFd = reqPipe[0];
257 resFd = resPipe[1];
258 // Client main arms feenableexcept unconditionally. The worker inherits
259 // that mask; LAMMPS EAM (PairEAM::compute) performs IEEE divisions that
260 // can raise FE_DIVBYZERO on near-coincident pairs during saddle / product
261 // minimisations. Under trapping that SIGFPEs, and the continue handler
262 // without MXCSR masking re-storms forever in the child (GB of identical
263 // "FPE (continuing)" lines). External pot code expects soft IEEE defaults;
264 // demote trapping for the whole worker process before any LAMMPS call.
266 runWorkerLoop(); // never returns
267 }
268
269 // Parent: keep reqPipe write end and resPipe read end.
270 //
271 // Writing to a worker that has already exited raises SIGPIPE, whose default
272 // action kills the client outright -- before writeExact can return the
273 // error the caller is written to handle. A search that had converged on a
274 // saddle at 0.062 eV died this way with signal 13 as its endpoints were
275 // about to be minimised. Ignoring it turns the same condition into an
276 // EPIPE return, which reaches the geometry-rejection path and respawns.
277 std::signal(SIGPIPE, SIG_IGN);
278 close(reqPipe[0]);
279 close(resPipe[1]);
280 reqFd = reqPipe[1];
281 resFd = resPipe[0];
282 workerPid = pid;
283 workerSpawned = true;
284}
int workerPid
Definition LAMMPSPot.h:153
bool workerSpawned
Definition LAMMPSPot.h:156
void runWorkerLoop()
void close(Handle h) noexcept
Definition DynLib.h:77
void disableFPE()

◆ force()

void LAMMPSPot::force ( long N,
const double * R,
const int * atomicNrs,
double * F,
double * U,
double * variance,
const double * box )
overridevirtual

Implements eonc::Potential.

Definition at line 419 of file LAMMPSPot.cpp.

420 {
421 variance = nullptr;
422
423#if defined(EONMPI) || defined(IS_WINDOWS)
424 std::lock_guard<std::mutex> stateLock(workerMutex);
425 try {
426 forceLocal(N, R, atomicNrs, F, U, box);
427 } catch (const std::exception &error) {
428 EONC_LOG_WARNING("[LAMMPSPot] evaluation failed; rejecting geometry: {}",
429 error.what());
430 cleanMemory();
431 haveStress_ = false;
432 stress_.setZero();
433 rejectGeometry(U, F, N);
434 }
435#else
436 // Drive the dedicated worker process so this image's LAMMPS runs in its own
437 // process (own MPI_COMM_WORLD). Per-image NEB threads thus evaluate forces
438 // as truly concurrent processes with no shared-communicator contention.
439 std::lock_guard<std::mutex> workerLock(workerMutex);
440 if (workerRespawnsLeft <= 0) {
441 rejectGeometry(U, F, N);
442 return;
443 }
444 const bool newWorker = !workerSpawned;
445 ensureWorker();
446 const bool screenRestart = newWorker || noteScreenGeometry(N, box);
447
448 std::vector<double> mask(static_cast<size_t>(3 * N), 0.0);
449 {
450 std::lock_guard<std::mutex> lock(maskMutex_);
451 if (maskN_ == N && fixedMask_.size() == static_cast<size_t>(3 * N)) {
452 mask = fixedMask_;
453 }
454 }
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))) {
461 // A worker stopped by an earlier rejected geometry leaves the request
462 // pipe closed, so the first send after it fails. Respawning happens on
463 // the next evaluation; reject this one rather than end the client.
465 EONC_LOG_WARNING("[LAMMPSPot] send to worker failed; {} respawns left",
467 stopWorker();
468 rejectGeometry(U, F, N);
469 return;
470 }
471
472 // A worker stuck on a pathological geometry (LAMMPS spinning, or
473 // a NaN it never returns) would make the blocking read below hang until the
474 // akmc pass times out. Bound the wait: if the worker is silent past a
475 // generous per-eval deadline, kill and reap it (a stuck worker never reaches
476 // EOF, so a plain waitpid would block too) and fail this evaluation so the
477 // search discards the geometry; ensureWorker respawns on the next call.
478 {
479 struct pollfd pfd;
480 pfd.fd = resFd;
481 pfd.events = POLLIN;
482 pfd.revents = 0;
483 int pr = poll(&pfd, 1, 90000); // 90 s: orders beyond a normal force eval
484 if (pr <= 0) {
485 if (workerPid > 0) {
486 kill(workerPid, SIGKILL);
487 }
490 "[LAMMPSPot] worker force eval timed out; {} respawns left",
492 stopWorker();
493 rejectGeometry(U, F, N);
494 return;
495 }
496 }
497 int status = 0;
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",
507 stopWorker();
508 rejectGeometry(U, F, N);
509 return;
510 }
511 if (screenRestart) {
513 }
514 stress_.setZero();
515 for (int row = 0; row < 3; ++row) {
516 for (int col = 0; col < 3; ++col) {
517 stress_(row, col) = stressRaw[row * 3 + col];
518 }
519 }
520 haveStress_ = status == 0 && stress_.allFinite();
521 if (status != 0) {
525 "[LAMMPSPot] worker reported an evaluation error; {} respawns left",
527 stopWorker();
528 rejectGeometry(U, F, N);
529 return;
530 }
532 // A saddle search that never terminates silently truncates the event
533 // table: the KMC residence time is 1/sum_j k_j over the discovered
534 // mechanisms, so a dropped search removes a term and biases the clock
535 // (Pedersen and Jónsson, Math. Comput. Simul. 80, 1487 (2010),
536 // doi:10.1016/j.matcom.2009.02.010, Fig. 1; Alexander and Schuh,
537 // Modelling Simul. Mater. Sci. Eng. 24, 065014 (2016),
538 // doi:10.1088/0965-0393/24/6/065014, on catalog completeness).
539 // An over-aggressive saddle-search kick can drive atoms on top of
540 // each other; the EAM force overflows to NaN/Inf and LAMMPS returns it
541 // rather than crashing. The min-mode search then spins on non-finite
542 // gradients until the akmc pass times out (0 processes). Reject the
543 // evaluation so the search discards that displacement and continues.
544 // Reject the geometry, not the process. Nothing between this call and
545 // main catches a throw here, so the client terminates: a search that was
546 // making progress is lost, and every other search sharing the pass goes
547 // with it. A large finite energy with zeroed forces reads to the
548 // optimiser as an impassable wall, so it backs out of the step and the
549 // search abandons this configuration and carries on.
550 bool nonfinite = !std::isfinite(*U);
551 for (long i = 0; i < 3 * N && !nonfinite; ++i) {
552 nonfinite = !std::isfinite(F[i]);
553 }
554 if (nonfinite) {
555 EONC_LOG_WARNING("[LAMMPSPot] non-finite force or energy; rejecting "
556 "geometry");
557 rejectGeometry(U, F, N);
558 }
559#endif
560}
#define EONC_LOG_WARNING(...)
Definition EonLogger.h:322
int workerRespawnsLeft
Definition LAMMPSPot.h:144
bool noteScreenGeometry(long N, const double *box)
void forceLocal(long N, const double *R, const int *atomicNrs, double *F, double *U, const double *box)
std::mutex workerMutex
Definition LAMMPSPot.h:126
std::string error()
Definition DynLib.h:82

◆ forceLocal()

void LAMMPSPot::forceLocal ( long N,
const double * R,
const int * atomicNrs,
double * F,
double * U,
const double * box )
private

Definition at line 562 of file LAMMPSPot.cpp.

563 {
564 // Same contract as ASE / Metatomic: external pot libraries are not written
565 // for FE traps. Cover in-process (EONMPI / Windows) and any path that still
566 // has trapping armed when forceLocal runs. Always restore so FE traps do not
567 // stay demoted for the rest of the process after the first force call.
568 eonc::FPEHandler fpeh;
569 fpeh.eat_fpe();
570 try {
571 auto &lmp = loader_;
572
573 bool newLammps = false;
574 for (int i = 0; i < 9; i++) {
575 if (oldBox[i] != box[i])
576 newLammps = true;
577 }
578 if (numberOfAtoms != N)
579 newLammps = true;
580 if (newLammps) {
581 makeNewLAMMPS(N, R, atomicNrs, box);
582 }
583 if (!LAMMPSObj) {
584 throw std::runtime_error("Should have a LAMMPS instance by now");
585 }
586
587 lmp.scatter_atoms(LAMMPSObj, "x", 1, 3, const_cast<double *>(R));
588 applySetforce(N);
589 // New instance / box change: rebuild neighbors. create_atoms sits at
590 // the origin; pre no would evaluate on that neighbor list.
591 if (newLammps) {
592 lammpsCommand("run 1 pre yes post no");
593 } else {
594 lammpsCommand("run 1 pre no post no");
595 }
596
597 auto *pe =
598 static_cast<double *>(lmp.extract_variable(LAMMPSObj, "pe", nullptr));
599 auto *fx =
600 static_cast<double *>(lmp.extract_variable(LAMMPSObj, "fx", "all"));
601 auto *fy =
602 static_cast<double *>(lmp.extract_variable(LAMMPSObj, "fy", "all"));
603 auto *fz =
604 static_cast<double *>(lmp.extract_variable(LAMMPSObj, "fz", "all"));
605 if (!pe || !fx || !fy || !fz) {
606 if (pe)
607 free(pe);
608 if (fx)
609 free(fx);
610 if (fy)
611 free(fy);
612 if (fz)
613 free(fz);
614 throw std::runtime_error(
615 "LAMMPS: extract_variable returned null (pe/fx/fy/fz)");
616 }
617 *U = *pe;
618 free(pe);
619
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];
624 }
625
626 // Convert kCal/mol -> eV if LAMMPS is using real units
627 if (realunits) {
628 constexpr double kcalPerEv = 23.0609;
629 *U /= kcalPerEv;
630 for (long i = 0; i < 3 * N; i++) {
631 F[i] /= kcalPerEv;
632 }
633 }
634
635 free(fx);
636 free(fy);
637 free(fz);
638
639 // LAMMPS pressure is positive when the cell pushes outward. sigma =
640 // (1/V) dE/dε has the opposite sign. metal reports bars, real reports
641 // atmospheres. 1 eV/Angstrom^3 = 1.6021766208e6 bar.
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 = [&]() {
656 if (sxx)
657 free(sxx);
658 if (syy)
659 free(syy);
660 if (szz)
661 free(szz);
662 if (sxy)
663 free(sxy);
664 if (sxz)
665 free(sxz);
666 if (syz)
667 free(syz);
668 };
669 if (!got) {
670 release();
671 haveStress_ = false;
672 stress_.setZero();
673 } else {
674 constexpr double barPerEvA3 = 1.6021766208e6;
675 constexpr double barPerAtm = 1.01325;
676 const double toEv =
677 realunits ? -(barPerAtm / barPerEvA3) : -(1.0 / barPerEvA3);
678 stress_.setZero();
679 stress_(0, 0) = (*sxx) * toEv;
680 stress_(1, 1) = (*syy) * toEv;
681 stress_(2, 2) = (*szz) * toEv;
682 stress_(0, 1) = stress_(1, 0) = (*sxy) * toEv;
683 stress_(0, 2) = stress_(2, 0) = (*sxz) * toEv;
684 stress_(1, 2) = stress_(2, 1) = (*syz) * toEv;
685 haveStress_ = stress_.allFinite();
686 release();
687 }
688 } catch (...) {
689 fpeh.restore_fpe();
690 throw;
691 }
692 fpeh.restore_fpe();
693}
double oldBox[9]
Definition LAMMPSPot.h:113
void makeNewLAMMPS(long N, const double *R, const int *atomicNrs, const double *box)
bool realunits
Definition LAMMPSPot.h:118
long numberOfAtoms
Definition LAMMPSPot.h:112
void applySetforce(long N)
void release(State *state)

◆ lammpsCommand()

void LAMMPSPot::lammpsCommand ( const char * cmd)
private

Definition at line 732 of file LAMMPSPot.cpp.

732 {
733 loader_.command(LAMMPSObj, cmd);
734 // The forked worker must not call the process logger. The parent copies
735 // the screen file after the child returns the force.
736 if (!workerChild_) {
738 }
739}

◆ makeNewLAMMPS()

void LAMMPSPot::makeNewLAMMPS ( long N,
const double * R,
const int * atomicNrs,
const double * box )
private

Definition at line 741 of file LAMMPSPot.cpp.

742 {
743 auto &lmp = loader_;
744
745 numberOfAtoms = N;
746 std::memcpy(oldBox, box, 9 * sizeof(double));
747
748 if (LAMMPSObj != nullptr) {
750 loader_.close(LAMMPSObj);
751 LAMMPSObj = nullptr;
752 }
753
754 // Map atomic numbers to LAMMPS type indices (1-based)
755 std::map<int, int> type_map;
756 int ntypes = 0;
757 for (long i = 0; i < N; i++) {
758 if (type_map.count(atomicNrs[i]) == 0) {
759 type_map.insert({atomicNrs[i], ++ntypes});
760 }
761 }
762
763#ifdef EONMPI
764 const bool useMpi = lmp.open_mpi != nullptr;
765 if (!useMpi) {
766 int initialized = 0;
767 MPI_Initialized(&initialized);
768 int ranks = 1;
769 if (initialized) {
770 MPI_Comm_size(mpiComm, &ranks);
771 }
772 if (ranks > 1) {
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.");
776 }
777 }
778#else
779 constexpr bool useMpi = false;
780#endif
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()));
787 }
788 int lmpargc = static_cast<int>(argPtrs.size());
789#ifdef EONMPI
790 if (useMpi) {
791 MPI_Comm inst_comm = MPI_COMM_NULL;
792 MPI_Comm_dup(mpiComm, &inst_comm); // private comm per per-image instance
793 LAMMPSObj = lmp.open_mpi(lmpargc, argPtrs.data(), inst_comm, nullptr);
794 } else
795#endif
796 {
797 LAMMPSObj = lmp.open_no_mpi(lmpargc, argPtrs.data(), nullptr);
798 }
799 // -screen opens with truncation. The next copy starts at the new file.
801
802 if (lammpsThr > 0) {
803 std::string cmd = std::format("package omp {} force/neigh", lammpsThr);
804 lammpsCommand(cmd.c_str());
805 }
806
807 // Detect units from in.lammps: look for "#!units real" marker
808 realunits = false;
809 if (std::filesystem::exists("in.lammps")) {
810 std::ifstream infile("in.lammps");
811 std::string line;
812 while (std::getline(infile, line)) {
813 if (line == "#!units real") {
814 realunits = true;
815 break;
816 }
817 }
818 } else {
819 if (LAMMPSObj != nullptr) {
821 lmp.close(LAMMPSObj);
822 LAMMPSObj = nullptr;
823 }
824 throw std::runtime_error(
825 "LAMMPS: in.lammps not found in working directory");
826 }
827
828 if (realunits) {
829 lammpsCommand("units real");
830 } else {
831 lammpsCommand("units metal");
832 }
833
834 lammpsCommand("atom_style charge");
835 lammpsCommand("atom_modify map array sort 0 0");
836 lammpsCommand("neigh_modify delay 1");
837
838 // LAMMPS restricted triclinic: (ax, by, cz, bx, cx, cy).
839 // Row-major Matter cell also has ay, az, bz at box[1], box[2], box[5].
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)");
845 }
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]);
849 lammpsCommand(region_cmd.c_str());
850
851 std::string create_box_cmd = std::format("create_box {} cell", ntypes);
852 lammpsCommand(create_box_cmd.c_str());
853
854 // Initialize atoms
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);
859 lammpsCommand(atom_cmd.c_str());
860 }
861
862 lammpsCommand("mass * 1.0");
863
864 // Load user LAMMPS input script
865 lmp.file(LAMMPSObj, "in.lammps");
866
867 // Define variables for force/energy extraction
868 lammpsCommand("variable fx atom fx");
869 lammpsCommand("variable fy atom fy");
870 lammpsCommand("variable fz atom fz");
871 lammpsCommand("variable pe equal pe");
872 // Kinetic part omitted. The six components are pxx pyy pzz pxy pxz pyz.
873 lammpsCommand("compute eon_press all pressure NULL virial");
874 lammpsCommand("variable eon_sxx equal c_eon_press[1]");
875 lammpsCommand("variable eon_syy equal c_eon_press[2]");
876 lammpsCommand("variable eon_szz equal c_eon_press[3]");
877 lammpsCommand("variable eon_sxy equal c_eon_press[4]");
878 lammpsCommand("variable eon_sxz equal c_eon_press[5]");
879 lammpsCommand("variable eon_syz equal c_eon_press[6]");
880}
std::vector< std::string > lammpsOpenArgs(bool logging, bool with_omp, const std::string &screen)
LAMMPS argv.
Definition LAMMPSPot.h:51

◆ needsPerImageInstance()

bool LAMMPSPot::needsPerImageInstance ( ) const
inlinenodiscardoverridevirtualnoexcept

Whether NEB should create separate Potential instances per image for true parallel force evaluation.

When true, NEB calls makePotential() once per image instead of sharing one instance. Override in potentials that use internal mutexes (e.g. MetatomicPotential).

Reimplemented from eonc::Potential.

Definition at line 73 of file LAMMPSPot.h.

73 {
74 return true;
75 }

◆ noteScreenGeometry()

bool LAMMPSPot::noteScreenGeometry ( long N,
const double * box )
private

Definition at line 402 of file LAMMPSPot.cpp.

402 {
403 bool changed = !screenHaveGeom_ || N != screenAtoms_;
404 if (!changed) {
405 for (int i = 0; i < 9; ++i) {
406 if (screenBox_[i] != box[i]) {
407 changed = true;
408 break;
409 }
410 }
411 }
412 screenHaveGeom_ = true;
413 screenAtoms_ = N;
414 std::memcpy(screenBox_, box, sizeof(screenBox_));
415 return changed;
416}
bool screenHaveGeom_
Definition LAMMPSPot.h:159
long screenAtoms_
Definition LAMMPSPot.h:160
double screenBox_[9]
Definition LAMMPSPot.h:161

◆ runWorkerLoop()

void LAMMPSPot::runWorkerLoop ( )
private

Definition at line 302 of file LAMMPSPot.cpp.

302 {
303 workerChild_ = true;
304 // Running in the forked child. Evaluate forces with an in-process LAMMPS
305 // (this child's own MPI_COMM_WORLD) and stream results back to the parent.
306 for (;;) {
307 long N = 0;
308 if (!readExact(reqFd, &N, sizeof(N))) {
309 workerExit(0); // request pipe closed -> shut down cleanly
310 }
311 if (N < 0) {
312 workerExit(0); // explicit shutdown sentinel from stopWorker()
313 }
314 std::vector<int> atomicNrs(static_cast<size_t>(N));
315 std::vector<double> R(static_cast<size_t>(3 * N));
316 double box[9];
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))) {
322 workerExit(1);
323 }
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))) {
327 workerExit(1);
328 }
329 setFixedMask(N, mask.data());
330
331 std::vector<double> F(static_cast<size_t>(3 * N), 0.0);
332 double U = 0.0;
333 int status = 0;
334 try {
335 forceLocal(N, R.data(), atomicNrs.data(), F.data(), &U, box);
336 } catch (...) {
337 status = 1;
338 haveStress_ = false;
339 }
340 double stressRaw[9] = {};
341 if (haveStress_) {
342 for (int i = 0; i < 9; ++i) {
343 stressRaw[i] = stress_(i / 3, i % 3);
344 }
345 }
346
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))) {
352 workerExit(1);
353 }
354 }
355}
void setFixedMask(long nAtoms, const double *isFixed) override
Optional frozen-atom mask (nAtoms*3, 1.0 = fixed).
Definition LAMMPSPot.cpp:87

◆ setFixedMask()

void LAMMPSPot::setFixedMask ( long nAtoms,
const double * isFixed )
overridevirtual

Optional frozen-atom mask (nAtoms*3, 1.0 = fixed).

Default no-op. Matter calls this immediately before force() so wrappers that need it (LAMMPS setforce) see Atom.fixed without changing force().

Reimplemented from eonc::Potential.

Definition at line 87 of file LAMMPSPot.cpp.

87 {
88 std::unique_lock<std::mutex> lock(maskMutex_, std::defer_lock);
89 if (!workerChild_) {
90 lock.lock();
91 }
92 if (nAtoms <= 0 || isFixed == nullptr) {
93 fixedMask_.clear();
94 maskN_ = 0;
95 return;
96 }
97 fixedMask_.assign(isFixed, isFixed + 3 * nAtoms);
98 maskN_ = nAtoms;
99}

◆ stopWorker()

void LAMMPSPot::stopWorker ( )
private

Definition at line 357 of file LAMMPSPot.cpp.

357 {
358 if (!workerSpawned)
359 return;
360 if (reqFd >= 0) {
361 // Send an explicit shutdown sentinel, then close. A sentinel (rather than
362 // relying on pipe EOF) guarantees the child exits even when sibling worker
363 // processes hold an inherited copy of this write end.
364 long sentinel = -1;
365 writeExact(reqFd, &sentinel, sizeof(sentinel));
366 close(reqFd);
367 reqFd = -1;
368 }
369 if (resFd >= 0) {
370 close(resFd);
371 resFd = -1;
372 }
373 if (workerPid > 0) {
374 // A wedged worker never acts on the sentinel or the closed pipe, and an
375 // unconditional wait then blocks the client forever at no CPU cost -- the
376 // hang this teardown exists to avoid. Give the child a brief chance to
377 // exit on its own, then insist.
378 int st = 0;
379 bool reaped = false;
380 for (int i = 0; i < 100; ++i) { // up to ~1 s
381 pid_t r = waitpid(workerPid, &st, WNOHANG);
382 if (eonc::lammpsWorkerReaped(r, workerPid, errno)) {
383 reaped = true;
384 break;
385 }
386 usleep(10000);
387 }
388 if (!reaped) {
389 kill(workerPid, SIGKILL);
390 for (;;) {
391 pid_t r = waitpid(workerPid, &st, 0);
392 if (r == workerPid || (r < 0 && errno != EINTR)) {
393 break;
394 }
395 }
396 }
397 workerPid = -1;
398 }
399 workerSpawned = false;
400}
bool lammpsWorkerReaped(long got, long child, int err)
True when waitpid has collected the child. EINTR is not a collection.
Definition LAMMPSPot.h:31

Member Data Documentation

◆ fixedMask_

std::vector<double> LAMMPSPot::fixedMask_
private

Definition at line 121 of file LAMMPSPot.h.

◆ haveStress_

bool LAMMPSPot::haveStress_ {false}
private

Definition at line 120 of file LAMMPSPot.h.

120{false};

◆ lammpsLogging_

bool LAMMPSPot::lammpsLogging_ {false}
private

Definition at line 97 of file LAMMPSPot.h.

97{false};

◆ lammpsLogIndex_

int LAMMPSPot::lammpsLogIndex_ {0}
private

Definition at line 98 of file LAMMPSPot.h.

98{0};

◆ LAMMPSObj

void* LAMMPSPot::LAMMPSObj {nullptr}
private

Definition at line 114 of file LAMMPSPot.h.

114{nullptr};

◆ lammpsScreenPath_

std::string LAMMPSPot::lammpsScreenPath_
private

Definition at line 103 of file LAMMPSPot.h.

◆ lammpsScreenPos_

std::int64_t LAMMPSPot::lammpsScreenPos_ {0}
private

Definition at line 104 of file LAMMPSPot.h.

104{0};

◆ lammpsScreenRestart_

bool LAMMPSPot::lammpsScreenRestart_ {false}
private

Definition at line 107 of file LAMMPSPot.h.

107{false};

◆ lammpsThr

int LAMMPSPot::lammpsThr {0}
private

Definition at line 96 of file LAMMPSPot.h.

96{0};

◆ loader_

eonc::ILammpsLoader& LAMMPSPot::loader_
private

Definition at line 95 of file LAMMPSPot.h.

◆ maskMutex_

std::mutex LAMMPSPot::maskMutex_
private

Definition at line 99 of file LAMMPSPot.h.

◆ maskN_

long LAMMPSPot::maskN_ {0}
private

Definition at line 122 of file LAMMPSPot.h.

122{0};

◆ numberOfAtoms

long LAMMPSPot::numberOfAtoms {0}
private

Definition at line 112 of file LAMMPSPot.h.

112{0};

◆ oldBox

double LAMMPSPot::oldBox[9] {}
private

Definition at line 113 of file LAMMPSPot.h.

113{};

◆ realunits

bool LAMMPSPot::realunits {false}
private

Definition at line 118 of file LAMMPSPot.h.

118{false};

◆ reqFd

int LAMMPSPot::reqFd {-1}
private

Definition at line 154 of file LAMMPSPot.h.

154{-1}; // parent writes requests here (child stdin side)

◆ resFd

int LAMMPSPot::resFd {-1}
private

Definition at line 155 of file LAMMPSPot.h.

155{-1}; // parent reads results here (child stdout side)

◆ screenAtoms_

long LAMMPSPot::screenAtoms_ {0}
private

Definition at line 160 of file LAMMPSPot.h.

160{0};

◆ screenBox_

double LAMMPSPot::screenBox_[9] {}
private

Definition at line 161 of file LAMMPSPot.h.

161{};

◆ screenHaveGeom_

bool LAMMPSPot::screenHaveGeom_ {false}
private

Definition at line 159 of file LAMMPSPot.h.

159{false};

◆ stress_

Matrix3d LAMMPSPot::stress_ {Matrix3d::Zero()}
private

Definition at line 119 of file LAMMPSPot.h.

119{Matrix3d::Zero()};

◆ workerChild_

bool LAMMPSPot::workerChild_ {false}
private

Definition at line 108 of file LAMMPSPot.h.

108{false};

◆ workerMutex

std::mutex LAMMPSPot::workerMutex
private

Definition at line 126 of file LAMMPSPot.h.

◆ workerPid

int LAMMPSPot::workerPid {-1}
private

Definition at line 153 of file LAMMPSPot.h.

153{-1};

◆ workerRespawnsLeft

int LAMMPSPot::workerRespawnsLeft {3}
private

Definition at line 144 of file LAMMPSPot.h.

144{3};

◆ workerSpawned

bool LAMMPSPot::workerSpawned {false}
private

Definition at line 156 of file LAMMPSPot.h.

156{false};

The documentation for this class was generated from the following files:
  • /home/runner/work/eOn/eOn/include/eon/potentials/LAMMPS/LAMMPSPot.h
  • /home/runner/work/eOn/eOn/client/potentials/LAMMPS/LAMMPSPot.cpp