def test_parse_POSCAR_1(self): example_poscar = """whatever 1.0 1 2 3 0.5 0.7 0.91 3 1 0 1 3 Direct -21.84 72 -4.72 17.38 -54 3.54 2.66 -8 0.48 0 0 0""" lay = sc.parse_POSCAR(example_poscar, ["Fe", "Zn"], magmom="2*-1 1 1*0") # a hack around np.ndarray's lack of test-friendly __eq__ self.assertEqual( [lay.vectors()[j][i] for j in range(3) for i in range(3)], [1, 2, 3, 0.5, 0.7, 0.91, 3, 1, 0]) atoms = [ sc.Atom("Fe", (0, 2, 0), spin=(0, 0, -1)), sc.Atom("Zn", (1, 0.5, 3), spin=(0, 0, -1)), sc.Atom("Zn", (0.1, 0.2, 0.7), spin=(0, 0, 1)), sc.Atom("Zn", (0, 0, 0), spin=(0, 0, 0)) ] for a1, a2 in zip(atoms, lay.atoms()): self.assertEqual(a1, a2) self.assertEqual(len(lay.atoms()), 4)
def test_translate(self): l = sc.lattice().set_vectors([2, 0], [0, 2]) l.add_atom("H", (0.0, 1.0)) l.add_atom("He", (1.0, 3.0)) l.translate_atoms((-1, -2)) self.assertEqual(l.atoms()[0], sc.Atom("H", (-1, -1))) self.assertEqual(l.atoms()[1], sc.Atom("He", (0, 1))) l.translate_atoms((0.5, 0.5), unit=sc.Unit.Crystal) self.assertEqual(l.atoms()[0], sc.Atom("H", (0, 0))) self.assertEqual(l.atoms()[1], sc.Atom("He", (1, 2)))
def test_read_poscar_without_atomic_species(self): graphene = sc.read_POSCAR(os.path.join( os.path.dirname(__file__), "../resources/vasp/graphene/POSCAR"), atomic_species=["C"]) self.assertEqual(graphene.atoms()[0], sc.Atom("C", (0, 0))) self.assertEqual( graphene.atoms()[1], sc.Atom("C", np.array([2 / 3, 2 / 3, 0]) @ graphene.vectors())) self.assertAlmostEqual(graphene.vectors()[0][0], 2.133341911) self.assertAlmostEqual(graphene.vectors()[0][1], -1.231685527) self.assertAlmostEqual(graphene.vectors()[1][0], 2.133341911) self.assertAlmostEqual(graphene.vectors()[1][1], 1.231685527)
def test_save_POSCAR(self): os.system("mkdir -p tmp") fn = "tmp/test_POSCAR" lay = sc.lattice() lay.set_vectors([1, 2, 3], [0.5, 0.7, 0.91], [3, 1, 0]) lay.add_atoms([ sc.Atom("Fe", (0, 2)), sc.Atom("Zn", (1, 0.5, 3), spin=(0, 1, -1)), sc.Atom("Zn", (0.1, 0.2, 0.7)), sc.Atom("Zn", (0, 0, 0)) ]) lay.add_atoms([ sc.Atom("Zn", (0.1, 0, 0), pos_unit=sc.Unit.Crystal, spin=(0, 0, 1)), sc.Atom("Zn", (0.2, 0, 0), pos_unit=sc.Unit.Crystal, spin=(0, 0, -1)), sc.Atom("Zn", (0.3, 0, 0), pos_unit=sc.Unit.Crystal, spin=(0, 0, 1)) ]) # first goes Fe (-21.84...) # then Zn, starting with z-spin=1 and in order of adding, # so: z-spin=1: (0.1, 0, 0), (0.3, 0, 0) etc. expected_poscar = """supercell_generated_POSCAR 1.0 1 2 3 0.5 0.7 0.91 3 1 0 Fe Zn 1 6 Direct -21.84 72 -4.72 0.1 0 0 0.3 0 0 2.66 -8 0.48 0 0 0 17.38 -54 3.54 0.2 0 0 """ lay.save_POSCAR(filename=fn) with open(fn, 'r') as f: poscar = f.read() os.system("rm -f " + fn) self.assertEqual(expected_poscar, poscar) # test stdout names = ["Fe", "Zn"] # https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python with patch('sys.stdout', new=StringIO()) as fakeOutput: lay.save_POSCAR() self.assertEqual(fakeOutput.getvalue(), expected_poscar + "\n" + \ "MAGMOM flag: 0 2*1 2*0 2*-1\n")
def test_read_POSCAR_2(self): example_poscar = """ Si 1.00000000000000 5.4365330000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 5.4365330000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 5.4365330000000000 1 Cartesian 0.8750000000000000 0.8750000000000000 0.8750000000000000""" lay = sc.parse_POSCAR(example_poscar, ["Si"]) self.assertEqual(lay.atoms()[0], sc.Atom("Si", (0.875, 0.875, 0.875))) self.assertEqual(len(lay.atoms()), 1)
def test_save_xsf(self): os.system("mkdir -p tmp") fn = "tmp/test.xsf" lay = sc.lattice() lay.set_vectors([1, 2, 3], [0.5, 0.7, 0.91], [3, 1, 0]) lay.add_atoms([ sc.Atom("Fe", (0, 2)), sc.Atom("Zn", (1, 0.5, 3), spin=(0, 1, -1)), sc.Atom("Zn", (0.1, 0.2, 0.7)), sc.Atom("Zn", (0, 0, 0)) ]) expected_xsf = """CRYSTAL PRIMVEC 1 2 3 0.5 0.7 0.91 3 1 0 PRIMCOORD 4 1 26 0 2 0 0 0 0 30 1 0.5 3 0 1 -1 30 0.1 0.2 0.7 0 0 0 30 0 0 0 0 0 0 """ lay.save_xsf(filename=fn) with open(fn, 'r') as f: xsf = f.read() os.system("rm -f " + fn) self.assertEqual(expected_xsf, xsf) # test stdout # https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python with patch('sys.stdout', new=StringIO()) as fakeOutput: lay.save_xsf() self.assertEqual(fakeOutput.getvalue(), expected_xsf)
def test_parse_POSCAR_2(self): # This time with specie names, selective dynamics and initial velocities example_poscar = """whatever 1.0 1 2 3 0.5 0.7 0.91 3 1 0 1 3 Selective dynamics d -21.84 72 -4.72 T T F 17.38 -54 3.54 F T F 2.66 -8 0.48 F F T 0 0 0 F F F 0 0 0 1 2 3 1.00000 2.000000 3.000000 3.0 1 0.000""" lay = sc.parse_POSCAR(example_poscar, ["Fe", "Zn"], magmom="2*-1 1 1*0") # a hack around np.ndarray's lack of test-friendly __eq__ self.assertEqual( [lay.vectors()[j][i] for j in range(3) for i in range(3)], [1, 2, 3, 0.5, 0.7, 0.91, 3, 1, 0]) atoms = [ sc.Atom("Fe", (0, 2, 0), spin=(0, 0, -1)), sc.Atom("Zn", (1, 0.5, 3), spin=(0, 0, -1)), sc.Atom("Zn", (0.1, 0.2, 0.7), spin=(0, 0, 1)), sc.Atom("Zn", (0, 0, 0), spin=(0, 0, 0)) ] for a1, a2 in zip(atoms, lay.atoms()): self.assertEqual(a1, a2) self.assertEqual(len(lay.atoms()), 4)
def test_add_atoms_good(self) -> None: """ Tests methods: add_atom, add_atoms, and atoms """ lay = sc.lattice() # Test default (no atoms in an elementary cell) self.assertEqual(lay.atoms(), []) # Test add_atom Helium (default unit: angstrom) he = sc.Atom("He", (0.1, 0.2, 0.3)) lay.add_atom(he.element, he.pos) self.assertEqual(lay.atoms()[0], he) # Test add_atoms: Hydrogen and Lithium # Retaining element order is expected h, li = sc.Atom("H", np.array([0, 0, 0])), \ sc.Atom("Li", np.array([0.9, 0.9, 0.9]), spin=(0, 1, 2)) lay.add_atoms([h, li]) for a1, a2 in zip(lay.atoms(), [he, h, li]): self.assertEqual(a1, a2) # Add atom using 2D position vector be = sc.Atom("Be", (0.5, 0.5), spin=(0, 0, 1)) lay.add_atoms([be]) self.assertEqual( lay.atoms()[-1], sc.Atom("Be", np.array([0.5, 0.5, 0]), spin=(0, 0, 1))) # When atom is outside the elementary cell, a warning should be logged with self.assertWarns(UserWarning, msg=WarningText.AtomOutsideElementaryCell): lay.add_atom("C", (2, 0, 0)) # Element symbol not in the periodic table with self.assertWarns(UserWarning, msg=WarningText.UnknownChemicalElement): lay.add_atom("Helium", (1, 0, 0)) # should be: "He" # Add atom using crystal units lay = sc.lattice() lay.set_vectors([2, 0, 0], [2, 2, 0], [0, 0, 3]) lay.add_atom("Na", (0.5, 0.5, 0.5), unit=sc.Unit.Crystal) self.assertEqual(lay.atoms()[0], sc.Atom("Na", (2, 1, 1.5))) # Change lattice vectors after adding atoms lay.set_vectors([4, 0, 0], [4, 4, 0], [0, 0, 6], atoms_behaviour=sc.Unit.Crystal) self.assertEqual(lay.atoms()[0], sc.Atom("Na", (4, 2, 3))) lay.set_vectors([8, 0, 0], [8, 8, 0], [0, 0, 12], atoms_behaviour=sc.Unit.Angstrom) self.assertEqual(lay.atoms()[0], sc.Atom("Na", (4, 2, 3))) # List atoms using CRYSTAL units lay = sc.lattice() lay.set_vectors([2, 0, 0], [2, 2, 0], [0, 0, 3]) lay.add_atom("Na", (0.5, 0.5, 0.5), unit=sc.Unit.Crystal) self.assertEqual(lay.atoms()[0], sc.Atom("Na", (2, 1, 1.5))) self.assertEqual( lay.atoms(unit=sc.Unit.Crystal)[0], sc.Atom("Na", (0.5, 0.5, 0.5), pos_unit=sc.Unit.Crystal))