Loading...
Searching...
No Matches
LAMMPSPot.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
13#include "eon/EonLogger.h"
14#include "eon/Parameters.h"
15#include "eon/fpe_handler.h"
17
18#include <atomic>
19#include <cmath>
20#include <cstring>
21#include <filesystem>
22#include <format>
23#include <fstream>
24#include <map>
25#include <stdexcept>
26#include <string>
27
28#if !defined(EONMPI) && !defined(IS_WINDOWS)
29#include <cerrno>
30#include <cstdlib>
31#include <vector>
32
33#include <csignal>
34#include <poll.h>
35#include <sys/wait.h>
36#include <unistd.h>
37#endif
38
39#ifdef EONMPI
40#define LAMMPS_LIB_MPI
41#include "eon/ParametersMpi.h"
42#endif
43
45 : LAMMPSPot(p, eonc::LammpsLoader::instance(), true) {}
46
48 : LAMMPSPot(p, loader, false) {}
49
51 bool isolate_worker)
52 : eonc::Potential(p),
53 loader_{loader},
54 lammpsThr{p.potential_options().LAMMPSThreads},
55 lammpsLogging_{p.potential_options().LAMMPSLogging}
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}
84
86
87void LAMMPSPot::setFixedMask(long nAtoms, const double *isFixed) {
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}
100
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}
155
157#if !defined(EONMPI) && !defined(IS_WINDOWS)
158 stopWorker();
159#endif
160 if (LAMMPSObj != nullptr) {
162 loader_.close(LAMMPSObj);
163 LAMMPSObj = nullptr;
164 }
165}
166
167namespace {
168// Rejected geometries carry a high energy and a nonzero force so force-based
169// optimizers cannot accept a failed evaluation as a stationary structure.
170void rejectGeometry(double *U, double *F, long N) {
171 *U = 1.0e6;
172 // Zero forces would be read as convergence: an optimiser judges a point
173 // converged on force magnitude alone, so a rejected geometry with no force
174 // is accepted as a minimum and the wall energy is recorded as that
175 // minimum's energy. It then reaches the barrier as E_saddle - 1e6, which
176 // eOn reports as a negative barrier and discards -- a real 0.062 eV copper
177 // saddle was lost exactly this way. Return a force far above any
178 // convergence threshold so the point can never be mistaken for a
179 // stationary one, alternating its sign so the frame gains no net force.
180 for (long i = 0; i < 3 * N; ++i) {
181 F[i] = 0.0;
182 }
183 for (long i = 0; i < N; ++i) {
184 F[3 * i] = (i % 2 == 0) ? 1.0 : -1.0;
185 }
186}
187
188} // namespace
189
190#if !defined(EONMPI) && !defined(IS_WINDOWS)
191// Process-per-image worker plumbing (POSIX only).
192namespace {
193// Blocking read/write of exactly n bytes over a pipe. Returns false on EOF or
194// error, so a dead peer is detected rather than silently producing garbage.
195bool readExact(int fd, void *buf, size_t n) {
196 auto *p = static_cast<char *>(buf);
197 while (n > 0) {
198 ssize_t r = read(fd, p, n);
199 if (r <= 0) {
200 if (r < 0 && errno == EINTR)
201 continue;
202 return false;
203 }
204 p += r;
205 n -= static_cast<size_t>(r);
206 }
207 return true;
208}
209bool writeExact(int fd, const void *buf, size_t n) {
210 const auto *p = static_cast<const char *>(buf);
211 while (n > 0) {
212 ssize_t w = write(fd, p, n);
213 if (w < 0) {
214 if (errno == EINTR)
215 continue;
216 return false;
217 }
218 p += w;
219 n -= static_cast<size_t>(w);
220 }
221 return true;
222}
223} // namespace
224
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}
285
286// A weak reference does not pull the counter writer out of libgcov, and the
287// child then leaves its counters at zero. The strong call is compiled only
288// into a coverage build, which links that writer.
289#ifdef EON_GCOV_DUMP
290extern "C" void __gcov_dump(void);
291#endif
292
293namespace {
294void workerExit(int code) {
295#ifdef EON_GCOV_DUMP
296 __gcov_dump();
297#endif
298 _exit(code);
299}
300} // namespace
301
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}
356
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}
401
402bool LAMMPSPot::noteScreenGeometry(long N, const double *box) {
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}
417#endif // !EONMPI && !IS_WINDOWS
418
419void LAMMPSPot::force(long N, const double *R, const int *atomicNrs, double *F,
420 double *U, double *variance, const double *box) {
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}
561
562void LAMMPSPot::forceLocal(long N, const double *R, const int *atomicNrs,
563 double *F, double *U, const double *box) {
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}
694
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}
731
732void LAMMPSPot::lammpsCommand(const char *cmd) {
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}
740
741void LAMMPSPot::makeNewLAMMPS(long N, const double *R, const int *atomicNrs,
742 const double *box) {
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}
#define EONC_LOG_WARNING(...)
Definition EonLogger.h:322
#define EONC_LOG_INFO(...)
Definition EonLogger.h:316
double oldBox[9]
Definition LAMMPSPot.h:113
int workerPid
Definition LAMMPSPot.h:153
bool screenHaveGeom_
Definition LAMMPSPot.h:159
bool lammpsScreenRestart_
Definition LAMMPSPot.h:107
std::string lammpsScreenPath_
Definition LAMMPSPot.h:103
void setFixedMask(long nAtoms, const double *isFixed) override
Optional frozen-atom mask (nAtoms*3, 1.0 = fixed).
Definition LAMMPSPot.cpp:87
std::mutex maskMutex_
Definition LAMMPSPot.h:99
std::vector< double > fixedMask_
Definition LAMMPSPot.h:121
void makeNewLAMMPS(long N, const double *R, const int *atomicNrs, const double *box)
void drainLammpsScreen()
int lammpsThr
Definition LAMMPSPot.h:96
LAMMPSPot(const eonc::Parameters &p)
Production: process-default LammpsLoader and POSIX worker isolation.
Definition LAMMPSPot.cpp:44
Matrix3d stress_
Definition LAMMPSPot.h:119
bool lammpsLogging_
Definition LAMMPSPot.h:97
bool workerSpawned
Definition LAMMPSPot.h:156
void cleanMemory()
void lammpsCommand(const char *cmd)
int lammpsLogIndex_
Definition LAMMPSPot.h:98
int workerRespawnsLeft
Definition LAMMPSPot.h:144
long maskN_
Definition LAMMPSPot.h:122
bool realunits
Definition LAMMPSPot.h:118
void stopWorker()
bool haveStress_
Definition LAMMPSPot.h:120
long screenAtoms_
Definition LAMMPSPot.h:160
void * LAMMPSObj
Definition LAMMPSPot.h:114
std::int64_t lammpsScreenPos_
Definition LAMMPSPot.h:104
void ensureWorker()
void runWorkerLoop()
long numberOfAtoms
Definition LAMMPSPot.h:112
double screenBox_[9]
Definition LAMMPSPot.h:161
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)
bool workerChild_
Definition LAMMPSPot.h:108
std::mutex workerMutex
Definition LAMMPSPot.h:126
void applySetforce(long N)
eonc::ILammpsLoader & loader_
Definition LAMMPSPot.h:95
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.
Definition LAMMPSPot.h:41
bool lammpsWorkerReaped(long got, long child, int err)
True when waitpid has collected the child. EINTR is not a collection.
Definition LAMMPSPot.h:31
std::vector< std::string > lammpsOpenArgs(bool logging, bool with_omp, const std::string &screen)
LAMMPS argv.
Definition LAMMPSPot.h:51
void disableFPE()