def get_structures_from_trajectory(self): """ Convert the coordinates in each time step to a structure(boxed molecule). Used to construct DiffusionAnalyzer object. Returns: list of Structure objects """ structures = [] mass_to_symbol = dict( (round(y["Atomic mass"], 1), x) for x, y in _pt_data.items()) unique_atomic_masses = np.array(self.lammps_data.atomic_masses)[:, 1] for step in range(self.timesteps.size): begin = step * self.natoms end = (step + 1) * self.natoms mol_vector_structured = \ self.trajectory[begin:end][:][["x", "y", "z"]] new_shape = mol_vector_structured.shape + (-1, ) mol_vector = mol_vector_structured.view( np.float64).reshape(new_shape) coords = mol_vector.copy() species = [ mass_to_symbol[round(unique_atomic_masses[atype - 1], 1)] for atype in self.trajectory[begin:end][:]["atom_type"] ] mol = Molecule(species, coords) try: boxed_mol = mol.get_boxed_structure(*self.box_lengths) except ValueError as error: print("Error: '{}' at timestep {} in the trajectory".format( error, int(self.timesteps[step]))) structures.append(boxed_mol) return structures
def get_structures_from_trajectory(self): """ Convert the coordinates in each time step to a structure(boxed molecule). Used to construct DiffusionAnalyzer object. Returns: list of Structure objects """ structures = [] mass_to_symbol = dict( (round(y["Atomic mass"], 1), x) for x, y in _pt_data.items()) unique_atomic_masses = np.array(self.lammps_data.atomic_masses)[:, 1] for step in range(self.timesteps.size): begin = step * self.natoms end = (step + 1) * self.natoms mol_vector_structured = \ self.trajectory[begin:end][:][["x", "y", "z"]] new_shape = mol_vector_structured.shape + (-1,) mol_vector = mol_vector_structured.view(np.float64).reshape( new_shape) coords = mol_vector.copy() species = [mass_to_symbol[round(unique_atomic_masses[atype - 1], 1)] for atype in self.trajectory[begin:end][:]["atom_type"]] mol = Molecule(species, coords) try: boxed_mol = mol.get_boxed_structure(*self.box_lengths) except ValueError as error: print("Error: '{}' at timestep {} in the trajectory".format( error, int(self.timesteps[step]))) structures.append(boxed_mol) return structures
def get_displacements(self): """ Return the initial structure and displacements for each time step. Used to interface witht the DiffusionAnalyzer. Returns: Structure object, numpy array of displacements """ lattice = Lattice([[self.box_lengths[0], 0, 0], [0, self.box_lengths[1], 0], [0, 0, self.box_lengths[2]]]) mass_to_symbol = dict( (round(y["Atomic mass"], 1), x) for x, y in _pt_data.items()) unique_atomic_masses = np.array(self.lammps_data.atomic_masses)[:, 1] frac_coords = [] for step in range(self.timesteps.size): begin = step * self.natoms end = (step + 1) * self.natoms mol_vector_structured = \ self.trajectory[begin:end][:][["x", "y", "z"]] new_shape = mol_vector_structured.shape + (-1, ) mol_vector = mol_vector_structured.view( np.float64).reshape(new_shape) coords = mol_vector.copy() if step == 0: species = [ mass_to_symbol[round(unique_atomic_masses[atype - 1], 1)] for atype in self.trajectory[begin:end][:]["atom_type"] ] mol = Molecule(species, coords) structure = mol.get_boxed_structure(*self.box_lengths) step_frac_coords = [ lattice.get_fractional_coords(crd) for crd in coords ] frac_coords.append(np.array(step_frac_coords)[:, None]) frac_coords = np.concatenate(frac_coords, axis=1) dp = frac_coords[:, 1:] - frac_coords[:, :-1] dp = dp - np.round(dp) f_disp = np.cumsum(dp, axis=1) disp = lattice.get_cartesian_coords(f_disp) return structure, disp
def boxed_molecule(cls, pseudos, cart_coords, acell=3*(10,)): """ Creates a molecule in a periodic box of lengths acell [Bohr] Args: pseudos: List of pseudopotentials cart_coords: Cartesian coordinates acell: Lengths of the box in *Bohr* """ cart_coords = np.atleast_2d(cart_coords) molecule = Molecule([p.symbol for p in pseudos], cart_coords) l = Bohr2Ang(acell) structure = molecule.get_boxed_structure(l[0], l[1], l[2]) return cls(structure)
def boxed_molecule(cls, pseudos, cart_coords, acell=3*(10,)): """ Creates a molecule in a periodic box of lengths acell [Bohr] Args: pseudos: List of pseudopotentials cart_coords: Cartesian coordinates acell: Lengths of the box in *Bohr* """ cart_coords = np.atleast_2d(cart_coords) molecule = Molecule([p.symbol for p in pseudos], cart_coords) l = ArrayWithUnit(acell, "bohr").to("ang") structure = molecule.get_boxed_structure(l[0], l[1], l[2]) return cls(structure)
def get_displacements(self): """ Return the initial structure and displacements for each time step. Used to interface witht the DiffusionAnalyzer. Returns: Structure object, numpy array of displacements """ lattice = Lattice([[self.box_lengths[0], 0, 0], [0, self.box_lengths[1], 0], [0, 0, self.box_lengths[2]]]) mass_to_symbol = dict( (round(y["Atomic mass"], 1), x) for x, y in _pt_data.items()) unique_atomic_masses = np.array(self.lammps_data.atomic_masses)[:, 1] frac_coords = [] for step in range(self.timesteps.size): begin = step * self.natoms end = (step + 1) * self.natoms mol_vector_structured = \ self.trajectory[begin:end][:][["x", "y", "z"]] new_shape = mol_vector_structured.shape + (-1,) mol_vector = mol_vector_structured.view(np.float64).reshape( new_shape) coords = mol_vector.copy() if step == 0: species = [ mass_to_symbol[round(unique_atomic_masses[atype - 1], 1)] for atype in self.trajectory[begin:end][:]["atom_type"]] mol = Molecule(species, coords) structure = mol.get_boxed_structure(*self.box_lengths) step_frac_coords = [lattice.get_fractional_coords(crd) for crd in coords] frac_coords.append(np.array(step_frac_coords)[:, None]) frac_coords = np.concatenate(frac_coords, axis=1) dp = frac_coords[:, 1:] - frac_coords[:, :-1] dp = dp - np.round(dp) f_disp = np.cumsum(dp, axis=1) disp = lattice.get_cartesian_coords(f_disp) return structure, disp
class IMoleculeTest(PymatgenTest): def setUp(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000]] self.coords = coords self.mol = Molecule(["C", "H", "H", "H", "H"], coords) def test_bad_molecule(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000], [-0.513360, 0.889165, -0.36301]] self.assertRaises(StructureError, Molecule, ["C", "H", "H", "H", "H", "H"], coords, validate_proximity=True) def test_get_angle_dihedral(self): self.assertAlmostEqual(self.mol.get_angle(1, 0, 2), 109.47122144618737) self.assertAlmostEqual(self.mol.get_angle(3, 1, 2), 60.00001388659683) self.assertAlmostEqual(self.mol.get_dihedral(0, 1, 2, 3), - 35.26438851071765) coords = list() coords.append([0, 0, 0]) coords.append([0, 0, 1]) coords.append([0, 1, 1]) coords.append([1, 1, 1]) self.mol2 = Molecule(["C", "O", "N", "S"], coords) self.assertAlmostEqual(self.mol2.get_dihedral(0, 1, 2, 3), -90) def test_get_covalent_bonds(self): self.assertEqual(len(self.mol.get_covalent_bonds()), 4) def test_properties(self): self.assertEqual(len(self.mol), 5) self.assertTrue(self.mol.is_ordered) self.assertEqual(self.mol.formula, "H4 C1") def test_repr_str(self): ans = """Full Formula (H4 C1) Reduced Formula: H4C Charge = 0, Spin Mult = 1 Sites (5) 0 C 0.000000 0.000000 0.000000 1 H 0.000000 0.000000 1.089000 2 H 1.026719 0.000000 -0.363000 3 H -0.513360 -0.889165 -0.363000 4 H -0.513360 0.889165 -0.363000""" self.assertEqual(self.mol.__str__(), ans) ans = """Molecule Summary Site: C (0.0000, 0.0000, 0.0000) Site: H (0.0000, 0.0000, 1.0890) Site: H (1.0267, 0.0000, -0.3630) Site: H (-0.5134, -0.8892, -0.3630) Site: H (-0.5134, 0.8892, -0.3630)""" self.assertEqual(repr(self.mol), ans) def test_site_properties(self): propertied_mol = Molecule(["C", "H", "H", "H", "H"], self.coords, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) self.assertEqual(propertied_mol[0].magmom, 0.5) self.assertEqual(propertied_mol[1].magmom, -0.5) def test_get_boxed_structure(self): s = self.mol.get_boxed_structure(9, 9, 9) # C atom should be in center of box. self.assertArrayAlmostEqual(s[4].frac_coords, [0.50000001, 0.5, 0.5]) self.assertArrayAlmostEqual(s[1].frac_coords, [0.6140799, 0.5, 0.45966667]) self.assertRaises(ValueError, self.mol.get_boxed_structure, 1, 1, 1) s2 = self.mol.get_boxed_structure(5, 5, 5, (2, 3, 4)) self.assertEqual(len(s2), 24 * 5) self.assertEqual(s2.lattice.abc, (10, 15, 20)) def test_get_distance(self): self.assertAlmostEqual(self.mol.get_distance(0, 1), 1.089) def test_get_neighbors(self): nn = self.mol.get_neighbors(self.mol[0], 1) self.assertEqual(len(nn), 0) nn = self.mol.get_neighbors(self.mol[0], 2) self.assertEqual(len(nn), 4) def test_get_neighbors_in_shell(self): nn = self.mol.get_neighbors_in_shell([0, 0, 0], 0, 1) self.assertEqual(len(nn), 1) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 1, 0.9) self.assertEqual(len(nn), 4) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 1, 0.9) self.assertEqual(len(nn), 4) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 2, 0.1) self.assertEqual(len(nn), 0) def test_get_dist_matrix(self): ans = [[0.0, 1.089, 1.08899995636, 1.08900040717, 1.08900040717], [1.089, 0.0, 1.77832952654, 1.7783298026, 1.7783298026], [1.08899995636, 1.77832952654, 0.0, 1.77833003783, 1.77833003783], [1.08900040717, 1.7783298026, 1.77833003783, 0.0, 1.77833], [1.08900040717, 1.7783298026, 1.77833003783, 1.77833, 0.0]] self.assertArrayAlmostEqual(self.mol.distance_matrix, ans) def test_break_bond(self): (mol1, mol2) = self.mol.break_bond(0, 1) self.assertEqual(mol1.formula, "H3 C1") self.assertEqual(mol2.formula, "H1") def test_prop(self): self.assertEqual(self.mol.charge, 0) self.assertEqual(self.mol.spin_multiplicity, 1) self.assertEqual(self.mol.nelectrons, 10) self.assertArrayAlmostEqual(self.mol.center_of_mass, [0, 0, 0]) self.assertRaises(ValueError, Molecule, ["C", "H", "H", "H", "H"], self.coords, charge=1, spin_multiplicity=1) mol = Molecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertEqual(mol.spin_multiplicity, 2) self.assertEqual(mol.nelectrons, 9) #Triplet O2 mol = IMolecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) self.assertEqual(mol.spin_multiplicity, 3) def test_equal(self): mol = IMolecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertNotEqual(mol, self.mol) def test_get_centered_molecule(self): mol = IMolecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) centered = mol.get_centered_molecule() self.assertArrayAlmostEqual(centered.center_of_mass, [0, 0, 0]) def test_to_from_dict(self): d = self.mol.as_dict() mol2 = IMolecule.from_dict(d) self.assertEqual(type(mol2), IMolecule) propertied_mol = Molecule(["C", "H", "H", "H", "H"], self.coords, charge=1, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) d = propertied_mol.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 0.5) mol = Molecule.from_dict(d) self.assertEqual(propertied_mol, mol) self.assertEqual(mol[0].magmom, 0.5) self.assertEqual(mol.formula, "H4 C1") self.assertEqual(mol.charge, 1) def test_to_from_file_string(self): for fmt in ["xyz", "json", "g03", "yaml"]: s = self.mol.to(fmt=fmt) self.assertIsNotNone(s) m = IMolecule.from_str(s, fmt=fmt) self.assertEqual(m, self.mol) self.assertIsInstance(m, IMolecule) self.mol.to(filename="CH4_testing.xyz") self.assertTrue(os.path.exists("CH4_testing.xyz")) os.remove("CH4_testing.xyz") self.mol.to(filename="CH4_testing.yaml") self.assertTrue(os.path.exists("CH4_testing.yaml")) mol = Molecule.from_file("CH4_testing.yaml") self.assertEqual(self.mol, mol) os.remove("CH4_testing.yaml")
class IMoleculeTest(PymatgenTest): def setUp(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000]] self.coords = coords self.mol = Molecule(["C", "H", "H", "H", "H"], coords) def test_bad_molecule(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000], [-0.513360, 0.889165, -0.36301]] self.assertRaises(StructureError, Molecule, ["C", "H", "H", "H", "H", "H"], coords, validate_proximity=True) def test_get_angle_dihedral(self): self.assertAlmostEqual(self.mol.get_angle(1, 0, 2), 109.47122144618737) self.assertAlmostEqual(self.mol.get_angle(3, 1, 2), 60.00001388659683) self.assertAlmostEqual(self.mol.get_dihedral(0, 1, 2, 3), -35.26438851071765) coords = list() coords.append([0, 0, 0]) coords.append([0, 0, 1]) coords.append([0, 1, 1]) coords.append([1, 1, 1]) self.mol2 = Molecule(["C", "O", "N", "S"], coords) self.assertAlmostEqual(self.mol2.get_dihedral(0, 1, 2, 3), -90) def test_get_covalent_bonds(self): self.assertEqual(len(self.mol.get_covalent_bonds()), 4) def test_properties(self): self.assertEqual(len(self.mol), 5) self.assertTrue(self.mol.is_ordered) self.assertEqual(self.mol.formula, "H4 C1") def test_repr_str(self): ans = """Molecule Summary (H4 C1) Reduced Formula: H4C Charge = 0, Spin Mult = 1 Sites (5) 1 C 0.000000 0.000000 0.000000 2 H 0.000000 0.000000 1.089000 3 H 1.026719 0.000000 -0.363000 4 H -0.513360 -0.889165 -0.363000 5 H -0.513360 0.889165 -0.363000""" self.assertEqual(str(self.mol), ans) ans = """Molecule Summary Site: C (0.0000, 0.0000, 0.0000) Site: H (0.0000, 0.0000, 1.0890) Site: H (1.0267, 0.0000, -0.3630) Site: H (-0.5134, -0.8892, -0.3630) Site: H (-0.5134, 0.8892, -0.3630)""" self.assertEqual(repr(self.mol), ans) def test_site_properties(self): propertied_mol = Molecule( ["C", "H", "H", "H", "H"], self.coords, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) self.assertEqual(propertied_mol[0].magmom, 0.5) self.assertEqual(propertied_mol[1].magmom, -0.5) def test_get_boxed_structure(self): s = self.mol.get_boxed_structure(9, 9, 9) # C atom should be in center of box. self.assertArrayAlmostEqual(s[4].frac_coords, [0.50000001, 0.5, 0.5]) self.assertArrayAlmostEqual(s[1].frac_coords, [0.6140799, 0.5, 0.45966667]) self.assertRaises(ValueError, self.mol.get_boxed_structure, 1, 1, 1) s2 = self.mol.get_boxed_structure(5, 5, 5, (2, 3, 4)) self.assertEqual(len(s2), 24 * 5) self.assertEqual(s2.lattice.abc, (10, 15, 20)) def test_get_distance(self): self.assertAlmostEqual(self.mol.get_distance(0, 1), 1.089) def test_get_neighbors(self): nn = self.mol.get_neighbors(self.mol[0], 1) self.assertEqual(len(nn), 0) nn = self.mol.get_neighbors(self.mol[0], 2) self.assertEqual(len(nn), 4) def test_get_neighbors_in_shell(self): nn = self.mol.get_neighbors_in_shell([0, 0, 0], 0, 1) self.assertEqual(len(nn), 1) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 1, 0.9) self.assertEqual(len(nn), 4) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 2, 0.1) self.assertEqual(len(nn), 0) def test_get_dist_matrix(self): ans = [[0.0, 1.089, 1.08899995636, 1.08900040717, 1.08900040717], [1.089, 0.0, 1.77832952654, 1.7783298026, 1.7783298026], [ 1.08899995636, 1.77832952654, 0.0, 1.77833003783, 1.77833003783 ], [1.08900040717, 1.7783298026, 1.77833003783, 0.0, 1.77833], [1.08900040717, 1.7783298026, 1.77833003783, 1.77833, 0.0]] self.assertArrayAlmostEqual(self.mol.distance_matrix, ans) def test_break_bond(self): (mol1, mol2) = self.mol.break_bond(0, 1) self.assertEqual(mol1.formula, "H3 C1") self.assertEqual(mol2.formula, "H1") def test_prop(self): self.assertEqual(self.mol.charge, 0) self.assertEqual(self.mol.spin_multiplicity, 1) self.assertEqual(self.mol.nelectrons, 10) self.assertArrayAlmostEqual(self.mol.center_of_mass, [0, 0, 0]) self.assertRaises(ValueError, Molecule, ["C", "H", "H", "H", "H"], self.coords, charge=1, spin_multiplicity=1) mol = Molecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertEqual(mol.spin_multiplicity, 2) self.assertEqual(mol.nelectrons, 9) #Triplet O2 mol = IMolecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) self.assertEqual(mol.spin_multiplicity, 3) def test_equal(self): mol = IMolecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertNotEqual(mol, self.mol) def test_get_centered_molecule(self): mol = IMolecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) centered = mol.get_centered_molecule() self.assertArrayAlmostEqual(centered.center_of_mass, [0, 0, 0]) def test_to_from_dict(self): d = self.mol.to_dict mol2 = IMolecule.from_dict(d) self.assertEqual(type(mol2), IMolecule) propertied_mol = Molecule( ["C", "H", "H", "H", "H"], self.coords, charge=1, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) d = propertied_mol.to_dict self.assertEqual(d['sites'][0]['properties']['magmom'], 0.5) mol = Molecule.from_dict(d) self.assertEqual(propertied_mol, mol) self.assertEqual(mol[0].magmom, 0.5) self.assertEqual(mol.formula, "H4 C1") self.assertEqual(mol.charge, 1)
class MoleculeTest(unittest.TestCase): def setUp(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000]] self.coords = coords self.mol = Molecule(["C", "H", "H", "H", "H"], coords) def test_get_angle_dihedral(self): self.assertAlmostEqual(self.mol.get_angle(1, 0, 2), 109.47122144618737) self.assertAlmostEqual(self.mol.get_angle(3, 1, 2), 60.00001388659683) self.assertAlmostEqual(self.mol.get_dihedral(0, 1, 2, 3), - 35.26438851071765) coords = list() coords.append([0, 0, 0]) coords.append([0, 0, 1]) coords.append([0, 1, 1]) coords.append([1, 1, 1]) self.mol2 = Molecule(["C", "O", "N", "S"], coords) self.assertAlmostEqual(self.mol2.get_dihedral(0, 1, 2, 3), -90) def test_get_covalent_bonds(self): self.assertEqual(len(self.mol.get_covalent_bonds()), 4) def test_properties(self): self.assertEqual(len(self.mol), 5) self.assertTrue(self.mol.is_ordered) self.assertEqual(self.mol.formula, "H4 C1") def test_repr_str(self): ans = """Molecule Summary (H4 C1) Reduced Formula: H4C Sites (5) 1 C 0.000000 0.000000 0.000000 2 H 0.000000 0.000000 1.089000 3 H 1.026719 0.000000 -0.363000 4 H -0.513360 -0.889165 -0.363000 5 H -0.513360 0.889165 -0.363000""" self.assertEqual(str(self.mol), ans) ans = """Molecule Summary Site: C (0.0000, 0.0000, 0.0000) Site: H (0.0000, 0.0000, 1.0890) Site: H (1.0267, 0.0000, -0.3630) Site: H (-0.5134, -0.8892, -0.3630) Site: H (-0.5134, 0.8892, -0.3630)""" self.assertEqual(repr(self.mol), ans) def test_site_properties(self): propertied_mol = Molecule(["C", "H", "H", "H", "H"], self.coords, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) self.assertEqual(propertied_mol[0].magmom, 0.5) self.assertEqual(propertied_mol[1].magmom, -0.5) def test_to_from_dict(self): propertied_mol = Molecule(["C", "H", "H", "H", "H"], self.coords, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) d = propertied_mol.to_dict self.assertEqual(d['sites'][0]['properties']['magmom'], 0.5) mol = Molecule.from_dict(d) self.assertEqual(propertied_mol, mol) self.assertEqual(mol[0].magmom, 0.5) self.assertEqual(mol.formula, "H4 C1") def test_get_boxed_structure(self): s = self.mol.get_boxed_structure(9, 9, 9) self.assertTrue(np.allclose(s[1].frac_coords, [0.000000, 0.000000, 0.121000])) self.assertRaises(ValueError, self.mol.get_boxed_structure, 1, 1, 1) def test_get_distance(self): self.assertAlmostEqual(self.mol.get_distance(0, 1), 1.089) def test_get_neighbors(self): nn = self.mol.get_neighbors(self.mol[0], 1) self.assertEqual(len(nn), 0) nn = self.mol.get_neighbors(self.mol[0], 2) self.assertEqual(len(nn), 4) def test_get_neighbors_in_shell(self): nn = self.mol.get_neighbors_in_shell([0, 0, 0], 0, 1) self.assertEqual(len(nn), 1) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 1, 0.9) self.assertEqual(len(nn), 4) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 2, 0.1) self.assertEqual(len(nn), 0) def test_get_dist_matrix(self): ans = [[0.0, 1.089, 1.08899995636, 1.08900040717, 1.08900040717], [1.089, 0.0, 1.77832952654, 1.7783298026, 1.7783298026], [1.08899995636, 1.77832952654, 0.0, 1.77833003783, 1.77833003783], [1.08900040717, 1.7783298026, 1.77833003783, 0.0, 1.77833], [1.08900040717, 1.7783298026, 1.77833003783, 1.77833, 0.0]] self.assertTrue(np.allclose(self.mol.distance_matrix, ans)) def test_break_bond(self): (mol1, mol2) = self.mol.break_bond(0, 1) self.assertEqual(mol1.formula, "H3 C1") self.assertEqual(mol2.formula, "H1") def test_prop(self): self.assertEqual(self.mol.charge, 0) self.assertEqual(self.mol.spin_multiplicity, 1) self.assertEqual(self.mol.nelectrons, 10) self.assertTrue(np.allclose(self.mol.center_of_mass, np.zeros(3), atol=1e-7)) self.assertRaises(ValueError, Molecule, ["C", "H", "H", "H", "H"], self.coords, charge=1, spin_multiplicity=1) mol = Molecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertEqual(mol.spin_multiplicity, 2) self.assertEqual(mol.nelectrons, 9) #Triplet O2 mol = Molecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) self.assertEqual(mol.spin_multiplicity, 3) def test_equal(self): mol = Molecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertNotEqual(mol, self.mol) def test_get_centered_molecule(self): mol = Molecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) centered = mol.get_centered_molecule() self.assertFalse(np.allclose(mol.center_of_mass, np.zeros(3), atol=1e-7)) self.assertTrue(np.allclose(centered.center_of_mass, np.zeros(3), atol=1e-7))
class IMoleculeTest(PymatgenTest): def setUp(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000]] self.coords = coords self.mol = Molecule(["C", "H", "H", "H", "H"], coords) def test_set_item(self): s = self.mol.copy() s[0] = "Si" self.assertEqual(s.formula, "Si1 H4") s[(0, 1)] = "Ge" self.assertEqual(s.formula, "Ge2 H3") s[0:2] = "Sn" self.assertEqual(s.formula, "Sn2 H3") s = self.mol.copy() s["H"] = "F" self.assertEqual(s.formula, "C1 F4") s["C"] = "C0.25Si0.5" self.assertEqual(s.formula, "Si0.5 C0.25 F4") s["C"] = "C0.25Si0.5" self.assertEqual(s.formula, "Si0.625 C0.0625 F4") def test_bad_molecule(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360, 0.889165, -0.363000], [-0.513360, 0.889165, -0.36301]] self.assertRaises(StructureError, Molecule, ["C", "H", "H", "H", "H", "H"], coords, validate_proximity=True) def test_get_angle_dihedral(self): self.assertAlmostEqual(self.mol.get_angle(1, 0, 2), 109.47122144618737) self.assertAlmostEqual(self.mol.get_angle(3, 1, 2), 60.00001388659683) self.assertAlmostEqual(self.mol.get_dihedral(0, 1, 2, 3), -35.26438851071765) coords = list() coords.append([0, 0, 0]) coords.append([0, 0, 1]) coords.append([0, 1, 1]) coords.append([1, 1, 1]) self.mol2 = Molecule(["C", "O", "N", "S"], coords) self.assertAlmostEqual(self.mol2.get_dihedral(0, 1, 2, 3), -90) def test_get_covalent_bonds(self): self.assertEqual(len(self.mol.get_covalent_bonds()), 4) def test_properties(self): self.assertEqual(len(self.mol), 5) self.assertTrue(self.mol.is_ordered) self.assertEqual(self.mol.formula, "H4 C1") def test_repr_str(self): ans = """Full Formula (H4 C1) Reduced Formula: H4C Charge = 0, Spin Mult = 1 Sites (5) 0 C 0.000000 0.000000 0.000000 1 H 0.000000 0.000000 1.089000 2 H 1.026719 0.000000 -0.363000 3 H -0.513360 -0.889165 -0.363000 4 H -0.513360 0.889165 -0.363000""" self.assertEqual(self.mol.__str__(), ans) ans = """Molecule Summary Site: C (0.0000, 0.0000, 0.0000) Site: H (0.0000, 0.0000, 1.0890) Site: H (1.0267, 0.0000, -0.3630) Site: H (-0.5134, -0.8892, -0.3630) Site: H (-0.5134, 0.8892, -0.3630)""" self.assertEqual(repr(self.mol), ans) def test_site_properties(self): propertied_mol = Molecule( ["C", "H", "H", "H", "H"], self.coords, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) self.assertEqual(propertied_mol[0].magmom, 0.5) self.assertEqual(propertied_mol[1].magmom, -0.5) def test_get_boxed_structure(self): s = self.mol.get_boxed_structure(9, 9, 9) # C atom should be in center of box. self.assertArrayAlmostEqual(s[4].frac_coords, [0.50000001, 0.5, 0.5]) self.assertArrayAlmostEqual(s[1].frac_coords, [0.6140799, 0.5, 0.45966667]) self.assertRaises(ValueError, self.mol.get_boxed_structure, 1, 1, 1) s2 = self.mol.get_boxed_structure(5, 5, 5, (2, 3, 4)) self.assertEqual(len(s2), 24 * 5) self.assertEqual(s2.lattice.abc, (10, 15, 20)) # Test offset option s3 = self.mol.get_boxed_structure(9, 9, 9, offset=[0.5, 0.5, 0.5]) self.assertArrayAlmostEqual(s3[4].coords, [5, 5, 5]) # Test no_cross option self.assertRaises(ValueError, self.mol.get_boxed_structure, 5, 5, 5, offset=[10, 10, 10], no_cross=True) def test_get_distance(self): self.assertAlmostEqual(self.mol.get_distance(0, 1), 1.089) def test_get_neighbors(self): nn = self.mol.get_neighbors(self.mol[0], 1) self.assertEqual(len(nn), 0) nn = self.mol.get_neighbors(self.mol[0], 2) self.assertEqual(len(nn), 4) def test_get_neighbors_in_shell(self): nn = self.mol.get_neighbors_in_shell([0, 0, 0], 0, 1) self.assertEqual(len(nn), 1) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 1, 0.9) self.assertEqual(len(nn), 4) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 1, 0.9) self.assertEqual(len(nn), 4) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 2, 0.1) self.assertEqual(len(nn), 0) def test_get_dist_matrix(self): ans = [[0.0, 1.089, 1.08899995636, 1.08900040717, 1.08900040717], [1.089, 0.0, 1.77832952654, 1.7783298026, 1.7783298026], [ 1.08899995636, 1.77832952654, 0.0, 1.77833003783, 1.77833003783 ], [1.08900040717, 1.7783298026, 1.77833003783, 0.0, 1.77833], [1.08900040717, 1.7783298026, 1.77833003783, 1.77833, 0.0]] self.assertArrayAlmostEqual(self.mol.distance_matrix, ans) def test_break_bond(self): (mol1, mol2) = self.mol.break_bond(0, 1) self.assertEqual(mol1.formula, "H3 C1") self.assertEqual(mol2.formula, "H1") def test_prop(self): self.assertEqual(self.mol.charge, 0) self.assertEqual(self.mol.spin_multiplicity, 1) self.assertEqual(self.mol.nelectrons, 10) self.assertArrayAlmostEqual(self.mol.center_of_mass, [0, 0, 0]) self.assertRaises(ValueError, Molecule, ["C", "H", "H", "H", "H"], self.coords, charge=1, spin_multiplicity=1) mol = Molecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertEqual(mol.spin_multiplicity, 2) self.assertEqual(mol.nelectrons, 9) #Triplet O2 mol = IMolecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) self.assertEqual(mol.spin_multiplicity, 3) def test_equal(self): mol = IMolecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertNotEqual(mol, self.mol) def test_get_centered_molecule(self): mol = IMolecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) centered = mol.get_centered_molecule() self.assertArrayAlmostEqual(centered.center_of_mass, [0, 0, 0]) def test_to_from_dict(self): d = self.mol.as_dict() mol2 = IMolecule.from_dict(d) self.assertEqual(type(mol2), IMolecule) propertied_mol = Molecule( ["C", "H", "H", "H", "H"], self.coords, charge=1, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) d = propertied_mol.as_dict() self.assertEqual(d['sites'][0]['properties']['magmom'], 0.5) mol = Molecule.from_dict(d) self.assertEqual(propertied_mol, mol) self.assertEqual(mol[0].magmom, 0.5) self.assertEqual(mol.formula, "H4 C1") self.assertEqual(mol.charge, 1) def test_to_from_file_string(self): for fmt in ["xyz", "json", "g03", "yaml"]: s = self.mol.to(fmt=fmt) self.assertIsNotNone(s) m = IMolecule.from_str(s, fmt=fmt) self.assertEqual(m, self.mol) self.assertIsInstance(m, IMolecule) self.mol.to(filename="CH4_testing.xyz") self.assertTrue(os.path.exists("CH4_testing.xyz")) os.remove("CH4_testing.xyz") self.mol.to(filename="CH4_testing.yaml") self.assertTrue(os.path.exists("CH4_testing.yaml")) mol = Molecule.from_file("CH4_testing.yaml") self.assertEqual(self.mol, mol) os.remove("CH4_testing.yaml")
class MoleculeTest(unittest.TestCase): def setUp(self): coords = [[0.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.089000], [1.026719, 0.000000, -0.363000], [-0.513360, -0.889165, -0.363000], [-0.513360 , 0.889165 , -0.363000]] self.coords = coords self.mol = Molecule(["C", "H", "H", "H", "H"], coords) def test_get_angle_dihedral(self): self.assertAlmostEqual(self.mol.get_angle(1, 0, 2), 109.47122144618737) self.assertAlmostEqual(self.mol.get_angle(3, 1, 2), 60.00001388659683) self.assertAlmostEqual(self.mol.get_dihedral(0, 1, 2, 3), -35.26438851071765) coords = list() coords.append([0, 0, 0]) coords.append([0, 0, 1]) coords.append([0, 1, 1]) coords.append([1, 1, 1]) self.mol2 = Molecule(["C", "O", "N", "S"], coords) self.assertAlmostEqual(self.mol2.get_dihedral(0, 1, 2, 3), -90) def test_properties(self): self.assertEqual(len(self.mol), 5) self.assertTrue(self.mol.is_ordered) self.assertEqual(self.mol.formula, "H4 C1") def test_repr_str(self): ans = """Molecule Summary (H4 C1) Reduced Formula: H4C Sites (5) 1 C 0.000000 0.000000 0.000000 2 H 0.000000 0.000000 1.089000 3 H 1.026719 0.000000 -0.363000 4 H -0.513360 -0.889165 -0.363000 5 H -0.513360 0.889165 -0.363000""" self.assertEqual(str(self.mol), ans) ans = """Molecule Summary Non-periodic Site xyz : (0.0000, 0.0000, 0.0000) element : C occupation : 1.00 Non-periodic Site xyz : (0.0000, 0.0000, 1.0890) element : H occupation : 1.00 Non-periodic Site xyz : (1.0267, 0.0000, -0.3630) element : H occupation : 1.00 Non-periodic Site xyz : (-0.5134, -0.8892, -0.3630) element : H occupation : 1.00 Non-periodic Site xyz : (-0.5134, 0.8892, -0.3630) element : H occupation : 1.00""" self.assertEqual(repr(self.mol), ans) def test_site_properties(self): propertied_mol = Molecule(["C", "H", "H", "H", "H"], self.coords, site_properties={'magmom':[0.5, -0.5, 1, 2, 3]}) self.assertEqual(propertied_mol[0].magmom, 0.5) self.assertEqual(propertied_mol[1].magmom, -0.5) def test_to_from_dict(self): propertied_mol = Molecule(["C", "H", "H", "H", "H"], self.coords, site_properties={'magmom':[0.5, -0.5, 1, 2, 3]}) d = propertied_mol.to_dict self.assertEqual(d['sites'][0]['properties']['magmom'], 0.5) mol = Molecule.from_dict(d) self.assertEqual(mol[0].magmom, 0.5) def test_get_boxed_structure(self): s = self.mol.get_boxed_structure(9, 9, 9) self.assertTrue(np.allclose(s[1].frac_coords, [0.000000 , 0.000000, 0.121000])) self.assertRaises(ValueError, self.mol.get_boxed_structure, 1, 1, 1) def test_get_distance(self): self.assertAlmostEqual(self.mol.get_distance(0, 1), 1.089) def test_get_neighbors(self): nn = self.mol.get_neighbors(self.mol[0], 1) self.assertEqual(len(nn), 0) nn = self.mol.get_neighbors(self.mol[0], 2) self.assertEqual(len(nn), 4) def test_get_neighbors_in_shell(self): nn = self.mol.get_neighbors_in_shell([0, 0, 0], 0, 1) self.assertEqual(len(nn), 1) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 1, 0.9) self.assertEqual(len(nn), 4) nn = self.mol.get_neighbors_in_shell([0, 0, 0], 2, 0.1) self.assertEqual(len(nn), 0) def test_get_dist_matrix(self): ans = [[0.0, 1.089, 1.08899995636, 1.08900040717, 1.08900040717], [1.089, 0.0, 1.77832952654, 1.7783298026, 1.7783298026], [1.08899995636, 1.77832952654, 0.0, 1.77833003783, 1.77833003783], [1.08900040717, 1.7783298026, 1.77833003783, 0.0, 1.77833], [1.08900040717, 1.7783298026, 1.77833003783, 1.77833, 0.0]] self.assertTrue(np.allclose(self.mol.distance_matrix, ans))