Read the last line of a file

Dear users,

I want to read the last line of a data file and assign it to a variable. As the file updates every step, I want to do the operation every step too. As I know, the command “variable file…” can assign the first line of the file to the variable and command “next” can read the next line of the file. But I still can’t get what I want. Any advice will be helpful!

Best wishes

Have a look at the python command and python style variables. While the LAMMPS script language is rather limited in its functionality, there are many things that can be done with python.

Thanks for your advice. But I still wondered how to install python package in the windows version of LAMMPS.

Best wishes

Perhaps some context would be helpful here:

Please note that when you are running LAMMPS on Windows, you are an exception and thus your situation is not what is normally expected. The typical use case of LAMMPS is on HPC clusters, most of which these days are running the Linux operating system, or on Linux workstation. On those machines it is generally easy to build a custom LAMMPS executable, since development tools are easily available on those machines (including MPI support). Traditionally, people doing scientific computing were mostly using Unix-like operating systems since the early days and customizing and compiling MD simulation software was the normal case. A lot of the LAMMPS documentation is (still) based on this assumption (often for historical reasons, LAMMPS as a package exists for over 25 years). In recent years Linux distributions with ever expanding package repositories and deployment systems like pip or conda make it easier to install and maintain a software stack without having to compile it.

On Windows the situation is more complex since a) Windows users are rarely familiar with building applications from source code, b) LAMMPS is not ported to the “native” Compiler toolchain (which is a commercial offering to boot, although a “community edition” has been made available at no cost recently), c) there is no officially supported and consistent MPI library available.

While the LAMMPS developers do not offer pre-compiled binaries for Linux or macOS or other Unix-like operating systems (since there are far too many variants in use), we do have a repository for pre-compiled Windows installer packages: LAMMPS Windows Installer Repository

Please note that there are two ways to have LAMMPS interact with Python: you can call LAMMPS from python (using the LAMMPS Python module) and you can call Python from LAMMPS (using the LAMMPS PYTHON package). The former “only” requires that LAMMPS is compiled as a shared library (or .dll file in “Windows-speak”) and can be loaded into a variety of compatible Python installation, the latter requires that LAMMPS is linked with a Python installation and hence a matching Python runtime environment must be bundled with LAMMPS (you cannot call out to a different Python installation, e.g. one that is installed manually or via conda or some other package management tool, unless you compile your own LAMMPS executable from source code. However - as explained earlier - this is not supported for the “native” Windows compiler toolchain).

In the download folders for the LAMMPS Windows installer packages, there are different variants of packages available. 32-bit and 64-bit, with or without MPI and also without or with a bundled Python library. All of them contain the LAMMPS python module, but only the ones with “Python” in their name also include the PYTHON package ( 6.2. Package details — LAMMPS documentation ). This is only supported since a few months ago and thus you need to download and install a recent LAMMPS patch release version (30 July 2021 is currently recommended). For the other packages you should get an error ERROR: LAMMPS is not built with Python embedded (src/variable.cpp:800)

Please note that even though the nomenclature of “installing a package” seems to imply that there is some add-on that you can download and add to your executable, this refers to the process of compiling a customized LAMMPS executable with added source files in this context.

1 Like