add unit tests for added APIs

This commit is contained in:
Axel Kohlmeyer
2020-10-09 07:16:34 -04:00
parent 7a9b4ef0d4
commit 471acb2ef8
2 changed files with 70 additions and 8 deletions

View File

@ -1468,7 +1468,6 @@ class lammps(object):
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@property
def force_timeout(self): def force_timeout(self):
""" Trigger an immediate timeout, i.e. a "soft stop" of a run. """ 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 :return: list of id names in given category
:rtype: list :rtype: list
""" """
self._available_ids = {}
if category not in self._available_ids: categories = ['compute','dump','fix','group','molecule','region','variable']
self._available_ids[category] = [] available_ids = []
nstyles = self.lib.lammps_id_count(self.lmp, category.encode()) if category in categories:
num = self.lib.lammps_id_count(self.lmp, category.encode())
sb = create_string_buffer(100) 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.lib.lammps_id_name(self.lmp, category.encode(), idx, sb, 100)
self._available_ids[category].append(sb.value.decode()) available_ids.append(sb.value.decode())
return self._available_ids[category] return available_ids
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------

View File

@ -60,5 +60,68 @@ class PythonCapabilities(unittest.TestCase):
pairs = self.lmp.available_styles('pair') pairs = self.lmp.available_styles('pair')
self.assertIn('lj/cut', pairs) 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__": if __name__ == "__main__":
unittest.main() unittest.main()