Unable to set variable value from the python function using fix python/invoke

Hello All,

I am attempting on using python with LAMMPS using fix python/invoke. In the python function using the lmp.extract_atom() command, I am returning an array of atom ids out of which I would like to store 2 atom ids as variables. These atom id pair will change every step of the run. I tried to save the atom id pair using the lmp.set_variable() command and the flag returned was 0 but the variables do not seem to be set. Below you can see both my input file and the output. Please guide me on where I went wrong.

Thanking you in advance,
Birva Patel

INPUT FILE:
units real
atom_style full

bond_style harmonic
angle_style harmonic
dihedral_style harmonic
special_bonds amber

pair_style lj/cut/coul/long 13
kspace_style ewald 0.000001
read_data data.read extra/bond/per/atom 30 extra/angle/per/atom 30 extra/dihedral/per/atom 30 extra/improper/per/atom 30 extra/special/per/atom 200
molecule pub1 data.mol
create_atoms 0 single -17.75125215 -9.703424300000009 -26.129375200000002 mol pub1 1 units box
ndx2group index.ndx
neigh_modify delay 0 every 1 check yes
group rotategroup id 1:1222
timestep 1

python end_of_step_callback here “”"
from future import print_function
from lammps import lammps
def end_of_step_callback(lammps_ptr):
lmp = lammps(ptr=lammps_ptr)

access LAMMPS state using Python interface

atomIdArray = lmp.extract_atom(“grouparr”,0) #self written code with returns an array of atom ids but it does not interfere with the actual LAMMPS code
atom1 = atomIdArray[2587]
atom2 = atomIdArray[2588]
flag1 = lmp.set_variable(“atom1”,atom1)
flag2 = lmp.set_variable(“atom2”,atom2)
print (“flags :”,flag1," ",flag2)
a2 = lmp.get_thermo(“atom2”)
a1 = lmp.get_thermo(“atom1”)
print ("set atom pair o/p(from python): “,a1,” ",a2)
print ("actual atom pair: “,atom1,” ",atom2)

“”"
variable atom1 string 1222 #arbitrary initial atom value
variable atom2 string 1224 #arbitrary initial atom value
variable a1 equal {atom1} variable a2 equal {atom2}
variable x1 equal x[{atom1}] variable y1 equal y[{atom1}]
variable z1 equal z[{atom1}] variable x2 equal x[{atom2}]
variable y2 equal y[{atom2}] variable z2 equal z[{atom2}]
variable orgx equal {x2}+{x1}/2
variable orgy equal {y2}+{y1}/2
variable orgz equal {z2}+{z1}/2
variable rx equal {x1}-{x2}
variable ry equal {y1}-{y2}
variable rz equal {z1}-{z2}
variable deg equal 5

fix 1 all python/invoke 1 end_of_step end_of_step_callback
fix 2 rotategroup move rotate {orgx} {orgy} {orgz} {rx} {ry} {rz} ${deg} units box
thermo 1
thermo_style custom step v_a1 v_a2
restart 100 restart_mc.1.bin restart_mc.2.bin
dump 2 all xtc 1 3md3_rigid_mc.xtc
run 100

OUTPUT:

Step v_a1 v_a2
0 1222 1224
flags : 0 0
set atom pair o/p(from python): 0.0 0.0
actual atom pair: 1372 1371
1 1222 1224
flags : 0 0
set atom pair o/p(from python): 0.0 0.0
actual atom pair: 1298 1297
2 1222 1224
flags : 0 0
set atom pair o/p(from python): 0.0 0.0
actual atom pair: 1224 1226
3 1222 1224
flags : 0 0
set atom pair o/p(from python): 0.0 0.0
actual atom pair: 1325 1318
4 1222 1224
flags : 0 0
set atom pair o/p(from python): 0.0 0.0
actual atom pair: 1226 1232

Hello Birva, lammps_set_variable() sets a string, but lammps_get_thermo() reads a floating-point number. Have you tried lammps_extract_variable() to read the variable back?

Giacomo