Reading multiple input scripts while keeping LAMMPS open

Hello,

I am running a C++ program with a loop that calls LAMMPS many times with the command: system("lammps-daily < in.script"). Note that I am using a pre-built executable called "lammps-daily".

This is time consuming since my input script evaluates almost instantly (0 timesteps) but running LAMMPS consecutively starts to take a while for many loops.

I would like to run LAMMPS once and keep it open, while running the input script every loop. Is there a way to do this?

Thanks for your time.

Hello,

I am running a C++ program with a loop that calls LAMMPS many times with the command: system(“lammps-daily < in.script”). Note that I am using a pre-built executable called “lammps-daily”.

This is time consuming since my input script evaluates almost instantly (0 timesteps) but running LAMMPS consecutively starts to take a while for many loops.

I would like to run LAMMPS once and keep it open, while running the input script every loop. Is there a way to do this?

Have you tried using a pipe?

Why not use the library interface?

There are several ways to do it.

a) if you have a series of quick 0-step input scripts

to run, you could make each do a “clear” at the

end and “jump” to the next. See the jump and clear

commands.

b) a script can jump to itself, e.g. in a looping fashion.

See the jump SELF command

c) you can run multiple scripts from one script, See

Section howto 6.4 of the manual.

d) you can call LAMMPS as a library, either from

another code or a Python script, in which case you

can pass it a script, invoke a clear, pass it a script,

invoke a clear, etc

In all those cases a single instance of LAMMPS never

stops running.

Steve

Hello,

I am running a C++ program with a loop that calls LAMMPS many times with
the command: system("lammps-daily < in.script"). Note that I am using a
pre-built executable called "lammps-daily".

This is time consuming since my input script evaluates almost instantly (0
timesteps) but running LAMMPS consecutively starts to take a while for many
loops.

I would like to run LAMMPS once and keep it open, while running the input
script every loop. Is there a way to do this?

Have you tried using a pipe?

...and here is a simple example (took just 5min to write):

#define LMP_CMD "lmp_g++"

#include <cstdio>

int main(int, char**)
{
    FILE *lmp;

    lmp = popen(LMP_CMD,"w");

    fprintf(lmp,"clear\n");
    fprintf(lmp,"include in.melt\n");
    fprintf(lmp,"clear\n");
    fprintf(lmp,"include in.lj\n");
    fprintf(lmp,"run 1000\n");
    fflush(lmp);
    pclose(lmp);

    return 0;
}

axel.

Thank you guys. I got it to work in both Python and and C++. Even though it is explained in the LAMMPS manual, I had some difficulty interpreting all the computer lingo.

I will post my solutions for others who also don't have experience with makefiles and linking libraries.

For Python,
1) I built LAMMPS as a shared library with the bash command "make mode=shlib ompi_g++" in the LAMMPS /src directory. The "ompi_g++" is just the particular machine I wanted, "serial" or any others will work as well. This makes a shared object file "liblammps_ompi_g++.so" and the link "liblammps.so", which links to "liblammps_ompi_g++.so".
2) I then copied both shared object files to my /usr/lib folder, where my system reads all of my shared object files (this may be different for other Linux users, but there is a nice description of how to change the path in the LAMMPS manual for Python).
3) I can now use LAMMPS in a python session with the bash command "python test.py", which calls python to run a python script. An example script is:
     from lammps import lammps
     lmp = lammps("in.lj")
     lmp = lammps("in.lj")
     lmp = lammps("in.lj")
This basically just runs the input script 3 times during the same instance of LAMMPS. You might need to use the "pre" and "post" run commands to accommodate your needs.

For C++,
1) Same as step 1 for Python.
2) Copy the simple.cpp example in /Examples/COUPLE to the LAMMPS /src directory so that it can find all the necessary files for compilation.
3) Compile simple.cpp with "mpicxx -o simpleCC simple.cpp liblammps.so", which will make the executable "simpleCC". There are also other g++ commands which let you use a shared library in another directory, but I've just decided to compile this in the LAMMPS /src directory for simplicity. Play around with the code to change it to your needs, using functions from "library.cpp" if needed.

An example script is:

#include "mpi.h"
#include "lammps.h"
#include "input.h"
#include "string.h"

using namespace LAMMPS_NS;

/* ----------------------------------------------------------------------
   main program to drive LAMMPS
------------------------------------------------------------------------- */
char *args1[] = {
    (char *) "lammps1",
    (char *) "-log",
    (char*) "log.lammps-1",
     0};

int main(int argc, char **argv)
{
  MPI_Init(&argc,&argv);

  LAMMPS *lammps1;

  lammps1 = new LAMMPS(3,args1,MPI_COMM_WORLD);

  lammps1->input->file("in.lj");
  lammps1->input->file("in.lj");
  lammps1->input->file("in.lj");
  lammps1->input->file("in.lj");
  lammps1->input->file("in.lj");

  delete lammps1;

  MPI_Finalize();
}

This just runs LAMMPS 5 times while keeping the same instance running.

I hope this helps others who are unfamiliar with makefiles and linking libraries.

from lammps import lammps
lmp = lammps(“in.lj”)
lmp = lammps(“in.lj”)
lmp = lammps(“in.lj”)

Actually, this syntax will create and destroy
an instance of LAMMPS 3 times. To run 3 files
consecutively in one instance of LAMMPS (minimum
overhead), you want something more like your C
program. In python:

lmp = lammps()
lmp.file(“in.lj”)
lmp.command(“clear”)
lmp.file(“in.lj”)

The clear commands are also important,
both from C and Python, else your 2nd script
likely won’t run if it re-reads a data file, etc.

Steve