diff --git a/python/lammps.py b/python/lammps.py index f571557488..00e12c3e64 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -1468,7 +1468,6 @@ class lammps(object): # ------------------------------------------------------------------------- - @property def force_timeout(self): """ Trigger an immediate timeout, i.e. a "soft stop" of a run. @@ -1650,16 +1649,16 @@ class lammps(object): :return: list of id names in given category :rtype: list """ - self._available_ids = {} - if category not in self._available_ids: - self._available_ids[category] = [] - nstyles = self.lib.lammps_id_count(self.lmp, category.encode()) + categories = ['compute','dump','fix','group','molecule','region','variable'] + available_ids = [] + if category in categories: + num = self.lib.lammps_id_count(self.lmp, category.encode()) sb = create_string_buffer(100) - for idx in range(nstyles): + for idx in range(num): self.lib.lammps_id_name(self.lmp, category.encode(), idx, sb, 100) - self._available_ids[category].append(sb.value.decode()) - return self._available_ids[category] + available_ids.append(sb.value.decode()) + return available_ids # ------------------------------------------------------------------------- diff --git a/unittest/python/python-capabilities.py b/unittest/python/python-capabilities.py index e5dd63707b..2b47b8ca90 100644 --- a/unittest/python/python-capabilities.py +++ b/unittest/python/python-capabilities.py @@ -60,5 +60,68 @@ class PythonCapabilities(unittest.TestCase): pairs = self.lmp.available_styles('pair') self.assertIn('lj/cut', pairs) + def test_has_id(self): + self.lmp.command('fix charge all property/atom q ghost yes') + self.lmp.command('region box block 0 1 0 1 0 1') + self.lmp.command('create_box 1 box') + self.lmp.command('group none empty') + self.lmp.command('variable test index 1') + self.assertTrue(self.lmp.has_id('compute', 'thermo_temp')) + self.assertTrue(self.lmp.has_id('compute', 'thermo_press')) + self.assertTrue(self.lmp.has_id('compute', 'thermo_pe')) + self.assertFalse(self.lmp.has_id('compute', 'xxx')) + self.assertFalse(self.lmp.has_id('dump', 'xxx')) + self.assertTrue(self.lmp.has_id('fix', 'charge')) + self.assertFalse(self.lmp.has_id('fix', 'xxx')) + self.assertTrue(self.lmp.has_id('group', 'all')) + self.assertTrue(self.lmp.has_id('group', 'none')) + self.assertFalse(self.lmp.has_id('group', 'xxx')) + self.assertTrue(self.lmp.has_id('region', 'box')) + self.assertFalse(self.lmp.has_id('region', 'xxx')) + self.assertTrue(self.lmp.has_id('variable', 'test')) + self.assertFalse(self.lmp.has_id('variable', 'xxx')) + + def test_available_id(self): + self.lmp.command('fix charge all property/atom q ghost yes') + self.lmp.command('region box block 0 1 0 1 0 1') + self.lmp.command('create_box 1 box') + self.lmp.command('group none empty') + self.lmp.command('variable test index 1') + ids = self.lmp.available_ids('compute') + self.assertIn('thermo_pe', ids) + self.assertEqual(len(ids),3) + ids = self.lmp.available_ids('dump') + self.assertEqual(len(ids),0) + ids = self.lmp.available_ids('fix') + self.assertIn('charge', ids) + self.assertEqual(len(ids),1) + ids = self.lmp.available_ids('group') + self.assertIn('none', ids) + self.assertEqual(len(ids),2) + ids = self.lmp.available_ids('molecule') + self.assertEqual(len(ids),0) + ids = self.lmp.available_ids('region') + self.assertIn('box', ids) + self.assertEqual(len(ids),1) + ids = self.lmp.available_ids('variable') + self.assertIn('test', ids) + self.assertEqual(len(ids),1) + + def test_is_running(self): + self.assertFalse(self.lmp.is_running) + + def test_force_timeout(self): + self.lmp.command('region box block 0 1 0 1 0 1') + self.lmp.command('create_box 1 box') + self.lmp.command('mass * 1.0') + self.lmp.command('run 10') + self.assertEqual(self.lmp.extract_global('ntimestep'),10) + self.lmp.force_timeout() + self.lmp.command('run 10') + self.assertEqual(self.lmp.extract_global('ntimestep'),10) + self.lmp.command('timer timeout off') + self.lmp.command('run 10') + self.assertEqual(self.lmp.extract_global('ntimestep'),20) + if __name__ == "__main__": unittest.main()