Ejemplo n.º 1
0
    def test_wrapping(self):
        """Test for ``BondedAtoms.wrap()``."""
        atoms = BondedAtoms(molecule("CH3CH2OCH3"))

        positions = atoms.get_positions()
        bonded_pairs = [
            (i, j) for i, j in itertools.combinations(range(len(atoms)), 2)
            if np.linalg.norm(positions[i] - positions[j]) < 1.55
        ]

        # there are eleven bonds in CH3CH2OCH3
        self.assertEqual(len(bonded_pairs), 11)

        bond_lengths = sorted(
            atoms.get_distance(*pair) for pair in bonded_pairs)

        for t in bonded_pairs:
            atoms.add_bond(*t)

        def get_bond_length(atoms):

            bond_array = atoms.get_bonds()
            cell = atoms.get_cell()
            positions = atoms.get_positions()

            return sorted(
                np.linalg.norm((cell * bond[1:]).sum(axis=0) +
                               positions[i + bond[0]] - positions[i])
                for i, bonds in enumerate(bond_array) for bond in bonds
                if 0 < bond[0])

        # bond length is correct before wrapping?
        self.assertTrue(np.allclose(bond_lengths, get_bond_length(atoms)))

        atoms.set_cell([[5., 0., 0.], [0., 5., 0.], [0., 0., 5.]])
        atoms.set_pbc(True)

        atoms.wrap()

        # bond length is correct after wrapping?
        self.assertTrue(np.allclose(bond_lengths, get_bond_length(atoms)))
Ejemplo n.º 2
0
    def test_methanol(self):
        """Test for equivalence between original and written/read data."""
        atoms = BondedAtoms(molecule("CH3OH"))

        # confirm atomic numbers
        self.assertTrue(
            np.allclose(atoms.get_atomic_numbers(),
                        np.array([6, 8, 1, 1, 1, 1])))

        # confirm O-H distance
        self.assertTrue(np.allclose(atoms.get_distance(1, 3), 0.97))

        atoms.set_types([1, 2, 3, 4, 3, 3])

        positions = atoms.get_positions()

        bonded_pairs = [
            (i, j) for i, j in itertools.combinations(range(len(atoms)), 2)
            if np.linalg.norm(positions[i] - positions[j]) < 1.5
        ]

        # there are five bonds in CH3OH
        self.assertEqual(len(bonded_pairs), 5)

        for pair in bonded_pairs:
            atoms.add_bond(*pair)

        atoms.sort_bonds()

        atoms.set_cell([[5., 0., 0.], [0., 5., 0.], [0., 0., 5.]])
        atoms.center()

        write_files(atoms)

        atoms_from_data = create_atoms_from_data("data.tmp", "molecular")
        atoms_from_molecule = create_atoms_from_molecule("molecule.tmp")

        # atoms from Lammps' data and molecule file must be eaqual.
        self.assertTrue(
            np.allclose(atoms_from_data.get_positions(),
                        atoms_from_molecule.get_positions()))
        self.assertTrue(
            np.allclose(atoms_from_data.get_masses(),
                        atoms_from_molecule.get_masses()))
        self.assertTrue(
            np.allclose(atoms_from_data.get_types(),
                        atoms_from_molecule.get_types()))
        self.assertTrue(
            np.allclose(atoms_from_data.get_bonds(),
                        atoms_from_molecule.get_bonds()))

        # comparison with original atoms
        self.assertTrue(
            np.allclose(atoms_from_data.get_positions(),
                        atoms.get_positions()))
        self.assertTrue(
            np.allclose(atoms_from_data.get_masses(), atoms.get_masses()))
        self.assertTrue(
            np.allclose(atoms_from_data.get_types(), atoms.get_types()))

        # storing order of bonds might be changed
        atoms_from_data.sort_bonds()
        self.assertTrue(
            np.allclose(atoms_from_data.get_bonds(), atoms.get_bonds()))

        remove_files()