From d5169a9dc28e8b9adaa39889d3136410b47914db Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Thu, 12 Nov 2020 12:29:13 -0500 Subject: [PATCH 1/7] Add Python unit tests for extract_box and reset_box --- unittest/python/python-commands.py | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/unittest/python/python-commands.py b/unittest/python/python-commands.py index 6bd5a2a247..288efa58fe 100644 --- a/unittest/python/python-commands.py +++ b/unittest/python/python-commands.py @@ -127,6 +127,71 @@ 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) + ############################## if __name__ == "__main__": From 875057538f0483d6110634ea645435d0628ac593 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 23 Nov 2020 12:45:37 -0500 Subject: [PATCH 2/7] Fix docstring of extract_variable --- python/lammps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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. From 5ff0c3d4f0db4c9147025a99bd90150a99fd3eb4 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 23 Nov 2020 13:07:00 -0500 Subject: [PATCH 3/7] Add unit test for extract_variable (equalstyle) --- unittest/python/python-commands.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/unittest/python/python-commands.py b/unittest/python/python-commands.py index 288efa58fe..7f2593945f 100644 --- a/unittest/python/python-commands.py +++ b/unittest/python/python-commands.py @@ -192,6 +192,14 @@ create_atoms 1 single & 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) ############################## if __name__ == "__main__": From 3ddc1e680cc1966d808ea025c15e5155e20df454 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 23 Nov 2020 13:14:15 -0500 Subject: [PATCH 4/7] Add unit test for extract_variable (atomstyle) --- unittest/python/python-commands.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/unittest/python/python-commands.py b/unittest/python/python-commands.py index 7f2593945f..1954380562 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): @@ -201,6 +201,28 @@ create_atoms 1 single & 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) + nlocal = self.lmp.extract_global("nlocal") + 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]) + ############################## if __name__ == "__main__": unittest.main() From 2ce10cc435cc1d86cf889f10c61f93c6dc0c6708 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 23 Nov 2020 13:35:17 -0500 Subject: [PATCH 5/7] Add unit test for get_thermo --- unittest/python/python-commands.py | 46 +++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/unittest/python/python-commands.py b/unittest/python/python-commands.py index 1954380562..ffc846aa8a 100644 --- a/unittest/python/python-commands.py +++ b/unittest/python/python-commands.py @@ -217,12 +217,56 @@ create_atoms 1 single & types = [1, 1] self.assertEqual(self.lmp.create_atoms(2, id=None, type=types, x=x), 2) - nlocal = self.lmp.extract_global("nlocal") 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) + ############################## if __name__ == "__main__": unittest.main() From ebf3c180c28e20ad8e5850bcaee168326d9e11a3 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 23 Nov 2020 13:43:15 -0500 Subject: [PATCH 6/7] Add unit test for extract_global (LAMMPS_DOUBLE) --- unittest/python/python-commands.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/unittest/python/python-commands.py b/unittest/python/python-commands.py index ffc846aa8a..4c80e8d227 100644 --- a/unittest/python/python-commands.py +++ b/unittest/python/python-commands.py @@ -267,6 +267,16 @@ create_atoms 1 single & 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__": unittest.main() From 7aa45ea816a2872933b744241a3b4e684840798c Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 23 Nov 2020 14:13:58 -0500 Subject: [PATCH 7/7] Add numpy variants of extract_variable tests --- unittest/python/python-numpy.py | 35 ++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) 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()