Invoking a python script from liggghts input script

Submitted by Adithya Ramgopal on Wed, 01/16/2019 - 09:16

Hello all,

I need to invoke a python script during the run time of the simulation . The script has to be invoked in every timestep. Is there a possibility to do that?
I know the other way around (python->Liggghts) is possible. But I need to compute some paramaters outside liggghts at each and every timestep and I need to use that data again in the simulation. So please suggest me some ideas on this.

Thank you .

deepakpawar.2310 | Wed, 01/16/2019 - 10:48

First, you only need to build LIGGGHTS with the PYTHON package.

You have to see those python functions which can “call back” to LIGGGHTS through its Python-wrapped library interface, (For detail see the Python run doc page.)
For examples

fix python/invoke
This command can execute Python code at selected timesteps during a simulation run.
pair_style python
The command allows you to define pairwise force field

See more on the Documentation page

Good Luck !

Adithya Ramgopal | Wed, 01/16/2019 - 11:18

In the python doc page there is information on how to run a LIGGGHTS script using python (as I have already mentioned) . fix python/invoke is a lammps command so I can't use that in LIGGGHTS. I have built the LIGGGHTS with python package .

mschramm | Wed, 01/16/2019 - 14:45

Hello

Assuming you called liggghts in python by

from liggghts import liggghts
lmp = liggghts()

the python commands you will want to look at are.
lmp.command('STRING')
lmp.file('FILE')
lmp.extract_variable('VAR',0,0)
where STRING is any liggghts command, like 'variable my_var eqaul 1.0', or 'run 100 upto'
FILE is an existing liggghts input file
VAR is a user created variable from liggghts

As an example for your situation
In your liggghts script you may define all of your preliminary set up up to your run command. Let us also assume you created a variable by
variable my_ke equal ke
in your input script
Then in python invoke the liggghts script
lmp.file('in.liggghts')
lmp.command('run 1 post no')
for k in range(0,100):
ke = lmp.extract_variable('my_ke', 0, 0)
the_function_you_run_every_time_step(ke)
lmp.command('run 1 pre no post no')

lmp.extract_variable can also extract vectors but I don't know the correct syntax for it, I think its extract_variable('VAR',1,0) but don't quote me...

best of luck