I am trying to calculate the kinetic and potential energy of a system using the python bindings for Lammps.
This is my in.demo file.
# in.demo
pair_style lj/cut/coul/long 10.5 10.5
pair_modify mix arithmetic
pair_modify tail yes
kspace_style pppm 1.0e-4
read_restart "restart"
thermo_style custom step etotal pe ke
thermo 1
timestep 1.0
fix 1 all nve
thermo_style custom step temp etotal pe ke
thermo_modify lost warn
run 1
And my demo.py file.
# demo.py
from lammps import lammps, LMP_STYLE_GLOBAL, LMP_TYPE_SCALAR
lmp = lammps()
lmp.file("in.demo")
pe = lmp.extract_compute("thermo_pe", 0, 0)
print(f"Potential energy from extract_compute = {pe}")
ke = lmp.extract_compute("thermo_ke", 0, 0)
print(f"Kinetic energy from extract_variable = {ke}")
The potential energy command works as expected and returns Potential energy from extract_compute = -3914.0713226831754
but the Kinetic energy command throws an error.
Traceback (most recent call last):
File "/kuhpc/scratch/thompson/i007s230/git_repos/fluct-theory-code/lammps/examples/bulk_water/testing/demo.py", line 11, in <module>
ke = lmp.extract_compute("thermo_ke", 0, 0)
File "/home/i007s230/.local/lib/python3.10/site-packages/lammps/core.py", line 1165, in extract_compute
return ptr[0]
ValueError: NULL pointer access
I know that the system has calculations for the kinetic energy because it is returned in the lammps output.
Per MPI rank memory allocation (min/avg/max) = 9.552 | 9.552 | 9.552 Mbytes
Step Temp TotEng PotEng KinEng
200 203.03052 -3228.0577 -3850.1986 622.14088
201 221.66596 -3234.8264 -3914.0713 679.24496
Loop time of 0.0189466 on 1 procs for 1 steps with 1029 atoms
How do I get the kinetic energy with python?