From a193d9d429ccf8129db46731bf3716b5b58b2c8c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 25 Mar 2021 11:23:28 -0400 Subject: [PATCH] fix several issues when using extract_global() from python exposed by tests --- python/lammps/core.py | 3 ++- src/library.cpp | 9 +++++++-- unittest/python/python-commands.py | 13 +++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/python/lammps/core.py b/python/lammps/core.py index b3cdceb1a6..e112c4f3f8 100644 --- a/python/lammps/core.py +++ b/python/lammps/core.py @@ -777,10 +777,11 @@ class lammps(object): target_type = float elif dtype == LAMMPS_STRING: self.lib.lammps_extract_global.restype = c_char_p - target_type = lambda x: str(x, 'ascii') ptr = self.lib.lammps_extract_global(self.lmp, name) if ptr: + if dtype == LAMMPS_STRING: + return ptr.decode('utf-8') if veclen > 1: result = [] for i in range(0,veclen): diff --git a/src/library.cpp b/src/library.cpp index 6f08cffcc2..a950d31e4e 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -1387,8 +1387,13 @@ void *lammps_extract_global(void *handle, const char *name) if (strcmp(name,"boxhi") == 0) return (void *) lmp->domain->boxhi; if (strcmp(name,"sublo") == 0) return (void *) lmp->domain->sublo; if (strcmp(name,"subhi") == 0) return (void *) lmp->domain->subhi; - if (strcmp(name,"sublo_lambda") == 0) return (void *) lmp->domain->sublo_lamda; - if (strcmp(name,"subhi_lambda") == 0) return (void *) lmp->domain->subhi_lamda; + // these are only valid for a triclinic cell + if (lmp->domain->triclinic) { + if (strcmp(name,"sublo_lambda") == 0) + return (void *) lmp->domain->sublo_lamda; + if (strcmp(name,"subhi_lambda") == 0) + return (void *) lmp->domain->subhi_lamda; + } if (strcmp(name,"boxxlo") == 0) return (void *) &lmp->domain->boxlo[0]; if (strcmp(name,"boxxhi") == 0) return (void *) &lmp->domain->boxhi[0]; if (strcmp(name,"boxylo") == 0) return (void *) &lmp->domain->boxlo[1]; diff --git a/unittest/python/python-commands.py b/unittest/python/python-commands.py index 3bcb785b74..3661feb8a0 100644 --- a/unittest/python/python-commands.py +++ b/unittest/python/python-commands.py @@ -259,9 +259,13 @@ create_atoms 1 single & result = self.lmp.get_thermo(key) self.assertEqual(value, result, key) - def test_extract_global_double(self): + def test_extract_global(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("units"), "lj") + self.assertEqual(self.lmp.extract_global("ntimestep"), 0) + self.assertEqual(self.lmp.extract_global("dt"), 0.005) + 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) @@ -273,7 +277,9 @@ create_atoms 1 single & self.assertEqual(self.lmp.extract_global("sublo"), [-1.0, -2.0, -3.0]) self.assertEqual(self.lmp.extract_global("subhi"), [1.0, 2.0, 3.0]) self.assertEqual(self.lmp.extract_global("periodicity"), [1,1,1]) - # only valid for triclinic box + self.assertEqual(self.lmp.extract_global("triclinic"), 0) + self.assertEqual(self.lmp.extract_global("sublo_lambda"), None) + self.assertEqual(self.lmp.extract_global("subhi_lambda"), None) self.assertEqual(self.lmp.extract_global("respa_levels"), None) self.assertEqual(self.lmp.extract_global("respa_dt"), None) @@ -284,7 +290,10 @@ create_atoms 1 single & self.assertEqual(self.lmp.extract_global("ntimestep"), 1) self.assertEqual(self.lmp.extract_global("respa_levels"), 3) self.assertEqual(self.lmp.extract_global("respa_dt"), [0.0005, 0.0025, 0.005]) + + # checks only for triclinic boxes self.lmp.command("change_box all triclinic") + self.assertEqual(self.lmp.extract_global("triclinic"), 1) self.assertEqual(self.lmp.extract_global("sublo_lambda"), [0.0, 0.0, 0.0]) self.assertEqual(self.lmp.extract_global("subhi_lambda"), [1.0, 1.0, 1.0])