diff --git a/python/lammps.py b/python/lammps.py index 4be2f48b0d..b74e111dc6 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -1115,7 +1115,7 @@ class lammps(object): The variable must be either an equal-style (or equivalent) variable or an atom-style variable. The variable type has to provided as ``vartype`` parameter which may be one of two constants: - ``LMP_VAR_EQUAL`` or ``LMP_VAR_STRING``; it defaults to + ``LMP_VAR_EQUAL`` or ``LMP_VAR_ATOM``; it defaults to equal-style variables. The group parameter is only used for atom-style variables and defaults to the group "all" if set to ``None``, which is the default. diff --git a/unittest/python/python-commands.py b/unittest/python/python-commands.py index 6bd5a2a247..4c80e8d227 100644 --- a/unittest/python/python-commands.py +++ b/unittest/python/python-commands.py @@ -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__": diff --git a/unittest/python/python-numpy.py b/unittest/python/python-numpy.py index 46794590f4..9f1d5f12c8 100644 --- a/unittest/python/python-numpy.py +++ b/unittest/python/python-numpy.py @@ -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()