Exemple #1
0
    def test_methanol(self):
        """Test for topology computation."""
        methanol = BondedAtoms(molecule("CH3OH"))

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

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

        bond_list = [(0, 1), (0, 2), (0, 4), (0, 5), (1, 3)]

        for t in bond_list:
            methanol.add_bond(*t)

        # bonds are calculated correctly?
        self.assertEqual(set(tuple(b) for b in methanol.get_bonded_bonds()),
                         {(0, 1), (1, 3), (0, 5), (0, 4), (0, 2)})

        # angles are calculated correctly?
        self.assertEqual(
            set(tuple(a) for a in methanol.get_bonded_angles()), {(1, 0, 2),
                                                                  (1, 0, 4),
                                                                  (1, 0, 5),
                                                                  (2, 0, 4),
                                                                  (2, 0, 5),
                                                                  (4, 0, 5),
                                                                  (0, 1, 3)})

        # dihedrals are calculated correctly?
        dihedrals_ref = {(2, 0, 1, 3), (4, 0, 1, 3), (5, 0, 1, 3)}
        for d in methanol.get_bonded_dihedrals():
            if tuple(d) in dihedrals_ref:
                dihedrals_ref.remove(tuple(d))
            elif tuple(d[::-1]) in dihedrals_ref:
                dihedrals_ref.remove(tuple(d[::-1]))
        self.assertTrue(len(dihedrals_ref) == 0)

        # impropers are calculated correctly?
        self.assertEqual(
            set(tuple(i) for i in methanol.get_bonded_impropers()),
            {(0, 1, 2, 4), (0, 1, 2, 5), (0, 1, 4, 5), (0, 2, 4, 5)})
Exemple #2
0
    def test_casting(self):
        """Test for down-casting from ``ase.Atoms`` to BondedAtoms."""
        atoms = molecule("CH3CH2OH")
        bonded_atoms = BondedAtoms(atoms)

        # the instance is BondedAtoms?
        self.assertTrue(isinstance(bonded_atoms, BondedAtoms))
        # 'bonds' property has been created?
        self.assertTrue("bonds" in bonded_atoms.arrays)
        # 'types' property has been created?
        self.assertTrue("types" in bonded_atoms.arrays)
        # all data except for 'bonds' stays unchanged?
        self.assertTrue(
            all(
                np.allclose(v, atoms.arrays[k])
                for k, v in bonded_atoms.arrays.items()
                if k not in ["bonds", "types"]))
Exemple #3
0
    def test_gold(self):
        """Test for basic operations."""
        a = 4.05  # gold lattice constant
        b = a / 2
        au = BondedAtoms("Au",
                         cell=[(0, b, b), (b, 0, b), (b, b, 0)],
                         pbc=True)

        # initial types are 1?
        self.assertTrue(np.allclose(au.get_types(), np.ones(len(au))))

        au.add_bond(0, 0, img2=(1, 0, 0))

        # a bond is added?
        self.assertTrue(
            np.allclose(
                au.get_bonds(),
                [[[0, 1, 0, 0], [0, -1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]))

        # multiplying and repeating give the same result?
        self.assertTrue(
            np.allclose((au * (2, 2, 2)).get_bonds(),
                        au.repeat((2, 2, 2)).get_bonds()))

        chain = au * (3, 1, 1)

        # types are correctly copied?
        self.assertTrue(np.allclose(chain.get_types(), np.ones(len(chain))))

        chain.change_max_bonds(2)

        # the maximum number of bonds per atoms can be reduced?
        self.assertTrue(
            np.allclose(
                chain.get_bonds(),
                [[[1, 0, 0, 0], [2, -1, 0, 0]], [[1, 0, 0, 0], [-1, 0, 0, 0]],
                 [[-2, 1, 0, 0], [-1, 0, 0, 0]]]))

        chain.remove_bond(0, 2, img2=(-1, 0, 0))

        # a specified bond is removed?
        self.assertTrue(
            np.allclose(
                chain.get_bonds(),
                [[[1, 0, 0, 0], [0, 0, 0, 0]], [[1, 0, 0, 0], [-1, 0, 0, 0]],
                 [[-1, 0, 0, 0], [0, 0, 0, 0]]]))

        chain.set_types([1, 2, 3])

        # types are correctly set?
        self.assertTrue(np.allclose(chain.get_types(), [1, 2, 3]))

        del chain[2]

        # a specified atom is deleted?
        self.assertTrue(np.allclose(chain.get_types(), [1, 2]))

        chain.change_max_bonds(4)

        # the maximum number of bonds per atoms can be increased?
        self.assertTrue(
            np.allclose(
                chain.get_bonds(),
                [[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
                 [[-1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]))
Exemple #4
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)))
    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()
    def test_nacl(self):
        """Test for equivalence between original and written/read data."""
        atoms = BondedAtoms(bulk("NaCl", "rocksalt", a=5.64,
                                 orthorhombic=True))

        # confirm atomic numbers
        self.assertTrue(
            np.allclose(atoms.get_atomic_numbers(), np.array([11, 17, 11,
                                                              17])))

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

        atoms.change_max_bonds(6)

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

        for i, j in itertools.combinations(range(len(atoms)), 2):
            r_original = positions[j] - positions[i]
            for ix, iy, iz in itertools.product(*[(-1, 0, 1)] * 3):
                r = r_original + ix * cell[0] + iy * cell[1] + iz * cell[2]
                if np.isclose(np.linalg.norm(r), 2.82):
                    atoms.add_bond(i, j, img2=(ix, iy, iz))

        atoms *= 5

        atoms.sort_bonds()

        write_files(atoms)

        atoms_from_data = create_atoms_from_data("data.tmp",
                                                 "molecular",
                                                 pbc=True)

        # 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()