def testQueryLatticeSites(self): """ Test that the returned lattice site data is coorect. """ cell_vectors = [[2.3, 0.0, 0.0], [2.4, 3.0, 0.0], [0.0, 0.0, 11.8]] basis_points = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.0]] unit_cell = KMCUnitCell(cell_vectors=cell_vectors, basis_points=basis_points) # Setup the other input. repetitions = (2, 1, 1) periodic = (True, True, False) # Construct the KMCLattice object. lattice = KMCLattice(unit_cell=unit_cell, repetitions=repetitions, periodic=periodic) # These are all the sites in the lattice, given in # the fractional coordinates of the original cell. sites = lattice.sites() ref_sites = numpy.array([[0., 0., 0.], [0.5, 0.5, 0.], [1., 0., 0.], [1.5, 0.5, 0.]]) # Check against a hardcoded reference. self.assertAlmostEqual(numpy.linalg.norm(sites - ref_sites), 0.0, 10) # Test with another repetition. repetitions = (2, 3, 4) lattice = KMCLattice(unit_cell=unit_cell, repetitions=repetitions, periodic=periodic) sites = lattice.sites() ref_sites = numpy.array([[0., 0., 0.], [0.5, 0.5, 0.], [0., 0., 1.], [0.5, 0.5, 1.], [0., 0., 2.], [0.5, 0.5, 2.], [0., 0., 3.], [0.5, 0.5, 3.], [0., 1., 0.], [0.5, 1.5, 0.], [0., 1., 1.], [0.5, 1.5, 1.], [0., 1., 2.], [0.5, 1.5, 2.], [0., 1., 3.], [0.5, 1.5, 3.], [0., 2., 0.], [0.5, 2.5, 0.], [0., 2., 1.], [0.5, 2.5, 1.], [0., 2., 2.], [0.5, 2.5, 2.], [0., 2., 3.], [0.5, 2.5, 3.], [1., 0., 0.], [1.5, 0.5, 0.], [1., 0., 1.], [1.5, 0.5, 1.], [1., 0., 2.], [1.5, 0.5, 2.], [1., 0., 3.], [1.5, 0.5, 3.], [1., 1., 0.], [1.5, 1.5, 0.], [1., 1., 1.], [1.5, 1.5, 1.], [1., 1., 2.], [1.5, 1.5, 2.], [1., 1., 3.], [1.5, 1.5, 3.], [1., 2., 0.], [1.5, 2.5, 0.], [1., 2., 1.], [1.5, 2.5, 1.], [1., 2., 2.], [1.5, 2.5, 2.], [1., 2., 3.], [1.5, 2.5, 3.]]) # Check against a hardcoded reference. self.assertAlmostEqual(numpy.linalg.norm(sites - ref_sites), 0.0, 10)
def testConstructionShortFormat(self): """ Test that the KMCConfiguration class can be constructed. """ # Setup a valid KMCUnitCell. unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.8, 0.0, 0.0], [0.0, 3.2, 0.0], [0.0, 0.5, 3.0]]), basis_points=[[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [0.25, 0.25, 0.75]]) # Setup the lattice. lattice = KMCLattice(unit_cell=unit_cell, repetitions=(4, 4, 1), periodic=(True, True, False)) types = [ 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'b', 'a' ] # Setup the configuration. config = KMCConfiguration(lattice=lattice, types=types, possible_types=['a', 'c', 'b']) # Get the types information out. ret_types = config.types() # Check that they are what we inserted. self.assertEqual(types, ret_types) # Check that the possible types are what we expect. self.assertEqual(set(['a', 'c', 'b', '*']), set(config._KMCConfiguration__possible_types.keys())) # Check that the number of lattice sites corresponds to the lattice. self.assertEqual(config._KMCConfiguration__n_lattice_sites, len(lattice.sites())) # Check that the lattice site can be returned from the configuration. self.assertAlmostEqual( numpy.linalg.norm( numpy.array(config.sites()) - numpy.array(lattice.sites())), 0.0, 10) # Construct without possible types and check that the list is set correctly # from the given types. config = KMCConfiguration(lattice=lattice, types=types) self.assertEqual(set(['a', 'b', '*']), set(config._KMCConfiguration__possible_types.keys()))
def testConstructionLongFormat(self): """ Test that the KMCConfiguration class can be constructed with the long types format. """ # Setup a valid KMCUnitCell. unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.8, 0.0, 0.0], [0.0, 3.2, 0.0], [0.0, 0.5, 3.0]]), basis_points=[[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [0.25, 0.25, 0.75]]) # Setup the lattice. lattice = KMCLattice(unit_cell=unit_cell, repetitions=(4, 3, 2), periodic=(True, True, False)) types = [(0, 0, 0, 0, 'g'), (3, 2, 1, 2, 'h')] default_type = 'a' # Setup the configuration. config = KMCConfiguration(lattice=lattice, types=types, default_type=default_type) # Get the types information out. ret_types = config.types() ref_types = [ 'g', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'h' ] # Check that they are what we inserted. self.assertEqual(ref_types, ret_types) # Check that the possible types are what we expect. self.assertEqual(set(['a', 'g', 'h', '*']), set(config._KMCConfiguration__possible_types)) # Check that the number of lattice sites corresponds # to the lattice. self.assertEqual(config._KMCConfiguration__n_lattice_sites, len(lattice.sites())) # Construct again, now with a list of possible types. config = KMCConfiguration(lattice=lattice, types=types, default_type=default_type, possible_types=['aa', 'a', 'h', 'g']) # Check that the possible types are what we expect. self.assertEqual(set(['aa', 'a', 'g', 'h', '*']), set(config._KMCConfiguration__possible_types))
def testConstructionLongFormat(self): """ Test that the KMCConfiguration class can be constructed with the long types format. """ # Setup a valid KMCUnitCell. unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.8,0.0,0.0], [0.0,3.2,0.0], [0.0,0.5,3.0]]), basis_points=[[0.0,0.0,0.0], [0.5,0.5,0.5], [0.25,0.25,0.75]]) # Setup the lattice. lattice = KMCLattice(unit_cell=unit_cell, repetitions=(4,3,2), periodic=(True,True,False)) types = [(0,0,0,0,'g'),(3,2,1,2,'h')] default_type = 'a' # Setup the configuration. config = KMCConfiguration(lattice=lattice, types=types, default_type=default_type) # Get the types information out. ret_types = config.types() ref_types = ['g', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'h'] # Check that they are what we inserted. self.assertEqual(ref_types, ret_types) # Check that the possible types are what we expect. self.assertEqual(set(['a','g','h','*']), set(config._KMCConfiguration__possible_types)) # Check that the number of lattice sites corresponds # to the lattice. self.assertEqual(config._KMCConfiguration__n_lattice_sites, len(lattice.sites())) # Construct again, now with a list of possible types. config = KMCConfiguration(lattice=lattice, types=types, default_type=default_type, possible_types=['aa','a','h','g']) # Check that the possible types are what we expect. self.assertEqual(set(['aa','a','g','h','*']), set(config._KMCConfiguration__possible_types))
def testConstructionShortFormat(self): """ Test that the KMCConfiguration class can be constructed. """ # Setup a valid KMCUnitCell. unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.8,0.0,0.0], [0.0,3.2,0.0], [0.0,0.5,3.0]]), basis_points=[[0.0,0.0,0.0], [0.5,0.5,0.5], [0.25,0.25,0.75]]) # Setup the lattice. lattice = KMCLattice(unit_cell=unit_cell, repetitions=(4,4,1), periodic=(True,True,False)) types = ['a','a','a','a','b','b', 'a','a','a','b','b','b', 'b','b','a','a','b','a', 'b','b','b','a','b','a', 'b','a','a','a','b','b', 'b','b','b','b','b','b', 'a','a','a','a','b','b', 'b','b','a','b','b','a'] # Setup the configuration. config = KMCConfiguration(lattice=lattice, types=types, possible_types=['a','c','b']) # Get the types information out. ret_types = config.types() # Check that they are what we inserted. self.assertEqual(types, ret_types) # Check that the possible types are what we expect. self.assertEqual(set(['a','c','b','*']), set(config._KMCConfiguration__possible_types.keys())) # Check that the number of lattice sites corresponds to the lattice. self.assertEqual(config._KMCConfiguration__n_lattice_sites, len(lattice.sites())) # Check that the lattice site can be returned from the configuration. self.assertAlmostEqual( numpy.linalg.norm(numpy.array(config.sites()) - numpy.array(lattice.sites())), 0.0, 10 ) # Construct without possible types and check that the list is set correctly # from the given types. config = KMCConfiguration(lattice=lattice, types=types) self.assertEqual(set(['a','b','*']), set(config._KMCConfiguration__possible_types.keys()))
def testGetScriptComponent(self): """ Test the general get script component function. """ # Set the path to the file to read from. module_path = os.path.abspath(os.path.dirname(__file__)) script_file_path = os.path.join(module_path, "..", "TestUtilities", "Scripts") script_file_path = os.path.join(script_file_path, "kmc_configuration_script.py") # Make sure the file exists. self.assertTrue(os.path.exists(script_file_path)) # Get the KMCLattice out of this script. lattice = getScriptComponent(script_file_path, KMCLattice) # Check that it is the correct lattice by cheking its values # against these known references. unit_cell = KMCUnitCell(cell_vectors=[[2.8, 0.0, 0.0], [0.0, 3.2, 0.0], [0.0, 0.5, 3.0]], basis_points=[[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [0.25, 0.25, 0.75]]) ref_lattice = KMCLattice(unit_cell=unit_cell, repetitions=(4, 4, 1), periodic=(True, True, False)) self.assertEqual(lattice.repetitions(), ref_lattice.repetitions()) self.assertEqual(lattice.periodic(), ref_lattice.periodic()) self.assertAlmostEqual( numpy.linalg.norm( numpy.array(lattice.sites()) - numpy.array(ref_lattice.sites())), 0.0, 12) self.assertEqual( numpy.linalg.norm( numpy.array(lattice.basis()) - numpy.array(ref_lattice.basis())), 0.0, 12) # Check that the function returns None if no component of the correct type is found. self.assertTrue( (getScriptComponent(script_file_path, numpy.ndarray) is None)) # Check that we fail in a controlled way if there is a problme with the file. self.assertRaises( Error, lambda: getScriptComponent("THIS IS NOT A VALID PATH", numpy .ndarray))
def testQueryLatticeSites(self): """ Test that the returned lattice site data is coorect. """ cell_vectors = [[2.3, 0.0, 0.0], [2.4, 3.0, 0.0], [0.0, 0.0, 11.8]] basis_points = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.0]] unit_cell = KMCUnitCell(cell_vectors=cell_vectors, basis_points=basis_points) # Setup the other input. repetitions = (2,1,1) periodic = (True,True,False) # Construct the KMCLattice object. lattice = KMCLattice(unit_cell=unit_cell, repetitions=repetitions, periodic=periodic) # These are all the sites in the lattice, given in # the fractional coordinates of the original cell. sites = lattice.sites() ref_sites = numpy.array([[ 0. , 0. , 0. ], [ 0.5, 0.5, 0. ], [ 1. , 0. , 0. ], [ 1.5, 0.5, 0. ]]) # Check against a hardcoded reference. self.assertAlmostEqual(numpy.linalg.norm(sites-ref_sites), 0.0, 10) # Test with another repetition. repetitions = (2,3,4) lattice = KMCLattice(unit_cell=unit_cell, repetitions=repetitions, periodic=periodic) sites = lattice.sites() ref_sites = numpy.array([[ 0. , 0. , 0. ], [ 0.5, 0.5, 0. ], [ 0. , 0. , 1. ], [ 0.5, 0.5, 1. ], [ 0. , 0. , 2. ], [ 0.5, 0.5, 2. ], [ 0. , 0. , 3. ], [ 0.5, 0.5, 3. ], [ 0. , 1. , 0. ], [ 0.5, 1.5, 0. ], [ 0. , 1. , 1. ], [ 0.5, 1.5, 1. ], [ 0. , 1. , 2. ], [ 0.5, 1.5, 2. ], [ 0. , 1. , 3. ], [ 0.5, 1.5, 3. ], [ 0. , 2. , 0. ], [ 0.5, 2.5, 0. ], [ 0. , 2. , 1. ], [ 0.5, 2.5, 1. ], [ 0. , 2. , 2. ], [ 0.5, 2.5, 2. ], [ 0. , 2. , 3. ], [ 0.5, 2.5, 3. ], [ 1. , 0. , 0. ], [ 1.5, 0.5, 0. ], [ 1. , 0. , 1. ], [ 1.5, 0.5, 1. ], [ 1. , 0. , 2. ], [ 1.5, 0.5, 2. ], [ 1. , 0. , 3. ], [ 1.5, 0.5, 3. ], [ 1. , 1. , 0. ], [ 1.5, 1.5, 0. ], [ 1. , 1. , 1. ], [ 1.5, 1.5, 1. ], [ 1. , 1. , 2. ], [ 1.5, 1.5, 2. ], [ 1. , 1. , 3. ], [ 1.5, 1.5, 3. ], [ 1. , 2. , 0. ], [ 1.5, 2.5, 0. ], [ 1. , 2. , 1. ], [ 1.5, 2.5, 1. ], [ 1. , 2. , 2. ], [ 1.5, 2.5, 2. ], [ 1. , 2. , 3. ], [ 1.5, 2.5, 3. ]]) # Check against a hardcoded reference. self.assertAlmostEqual(numpy.linalg.norm(sites-ref_sites), 0.0, 10)
def testGetScriptComponent(self): """ Test the general get script component function. """ # Set the path to the file to read from. module_path = os.path.abspath(os.path.dirname(__file__)) script_file_path = os.path.join(module_path,"..","TestUtilities","Scripts") script_file_path = os.path.join(script_file_path, "kmc_configuration_script.py") # Make sure the file exists. self.assertTrue( os.path.exists(script_file_path) ) # Get the KMCLattice out of this script. lattice = getScriptComponent(script_file_path, KMCLattice) # Check that it is the correct lattice by cheking its values # against these known references. unit_cell = KMCUnitCell(cell_vectors=[[2.8,0.0,0.0], [0.0,3.2,0.0], [0.0,0.5,3.0]], basis_points=[[0.0,0.0,0.0], [0.5,0.5,0.5], [0.25,0.25,0.75]]) ref_lattice = KMCLattice(unit_cell=unit_cell, repetitions=(4,4,1), periodic=(True,True,False)) self.assertEqual( lattice.repetitions(), ref_lattice.repetitions() ) self.assertEqual( lattice.periodic(), ref_lattice.periodic() ) self.assertAlmostEqual( numpy.linalg.norm(numpy.array(lattice.sites())-numpy.array(ref_lattice.sites())), 0.0, 12 ) self.assertEqual( numpy.linalg.norm(numpy.array(lattice.basis()) - numpy.array(ref_lattice.basis())), 0.0, 12 ) # Check that the function returns None if no component of the correct type is found. self.assertTrue( (getScriptComponent(script_file_path, numpy.ndarray) is None) ) # Check that we fail in a controlled way if there is a problme with the file. self.assertRaises( Error, lambda: getScriptComponent("THIS IS NOT A VALID PATH", numpy.ndarray) )