lammpstrj.hpp is a single-header C++14 library for parsing LAMMPS trajectory (*.lammpstrj) files.
It provides a simple and efficient interface to access system information and analyze simulation frames.
- Header-only, single-file library
- Reads basic simulation box info (box size and number of atoms)
- Provides a per-frame callback interface for analysis
- Written in standard C++14
- Lightweight and dependency-free
- MIT License
This library is provided as a single-header C++14 library located in:
include/lammpstrj/lammpstrj.hppYou can add this repository as a Git submodule to your own project:
git submodule add https://github.com/kaityo256/lammpstrj-parser external/lammpstrj-parserThen, add the include directory to your compiler’s include path. For example, with g++:
g++ -Iexternal/lammpstrj-parser/include ...Now you can include the header as:
#include <lammpstrj/lammpstrj.hpp>This approach keeps your dependencies organized and makes updates easy via submodules.
You can obtain the number of atoms and the box size from the first frame using:
auto si = lammpstrj::read_info("trajectory.lammpstrj");
printf("(LX, LY, LZ) = (%f, %f, %f)\n", si->LX, si->LY, si->LZ);
printf("N = %d\n", si->atoms);The return type is a std::unique_ptr to the following structure:
struct SystemInfo {
int atoms; // Number of atoms
double LX, LY, LZ; // Size of the simulation box
};You can pass a lambda or function to for_each_frame to analyze each frame:
using FrameCallback = std::function<void(const std::unique_ptr<lammpstrj::SystemInfo>&, std::vector<lammpstrj::Atom>&)>;
void lammpstrj::for_each_frame(const std::string& filename, FrameCallback callback);Example: Compute kinetic energy per frame (proportional to temperature):
lammpstrj::FrameCallback calc_temperature = [](const auto& si, const auto& atoms) {
static int index = 0;
double e = 0.0;
for (auto& a : atoms) {
e += a.vx * a.vx + a.vy * a.vy + a.vz * a.vz;
}
e /= static_cast<double>(si->atoms);
e /= 3.0;
printf("%d %f\n", index * 100, e);
index++;
};
lammpstrj::for_each_frame("trajectory.lammpstrj", calc_temperature);C++14 or later
This library is licensed under the MIT License. See theLICENSE file for details.