Unable to Calculate Kinentic Energy with Python Bindings

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?

If you add the command “info compute” to your in.demo file at there end, it will show you, that there is no “thermo_ke” compute defined.

Info-Info-Info-Info-Info-Info-Info-Info-Info-Info-Info
Printed on Sun Nov 17 21:57:45 2024

Compute information:
Compute[  0]:  thermo_temp,      style = temp,             group = all
Compute[  1]:  thermo_press,     style = pressure,         group = all
Compute[  2]:  thermo_pe,        style = pe,               group = all

Info-Info-Info-Info-Info-Info-Info-Info-Info-Info-Info

The kinetic energy is computed from the temperature compute value.

Instead to get access to the last printed thermo output, you can use:

vals = lmp.last_thermo()
print("Potential energy from get_thermo = ", vals['PotEng'])
print("Kinetic energy from get_thermo= ", vals['KinEng'])

or to re-evaluate thermo keywords, you can use:

pe = lmp.get_thermo("pe")
print(f"Potential energy from get_thermo = {pe}")
ke = lmp.get_thermo("ke")
print(f"Kinetic energy from get_thermo= {ke}")

For details, please see the LAMMPS python module documentation and the documentation for the corresponding calls in the LAMMPS C-library interface.