Merge pull request #2478 from rbberger/python_interface_coverage

Add Python unit tests to increase test coverage
This commit is contained in:
Richard Berger
2020-11-29 15:19:42 -05:00
committed by GitHub
3 changed files with 185 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import sys,os,unittest
from lammps import lammps
from lammps import lammps, LMP_VAR_ATOM
class PythonCommand(unittest.TestCase):
@ -127,6 +127,155 @@ create_atoms 1 single &
self.assertEqual(1, neighbors_i[0])
def test_extract_box_non_periodic(self):
self.lmp.command("boundary f f f")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
boxlo, boxhi, xy, yz, xz, periodicity, box_change = self.lmp.extract_box()
self.assertEqual(boxlo, [0.0, 0.0, 0.0])
self.assertEqual(boxhi, [2.0, 2.0, 2.0])
self.assertEqual(xy, 0.0)
self.assertEqual(yz, 0.0)
self.assertEqual(xz, 0.0)
self.assertEqual(periodicity, [0, 0, 0])
self.assertEqual(box_change, 0)
def test_extract_box_periodic(self):
self.lmp.command("boundary p p p")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
boxlo, boxhi, xy, yz, xz, periodicity, box_change = self.lmp.extract_box()
self.assertEqual(boxlo, [0.0, 0.0, 0.0])
self.assertEqual(boxhi, [2.0, 2.0, 2.0])
self.assertEqual(xy, 0.0)
self.assertEqual(yz, 0.0)
self.assertEqual(xz, 0.0)
self.assertEqual(periodicity, [1, 1, 1])
self.assertEqual(box_change, 0)
def test_extract_box_triclinic(self):
self.lmp.command("boundary p p p")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
self.lmp.command("change_box all triclinic")
self.lmp.command("change_box all xy final 0.1 yz final 0.2 xz final 0.3")
boxlo, boxhi, xy, yz, xz, periodicity, box_change = self.lmp.extract_box()
self.assertEqual(boxlo, [0.0, 0.0, 0.0])
self.assertEqual(boxhi, [2.0, 2.0, 2.0])
self.assertEqual(xy, 0.1)
self.assertEqual(yz, 0.2)
self.assertEqual(xz, 0.3)
self.assertEqual(periodicity, [1, 1, 1])
self.assertEqual(box_change, 0)
def test_reset_box(self):
self.lmp.command("boundary p p p")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
self.lmp.command("change_box all triclinic")
self.lmp.command("change_box all xy final 0.1 yz final 0.2 xz final 0.3")
self.lmp.reset_box([0,0,0], [1,1,1], 0, 0, 0)
boxlo, boxhi, xy, yz, xz, periodicity, box_change = self.lmp.extract_box()
self.assertEqual(boxlo, [0.0, 0.0, 0.0])
self.assertEqual(boxhi, [1.0, 1.0, 1.0])
self.assertEqual(xy, 0)
self.assertEqual(yz, 0)
self.assertEqual(xz, 0)
self.assertEqual(periodicity, [1, 1, 1])
self.assertEqual(box_change, 0)
def test_extract_variable_equalstyle(self):
self.lmp.command("variable a equal 100")
a = self.lmp.extract_variable("a")
self.assertEqual(a, 100)
self.lmp.command("variable a equal 3.14")
a = self.lmp.extract_variable("a")
self.assertEqual(a, 3.14)
def test_extract_variable_atomstyle(self):
self.lmp.command("units lj")
self.lmp.command("atom_style atomic")
self.lmp.command("atom_modify map array")
self.lmp.command("boundary f f f")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
x = [
1.0, 1.0, 1.0,
1.0, 1.0, 1.5
]
types = [1, 1]
self.assertEqual(self.lmp.create_atoms(2, id=None, type=types, x=x), 2)
self.lmp.command("variable a atom x*x+y*y+z*z")
a = self.lmp.extract_variable("a", "all", LMP_VAR_ATOM)
self.assertEqual(a[0], x[0]*x[0]+x[1]*x[1]+x[2]*x[2])
self.assertEqual(a[1], x[3]*x[3]+x[4]*x[4]+x[5]*x[5])
def test_get_thermo(self):
self.lmp.command("units lj")
self.lmp.command("atom_style atomic")
self.lmp.command("atom_modify map array")
self.lmp.command("boundary f f f")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
x = [
1.0, 1.0, 1.0,
1.0, 1.0, 1.5
]
types = [1, 1]
self.lmp.create_atoms(2, id=None, type=types, x=x)
state = {
"step": 0,
"elapsed" : 0.0,
"elaplong": 0,
"dt" : 0.005,
"time" : 0.0,
"atoms" : 2.0,
"temp" : 0,
"press" : 0,
"pe" : 0.0,
"ke" : 0.0,
"etotal" : 0.0,
"enthalpy" : 0.0,
"vol" : 8.0,
"lx" : 2.0,
"ly" : 2.0,
"lz" : 2.0,
"xlo" : 0,
"xhi" : 2.0,
"ylo" : 0,
"yhi" : 2.0,
"zlo" : 0,
"zhi" : 2.0
}
for key, value in state.items():
result = self.lmp.get_thermo(key)
self.assertEqual(value, result, key)
def test_extract_global_double(self):
self.lmp.command("region box block -1 1 -2 2 -3 3")
self.lmp.command("create_box 1 box")
self.assertEqual(self.lmp.extract_global("boxxlo"), -1.0)
self.assertEqual(self.lmp.extract_global("boxxhi"), 1.0)
self.assertEqual(self.lmp.extract_global("boxylo"), -2.0)
self.assertEqual(self.lmp.extract_global("boxyhi"), 2.0)
self.assertEqual(self.lmp.extract_global("boxzlo"), -3.0)
self.assertEqual(self.lmp.extract_global("boxzhi"), 3.0)
##############################
if __name__ == "__main__":

