From 4486119310f4d12fc48ebbfe353d8b8eb9e60378 Mon Sep 17 00:00:00 2001 From: Andreas Singraber Date: Sat, 25 Feb 2023 00:41:32 +0100 Subject: [PATCH 1/8] Updated LAMMPS interface files (ML-HDNNP) --- .../src/{USER-NNP => ML-HDNNP}/Install.sh | 23 +- src/interface/LAMMPS/src/ML-HDNNP/README | 26 ++ .../LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp | 365 +++++++++++++++ .../LAMMPS/src/ML-HDNNP/pair_hdnnp.h | 71 +++ src/interface/LAMMPS/src/USER-NNP/README | 0 .../LAMMPS/src/USER-NNP/pair_nnp.cpp | 414 ------------------ src/interface/LAMMPS/src/USER-NNP/pair_nnp.h | 60 --- src/interface/makefile | 13 +- 8 files changed, 483 insertions(+), 489 deletions(-) rename src/interface/LAMMPS/src/{USER-NNP => ML-HDNNP}/Install.sh (57%) create mode 100644 src/interface/LAMMPS/src/ML-HDNNP/README create mode 100644 src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp create mode 100644 src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.h delete mode 100644 src/interface/LAMMPS/src/USER-NNP/README delete mode 100644 src/interface/LAMMPS/src/USER-NNP/pair_nnp.cpp delete mode 100644 src/interface/LAMMPS/src/USER-NNP/pair_nnp.h diff --git a/src/interface/LAMMPS/src/USER-NNP/Install.sh b/src/interface/LAMMPS/src/ML-HDNNP/Install.sh similarity index 57% rename from src/interface/LAMMPS/src/USER-NNP/Install.sh rename to src/interface/LAMMPS/src/ML-HDNNP/Install.sh index b480b4451..f343f3885 100644 --- a/src/interface/LAMMPS/src/USER-NNP/Install.sh +++ b/src/interface/LAMMPS/src/ML-HDNNP/Install.sh @@ -26,6 +26,8 @@ action () { fi } +# all package files with no dependencies + for file in *.cpp *.h; do test -f ${file} && action $file done @@ -35,31 +37,30 @@ done if (test $1 = 1) then if (test -e ../Makefile.package) then - sed -i -e 's/[^ \t]*nnp[^ \t]* //g' ../Makefile.package - sed -i -e 's|^PKG_INC =[ \t]*|&-I..\/..\/lib\/nnp\/include |' ../Makefile.package - sed -i -e 's|^PKG_PATH =[ \t]*|&-L..\/..\/lib\/nnp\/lib |' ../Makefile.package - sed -i -e 's|^PKG_SYSINC =[ \t]*|&$(nnp_SYSINC) |' ../Makefile.package - sed -i -e 's|^PKG_SYSLIB =[ \t]*|&$(nnp_SYSLIB) |' ../Makefile.package - sed -i -e 's|^PKG_SYSPATH =[ \t]*|&$(nnp_SYSPATH) |' ../Makefile.package + sed -i -e 's/[^ \t]*hdnnp[^ \t]* //g' ../Makefile.package + sed -i -e 's|^PKG_INC =[ \t]*|&-I../../lib/hdnnp/includelink |' ../Makefile.package + sed -i -e 's|^PKG_PATH =[ \t]*|&-L../../lib/hdnnp/liblink |' ../Makefile.package + sed -i -e 's|^PKG_SYSINC =[ \t]*|&$(hdnnp_SYSINC) |' ../Makefile.package + sed -i -e 's|^PKG_SYSLIB =[ \t]*|&$(hdnnp_SYSLIB) |' ../Makefile.package + sed -i -e 's|^PKG_SYSPATH =[ \t]*|&$(hdnnp_SYSPATH) |' ../Makefile.package fi if (test -e ../Makefile.package.settings) then - sed -i -e '/^include.*nnp.*$/d' ../Makefile.package.settings + sed -i -e '/^[ \t]*include.*hdnnp.*$/d' ../Makefile.package.settings # multiline form needed for BSD sed on Macs sed -i -e '4 i \ -include ..\/..\/lib\/nnp\/lib\/Makefile.lammps-extra +include ..\/..\/lib\/hdnnp\/Makefile.lammps ' ../Makefile.package.settings - fi elif (test $1 = 0) then if (test -e ../Makefile.package) then - sed -i -e 's/[^ \t]*nnp[^ \t]* //g' ../Makefile.package + sed -i -e 's/[^ \t]*hdnnp[^ \t]* //g' ../Makefile.package fi if (test -e ../Makefile.package.settings) then - sed -i -e '/^include.*nnp.*$/d' ../Makefile.package.settings + sed -i -e '/^[ \t]*include.*hdnnp.*$/d' ../Makefile.package.settings fi fi diff --git a/src/interface/LAMMPS/src/ML-HDNNP/README b/src/interface/LAMMPS/src/ML-HDNNP/README new file mode 100644 index 000000000..1d9590f7d --- /dev/null +++ b/src/interface/LAMMPS/src/ML-HDNNP/README @@ -0,0 +1,26 @@ +This package implements the "pair_style hdnnp" command which can be used in a +LAMMPS input script. This pair style allows to use pre-trained high-dimensional +neural network potentials[1] via an interface to the n2p2 library +(https://github.com/CompPhysVienna/n2p2)[2]. + +Please see the main documentation for the "pair_style hdnnp" command for further +details on how the pair style is used. An example is provided in the +"examples/PACKAGES/hdnnp" directory of LAMMPS. + +The ML-HDNNP package requires the external library n2p2 which must be +downloaded and compiled before starting the build process of LAMMPS. A +guideline on how to build n2p2 is presented in "lib/hdnnp/README". This package +supports the LAMMPS build process via CMake and traditional makefiles, please +see the LAMMPS manual section on building with external libraries for more +details. + +This package was created by Andreas Singraber, please ask questions/report bugs +on the n2p2 Github issues page (https://github.com/CompPhysVienna/n2p2/issues). + +[1] Behler, J.; Parrinello, M. Generalized Neural-Network Representation of +High-Dimensional Potential-Energy Surfaces. Phys. Rev. Lett. 2007, 98 (14), +146401. https://doi.org/10.1103/PhysRevLett.98.146401 + +[2] Singraber, A.; Behler, J.; Dellago, C. Library-Based +LAMMPS Implementation of High-Dimensional Neural Network Potentials. J. Chem. +Theory Comput. 2019, 15 (3), 1827-1840. https://doi.org/10.1021/acs.jctc.8b00770 diff --git a/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp b/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp new file mode 100644 index 000000000..35b0dd0ae --- /dev/null +++ b/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp @@ -0,0 +1,365 @@ +/* -*- c++ -*- ---------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + https://www.lammps.org/ Sandia National Laboratories + LAMMPS development team: developers@lammps.org + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + This file initially came from n2p2 (https://github.com/CompPhysVienna/n2p2) + Copyright (2018) Andreas Singraber (University of Vienna) + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing author: Andreas Singraber +------------------------------------------------------------------------- */ + +#include "pair_hdnnp.h" + +#include "atom.h" +#include "citeme.h" +#include "comm.h" +#include "error.h" +#include "memory.h" +#include "neigh_list.h" +#include "neighbor.h" +#include "update.h" + +#include + +#include "InterfaceLammps.h" // n2p2 interface header + +using namespace LAMMPS_NS; + +static const char cite_user_hdnnp_package[] = + "ML-HDNNP package: doi:10.1021/acs.jctc.8b00770\n\n" + "@Article{Singraber19,\n" + " author = {Singraber, Andreas and Behler, J{\"o}rg and Dellago, Christoph},\n" + " title = {Library-Based {LAMMPS} Implementation of High-Dimensional\n" + " Neural Network Potentials},\n" + " year = {2019},\n" + " month = mar,\n" + " volume = {15},\n" + " pages = {1827--1840},\n" + " doi = {10.1021/acs.jctc.8b00770},\n" + " journal = {J.~Chem.\\ Theory Comput.},\n" + " number = {3}\n" + "}\n\n"; + +/* ---------------------------------------------------------------------- */ + +PairHDNNP::PairHDNNP(LAMMPS *lmp) : Pair(lmp) +{ + if (lmp->citeme) lmp->citeme->add(cite_user_hdnnp_package); + + single_enable = 0; // 1 if single() routine exists + restartinfo = 0; // 1 if pair style writes restart info + one_coeff = 1; // 1 if allows only one coeff * * call + manybody_flag = 1; // 1 if a manybody potential + unit_convert_flag = + 0; // TODO: Check possible values. value != 0 indicates support for unit conversion. + reinitflag = 0; // 1 if compatible with fix adapt and alike + + interface = new nnp::InterfaceLammps(); +} + +/* ---------------------------------------------------------------------- */ + +PairHDNNP::~PairHDNNP() +{ + delete interface; + memory->destroy(setflag); + memory->destroy(cutsq); +} + +/* ---------------------------------------------------------------------- */ + +void PairHDNNP::compute(int eflag, int vflag) +{ + ev_init(eflag, vflag); + + // Set number of local atoms and add element. + interface->setLocalAtoms(atom->nlocal, atom->type); + // Transfer tags separately. Interface::setLocalTags is overloaded internally + // to work with both -DLAMMPS_SMALLBIG (tagint = int) and -DLAMMPS_BIGBIG + // (tagint = int64_t) + interface->setLocalTags(atom->tag); + + // Transfer local neighbor list to n2p2 interface. + transferNeighborList(); + + // Compute symmetry functions, atomic neural networks and add up energy. + interface->process(); + + // Do all stuff related to extrapolation warnings. + if (showew || showewsum > 0 || maxew >= 0) { handleExtrapolationWarnings(); } + + // Calculate forces of local and ghost atoms. + interface->getForces(atom->f); + + // Add energy contribution to total energy. + if (eflag_global) + ev_tally(0, 0, atom->nlocal, 1, interface->getEnergy(), 0.0, 0.0, 0.0, 0.0, 0.0); + + // Add atomic energy if requested (CAUTION: no physical meaning!). + if (eflag_atom) + for (int i = 0; i < atom->nlocal; ++i) eatom[i] = interface->getAtomicEnergy(i); + + // If virial needed calculate via F dot r. + if (vflag_fdotr) virial_fdotr_compute(); +} + +/* ---------------------------------------------------------------------- + global settings +------------------------------------------------------------------------- */ + +void PairHDNNP::settings(int narg, char **arg) +{ + int iarg = 0; + + if (narg < 1) error->all(FLERR, "Illegal pair_style command"); + + maxCutoffRadius = utils::numeric(FLERR, arg[0], false, lmp); + iarg++; + + // default settings + directory = utils::strdup("hdnnp/"); + showew = true; + showewsum = 0; + maxew = 0; + resetew = false; + cflength = 1.0; + cfenergy = 1.0; + numExtrapolationWarningsTotal = 0; + numExtrapolationWarningsSummary = 0; + + while (iarg < narg) { + // set HDNNP directory + if (strcmp(arg[iarg], "dir") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal pair_style command"); + delete[] directory; + directory = utils::strdup(arg[iarg + 1]); + iarg += 2; + // show extrapolation warnings + } else if (strcmp(arg[iarg], "showew") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal pair_style command"); + showew = utils::logical(FLERR, arg[iarg + 1], false, lmp) == 1; + iarg += 2; + // show extrapolation warning summary + } else if (strcmp(arg[iarg], "showewsum") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal pair_style command"); + showewsum = utils::inumeric(FLERR, arg[iarg + 1], false, lmp); + iarg += 2; + // maximum allowed extrapolation warnings + } else if (strcmp(arg[iarg], "maxew") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal pair_style command"); + maxew = utils::inumeric(FLERR, arg[iarg + 1], false, lmp); + iarg += 2; + // reset extrapolation warning counter + } else if (strcmp(arg[iarg], "resetew") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal pair_style command"); + resetew = utils::logical(FLERR, arg[iarg + 1], false, lmp) == 1; + iarg += 2; + // length unit conversion factor + } else if (strcmp(arg[iarg], "cflength") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal pair_style command"); + cflength = utils::numeric(FLERR, arg[iarg + 1], false, lmp); + iarg += 2; + // energy unit conversion factor + } else if (strcmp(arg[iarg], "cfenergy") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal pair_style command"); + cfenergy = utils::numeric(FLERR, arg[iarg + 1], false, lmp); + iarg += 2; + } else + error->all(FLERR, "Illegal pair_style command"); + } +} + +/* ---------------------------------------------------------------------- + set coeffs for one or more type pairs +------------------------------------------------------------------------- */ + +void PairHDNNP::coeff(int narg, char **arg) +{ + int n = atom->ntypes; + + if (!allocated) allocate(); + + if (narg != 2 + n) error->all(FLERR, "Incorrect args for pair coefficients"); + + if (strcmp(arg[0], "*") != 0 || strcmp(arg[1], "*") != 0) + error->all(FLERR, "Incorrect args for pair coefficients"); + + int *map = new int[n + 1]; + for (int i = 0; i < n; i++) map[i] = 0; + + emap = ""; + for (int i = 2; i < narg; i++) { + if (strcmp(arg[i], "NULL") != 0) { + if (i != 2) emap += ","; + emap += std::to_string(i - 1) + ":" + arg[i]; + map[i - 1] = 1; + } + } + + int count = 0; + for (int i = 1; i <= n; i++) + for (int j = i; j <= n; j++) + if (map[i] > 0 && map[j] > 0) { + setflag[i][j] = 1; + count++; + } + + if (count == 0) error->all(FLERR, "Incorrect args for pair coefficients"); + + delete[] map; +} + +/* ---------------------------------------------------------------------- + init specific to this pair style +------------------------------------------------------------------------- */ + +void PairHDNNP::init_style() +{ + neighbor->add_request(this, NeighConst::REQ_FULL); + + // Return immediately if HDNNP setup is already completed. + if (interface->isInitialized()) return; + + // Activate screen and logfile output only for rank 0. + if (comm->me == 0) { + if (lmp->screen != nullptr) interface->log.registerCFilePointer(&(lmp->screen)); + if (lmp->logfile != nullptr) interface->log.registerCFilePointer(&(lmp->logfile)); + } + + // Initialize interface on all processors. + interface->initialize(directory, emap.c_str(), showew, resetew, showewsum, maxew, cflength, + cfenergy, maxCutoffRadius, atom->ntypes, comm->me); + + // LAMMPS cutoff radius (given via pair_coeff) should not be smaller than + // maximum symmetry function cutoff radius. + if (maxCutoffRadius < interface->getMaxCutoffRadius()) + error->all(FLERR, "Inconsistent cutoff radius"); +} + +/* ---------------------------------------------------------------------- + init for one type pair i,j and corresponding j,i +------------------------------------------------------------------------- */ + +double PairHDNNP::init_one(int, int) +{ + return maxCutoffRadius; +} + +/* ---------------------------------------------------------------------- + allocate all arrays +------------------------------------------------------------------------- */ + +void PairHDNNP::allocate() +{ + allocated = 1; + int n = atom->ntypes; + + memory->create(setflag, n + 1, n + 1, "pair:setflag"); + for (int i = 1; i <= n; i++) + for (int j = i; j <= n; j++) setflag[i][j] = 0; + + memory->create(cutsq, n + 1, n + 1, "pair:cutsq"); // TODO: Is this required? +} + +void PairHDNNP::transferNeighborList() +{ + // Transfer neighbor list to n2p2. + double rc2 = maxCutoffRadius * maxCutoffRadius; + for (int ii = 0; ii < list->inum; ++ii) { + int i = list->ilist[ii]; + for (int jj = 0; jj < list->numneigh[i]; ++jj) { + int j = list->firstneigh[i][jj]; + j &= NEIGHMASK; + double dx = atom->x[i][0] - atom->x[j][0]; + double dy = atom->x[i][1] - atom->x[j][1]; + double dz = atom->x[i][2] - atom->x[j][2]; + double d2 = dx * dx + dy * dy + dz * dz; + if (d2 <= rc2) { interface->addNeighbor(i, j, atom->tag[j], atom->type[j], dx, dy, dz, d2); } + } + } +} + +void PairHDNNP::handleExtrapolationWarnings() +{ + // Get number of extrapolation warnings for local atoms. + long numCurrentEW = (long) interface->getNumExtrapolationWarnings(); + + // Update (or set, resetew == true) total warnings counter. + if (resetew) + numExtrapolationWarningsTotal = numCurrentEW; + else + numExtrapolationWarningsTotal += numCurrentEW; + + // Update warnings summary counter. + if (showewsum > 0) { numExtrapolationWarningsSummary += numCurrentEW; } + + // If requested write extrapolation warnings. + // Requires communication of all symmetry functions statistics entries to + // rank 0. + if (showew > 0) { + // First collect an overview of extrapolation warnings per process. + long *numEWPerProc = nullptr; + if (comm->me == 0) numEWPerProc = new long[comm->nprocs]; + MPI_Gather(&numCurrentEW, 1, MPI_LONG, numEWPerProc, 1, MPI_LONG, 0, world); + + if (comm->me == 0) { + for (int i = 1; i < comm->nprocs; i++) { + if (numEWPerProc[i] > 0) { + long bs = 0; + MPI_Status ms; + // Get buffer size. + MPI_Recv(&bs, 1, MPI_LONG, i, 0, world, &ms); + auto buf = new char[bs]; + // Receive buffer. + MPI_Recv(buf, bs, MPI_BYTE, i, 0, world, &ms); + interface->extractEWBuffer(buf, bs); + delete[] buf; + } + } + interface->writeExtrapolationWarnings(); + } else if (numCurrentEW > 0) { + // Get desired buffer length for all extrapolation warning entries. + long bs = interface->getEWBufferSize(); + // Allocate and fill buffer. + auto buf = new char[bs]; + interface->fillEWBuffer(buf, bs); + // Send buffer size and buffer. + MPI_Send(&bs, 1, MPI_LONG, 0, 0, world); + MPI_Send(buf, bs, MPI_BYTE, 0, 0, world); + delete[] buf; + } + + if (comm->me == 0) delete[] numEWPerProc; + } + + // If requested gather number of warnings to display summary. + if (showewsum > 0 && update->ntimestep % showewsum == 0) { + long globalEW = 0; + // Communicate the sum over all processors to proc 0. + MPI_Reduce(&numExtrapolationWarningsSummary, &globalEW, 1, MPI_LONG, MPI_SUM, 0, world); + // Write to screen or logfile. + if (comm->me == 0) + utils::logmesg(lmp, "### NNP EW SUMMARY ### TS: {:10d} EW {:10d} EWPERSTEP {:10.3e}\n", + update->ntimestep, globalEW, double(globalEW) / showewsum); + // Reset summary counter. + numExtrapolationWarningsSummary = 0; + } + + // Stop if maximum number of extrapolation warnings is exceeded. + if (numExtrapolationWarningsTotal > maxew) { + error->one(FLERR, "Too many extrapolation warnings"); + } + + // Reset internal extrapolation warnings counters. + interface->clearExtrapolationWarnings(); +} diff --git a/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.h b/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.h new file mode 100644 index 000000000..feea0ab6f --- /dev/null +++ b/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.h @@ -0,0 +1,71 @@ +/* -*- c++ -*- ---------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + https://www.lammps.org/ Sandia National Laboratories + LAMMPS development team: developers@lammps.org + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + This file initially came from n2p2 (https://github.com/CompPhysVienna/n2p2) + Copyright (2018) Andreas Singraber (University of Vienna) + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing author: Andreas Singraber +------------------------------------------------------------------------- */ + +#ifdef PAIR_CLASS +// clang-format off +PairStyle(hdnnp,PairHDNNP); +// clang-format on +#else + +#ifndef LMP_PAIR_HDNNP_H +#define LMP_PAIR_HDNNP_H + +#include "pair.h" + +namespace nnp { +class InterfaceLammps; +} + +namespace LAMMPS_NS { + +class PairHDNNP : public Pair { + + public: + PairHDNNP(class LAMMPS *); + ~PairHDNNP() override; + void compute(int, int) override; + void settings(int, char **) override; + void coeff(int, char **) override; + void init_style() override; + double init_one(int, int) override; + + protected: + virtual void allocate(); + void transferNeighborList(); + void handleExtrapolationWarnings(); + + bool showew; + bool resetew; + int showewsum; + int maxew; + long numExtrapolationWarningsTotal; + long numExtrapolationWarningsSummary; + double cflength; + double cfenergy; + double maxCutoffRadius; + char *directory; + std::string emap; + nnp::InterfaceLammps *interface; +}; + +} // namespace LAMMPS_NS + +#endif +#endif diff --git a/src/interface/LAMMPS/src/USER-NNP/README b/src/interface/LAMMPS/src/USER-NNP/README deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/interface/LAMMPS/src/USER-NNP/pair_nnp.cpp b/src/interface/LAMMPS/src/USER-NNP/pair_nnp.cpp deleted file mode 100644 index 4ba757c97..000000000 --- a/src/interface/LAMMPS/src/USER-NNP/pair_nnp.cpp +++ /dev/null @@ -1,414 +0,0 @@ -// Copyright 2018 Andreas Singraber (University of Vienna) -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -#include -#include -#include "pair_nnp.h" -#include "atom.h" -#include "comm.h" -#include "neighbor.h" -#include "neigh_list.h" -#include "neigh_request.h" -#include "memory.h" -#include "error.h" -#include "update.h" -#include "utils.h" - -using namespace LAMMPS_NS; - -/* ---------------------------------------------------------------------- */ - -PairNNP::PairNNP(LAMMPS *lmp) : Pair(lmp) -{ -} - -/* ---------------------------------------------------------------------- - check if allocated, since class can be destructed when incomplete -------------------------------------------------------------------------- */ - -PairNNP::~PairNNP() -{ -} - -/* ---------------------------------------------------------------------- */ - -void PairNNP::compute(int eflag, int vflag) -{ - if(eflag || vflag) ev_setup(eflag,vflag); - else evflag = vflag_fdotr = eflag_global = eflag_atom = 0; - - // Set number of local atoms and add element. - interface.setLocalAtoms(atom->nlocal,atom->type); - // Transfer tags separately. Interface::setLocalTags is overloaded internally - // to work with both -DLAMMPS_SMALLBIG (tagint = int) and -DLAMMPS_BIGBIG - // (tagint = int64_t) - interface.setLocalTags(atom->tag); - - // Transfer local neighbor list to NNP interface. - transferNeighborList(); - - // Compute symmetry functions, atomic neural networks and add up energy. - interface.process(); - - // Do all stuff related to extrapolation warnings. - if(showew == true || showewsum > 0 || maxew >= 0) { - handleExtrapolationWarnings(); - } - - // Calculate forces of local and ghost atoms. - interface.getForces(atom->f); - - // Add energy contribution to total energy. - if (eflag_global) - ev_tally(0,0,atom->nlocal,1,interface.getEnergy(),0.0,0.0,0.0,0.0,0.0); - - // Add atomic energy if requested (CAUTION: no physical meaning!). - if (eflag_atom) - for (int i = 0; i < atom->nlocal; ++i) - eatom[i] = interface.getAtomicEnergy(i); - - // If virial needed calculate via F dot r. - if (vflag_fdotr) virial_fdotr_compute(); -} - -/* ---------------------------------------------------------------------- - global settings -------------------------------------------------------------------------- */ - -void PairNNP::settings(int narg, char **arg) -{ - int iarg = 0; - - if (narg == 0) error->all(FLERR,"Illegal pair_style command"); - - // default settings - int len = strlen("nnp/") + 1; - directory = new char[len]; - strcpy(directory,"nnp/"); - showew = true; - showewsum = 0; - maxew = 0; - resetew = false; - cflength = 1.0; - cfenergy = 1.0; - len = strlen("") + 1; - emap = new char[len]; - strcpy(emap,""); - numExtrapolationWarningsTotal = 0; - numExtrapolationWarningsSummary = 0; - - while(iarg < narg) { - // set NNP directory - if (strcmp(arg[iarg],"dir") == 0) { - if (iarg+2 > narg) - error->all(FLERR,"Illegal pair_style command"); - delete[] directory; - len = strlen(arg[iarg+1]) + 2; - directory = new char[len]; - sprintf(directory, "%s/", arg[iarg+1]); - iarg += 2; - // element mapping - } else if (strcmp(arg[iarg],"emap") == 0) { - if (iarg+2 > narg) - error->all(FLERR,"Illegal pair_style command"); - delete[] emap; - len = strlen(arg[iarg+1]) + 1; - emap = new char[len]; - sprintf(emap, "%s", arg[iarg+1]); - iarg += 2; - // show extrapolation warnings - } else if (strcmp(arg[iarg],"showew") == 0) { - if (iarg+2 > narg) - error->all(FLERR,"Illegal pair_style command"); - if (strcmp(arg[iarg+1],"yes") == 0) - showew = true; - else if (strcmp(arg[iarg+1],"no") == 0) - showew = false; - else - error->all(FLERR,"Illegal pair_style command"); - iarg += 2; - // show extrapolation warning summary - } else if (strcmp(arg[iarg],"showewsum") == 0) { - if (iarg+2 > narg) - error->all(FLERR,"Illegal pair_style command"); - showewsum = utils::inumeric(FLERR,arg[iarg+1],false,lmp); - iarg += 2; - // maximum allowed extrapolation warnings - } else if (strcmp(arg[iarg],"maxew") == 0) { - if (iarg+2 > narg) - error->all(FLERR,"Illegal pair_style command"); - maxew = utils::inumeric(FLERR,arg[iarg+1],false,lmp); - iarg += 2; - // reset extrapolation warning counter - } else if (strcmp(arg[iarg],"resetew") == 0) { - if (iarg+2 > narg) - error->all(FLERR,"Illegal pair_style command"); - if (strcmp(arg[iarg+1],"yes") == 0) - resetew = true; - else if (strcmp(arg[iarg+1],"no") == 0) - resetew = false; - else - error->all(FLERR,"Illegal pair_style command"); - iarg += 2; - // length unit conversion factor - } else if (strcmp(arg[iarg],"cflength") == 0) { - if (iarg+2 > narg) - error->all(FLERR,"Illegal pair_style command"); - cflength = utils::numeric(FLERR,arg[iarg+1],false,lmp); - iarg += 2; - // energy unit conversion factor - } else if (strcmp(arg[iarg],"cfenergy") == 0) { - if (iarg+2 > narg) - error->all(FLERR,"Illegal pair_style command"); - cfenergy = utils::numeric(FLERR,arg[iarg+1],false,lmp); - iarg += 2; - } else error->all(FLERR,"Illegal pair_style command"); - } -} - -/* ---------------------------------------------------------------------- - set coeffs for one or more type pairs -------------------------------------------------------------------------- */ - -void PairNNP::coeff(int narg, char **arg) -{ - if (!allocated) allocate(); - - if (narg != 3) error->all(FLERR,"Incorrect args for pair coefficients"); - - int ilo,ihi,jlo,jhi; - utils::bounds(FLERR,arg[0],1,atom->ntypes,ilo,ihi,error); - utils::bounds(FLERR,arg[1],1,atom->ntypes,jlo,jhi,error); - - maxCutoffRadius = utils::numeric(FLERR,arg[2],false,lmp); - - // TODO: Check how this flag is set. - int count = 0; - for(int i=ilo; i<=ihi; i++) { - for(int j=MAX(jlo,i); j<=jhi; j++) { - setflag[i][j] = 1; - count++; - } - } - - if (count == 0) error->all(FLERR,"Incorrect args for pair coefficients"); -} - -/* ---------------------------------------------------------------------- - init specific to this pair style -------------------------------------------------------------------------- */ - -void PairNNP::init_style() -{ - int irequest = neighbor->request((void *) this); - neighbor->requests[irequest]->pair = 1; - neighbor->requests[irequest]->half = 0; - neighbor->requests[irequest]->full = 1; - - // Return immediately if NNP setup is already completed. - if (interface.isInitialized()) return; - - // Activate screen and logfile output only for rank 0. - if (comm->me == 0) { - if (lmp->screen != NULL) - interface.log.registerCFilePointer(&(lmp->screen)); - if (lmp->logfile != NULL) - interface.log.registerCFilePointer(&(lmp->logfile)); - } - - // Initialize interface on all processors. - interface.initialize(directory, - emap, - showew, - resetew, - showewsum, - maxew, - cflength, - cfenergy, - maxCutoffRadius, - atom->ntypes, - comm->me); - - // LAMMPS cutoff radius (given via pair_coeff) should not be smaller than - // maximum symmetry function cutoff radius. - if (maxCutoffRadius < interface.getMaxCutoffRadius()) - error->all(FLERR,"Inconsistent cutoff radius"); -} - -/* ---------------------------------------------------------------------- - init for one type pair i,j and corresponding j,i -------------------------------------------------------------------------- */ - -double PairNNP::init_one(int i, int j) -{ - // TODO: Check how this actually works for different cutoffs. - return maxCutoffRadius; -} - -/* ---------------------------------------------------------------------- - proc 0 writes to restart file -------------------------------------------------------------------------- */ - -void PairNNP::write_restart(FILE *fp) -{ - return; -} - -/* ---------------------------------------------------------------------- - proc 0 reads from restart file, bcasts -------------------------------------------------------------------------- */ - -void PairNNP::read_restart(FILE *fp) -{ - return; -} - -/* ---------------------------------------------------------------------- - proc 0 writes to restart file -------------------------------------------------------------------------- */ - -void PairNNP::write_restart_settings(FILE *fp) -{ - return; -} - -/* ---------------------------------------------------------------------- - proc 0 reads from restart file, bcasts -------------------------------------------------------------------------- */ - -void PairNNP::read_restart_settings(FILE *fp) -{ - return; -} - -/* ---------------------------------------------------------------------- - allocate all arrays -------------------------------------------------------------------------- */ - -void PairNNP::allocate() -{ - allocated = 1; - int n = atom->ntypes; - - memory->create(setflag,n+1,n+1,"pair:setflag"); - for (int i = 1; i <= n; i++) - for (int j = i; j <= n; j++) - setflag[i][j] = 0; - - memory->create(cutsq,n+1,n+1,"pair:cutsq"); -} - -void PairNNP::transferNeighborList() -{ - // Transfer neighbor list to NNP. - double rc2 = maxCutoffRadius * maxCutoffRadius; - for (int ii = 0; ii < list->inum; ++ii) { - int i = list->ilist[ii]; - for (int jj = 0; jj < list->numneigh[i]; ++jj) { - int j = list->firstneigh[i][jj]; - j &= NEIGHMASK; - double dx = atom->x[i][0] - atom->x[j][0]; - double dy = atom->x[i][1] - atom->x[j][1]; - double dz = atom->x[i][2] - atom->x[j][2]; - double d2 = dx * dx + dy * dy + dz * dz; - if (d2 <= rc2) { - // atom->tag[j] will be implicitly converted to int64_t internally. - interface.addNeighbor(i,j,atom->tag[j],atom->type[j],dx,dy,dz,d2); - } - } - } -} - -void PairNNP::handleExtrapolationWarnings() -{ - // Get number of extrapolation warnings for local atoms. - // TODO: Is the conversion from std::size_t to long ok? - long numCurrentEW = (long)interface.getNumExtrapolationWarnings(); - - // Update (or set, resetew == true) total warnings counter. - if (resetew) numExtrapolationWarningsTotal = numCurrentEW; - else numExtrapolationWarningsTotal += numCurrentEW; - - // Update warnings summary counter. - if(showewsum > 0) { - numExtrapolationWarningsSummary += numCurrentEW; - } - - // If requested write extrapolation warnings. - // Requires communication of all symmetry functions statistics entries to - // rank 0. - if(showew > 0) { - // First collect an overview of extrapolation warnings per process. - long* numEWPerProc = NULL; - if(comm->me == 0) numEWPerProc = new long[comm->nprocs]; - MPI_Gather(&numCurrentEW, 1, MPI_LONG, numEWPerProc, 1, MPI_LONG, 0, world); - - if(comm->me == 0) { - for(int i=1;inprocs;i++) { - if(numEWPerProc[i] > 0) { - long bs = 0; - MPI_Status ms; - // Get buffer size. - MPI_Recv(&bs, 1, MPI_LONG, i, 0, world, &ms); - char* buf = new char[bs]; - // Receive buffer. - MPI_Recv(buf, bs, MPI_BYTE, i, 0, world, &ms); - interface.extractEWBuffer(buf, bs); - delete[] buf; - } - } - interface.writeExtrapolationWarnings(); - } - else if(numCurrentEW > 0) { - // Get desired buffer length for all extrapolation warning entries. - long bs = interface.getEWBufferSize(); - // Allocate and fill buffer. - char* buf = new char[bs]; - interface.fillEWBuffer(buf, bs); - // Send buffer size and buffer. - MPI_Send(&bs, 1, MPI_LONG, 0, 0, world); - MPI_Send(buf, bs, MPI_BYTE, 0, 0, world); - delete[] buf; - } - - if(comm->me == 0) delete[] numEWPerProc; - } - - // If requested gather number of warnings to display summary. - if(showewsum > 0 && update->ntimestep % showewsum == 0) { - long globalEW = 0; - // Communicate the sum over all processors to proc 0. - MPI_Reduce(&numExtrapolationWarningsSummary, - &globalEW, 1, MPI_LONG, MPI_SUM, 0, world); - // Write to screen or logfile. - if(comm->me == 0) { - if(screen) { - fprintf(screen, - "### NNP EW SUMMARY ### TS: %10ld EW %10ld EWPERSTEP %10.3E\n", - update->ntimestep, - globalEW, - double(globalEW) / showewsum); - } - if(logfile) { - fprintf(logfile, - "### NNP EW SUMMARY ### TS: %10ld EW %10ld EWPERSTEP %10.3E\n", - update->ntimestep, - globalEW, - double(globalEW) / showewsum); - } - } - // Reset summary counter. - numExtrapolationWarningsSummary = 0; - } - - // Stop if maximum number of extrapolation warnings is exceeded. - if (numExtrapolationWarningsTotal > maxew) { - error->one(FLERR,"Too many extrapolation warnings"); - } - - // Reset internal extrapolation warnings counters. - interface.clearExtrapolationWarnings(); -} diff --git a/src/interface/LAMMPS/src/USER-NNP/pair_nnp.h b/src/interface/LAMMPS/src/USER-NNP/pair_nnp.h deleted file mode 100644 index 7b9e6ff9d..000000000 --- a/src/interface/LAMMPS/src/USER-NNP/pair_nnp.h +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2018 Andreas Singraber (University of Vienna) -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -#ifdef PAIR_CLASS - -PairStyle(nnp,PairNNP) - -#else - -#ifndef LMP_PAIR_NNP_H -#define LMP_PAIR_NNP_H - -#include "pair.h" -#include "InterfaceLammps.h" - -namespace LAMMPS_NS { - -class PairNNP : public Pair { - - public: - - PairNNP(class LAMMPS *); - virtual ~PairNNP(); - virtual void compute(int, int); - virtual void settings(int, char **); - virtual void coeff(int, char **); - virtual void init_style(); - virtual double init_one(int, int); - virtual void write_restart(FILE *); - virtual void read_restart(FILE *); - virtual void write_restart_settings(FILE *); - virtual void read_restart_settings(FILE *); - - protected: - - virtual void allocate(); - void transferNeighborList(); - void handleExtrapolationWarnings(); - - bool showew; - bool resetew; - int showewsum; - int maxew; - long numExtrapolationWarningsTotal; - long numExtrapolationWarningsSummary; - double cflength; - double cfenergy; - double maxCutoffRadius; - char* directory; - char* emap; - nnp::InterfaceLammps interface; -}; - -} - -#endif -#endif diff --git a/src/interface/makefile b/src/interface/makefile index 7623bc457..295c8175e 100644 --- a/src/interface/makefile +++ b/src/interface/makefile @@ -20,7 +20,10 @@ COMP=gnu MODE=static # LAMMPS version string -LAMMPS_VERSION=stable_29Oct2020 +LAMMPS_VERSION=stable_23Jun2022 +#LAMMPS_VERSION=stable_29Sep2021 +#LAMMPS_VERSION=patch_27May2021 +#LAMMPS_VERSION=patch_2Jul2021 # Include global (project-wide) settings. include $(PROJECT_DIR)/src/makefile.$(COMP) @@ -44,8 +47,10 @@ lammps-nnp: wget https://github.com/lammps/lammps/archive/$(LAMMPS_VERSION).tar.gz; \ fi tar -xzf $(LAMMPS_VERSION).tar.gz && mv lammps-$(LAMMPS_VERSION) lammps-nnp - ln -s $(PROJECT_DIR)/../../ lammps-nnp/lib/nnp - cp -r ./LAMMPS/src/USER-NNP lammps-nnp/src/ + ln -s $(PROJECT_DIR)/../../../include/ lammps-nnp/lib/hdnnp/includelink + ln -s $(PROJECT_DIR)/../../../lib/ lammps-nnp/lib/hdnnp/liblink + ln -s $(PROJECT_DIR)/../../../lib/Makefile.lammps-extra lammps-nnp/lib/hdnnp/Makefile.lammps + cp -r ./LAMMPS/src/ML-HDNNP/* lammps-nnp/src/ML-HDNNP/ @sed -i.bak "s/^CC .*$$/CC = $(PROJECT_MPICC)/" lammps-nnp/src/MAKE/Makefile.mpi @sed -i.bak "s/^CCFLAGS .*$$/CCFLAGS = $(PROJECT_CFLAGS) $(PROJECT_CFLAGS_MPI)/" lammps-nnp/src/MAKE/Makefile.mpi @sed -i.bak "s/^LINK .*$$/LINK = $(PROJECT_MPICC)/" lammps-nnp/src/MAKE/Makefile.mpi @@ -55,7 +60,7 @@ lammps-nnp: sed -i.bak "/^LINKFLAGS =/ s/$$/ $(LAMMPS_DEBUG) $(PROJECT_TEST)/" lammps-nnp/src/MAKE/Makefile.mpi; \ fi @rm lammps-nnp/src/MAKE/Makefile.mpi.bak - cd lammps-nnp/src/ && $(MAKE) yes-user-nnp && $(MAKE) yes-molecule && $(MAKE) mpi + cd lammps-nnp/src/ && $(MAKE) yes-ml-hdnnp && $(MAKE) yes-molecule && $(MAKE) mpi cp lammps-nnp/src/lmp_mpi $(PROJECT_BIN)/ clean-lammps-nnp: From 876a96da9c7146f5d94ec15548969aee254dd996 Mon Sep 17 00:00:00 2001 From: Andreas Singraber Date: Mon, 27 Feb 2023 00:14:45 +0100 Subject: [PATCH 2/8] Updated to LAMMPS version stable_23Jun2022 --- .../Cu2S_PBE/{nnp-data => hdnnp-data}/input.nn | 0 .../{nnp-data => hdnnp-data}/nnp-train.log.0000 | 0 .../Cu2S_PBE/{nnp-data => hdnnp-data}/scaling.data | 0 .../{nnp-data => hdnnp-data}/weights.016.data | 0 .../{nnp-data => hdnnp-data}/weights.029.data | 0 examples/interface-LAMMPS/Cu2S_PBE/md.lmp | 12 ++++++------ .../H2O_RPBE-D3/{nnp-data => hdnnp-data}/input.nn | 0 .../{nnp-data => hdnnp-data}/nnp-train.log.0000 | 0 .../{nnp-data => hdnnp-data}/scaling.data | 0 .../{nnp-data => hdnnp-data}/weights.001.data | 0 .../{nnp-data => hdnnp-data}/weights.008.data | 0 examples/interface-LAMMPS/H2O_RPBE-D3/md.lmp | 12 ++++++------ src/interface/makefile | 3 --- src/libnnpif/makefile | 5 ----- src/makefile | 2 +- 15 files changed, 13 insertions(+), 21 deletions(-) rename examples/interface-LAMMPS/Cu2S_PBE/{nnp-data => hdnnp-data}/input.nn (100%) rename examples/interface-LAMMPS/Cu2S_PBE/{nnp-data => hdnnp-data}/nnp-train.log.0000 (100%) rename examples/interface-LAMMPS/Cu2S_PBE/{nnp-data => hdnnp-data}/scaling.data (100%) rename examples/interface-LAMMPS/Cu2S_PBE/{nnp-data => hdnnp-data}/weights.016.data (100%) rename examples/interface-LAMMPS/Cu2S_PBE/{nnp-data => hdnnp-data}/weights.029.data (100%) rename examples/interface-LAMMPS/H2O_RPBE-D3/{nnp-data => hdnnp-data}/input.nn (100%) rename examples/interface-LAMMPS/H2O_RPBE-D3/{nnp-data => hdnnp-data}/nnp-train.log.0000 (100%) rename examples/interface-LAMMPS/H2O_RPBE-D3/{nnp-data => hdnnp-data}/scaling.data (100%) rename examples/interface-LAMMPS/H2O_RPBE-D3/{nnp-data => hdnnp-data}/weights.001.data (100%) rename examples/interface-LAMMPS/H2O_RPBE-D3/{nnp-data => hdnnp-data}/weights.008.data (100%) diff --git a/examples/interface-LAMMPS/Cu2S_PBE/nnp-data/input.nn b/examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/input.nn similarity index 100% rename from examples/interface-LAMMPS/Cu2S_PBE/nnp-data/input.nn rename to examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/input.nn diff --git a/examples/interface-LAMMPS/Cu2S_PBE/nnp-data/nnp-train.log.0000 b/examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/nnp-train.log.0000 similarity index 100% rename from examples/interface-LAMMPS/Cu2S_PBE/nnp-data/nnp-train.log.0000 rename to examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/nnp-train.log.0000 diff --git a/examples/interface-LAMMPS/Cu2S_PBE/nnp-data/scaling.data b/examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/scaling.data similarity index 100% rename from examples/interface-LAMMPS/Cu2S_PBE/nnp-data/scaling.data rename to examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/scaling.data diff --git a/examples/interface-LAMMPS/Cu2S_PBE/nnp-data/weights.016.data b/examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/weights.016.data similarity index 100% rename from examples/interface-LAMMPS/Cu2S_PBE/nnp-data/weights.016.data rename to examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/weights.016.data diff --git a/examples/interface-LAMMPS/Cu2S_PBE/nnp-data/weights.029.data b/examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/weights.029.data similarity index 100% rename from examples/interface-LAMMPS/Cu2S_PBE/nnp-data/weights.029.data rename to examples/interface-LAMMPS/Cu2S_PBE/hdnnp-data/weights.029.data diff --git a/examples/interface-LAMMPS/Cu2S_PBE/md.lmp b/examples/interface-LAMMPS/Cu2S_PBE/md.lmp index f187d6666..9e53aad3d 100644 --- a/examples/interface-LAMMPS/Cu2S_PBE/md.lmp +++ b/examples/interface-LAMMPS/Cu2S_PBE/md.lmp @@ -1,5 +1,5 @@ ############################################################################### -# MD simulation for NN copper sulfide +# MD simulation for HDNNP copper sulfide ############################################################################### ############################################################################### @@ -12,8 +12,8 @@ variable cfgFile string "cu2s_144_low-chalcocite_Evans-1979_PBE.data" variable numSteps equal 100 variable dt equal 0.001 # NN -variable nnpCutoff equal 6.01 -variable nnpDir string "nnp-data" +variable hdnnpCutoff equal 6.01 +variable hdnnpDir string "hdnnp-data" # Masses variable mass_S equal 32.065 # mass for element 1 (S) (g/mol) variable mass_Cu equal 63.546 # mass for element 2 (Cu) (g/mol) @@ -31,10 +31,10 @@ timestep ${dt} thermo 10 ############################################################################### -# NN +# HDNNP ############################################################################### -pair_style nnp dir ${nnpDir} showew no showewsum 100 resetew no maxew 100 cflength 1.0 cfenergy 1.0 emap "1:S,2:Cu" -pair_coeff * * ${nnpCutoff} +pair_style hdnnp ${hdnnpCutoff} dir ${hdnnpDir} showew no showewsum 100 resetew no maxew 100 cflength 1.0 cfenergy 1.0 +pair_coeff * * S Cu ############################################################################### # INTEGRATOR diff --git a/examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/input.nn b/examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/input.nn similarity index 100% rename from examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/input.nn rename to examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/input.nn diff --git a/examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/nnp-train.log.0000 b/examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/nnp-train.log.0000 similarity index 100% rename from examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/nnp-train.log.0000 rename to examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/nnp-train.log.0000 diff --git a/examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/scaling.data b/examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/scaling.data similarity index 100% rename from examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/scaling.data rename to examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/scaling.data diff --git a/examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/weights.001.data b/examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/weights.001.data similarity index 100% rename from examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/weights.001.data rename to examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/weights.001.data diff --git a/examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/weights.008.data b/examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/weights.008.data similarity index 100% rename from examples/interface-LAMMPS/H2O_RPBE-D3/nnp-data/weights.008.data rename to examples/interface-LAMMPS/H2O_RPBE-D3/hdnnp-data/weights.008.data diff --git a/examples/interface-LAMMPS/H2O_RPBE-D3/md.lmp b/examples/interface-LAMMPS/H2O_RPBE-D3/md.lmp index 50dccc86c..c64960a84 100644 --- a/examples/interface-LAMMPS/H2O_RPBE-D3/md.lmp +++ b/examples/interface-LAMMPS/H2O_RPBE-D3/md.lmp @@ -1,5 +1,5 @@ ############################################################################### -# MD simulation for NN water +# MD simulation for HDNNP water ############################################################################### ############################################################################### @@ -12,8 +12,8 @@ variable cfgFile string "h2o_8640_liquid_NpT_RPBE-D3.data" variable numSteps equal 5 variable dt equal 0.0005 # NN -variable nnpCutoff equal 6.36 -variable nnpDir string "nnp-data" +variable hdnnpCutoff equal 6.36 +variable hdnnpDir string "hdnnp-data" # Masses variable mass_H equal 1.00794 variable mass_O equal 15.9994 @@ -31,10 +31,10 @@ timestep ${dt} thermo 1 ############################################################################### -# NN +# HDNNP ############################################################################### -pair_style nnp dir ${nnpDir} showew no showewsum 10 resetew no maxew 100 cflength 1.8897261328 cfenergy 0.0367493254 emap "1:H,2:O" -pair_coeff * * ${nnpCutoff} +pair_style hdnnp ${hdnnpCutoff} dir ${hdnnpDir} showew no showewsum 5 resetew no maxew 100 cflength 1.8897261328 cfenergy 0.0367493254 +pair_coeff * * H O ############################################################################### # INTEGRATOR diff --git a/src/interface/makefile b/src/interface/makefile index 295c8175e..78a9d2caf 100644 --- a/src/interface/makefile +++ b/src/interface/makefile @@ -21,9 +21,6 @@ MODE=static # LAMMPS version string LAMMPS_VERSION=stable_23Jun2022 -#LAMMPS_VERSION=stable_29Sep2021 -#LAMMPS_VERSION=patch_27May2021 -#LAMMPS_VERSION=patch_2Jul2021 # Include global (project-wide) settings. include $(PROJECT_DIR)/src/makefile.$(COMP) diff --git a/src/libnnpif/makefile b/src/libnnpif/makefile index 2213678c2..1a9dbfb7c 100644 --- a/src/libnnpif/makefile +++ b/src/libnnpif/makefile @@ -92,7 +92,6 @@ OBJ=$(SRC:.cpp=.o) headers \ lammps-prep \ lammps-mf \ - lammps-mf-internal \ lammps-cmake \ clean rebuild @@ -113,10 +112,6 @@ lammps-mf: @sed -i.bak -E "s,(hdnnp_SYSINC =).*,\1 $(LAMMPS_OPTIONS)," $(PROJECT_LIB)/Makefile.lammps-extra @rm $(PROJECT_LIB)/Makefile.lammps-extra.bak -lammps-mf-internal: lammps-mf - sed -i.bak -E "s,hdnnp,nnp," $(PROJECT_LIB)/Makefile.lammps-extra - @rm $(PROJECT_LIB)/Makefile.lammps-extra.bak - lammps-cmake: $(RM) -f $(PROJECT_LIB)/lammps-extra.cmake touch $(PROJECT_LIB)/lammps-extra.cmake diff --git a/src/makefile b/src/makefile index 34499b8b4..380b1be2e 100644 --- a/src/makefile +++ b/src/makefile @@ -163,7 +163,7 @@ $(CLEAN_APP): # lammps-nnp # ############## lammps-nnp: libnnpif - cd libnnpif && $(MAKE) lammps-mf-internal + cd libnnpif && $(MAKE) lammps-mf cd interface && $(MAKE) $@ COMP=$(COMP) MODE=$(MODE) clean-lammps-nnp: From fb7fa9fafd3be940c780b10b974bc864ab068a81 Mon Sep 17 00:00:00 2001 From: Andreas Singraber Date: Mon, 27 Feb 2023 09:39:27 +0100 Subject: [PATCH 3/8] Check CI problems with nnp-train --- .github/workflows/n2p2-ci.yaml | 2 +- test/cpp/makefile | 10 +++++----- test/cpp/test_nnp-train.cpp | 20 ++++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/n2p2-ci.yaml b/.github/workflows/n2p2-ci.yaml index ee660b188..9f3b53368 100644 --- a/.github/workflows/n2p2-ci.yaml +++ b/.github/workflows/n2p2-ci.yaml @@ -71,7 +71,7 @@ jobs: - name: Run C++ tests and coverage run: | - make cpp + make cpp LOG_LEVEL=all cd cpp make coverage rm *.gcda *.gcno diff --git a/test/cpp/makefile b/test/cpp/makefile index 6e1d4cf50..7d4974a11 100644 --- a/test/cpp/makefile +++ b/test/cpp/makefile @@ -80,8 +80,8 @@ clean-coverage: $(RM) -r htmlcov # Grep for "CI-CHECK" for code changes if you want to review output files from non-interactive CI run. -#test_nnp-train: test_nnp-train.cpp -# $(CC) $(CFLAGS) $(DEBUG) $(INCLUDES) $(OPTIONS) -o $@ $< $(LDFLAGS) -# -./$@ --color_output=yes --log_level=$(LOG_LEVEL) -# cat nnp-train.log.0000.* -# cat learning-curve.out.* +test_nnp-train: test_nnp-train.cpp + $(CC) $(CFLAGS) $(DEBUG) $(INCLUDES) $(OPTIONS) -o $@ $< $(LDFLAGS) + -./$@ --color_output=yes --log_level=$(LOG_LEVEL) + cat nnp-train.log.0000.* + cat learning-curve.out.* diff --git a/test/cpp/test_nnp-train.cpp b/test/cpp/test_nnp-train.cpp index 1061f1aa3..1dbc419e5 100644 --- a/test/cpp/test_nnp-train.cpp +++ b/test/cpp/test_nnp-train.cpp @@ -23,16 +23,16 @@ void nnpToolTestBody(Example_nnp_train const example) // Grep for "CI-CHECK" for code changes if you want to review output files // from non-interactive CI run. - //ifstream source("nnp-train.log.0000", ios::binary); - //ofstream dest("../nnp-train.log.0000." + example.name, ios::binary); - //dest << source.rdbuf(); - //source.close(); - //dest.close(); - //ifstream source2("learning-curve.out", ios::binary); - //ofstream dest2("../learning-curve.out." + example.name, ios::binary); - //dest2 << source2.rdbuf(); - //source2.close(); - //dest2.close(); + ifstream source("nnp-train.log.0000", ios::binary); + ofstream dest("../nnp-train.log.0000." + example.name, ios::binary); + dest << source.rdbuf(); + source.close(); + dest.close(); + ifstream source2("learning-curve.out", ios::binary); + ofstream dest2("../learning-curve.out." + example.name, ios::binary); + dest2 << source2.rdbuf(); + source2.close(); + dest2.close(); file.open("learning-curve.out"); BOOST_REQUIRE(file.is_open()); From a301a777427e429001464d150e6dfa1380bd5e9f Mon Sep 17 00:00:00 2001 From: Andreas Singraber Date: Mon, 27 Feb 2023 11:09:16 +0100 Subject: [PATCH 4/8] Updated nnp-train CI test accuracy --- test/cpp/Example_nnp_train.h | 2 +- test/cpp/makefile | 10 +++++----- test/cpp/test_nnp-train.cpp | 20 ++++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/test/cpp/Example_nnp_train.h b/test/cpp/Example_nnp_train.h index 52959cc17..d96fb3492 100644 --- a/test/cpp/Example_nnp_train.h +++ b/test/cpp/Example_nnp_train.h @@ -39,7 +39,7 @@ void BoostDataContainer::setup() e->rmseEnergyTest = 3.57909502E-04; e->rmseForcesTrain = 2.46334317E-02; e->rmseForcesTest = 1.22544616E-02; - e->accuracy = 2.5E-9; + e->accuracy = 6.0E-9; examples.push_back(Example_nnp_train("H2O_RPBE-D3_norm-force")); e = &(examples.back()); diff --git a/test/cpp/makefile b/test/cpp/makefile index 7d4974a11..6e1d4cf50 100644 --- a/test/cpp/makefile +++ b/test/cpp/makefile @@ -80,8 +80,8 @@ clean-coverage: $(RM) -r htmlcov # Grep for "CI-CHECK" for code changes if you want to review output files from non-interactive CI run. -test_nnp-train: test_nnp-train.cpp - $(CC) $(CFLAGS) $(DEBUG) $(INCLUDES) $(OPTIONS) -o $@ $< $(LDFLAGS) - -./$@ --color_output=yes --log_level=$(LOG_LEVEL) - cat nnp-train.log.0000.* - cat learning-curve.out.* +#test_nnp-train: test_nnp-train.cpp +# $(CC) $(CFLAGS) $(DEBUG) $(INCLUDES) $(OPTIONS) -o $@ $< $(LDFLAGS) +# -./$@ --color_output=yes --log_level=$(LOG_LEVEL) +# cat nnp-train.log.0000.* +# cat learning-curve.out.* diff --git a/test/cpp/test_nnp-train.cpp b/test/cpp/test_nnp-train.cpp index 1dbc419e5..1061f1aa3 100644 --- a/test/cpp/test_nnp-train.cpp +++ b/test/cpp/test_nnp-train.cpp @@ -23,16 +23,16 @@ void nnpToolTestBody(Example_nnp_train const example) // Grep for "CI-CHECK" for code changes if you want to review output files // from non-interactive CI run. - ifstream source("nnp-train.log.0000", ios::binary); - ofstream dest("../nnp-train.log.0000." + example.name, ios::binary); - dest << source.rdbuf(); - source.close(); - dest.close(); - ifstream source2("learning-curve.out", ios::binary); - ofstream dest2("../learning-curve.out." + example.name, ios::binary); - dest2 << source2.rdbuf(); - source2.close(); - dest2.close(); + //ifstream source("nnp-train.log.0000", ios::binary); + //ofstream dest("../nnp-train.log.0000." + example.name, ios::binary); + //dest << source.rdbuf(); + //source.close(); + //dest.close(); + //ifstream source2("learning-curve.out", ios::binary); + //ofstream dest2("../learning-curve.out." + example.name, ios::binary); + //dest2 << source2.rdbuf(); + //source2.close(); + //dest2.close(); file.open("learning-curve.out"); BOOST_REQUIRE(file.is_open()); From 365d082fed7d3f4e296ef9731e2e8f48bcf8ba1a Mon Sep 17 00:00:00 2001 From: Andreas Singraber Date: Wed, 20 Nov 2024 00:02:56 +0100 Subject: [PATCH 5/8] Updated to LAMMPS version stable_29Aug2024 Also added development-build warning to makefile. --- .../LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp | 2 +- src/interface/makefile | 2 +- src/makefile | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp b/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp index 35b0dd0ae..cfe1e0bb6 100644 --- a/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp +++ b/src/interface/LAMMPS/src/ML-HDNNP/pair_hdnnp.cpp @@ -200,7 +200,7 @@ void PairHDNNP::coeff(int narg, char **arg) emap = ""; for (int i = 2; i < narg; i++) { if (strcmp(arg[i], "NULL") != 0) { - if (i != 2) emap += ","; + if (!emap.empty()) emap += ","; emap += std::to_string(i - 1) + ":" + arg[i]; map[i - 1] = 1; } diff --git a/src/interface/makefile b/src/interface/makefile index 78a9d2caf..db7704355 100644 --- a/src/interface/makefile +++ b/src/interface/makefile @@ -20,7 +20,7 @@ COMP=gnu MODE=static # LAMMPS version string -LAMMPS_VERSION=stable_23Jun2022 +LAMMPS_VERSION=stable_29Aug2024 # Include global (project-wide) settings. include $(PROJECT_DIR)/src/makefile.$(COMP) diff --git a/src/makefile b/src/makefile index 380b1be2e..e4dd46d4a 100644 --- a/src/makefile +++ b/src/makefile @@ -162,9 +162,32 @@ $(CLEAN_APP): ############## # lammps-nnp # ############## +define LAMMPS_NNP_WARNING +############################################################## +# __ ___ ___ _ _ ___ _ _ ___ # +# \ \ / /_\ | _ \ \| |_ _| \| |/ __| # +# \ \/\/ / _ \| / .` || || .` | (_ | # +# \_/\_/_/ \_\_|_\_|\_|___|_|\_|\___| # +# # +# This is a development build of LAMMPS + n2p2! # +# # +# n2p2's makefile feature to build an automatically patched # +# version of LAMMPS is designed for development purposes and # +# not guarantueed to work. The recommended way to build and # +# link LAMMPS with n2p2 is based on the build system of # +# LAMMPS and described here: # +# # +# https://docs.lammps.org/Build_extras.html#ml-hdnnp # +# # +############################################################## +endef + +export LAMMPS_NNP_WARNING lammps-nnp: libnnpif + @echo "$$LAMMPS_NNP_WARNING" cd libnnpif && $(MAKE) lammps-mf cd interface && $(MAKE) $@ COMP=$(COMP) MODE=$(MODE) + @echo "$$LAMMPS_NNP_WARNING" clean-lammps-nnp: #cd interface && $(MAKE) $@ From 7fa5f64c2fdc3ec12baa0a0ca97f264162b4bd31 Mon Sep 17 00:00:00 2001 From: Andreas Singraber Date: Fri, 22 Nov 2024 00:10:50 +0100 Subject: [PATCH 6/8] Fixed pynnp test, Cython 3 now supports __rmul__ Also unfixed version for coverage package in CI --- .github/workflows/n2p2-ci.yaml | 10 +++++----- src/pynnp/Vec3D.pyx | 7 +++++-- src/pynnp/pynnp_dec.pxd | 2 +- test/python/test_Vec3D.py | 8 ++++---- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.github/workflows/n2p2-ci.yaml b/.github/workflows/n2p2-ci.yaml index 9f3b53368..729d02355 100644 --- a/.github/workflows/n2p2-ci.yaml +++ b/.github/workflows/n2p2-ci.yaml @@ -42,13 +42,13 @@ jobs: pip install breathe # pynnp - pip install cython + pip install cython==3.0.11 # python tests - pip install pytest - pip install pytest-cov - pip install coverage==4.5.4 - + pip install pytest #==7.1.3 + pip install pytest-cov #==4.0.0 + pip install coverage #==7.6.1 + - name: Build n2p2 libraries and applications run: make MODE=test -j4 working-directory: ./src diff --git a/src/pynnp/Vec3D.pyx b/src/pynnp/Vec3D.pyx index 4254fc33d..f0b107a5f 100644 --- a/src/pynnp/Vec3D.pyx +++ b/src/pynnp/Vec3D.pyx @@ -66,12 +66,15 @@ cdef class Vec3D: def __mul__(ls, rs): if isinstance(ls, Vec3D) and isinstance(rs, Vec3D): return ls.mul_Vec3D(rs) - elif isinstance(ls, float) and isinstance(rs, Vec3D): - return rs.mul_lfloat(ls) elif isinstance(ls, Vec3D) and isinstance(rs, float): return ls.mul_rfloat(rs) else: return NotImplemented + def __rmul__(rs, ls): + if isinstance(ls, float) and isinstance(rs, Vec3D): + return rs.mul_lfloat(ls) + else: + return NotImplemented def mul_Vec3D(Vec3D self, Vec3D v): return self.thisptr.mul_vec3d(deref(v.thisptr)) def mul_rfloat(Vec3D self, a): diff --git a/src/pynnp/pynnp_dec.pxd b/src/pynnp/pynnp_dec.pxd index 0cb56bdd7..3e35c3e8b 100644 --- a/src/pynnp/pynnp_dec.pxd +++ b/src/pynnp/pynnp_dec.pxd @@ -116,7 +116,7 @@ cdef extern from "Vec3D.h" namespace "nnp": #double& operator[](std::size_t const index); #double const& operator[](std::size_t const index) const; bool eq "operator=="(const Vec3D& rhs) except + - bool ne "operator!="(const Vec3D& rhs) except +; + bool ne "operator!="(const Vec3D& rhs) except + double norm() except + double norm2() except + Vec3D& normalize() except + diff --git a/test/python/test_Vec3D.py b/test/python/test_Vec3D.py index 2ee235e7f..f8d30ed7c 100644 --- a/test/python/test_Vec3D.py +++ b/test/python/test_Vec3D.py @@ -172,7 +172,7 @@ def test_equal_components(self, v1): assert (v1 == ve) == ( v1[0] == ve[0] and v1[1] == ve[1] and v1[2] == ve[2]), ( "Wrong comparison.") - + class Test___ne__(): def test_wrong_type(self, v1): with pytest.raises(TypeError): @@ -196,12 +196,12 @@ def test_correct_result(self, v1): from math import sqrt assert v1.norm() == pytest.approx(sqrt(v1x * v1x + v1y * v1y + - v1z * v1z)), "Norm incorrect." + v1z * v1z)), "Norm incorrect." class Test_norm2(): def test_correct_result(self, v1): - assert v1.norm2() == pytest.approx(v1x * v1x + - v1y * v1y + + assert v1.norm2() == pytest.approx(v1x * v1x + + v1y * v1y + v1z * v1z), "Norm incorrect." class Test_normalize(): From b0feb573d59d19e94c73a4314aca02315fcb95f4 Mon Sep 17 00:00:00 2001 From: Andreas Singraber Date: Fri, 22 Nov 2024 23:51:10 +0100 Subject: [PATCH 7/8] Updated build docs, changelog --- .github/workflows/n2p2-ci.yaml | 4 +- CHANGELOG.rst | 30 +++ src/doc/doxygen/Doxyfile | 2 +- src/doc/sphinx/source/conf.py | 2 +- src/doc/sphinx/source/index.rst | 3 +- .../sphinx/source/interfaces/if_lammps.rst | 181 +++++++++++------- .../source/{interfaces => old}/pair_nnp.rst | 0 src/doc/sphinx/source/topics/build.rst | 2 +- src/doc/sphinx/source/topics/cfg_file.rst | 34 ++++ src/doc/sphinx/source/topics/training.rst | 3 +- src/doc/sphinx/source/topics/units.rst | 4 +- src/interface/makefile | 50 ++--- src/makefile | 18 +- ...le_lammps_nnp.h => Example_lammps_hdnnp.h} | 18 +- test/cpp/makefile | 2 +- ...t_lammps_nnp.cpp => test_lammps_hdnnp.cpp} | 8 +- 16 files changed, 235 insertions(+), 126 deletions(-) rename src/doc/sphinx/source/{interfaces => old}/pair_nnp.rst (100%) rename test/cpp/{Example_lammps_nnp.h => Example_lammps_hdnnp.h} (72%) rename test/cpp/{test_lammps_nnp.cpp => test_lammps_hdnnp.cpp} (88%) diff --git a/.github/workflows/n2p2-ci.yaml b/.github/workflows/n2p2-ci.yaml index 729d02355..337709a65 100644 --- a/.github/workflows/n2p2-ci.yaml +++ b/.github/workflows/n2p2-ci.yaml @@ -66,12 +66,12 @@ jobs: working-directory: ./src - name: Build LAMMPS with n2p2 support - run: make MODE=test lammps-nnp + run: make MODE=test lammps-hdnnp working-directory: ./src - name: Run C++ tests and coverage run: | - make cpp LOG_LEVEL=all + make cpp cd cpp make coverage rm *.gcda *.gcno diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 372c8c9d6..a8ead8e33 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,36 @@ Changelog All notable changes to this project will be documented in this file. +Version 2.3.0 - 2024-11-?? +-------------------------- + +New features +^^^^^^^^^^^^ + +* Allow manual definition of train/test set (add ``set=train/test`` after ``begin`` in + input.data). +* Additional symmetry function log output, extra column ``spread``. +* Structure ``remap()`` function is now available in pynnp. + +Maintenance +^^^^^^^^^^^ + +* Fixed CI: Cython updated to version 3.0+, added ``__rmul__`` to ``Vec3D`` in + pynnp. + +Important changes +^^^^^^^^^^^^^^^^^ + +* Synchronized LAMMPS developer build with LAMMPS-distributed source: Finally + switched to pair style ``hdnnp`` everywhere. + +Documentation +^^^^^^^^^^^^^ + +* Removed old ``nnp`` pair style description page. +* Updated LAMMPS interface build instructions page. +* Added train/test set split documentation. + Version 2.2.0 - 2022-05-23 -------------------------- diff --git a/src/doc/doxygen/Doxyfile b/src/doc/doxygen/Doxyfile index 544c0b609..9115bf25d 100644 --- a/src/doc/doxygen/Doxyfile +++ b/src/doc/doxygen/Doxyfile @@ -915,7 +915,7 @@ RECURSIVE = YES # Note that relative paths are relative to the directory from which doxygen is # run. -EXCLUDE = ../../interface/lammps-nnp +EXCLUDE = ../../interface/lammps-hdnnp # The EXCLUDE_SYMLINKS tag can be used to select whether or not files or # directories that are symbolic links (a Unix file system feature) are excluded diff --git a/src/doc/sphinx/source/conf.py b/src/doc/sphinx/source/conf.py index 1bacc4875..a84b64e3f 100644 --- a/src/doc/sphinx/source/conf.py +++ b/src/doc/sphinx/source/conf.py @@ -133,7 +133,7 @@ # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. -language = None +language = 'en' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. diff --git a/src/doc/sphinx/source/index.rst b/src/doc/sphinx/source/index.rst index b204caba9..18a79cb7e 100644 --- a/src/doc/sphinx/source/index.rst +++ b/src/doc/sphinx/source/index.rst @@ -92,7 +92,7 @@ components are required: * :ref:`libnnp ` * `libnnpif` -* :ref:`lammps-nnp ` (for LAMMPS) +* :ref:`lammps-hdnnp ` (for LAMMPS) Training a new neural network potential --------------------------------------- @@ -180,7 +180,6 @@ list of keywords is provided :ref:`here `. :caption: Interfaces interfaces/if_lammps - interfaces/pair_nnp interfaces/if_cabanamd .. toctree:: diff --git a/src/doc/sphinx/source/interfaces/if_lammps.rst b/src/doc/sphinx/source/interfaces/if_lammps.rst index ac8600f7b..8dd0d105d 100644 --- a/src/doc/sphinx/source/interfaces/if_lammps.rst +++ b/src/doc/sphinx/source/interfaces/if_lammps.rst @@ -1,7 +1,7 @@ .. _if_lammps: -LAMMPS NNP interface -==================== +LAMMPS interface +================ Purpose ------- @@ -14,104 +14,151 @@ fully supported. Build instructions ------------------ -Automatic build -^^^^^^^^^^^^^^^ +The recommended way to build LAMMPS together with n2p2 is using the LAMMPS +package management and build system. If you are new to LAMMPS, please start with +the basics of the LAMMPS build system which are described +`here `__. Specific instructions for the +LAMMPS + n2p2 build can be found here: -.. important:: +* `LAMMPS Packages with extra build options: ML-HDNNP `__ +* `LAMMPS Packages details: ML-HDNNP `__ - The automatic compilation will only work on Unix-like systems as it relies on - tools such as ``sed`` and ``tar``. +For example, these commands will build LAMMPS with n2p2 support following the +traditional makefile method: -For convenience the main makefile provides the compilation target ``lammps-nnp`` -which will automatically download LAMMPS (version ``stable_3Mar2020`` from the -`GitHub releases page `__) into the -``interface`` directory, unpack it to ``lammps-nnp``, add the necessary n2p2 -files to it and compile the LAMMPS ``mpi`` target. Finally, the binary -``lmp_mpi`` will be copied to the n2p2 ``bin`` directory. Hence, compiling -LAMMPS with NNP support is as easy as typing +.. code-block:: shell -.. code-block:: none + cd /path/to/lammps/src - make lammps-nnp + # Enable ML-HDNNP package: + make yes-ml-hdnnp -in the n2p2 ``src`` directory. Uninstall with ``make clean-lammps-nnp``. + # Automatically download and build n2p2: + make lib-hdnnp args="-b" + # Alternatively, use existing n2p2 installation: + # (follow instructions in /lib/hdnnp/README) + # make lib-hdnnp args="-p path/to/n2p2 -.. note:: + # Build LAMMPS for desired target, for example: + make mpi -j - Before compiling the LAMMPS makefile (``src/MAKE/Makefile.mpi``) will be altered to - use the compiler and flags from the n2p2 target makefile (default: - ``makefile.gnu``). +Alternatively, the CMake build commands may look like this: -If this procedure fails or you prefer to add NNP support to an existing LAMMPS -installation somewhere else in your system please follow the manual build -instructions below. +.. code-block:: shell -Manual build -^^^^^^^^^^^^ + cd path/to/lammps + mkdir build; cd build + cmake ../cmake -D PKG_ML-HDNNP=yes -D DOWNLOAD_N2P2=yes + cmake --build . -j -To build LAMMPS with support for neural network potentials follow these steps: -First, build the required libraries: +Usage +----- -.. code-block:: none +The neural network potential method is introduced in the context of a pair style +named ``hdnnp``. LAMMPS comes with a large collection of these pair styles, e.g. for +a LJ or Tersoff potential, look +`here `__ for more information. The +setup of a ``hdnnp`` pair style is done by issuing two commands: ``pair_style`` and +``pair_coeff``. See the `pair style hdnnp +`__ documentation page for a detailed +description. - cd src - make libnnpif +Development build +----------------- -For dynamic linking add the argument ``MODE=shared``. +.. danger:: -.. note:: + This is not the recommended way to build LAMMPS + n2p2, please consider using + the LAMMPS package and build system as described above! The development build + usually does not offer any benefit. Instead, it may include unstable or + untested additions, or may even not compile at all. - If dynamic linking (\ ``make libnnpif MODE=shared``\ ) is used, you need to make the NNP - libraries visibile in your system, e.g. add this line in your ``.bashrc``\ : +For the development build the main makefile provides the compilation target +``lammps-hdnnp`` which will automatically download LAMMPS (from the `GitHub +releases page `__) into the +``interface`` directory, unpack it to ``lammps-hdnnp``, add the necessary n2p2 +files to it and compile the LAMMPS ``mpi`` target. Finally, the binary +``lmp_mpi`` will be copied to the n2p2 ``bin`` directory. Hence, compiling +LAMMPS with NNP support is as easy as typing -.. code-block:: none +The LAMMPS version which will be downloaded is determined in +``src/interface/makefile``. Close to the top there is a variable ``LAMMPS_VERSION`` +which contains the LAMMPS version string, e.g. ``stable_29Aug2024``. This +default value can be changed in the makefile or also on the command-line if +desired: - export LD_LIBRARY_PATH=/lib:${LD_LIBRARY_PATH} +.. code-block:: none -Then change to the LAMMPS root directory and link the repository root folder to -the ``lib`` subdirectory: + make LAMMPS_VERSION=stable_2Aug2023 lammps-hdnnp -j -.. code-block:: bash +The development build will perform the following actions: - cd / - ln -s lib/nnp +1. If not present, download the LAMMPS source tarball from GitHub. +2. Unpack the LAMMPS source code. +3. Link the necessary folders and files to the ``lammps-hdnnp/lib/hdnnp`` + directory. +4. Copy the contents of n2p2's ``src/LAMMPS/src/ML-HDNNP`` folder to the LAMMPS + ML-HDNNP package folder. +5. Modify the LAMMPS ``mpi`` target makefile to contain compiler flags from + n2p2. +6. Enable the ML-HDNNP package with ``make yes-ml-hdnnp``. +7. Compile the LAMMPS ``mpi`` target. +8. Copy the executable ``lmp_mpi`` to n2p2's ``bin`` directory. -.. danger:: +Since in step 4 the LAMMPS-distributed ``hdnnp`` pair style files are replaced +with the ones located in n2p2, this build may not be identical to the +recommended way of installing LAMMPS + n2p2 described above. - The link should be named ``nnp``\ , NOT ``n2p2``\ ! +.. note:: -Next, copy the USER-NNP package to the LAMMPS source directory: + The automatic compilation will only work on Unix-like systems as it relies on + tools such as ``wget``, ``sed`` and ``tar``. -.. code-block:: bash +To remove the developer build run ``make clean-lammps-hdnnp``. However, this +will not delete the downloaded LAMMPS tarballs in the ``src/interface`` +directory so they can be reused for the next build. Please delete them +manually if desired. - cp -r src/interface/LAMMPS/src/USER-NNP /src +.. important:: -Finally activate the NNP package in LAMMPS: + During development be aware that the **copied** files in the LAMMPS ``src`` + or ``src/ML-HDNNP`` directories are not managed by git. Be sure to transfer + modifications back to n2p2's ``src/interface/LAMMPS/src/ML-HDNNP`` directory + before cleaning! -.. code-block:: bash +Note on older development builds +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - cd /src - make yes-user-nnp +Previous versions of n2p2 (before 2.3.0) had a similar automatic development +build with **older** pair style source code files (pre-dating the `merge request +`__ for the official LAMMPS repo). +The most obvious difference is that the pair style name was ``nnp`` and not +``hdnnp``. This version of the LAMMPS interface is not supported anymore. For +future installations please always try to use the recommended build method +described at the top of this page. -Now, you can compile LAMMPS for your target as usual: +LAMMPS script files using the ``nnp`` pair style require three minor modifications +to be converted to the official ``hdnpp`` version: -.. code-block:: bash +1. Replace the pair style name. +2. The element mapping previously entered via the ``emap`` keyword is now listed + in the ``pair_coeff`` line in the usual LAMMPS style. +3. The cutoff radius is moved from the ``pair_coeff`` line to the first + (mandatory) argument of the ``pair_style`` line. - make +Here is an example: -.. note:: +.. code-block:: shell - If you want to compile a serial version of LAMMPS with neural network potential - support, the use of MPI needs to be deactivated for ``libnnpif``. Just enable the - ``-DNOMPI`` option in the settings makefile of your choice, e.g. ``makefile.gnu``. + # Old nnp pair style lines: + pair_style nnp showew no showewsum 100 maxew 1000 resetew yes emap "1:H,2:O" + pair_coeff * * 6.01 -Usage ------ + # Corresponding hdnnp pair style lines: + pair_style hdnnp 6.01 showew no showewsum 100 maxew 1000 resetew yes + pair_coeff * * H O -The neural network potential method is introduced in the context of a pair style -named ``nnp``. LAMMPS comes with a large collection of these pair styles, e.g. for -a LJ or Tersoff potential, look -`here `_ for more information. The -setup of a ``nnp`` pair style is done by issuing two commands: ``pair_style`` and -``pair_coeff``. See :ref:`this page ` for a detailed -description. +Please consult the `pair style hdnnp +`__ documentation page for details. The +documentation for the old ``nnp`` pair style is kept in the n2p2 repository +here: ``src/doc/sphinx/source/old/pair_nnp.rst``. diff --git a/src/doc/sphinx/source/interfaces/pair_nnp.rst b/src/doc/sphinx/source/old/pair_nnp.rst similarity index 100% rename from src/doc/sphinx/source/interfaces/pair_nnp.rst rename to src/doc/sphinx/source/old/pair_nnp.rst diff --git a/src/doc/sphinx/source/topics/build.rst b/src/doc/sphinx/source/topics/build.rst index 4d6016f4d..a84b89038 100644 --- a/src/doc/sphinx/source/topics/build.rst +++ b/src/doc/sphinx/source/topics/build.rst @@ -51,7 +51,7 @@ information). +---------------------------------+------------------------------+------------------------------------------------------+ | nnp-train | libnnptrain | Train a neural network potential. | +---------------------------------+------------------------------+------------------------------------------------------+ -| :ref:`lammps-nnp ` | libnnpif | Pair style `nnp` for LAMMPS | +| :ref:`lammps-hdnnp ` | libnnpif | Pair style ``hdnnp`` for LAMMPS | +---------------------------------+------------------------------+------------------------------------------------------+ | pynnp | libnnp, python, cython | Python interface to NNP library. | +---------------------------------+------------------------------+------------------------------------------------------+ diff --git a/src/doc/sphinx/source/topics/cfg_file.rst b/src/doc/sphinx/source/topics/cfg_file.rst index 9b8086377..e9833d439 100644 --- a/src/doc/sphinx/source/topics/cfg_file.rst +++ b/src/doc/sphinx/source/topics/cfg_file.rst @@ -96,3 +96,37 @@ configuration file with 3 structures, 2 periodic and 1 non-periodic: energy 543.210 charge 0.0 end + +Manual train/test set definition +-------------------------------- + +Since n2p2 version 2.3.0 it is possible to manually assign structures to the +training or test set already in the ``input.data`` file. Just add the string +``set=train`` or ``set=test`` right after the structure start marker ``begin``. +For example: + +.. code-block:: none + + begin set=train + lattice 1.0 0.0 0.0 + lattice 0.0 1.0 0.0 + lattice 0.0 0.0 1.0 + atom 0.1 0.2 0.3 Cd -0.1 0.0 -0.1 -0.3 0.1 + atom 0.1 0.1 0.4 S 0.1 0.0 1.1 -0.2 0.4 + energy 123.456 + charge 0.0 + end + begin set=test + lattice 1.0 0.0 0.0 + lattice 0.0 1.0 0.0 + lattice 0.0 0.0 1.0 + atom 0.1 0.2 0.3 Cd -0.1 0.0 -0.1 -0.3 0.1 + atom 0.1 0.1 0.4 S 0.1 0.0 1.1 -0.2 0.4 + energy 123.456 + charge 0.0 + end + +This assignment takes precedence over the usual random split performed by +``nnp-train``. Structures which are not labelled with the ``set=`` method will +still be assigned randomly to training/test sets according to the +``test_fraction`` keyword. diff --git a/src/doc/sphinx/source/topics/training.rst b/src/doc/sphinx/source/topics/training.rst index 547af9299..b251c355f 100644 --- a/src/doc/sphinx/source/topics/training.rst +++ b/src/doc/sphinx/source/topics/training.rst @@ -114,8 +114,7 @@ Try the potential by predicting energies and forces for a new configuration: Collect the files from training (``input.nn``, ``scaling.data`` and ``weights.???``) in a folder together with a single configuration (named again ``input.data``) and run the tool ``nnp-predict``. Alternatively, try to run a MD -simulation with LAMMPS (see setup instructions :ref:`here ` and -:ref:`here `). +simulation with LAMMPS (see setup instructions :ref:`here `). Please also have a look at the ``examples`` directory which provides working example setups for each tool. If there are problems don't hesitate to ask diff --git a/src/doc/sphinx/source/topics/units.rst b/src/doc/sphinx/source/topics/units.rst index 0daea3a80..1320e22bf 100644 --- a/src/doc/sphinx/source/topics/units.rst +++ b/src/doc/sphinx/source/topics/units.rst @@ -17,8 +17,8 @@ e.g. :math:`\eta, r_c`, need to be given in length units). potential is used to drive an MD simulation in LAMMPS. If the LAMMPS units (see command `units `__) are not matching with those used during NNP training, appropriate conversion factors - need to be provided. See the :ref:`pair_style nnp reference ` for - further details. + need to be provided. See the `pair_style hdnnp reference + `__ for further details. Normalizing the data set: "internal" units ------------------------------------------ diff --git a/src/interface/makefile b/src/interface/makefile index db7704355..b52e54377 100644 --- a/src/interface/makefile +++ b/src/interface/makefile @@ -29,42 +29,42 @@ include $(PROJECT_DIR)/src/makefile.$(COMP) ############################################################################### # RULES ############################################################################### -.PHONY: all lammps-nnp clean-lammps-nnp clean-lammps-nnp-keep-file +.PHONY: all lammps-hdnnp clean-lammps-hdnnp clean-lammps-hdnnp-keep-file -all: lammps-nnp +all: lammps-hdnnp -clean: clean-lammps-nnp +clean: clean-lammps-hdnnp -############## -# lammps-nnp # -############## +################ +# lammps-hdnnp # +################ LAMMPS_DEBUG := $(filter-out -pedantic-errors,$(PROJECT_DEBUG)) -lammps-nnp: +lammps-hdnnp: if [ ! -e $(LAMMPS_VERSION).tar.gz ]; then \ wget https://github.com/lammps/lammps/archive/$(LAMMPS_VERSION).tar.gz; \ fi - tar -xzf $(LAMMPS_VERSION).tar.gz && mv lammps-$(LAMMPS_VERSION) lammps-nnp - ln -s $(PROJECT_DIR)/../../../include/ lammps-nnp/lib/hdnnp/includelink - ln -s $(PROJECT_DIR)/../../../lib/ lammps-nnp/lib/hdnnp/liblink - ln -s $(PROJECT_DIR)/../../../lib/Makefile.lammps-extra lammps-nnp/lib/hdnnp/Makefile.lammps - cp -r ./LAMMPS/src/ML-HDNNP/* lammps-nnp/src/ML-HDNNP/ - @sed -i.bak "s/^CC .*$$/CC = $(PROJECT_MPICC)/" lammps-nnp/src/MAKE/Makefile.mpi - @sed -i.bak "s/^CCFLAGS .*$$/CCFLAGS = $(PROJECT_CFLAGS) $(PROJECT_CFLAGS_MPI)/" lammps-nnp/src/MAKE/Makefile.mpi - @sed -i.bak "s/^LINK .*$$/LINK = $(PROJECT_MPICC)/" lammps-nnp/src/MAKE/Makefile.mpi - @sed -i.bak "s/^LINKFLAGS .*$$/LINKFLAGS = $(PROJECT_CFLAGS) $(PROJECT_CFLAGS_MPI)/" lammps-nnp/src/MAKE/Makefile.mpi + tar -xzf $(LAMMPS_VERSION).tar.gz && mv lammps-$(LAMMPS_VERSION) lammps-hdnnp + ln -s $(PROJECT_DIR)/../../../include/ lammps-hdnnp/lib/hdnnp/includelink + ln -s $(PROJECT_DIR)/../../../lib/ lammps-hdnnp/lib/hdnnp/liblink + ln -s $(PROJECT_DIR)/../../../lib/Makefile.lammps-extra lammps-hdnnp/lib/hdnnp/Makefile.lammps + cp -r ./LAMMPS/src/ML-HDNNP/* lammps-hdnnp/src/ML-HDNNP/ + @sed -i.bak "s/^CC .*$$/CC = $(PROJECT_MPICC)/" lammps-hdnnp/src/MAKE/Makefile.mpi + @sed -i.bak "s/^CCFLAGS .*$$/CCFLAGS = $(PROJECT_CFLAGS) $(PROJECT_CFLAGS_MPI)/" lammps-hdnnp/src/MAKE/Makefile.mpi + @sed -i.bak "s/^LINK .*$$/LINK = $(PROJECT_MPICC)/" lammps-hdnnp/src/MAKE/Makefile.mpi + @sed -i.bak "s/^LINKFLAGS .*$$/LINKFLAGS = $(PROJECT_CFLAGS) $(PROJECT_CFLAGS_MPI)/" lammps-hdnnp/src/MAKE/Makefile.mpi if [ "$(MODE)" = "test" ]; then \ - sed -i.bak "/^CCFLAGS =/ s/$$/ $(LAMMPS_DEBUG) $(PROJECT_TEST)/" lammps-nnp/src/MAKE/Makefile.mpi; \ - sed -i.bak "/^LINKFLAGS =/ s/$$/ $(LAMMPS_DEBUG) $(PROJECT_TEST)/" lammps-nnp/src/MAKE/Makefile.mpi; \ + sed -i.bak "/^CCFLAGS =/ s/$$/ $(LAMMPS_DEBUG) $(PROJECT_TEST)/" lammps-hdnnp/src/MAKE/Makefile.mpi; \ + sed -i.bak "/^LINKFLAGS =/ s/$$/ $(LAMMPS_DEBUG) $(PROJECT_TEST)/" lammps-hdnnp/src/MAKE/Makefile.mpi; \ fi - @rm lammps-nnp/src/MAKE/Makefile.mpi.bak - cd lammps-nnp/src/ && $(MAKE) yes-ml-hdnnp && $(MAKE) yes-molecule && $(MAKE) mpi - cp lammps-nnp/src/lmp_mpi $(PROJECT_BIN)/ + @rm lammps-hdnnp/src/MAKE/Makefile.mpi.bak + cd lammps-hdnnp/src/ && $(MAKE) yes-ml-hdnnp && $(MAKE) yes-molecule && $(MAKE) mpi + cp lammps-hdnnp/src/lmp_mpi $(PROJECT_BIN)/ -clean-lammps-nnp: - $(RM) -r lammps-nnp +clean-lammps-hdnnp: + $(RM) -r lammps-hdnnp $(RM) $(LAMMPS_VERSION).tar.gz $(RM) $(PROJECT_BIN)/lmp_mpi -clean-lammps-nnp-keep-file: - $(RM) -r lammps-nnp +clean-lammps-hdnnp-keep-file: + $(RM) -r lammps-hdnnp $(RM) $(PROJECT_BIN)/lmp_mpi diff --git a/src/makefile b/src/makefile index e4dd46d4a..fba29f8a0 100644 --- a/src/makefile +++ b/src/makefile @@ -61,7 +61,7 @@ CLEAN_APP=$(patsubst %, clean-%, $(APP)) libnnp \ libnnptrain \ libnnpif \ - lammps-nnp \ + lammps-hdnnp \ pynnp \ clean-doc \ clean-doc-sphinx \ @@ -70,7 +70,7 @@ CLEAN_APP=$(patsubst %, clean-%, $(APP)) clean-libnnp \ clean-libnnptrain \ clean-libnnpif \ - clean-lammps-nnp \ + clean-lammps-hdnnp \ clean-pynnp \ install \ uninstall @@ -84,7 +84,7 @@ clean: clean-libnnp \ clean-libnnptrain \ clean-libnnpif \ clean-app \ - clean-lammps-nnp \ + clean-lammps-hdnnp \ clean-pynnp clean-all: clean \ @@ -159,9 +159,9 @@ $(APP_LIBNNPTRAIN): libnnptrain $(CLEAN_APP): cd application && $(MAKE) $@ -############## -# lammps-nnp # -############## +################ +# lammps-hdnnp # +################ define LAMMPS_NNP_WARNING ############################################################## # __ ___ ___ _ _ ___ _ _ ___ # @@ -183,15 +183,15 @@ define LAMMPS_NNP_WARNING endef export LAMMPS_NNP_WARNING -lammps-nnp: libnnpif +lammps-hdnnp: libnnpif @echo "$$LAMMPS_NNP_WARNING" cd libnnpif && $(MAKE) lammps-mf cd interface && $(MAKE) $@ COMP=$(COMP) MODE=$(MODE) @echo "$$LAMMPS_NNP_WARNING" -clean-lammps-nnp: +clean-lammps-hdnnp: #cd interface && $(MAKE) $@ - cd interface && $(MAKE) clean-lammps-nnp-keep-file + cd interface && $(MAKE) clean-lammps-hdnnp-keep-file ######### # pynnp # diff --git a/test/cpp/Example_lammps_nnp.h b/test/cpp/Example_lammps_hdnnp.h similarity index 72% rename from test/cpp/Example_lammps_nnp.h rename to test/cpp/Example_lammps_hdnnp.h index f22851a2e..454ea97f6 100644 --- a/test/cpp/Example_lammps_nnp.h +++ b/test/cpp/Example_lammps_hdnnp.h @@ -1,10 +1,10 @@ -#ifndef EXAMPLE_LAMMPS_NNP_H -#define EXAMPLE_LAMMPS_NNP_H +#ifndef EXAMPLE_LAMMPS_HDNNP_H +#define EXAMPLE_LAMMPS_HDNNP_H #include "Example.h" #include "BoostDataContainer.h" -struct Example_lammps_nnp : public Example +struct Example_lammps_hdnnp : public Example { std::string tool; std::string command; @@ -16,12 +16,12 @@ struct Example_lammps_nnp : public Example double potentialEnergy; double totalEnergy; - Example_lammps_nnp(std::string name) : + Example_lammps_hdnnp(std::string name) : pathBin("../../../bin"), pathData("../../examples/interface-LAMMPS/") { this->name = name; - this->tool = "lmp_mpi"; + this->tool = "lmp_mpi"; this->description = std::string("Test example \"") + this->name + "\" with tool \"" @@ -32,18 +32,18 @@ struct Example_lammps_nnp : public Example }; template<> -void BoostDataContainer::setup() +void BoostDataContainer::setup() { - Example_lammps_nnp* e = nullptr; + Example_lammps_hdnnp* e = nullptr; - examples.push_back(Example_lammps_nnp("H2O_RPBE-D3")); + examples.push_back(Example_lammps_hdnnp("H2O_RPBE-D3")); e = &(examples.back()); e->args = "-in md.lmp "; e->lastTimeStep = 5; e->potentialEnergy = -6000559.4; e->totalEnergy = -6000220.2; - examples.push_back(Example_lammps_nnp("Cu2S_PBE")); + examples.push_back(Example_lammps_hdnnp("Cu2S_PBE")); e = &(examples.back()); e->args = "-in md.lmp "; e->lastTimeStep = 100; diff --git a/test/cpp/makefile b/test/cpp/makefile index 6e1d4cf50..b692545e2 100644 --- a/test/cpp/makefile +++ b/test/cpp/makefile @@ -44,7 +44,7 @@ LDFLAGS=$(PROJECT_LIB)/libnnp.a $(PROJECT_LIB)/libnnptrain.a -lboost_system -lbo # DEFINE SOURCES ############################################################################### SRC=$(wildcard test_*.cpp) -SRC_NOIF=$(filter-out test_lammps_nnp.cpp, $(SRC)) +SRC_NOIF=$(filter-out test_lammps_hdnnp.cpp, $(SRC)) EXE=$(SRC:%.cpp=%) EXE_NOIF=$(SRC_NOIF:%.cpp=%) diff --git a/test/cpp/test_lammps_nnp.cpp b/test/cpp/test_lammps_hdnnp.cpp similarity index 88% rename from test/cpp/test_lammps_nnp.cpp rename to test/cpp/test_lammps_hdnnp.cpp index 23ef18282..d4769e53c 100644 --- a/test/cpp/test_lammps_nnp.cpp +++ b/test/cpp/test_lammps_hdnnp.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_DYN_LINK -#define BOOST_TEST_MODULE lammps-nnp -#include "Example_lammps_nnp.h" +#define BOOST_TEST_MODULE lammps-hdnnp +#include "Example_lammps_hdnnp.h" #include "nnp_test.h" #include "utility.h" @@ -14,11 +14,11 @@ using namespace nnp; double const accuracy = 10.0 * numeric_limits::epsilon(); -BoostDataContainer container; +BoostDataContainer container; NNP_TOOL_TEST_CASE() -void nnpToolTestBody(Example_lammps_nnp const example) +void nnpToolTestBody(Example_lammps_hdnnp const example) { bool timeStepFound = false; bool startReading = false; From e51667abf784bb9a6fd659857b6a9a878fdc6a90 Mon Sep 17 00:00:00 2001 From: Andreas Singraber Date: Sat, 23 Nov 2024 00:43:19 +0100 Subject: [PATCH 8/8] v2.3.0 --- CHANGELOG.rst | 2 +- src/doc/sphinx/source/conf.py | 2 +- src/libnnp/version.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a8ead8e33..482383a7a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,7 +3,7 @@ Changelog All notable changes to this project will be documented in this file. -Version 2.3.0 - 2024-11-?? +Version 2.3.0 - 2024-11-23 -------------------------- New features diff --git a/src/doc/sphinx/source/conf.py b/src/doc/sphinx/source/conf.py index a84b64e3f..241d74869 100644 --- a/src/doc/sphinx/source/conf.py +++ b/src/doc/sphinx/source/conf.py @@ -111,7 +111,7 @@ # General information about the project. project = u'n2p2 - A neural network potential package' -copyright = u'2022, Andreas Singraber' +copyright = u'2024, Andreas Singraber' author = u'Andreas Singraber' # The version info for the project you're documenting, acts as replacement for diff --git a/src/libnnp/version.h b/src/libnnp/version.h index 31bbeabbd..0f918ef1e 100644 --- a/src/libnnp/version.h +++ b/src/libnnp/version.h @@ -17,7 +17,7 @@ #ifndef VERSION_H #define VERSION_H -#define N2P2_VERSION "v2.2.0" +#define N2P2_VERSION "v2.3.0" #define N2P2_GIT_VERSION "" #define N2P2_GIT_REV "" #define N2P2_GIT_BRANCH ""