def test_all_OK(self): atom = Atom(coordinates=[0, 0.5, 0.989], species='Mn345') self.assertAlmostEqual(atom.x, 0.) self.assertAlmostEqual(atom.y, 0.5) self.assertAlmostEqual(atom.z, 0.989) self.assertEqual(atom.species, 'Mn345') self.assertEqual(atom.element, 'Mn345')
def test_add_atom(self): # non-`Atom` instance with self.assertRaises(StructureError): self.structure.add_atom('Si') # correct usage self.structure.add_atom(Atom([0, 0, 0], 'Si12')) self.assertEqual(len(self.structure.atoms), 1) self.assertListEqual(self.structure.list_of_species, ['Si12'])
def test_all_OK(self): self.structure.comment = 'bcc-CsCl' self.structure.lattice_vectors = [[3.42, 0, 0], [0, 3.42, 0], [0, 0, 3.42]] self.structure.coordinate_system = 'direct' self.structure.add_atom(Atom([0, 0, 0], 'Cs')) self.structure.add_atom(Atom([0.5, 0.5, 0.5], 'Cl')) # composition_dict self.assertDictEqual(self.structure.composition_dict, {'Cs': 1, 'Cl': 1}) # list_of_species self.assertListEqual(self.structure.list_of_species, ['Cl', 'Cs']) poscar = """bcc-CsCl 1.00000000000000 3.42000000000000 0.00000000000000 0.00000000000000 0.00000000000000 3.42000000000000 0.00000000000000 0.00000000000000 0.00000000000000 3.42000000000000 Cl Cs 1 1 Direct 0.50000000000000 0.50000000000000 0.50000000000000 0.00000000000000 0.00000000000000 0.00000000000000""" self.assertEqual(self.structure.POSCAR, poscar)
def read_poscar(poscar_file='POSCAR'): """ :param poscar_file: Location of the VASP POSCAR (version 5) file NOTE: Names of all species (line 6) need to begin with the symbol of a real element. :return: `kelpie.structure.Structure` object """ if not os.path.isfile(poscar_file): error_message = 'Specified POSCAR file {} not found'.format( poscar_file) raise FileNotFoundError(error_message) with open(poscar_file, 'r') as fr: poscar_lines = [line.strip() for line in fr.readlines()] if not _consistent_number_of_atoms(poscar_lines): error_message = 'Mismatch between the number of atoms (Line 7) and the number of atomic coordinates (Lines 9-)' raise KelpieIOError(error_message) poscar_blocks = [ 'system_title', 'scaling_constant', 'lattice_vectors', 'list_of_species', 'list_of_number_of_atoms', 'repeating_list_of_species', 'coordinate_system', 'list_of_atomic_coordinates' ] poscar_as_dict = {} for block in poscar_blocks: poscar_as_dict[block] = globals()['_{}'.format(block)](poscar_lines) s = Structure() s.comment = poscar_as_dict['system_title'] s.scaling_constant = poscar_as_dict['scaling_constant'] s.lattice_vectors = poscar_as_dict['lattice_vectors'] s.coordinate_system = poscar_as_dict['coordinate_system'] for ac, sp in zip(poscar_as_dict['list_of_atomic_coordinates'], poscar_as_dict['repeating_list_of_species']): atom = Atom(coordinates=ac, species=sp) s.add_atom(atom) return s
def test_no_coordinate(self): with self.assertRaises(AtomError): Atom(species='Al')
def test_non_string_species(self): with self.assertRaises(AtomError): Atom(coordinates=[0, 0, 0], species='1Al')
def test_species_starts_with_unknown_element_symbol(self): with self.assertRaises(AtomError): Atom(coordinates=[0, 0, 0], species='Af')
def test_no_species(self): with self.assertRaises(AtomError): Atom(coordinates=[0, 0, 0])
def test_coordinates_shape(self): with self.assertRaises(AtomError): Atom(coordinates=[], species='Al') with self.assertRaises(AtomError): Atom(coordinates=[0, 1, 2, 3], species='Al')
def test_non_float_coordinates(self): with self.assertRaises(AtomError): Atom(coordinates=[0., 'A', 0.], species='Al')