[lammps-users] Accessing variables when using LAMMPS as a library

Hi Everyone,

I am calling LAMMPS as a library in my simulations (I have written a wrapper for an existing input script). So, the calculations that I want LAMMPS to do were in the form of a large input script. To get around the problem of re-writing all the commands in C++ I just read the input script line by line and execute the commands using lmp->input->one(). Everything works fine except for file output. The input script has a bunch of variables that were being written into a file using the “fix print” command. However, when I execute lammps using my C++ with the following command: lmp->input->one(“run 10 pre no post no”) the file output doesn’t happen. If I do: lmp->input->one(“run 10 pre yes post no”) then the file output does happen. What is the reason for this? Is the fix command only executed if I run it with the pre yes option?

Also, I can print the variables of interest to screen using lmp->input->one("print {Temp}"), however, I am unable to print the variable to file. Is there any way of doing this easily? Also, is there any way I can access {Temp} from my C++ wrapper and store it in a variable (in the outer C++ code)?

I am fairly new to LAMMPS and any help would be greatly appreciated.

Best Regards,

If you look in library.cpp you'll see a handful of wrapper functions, like
lammps_command() that does the call to lmp->input->one() you
indicate below.

If you're using run pre/post, then presumably you're doing several
LAMMPS runs in succession. You need to use pre yes on the first
one or if you change anything dramatic (like move atoms a significant
distance). See the doc page on the run command. If that
doesn't work, post a simple input script that shows where fix print
is not being called during a run. I think it should be.

Re: printing variables to a file - fix print is the way to do that. Using
just print will print it to the screen (and logfile) but not a separate
file. Note that the run command has a print option which will
do a print periodicially.

Re: accessing a variable, if what you mean is you want to evaluate
a variable and extract it's result, then yes you could write a wrapper
function to do that. Look at the code in fix_print.cpp::end_of_step().
It does something like that.