import test infrastructure for c, c++ and python library usage

This commit is contained in:
Axel Kohlmeyer
2020-08-25 11:03:31 -04:00
parent dc241abb85
commit 69cffb2d04
11 changed files with 1010 additions and 0 deletions

View File

@ -0,0 +1,40 @@
# we must have shared libraries enabled for testing the python module
if(NOT BUILD_SHARED_LIBS)
message(STATUS "Skipping Tests for the LAMMPS Python Module: must enable BUILD_SHARED_LIBS")
return()
endif()
if(CMAKE_VERSION VERSION_LESS 3.12)
find_package(PythonInterp 3.5) # Deprecated since version 3.12
if(PYTHONINTERP_FOUND)
set(Python_EXECUTABLE ${PYTHON_EXECUTABLE})
endif()
else()
find_package(Python3 COMPONENTS Interpreter)
endif()
if (Python_EXECUTABLE)
# prepare to augment the environment so that the LAMMPS python module and the shared library is found.
set(PYTHON_TEST_ENVIRONMENT PYTHONPATH=${LAMMPS_PYTHON_DIR}:$ENV{PYTHONPATH})
if(APPLE)
list(APPEND PYTHON_TEST_ENVIRONMENT DYLD_LIBRARY_PATH=${CMAKE_BINARY_DIR}:$ENV{DYLD_LIBRARY_PATH})
else()
list(APPEND PYTHON_TEST_ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}:$ENV{LD_LIBRARY_PATH})
endif()
if(LAMMPS_MACHINE)
# convert from '_machine' to 'machine'
string(SUBSTRING ${LAMMPS_MACHINE} 1 -1 LAMMPS_MACHINE_NAME)
list(APPEND PYTHON_TEST_ENVIRONMENT LAMMPS_MACHINE_NAME=${LAMMPS_MACHINE_NAME})
endif()
add_test(NAME PythonOpen
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/python-open.py -v
WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
set_tests_properties(PythonOpen PROPERTIES ENVIRONMENT "${PYTHON_TEST_ENVIRONMENT}")
add_test(NAME PythonCommands
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/python-commands.py -v
WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
set_tests_properties(PythonCommands PROPERTIES ENVIRONMENT "${PYTHON_TEST_ENVIRONMENT}")
else()
message(STATUS "Skipping Tests for the LAMMPS Python Module: no suitable Python interpreter")
endif()

View File

@ -0,0 +1,90 @@
import sys,os,unittest
from lammps import lammps
class PythonCommand(unittest.TestCase):
def setUp(self):
machine=None
if 'LAMMPS_MACHINE_NAME' in os.environ:
machine=os.environ['LAMMPS_MACHINE_NAME']
self.lmp=lammps(name=machine,
cmdargs=['-nocite',
'-log','none',
'-echo','screen',
'-var','zpos','1.5',
'-var','x','2'])
# create demo input strings and files
# a few commands to set up a box with a single atom
self.demo_input="""
region box block 0 $x 0 2 0 2
create_box 1 box
create_atoms 1 single 1.0 1.0 ${zpos}
"""
# another command to add an atom and use a continuation line
self.cont_input="""
create_atoms 1 single &
0.2 0.1 0.1
"""
self.demo_file='in.test'
with open(self.demo_file,'w') as f:
f.write(self.demo_input)
self.cont_file='in.cont'
with open(self.cont_file,'w') as f:
f.write(self.cont_input)
# clean up temporary files
def tearDown(self):
if os.path.exists(self.demo_file):
os.remove(self.demo_file)
if os.path.exists(self.cont_file):
os.remove(self.cont_file)
##############################
def testFile(self):
"""Test reading commands from a file"""
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
self.lmp.file(self.demo_file)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,1)
self.lmp.file(self.cont_file)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,2)
def testNoFile(self):
"""Test (not) reading commands from no file"""
self.lmp.file(None)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
def testCommand(self):
"""Test executing individual commands"""
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
cmds = self.demo_input.splitlines()
for cmd in cmds:
self.lmp.command(cmd)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,1)
def testCommandsList(self):
"""Test executing commands from list of strings"""
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
cmds = self.demo_input.splitlines()+self.cont_input.splitlines()
self.lmp.commands_list(cmds)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,2)
def testCommandsString(self):
"""Test executing block of commands from string"""
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
self.lmp.commands_string(self.demo_input+self.cont_input)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,2)
##############################
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,57 @@
import sys,os,unittest
from lammps import lammps
has_mpi=False
has_mpi4py=False
try:
from mpi4py import __version__ as mpi4py_version
# tested to work with mpi4py versions 2 and 3
has_mpi4py = mpi4py_version.split('.')[0] in ['2','3']
except:
pass
try:
lmp = lammps()
has_mpi = lmp.has_mpi_support
lmp.close()
except:
pass
class PythonOpen(unittest.TestCase):
def setUp(self):
self.machine=None
if 'LAMMPS_MACHINE_NAME' in os.environ:
self.machine=os.environ['LAMMPS_MACHINE_NAME']
def testNoArgs(self):
"""Create LAMMPS instance without any arguments"""
lmp=lammps(name=self.machine)
self.assertIsNot(lmp.lmp,None)
self.assertEqual(lmp.opened,1)
self.assertEqual(has_mpi4py,lmp.has_mpi4py)
self.assertEqual(has_mpi,lmp.has_mpi_support)
lmp.close()
self.assertIsNone(lmp.lmp,None)
self.assertEqual(lmp.opened,0)
def testWithArgs(self):
"""Create LAMMPS instance with a few arguments"""
lmp=lammps(name=self.machine,
cmdargs=['-nocite','-sf','opt','-log','none'])
self.assertIsNot(lmp.lmp,None)
self.assertEqual(lmp.opened,1)
@unittest.skipIf(not (has_mpi and has_mpi4py),"Skipping MPI test since LAMMPS is not parallel or mpi4py is not found")
def testWithMPI(self):
from mpi4py import MPI
mycomm=MPI.Comm.Split(MPI.COMM_WORLD, 0, 1)
lmp=lammps(name=self.machine,comm=mycomm)
self.assertIsNot(lmp.lmp,None)
self.assertEqual(lmp.opened,1)
lmp.close()
if __name__ == "__main__":
unittest.main()