View File

@ -1,5 +1,7 @@
import sys,os,unittest
from lammps import lammps, LAMMPS_INT, LMP_STYLE_GLOBAL, LMP_STYLE_LOCAL, LMP_STYLE_ATOM, LMP_TYPE_VECTOR, LMP_TYPE_SCALAR, LMP_TYPE_ARRAY
from lammps import lammps, LAMMPS_INT, LMP_STYLE_GLOBAL, LMP_STYLE_LOCAL, \
LMP_STYLE_ATOM, LMP_TYPE_VECTOR, LMP_TYPE_SCALAR, LMP_TYPE_ARRAY, \
LMP_VAR_ATOM
from ctypes import c_void_p
try:
@ -178,5 +180,36 @@ class PythonNumpy(unittest.TestCase):
self.assertIn(1, neighbors_i)
self.assertNotIn(0, neighbors_j)
def test_extract_variable_equalstyle(self):
self.lmp.command("variable a equal 100")
a = self.lmp.numpy.extract_variable("a")
self.assertEqual(a, 100)
self.lmp.command("variable a equal 3.14")
a = self.lmp.numpy.extract_variable("a")
self.assertEqual(a, 3.14)
def test_extract_variable_atomstyle(self):
self.lmp.command("units lj")
self.lmp.command("atom_style atomic")
self.lmp.command("atom_modify map array")
self.lmp.command("boundary f f f")
self.lmp.command("region box block 0 2 0 2 0 2")
self.lmp.command("create_box 1 box")
x = [
1.0, 1.0, 1.0,
1.0, 1.0, 1.5
]
types = [1, 1]
self.assertEqual(self.lmp.create_atoms(2, id=None, type=types, x=x), 2)
self.lmp.command("variable a atom x*x+y*y+z*z")
a = self.lmp.numpy.extract_variable("a", "all", LMP_VAR_ATOM)
self.assertIs(type(a), numpy.ndarray)
self.assertEqual(a[0], x[0]*x[0]+x[1]*x[1]+x[2]*x[2])
self.assertEqual(a[1], x[3]*x[3]+x[4]*x[4]+x[5]*x[5])
if __name__ == "__main__":
unittest.main()