def test_atom_type(self): self.s = System.from_arrays( type_array=[ 'Cl', 'Cl', 'O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H', 'Na', 'Na' ], maps={ ('atom', 'molecule'): [0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6] }) idx = self.s.where(type_array='O') assert_npequal(idx['atom'], [ False, False, True, False, False, True, False, False, True, False, False, False, False ]) assert_npequal(idx['molecule'], [False, False, True, True, True, False, False]) self.s = System.from_arrays( type_array=[ 'Cl', 'Cl', 'O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H', 'Na', 'Na' ], maps={ ('atom', 'molecule'): [0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6] }) idx = self.s.where(type_array=['Na', 'Cl']) assert_npequal(idx['atom'], [ True, True, False, False, False, False, False, False, False, False, False, True, True ])
def test_query(): type_array = ['Cl', 'Cl', 'O', 'H', 'H', 'O', 'H', 'H', 'Na', 'Na'] mol_indices = [0, 1, 2, 5, 8, 9] s = System.from_arrays(type_array=type_array, mol_indices=mol_indices) array_eq_( s.where(type_in=['Na', 'Cl']), [True, True, False, False, False, False, False, False, True, True]) array_eq_( s.where(type='Cl'), [True, True, False, False, False, False, False, False, False, False]) # We move the Cl away cl = s.where(type='Cl') s.r_array[cl.nonzero()[0]] = [1, 0, 0] s.box_vectors = np.diag([3, 3, 3]) array_eq_(s.where(type_in=['H', 'O'], within_of=(0.2, [8, 9])), [False, False, True, True, True, True, True, True, False, False]) array_eq_(s.where(type_in=['H', 'O'], within_of=(0.2, 8)), [False, False, True, True, True, True, True, True, False, False]) array_eq_(s.where(type_in=['H', 'O'], within_of=(0.2, [8])), [False, False, True, True, True, True, True, True, False, False])
def test_query(): type_array = ['Cl', 'Cl', 'O', 'H', 'H', 'O', 'H', 'H', 'Na', 'Na'] maps = {('atom', 'molecule'): [0, 1, 2, 2, 2, 3, 3, 3, 4, 5]} s = System.from_arrays(type_array=type_array, maps=maps) assert_npequal( s.where(type_array=['Na', 'Cl'])['atom'], [True, True, False, False, False, False, False, False, True, True]) assert_npequal( s.where(type_array='Cl')['atom'], [True, True, False, False, False, False, False, False, False, False]) # We move the Cl away cl = s.where(type_array='Cl')['atom'] s.r_array[cl.nonzero()[0]] = [1, 0, 0] s.box_vectors = np.diag([3, 3, 3]) assert_npequal( s.where(type_array=['H', 'O'], within_of=(0.2, [8, 9]))['atom'], [False, False, True, True, True, True, True, True, False, False]) assert_npequal( s.where(type_array=['H', 'O'], within_of=(0.2, 8))['atom'], [False, False, True, True, True, True, True, True, False, False]) assert_npequal( s.where(type_array=['H', 'O'], within_of=(0.2, [8]))['atom'], [False, False, True, True, True, True, True, True, False, False])
def test_attributes(): coords = [np.random.rand(10, 3) for i in range(10)] t = np.arange(0, 10, 0.1) traj = Trajectory(coords, t) system = System.from_arrays(r_array=coords[0]) system.update(traj.at(1)) npeq_(system.r_array, coords[1])
def test_bonds(): from chemlab.io import datafile bz = datafile("tests/data/benzene.mol").read('molecule') na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) # Adding bonds s = System.empty(2, 2*bz.n_atoms) s.add(bz) assert_npequal(s.bonds, bz.bonds) assert_npequal(bz.bond_orders, [1, 2, 2, 1, 1, 2]) assert_npequal(s.bond_orders, bz.bond_orders) s.add(bz) assert_npequal(s.bonds, np.concatenate((bz.bonds, bz.bonds + 6))) #assert_npequal(s.bond_orders) # Reordering orig = np.array([[0, 1], [6, 8]]) s.bonds = orig s.reorder_molecules([1, 0]) assert_npequal(s.bonds, np.array([[6, 7], [0, 2]])) # This doesn't change the bond_ordering # Selection ss = subsystem_from_molecules(s, [1]) assert_npequal(ss.bonds, np.array([[0, 1]])) import inspect ss2 = System.from_arrays(**dict(inspect.getmembers(ss))) ss2.r_array += 10.0 ms = merge_systems(ss, ss2) assert_npequal(ms.bonds, np.array([[0, 1], [6, 7]])) assert_npequal(ms.bond_orders, np.array([1, 1])) # From_arrays s = System.from_arrays(mol_indices=[0], bonds=bz.bonds, **bz.__dict__) assert_npequal(s.bonds, bz.bonds) assert_npequal(s.bond_orders, bz.bond_orders) # Get molecule entry # Test the bonds when they're 0 s.bonds = np.array([]) assert_equals(s.get_derived_molecule_array('formula'), 'C6')
def test_bonds(): from chemlab.io import datafile bz = datafile("tests/data/benzene.mol").read('molecule') na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) # Adding bonds s = System.empty(2, 2 * bz.n_atoms) s.add(bz) assert_npequal(s.bonds, bz.bonds) assert_npequal(bz.bond_orders, [1, 2, 2, 1, 1, 2]) assert_npequal(s.bond_orders, bz.bond_orders) s.add(bz) assert_npequal(s.bonds, np.concatenate((bz.bonds, bz.bonds + 6))) #assert_npequal(s.bond_orders) # Reordering orig = np.array([[0, 1], [6, 8]]) s.bonds = orig s.reorder_molecules([1, 0]) assert_npequal(s.bonds, np.array([[6, 7], [0, 2]])) # This doesn't change the bond_ordering # Selection ss = subsystem_from_molecules(s, [1]) assert_npequal(ss.bonds, np.array([[0, 1]])) import inspect ss2 = System.from_arrays(**dict(inspect.getmembers(ss))) ss2.r_array += 10.0 ms = merge_systems(ss, ss2) assert_npequal(ms.bonds, np.array([[0, 1], [6, 7]])) assert_npequal(ms.bond_orders, np.array([1, 1])) # From_arrays s = System.from_arrays(mol_indices=[0], bonds=bz.bonds, **bz.__dict__) assert_npequal(s.bonds, bz.bonds) assert_npequal(s.bond_orders, bz.bond_orders) # Get molecule entry # Test the bonds when they're 0 s.bonds = np.array([]) assert_equals(s.get_derived_molecule_array('formula'), 'C6')
def test_display(): d = Display('povray') s = System.from_arrays(type_array=['O', 'H', 'H'], r_array=[[0.0, 0.0, 0.0], [0.15, 0.0, 0.0], [0.0, 0.15, 0.0]]) d.system(s) s.r_array = s.r_array + 0.3 d.system(s, alpha=0.5) d.render()
def test_system_remove(): # 3 water molecules r_array = np.random.random((9, 3)) type_array = ['O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H'] mol_indices = [0, 3, 6] mol_n_atoms = [3, 3, 3] s2 = System.from_arrays(r_array=r_array, type_array=type_array, mol_indices=mol_indices, mol_n_atoms=mol_n_atoms) s2.remove_atoms([0, 1]) print s2.type_array
def test_atom_type(self): self.s = System.from_arrays( type_array=["Cl", "Cl", "O", "H", "H", "O", "H", "H", "O", "H", "H", "Na", "Na"], maps={("atom", "molecule"): [0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6]}, ) idx = self.s.where(atom_type="O") assert_npequal( idx["atom"], [False, False, True, False, False, True, False, False, True, False, False, False, False] ) assert_npequal(idx["molecule"], [False, False, True, True, True, False, False]) self.s = System.from_arrays( type_array=["Cl", "Cl", "O", "H", "H", "O", "H", "H", "O", "H", "H", "Na", "Na"], maps={("atom", "molecule"): [0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6]}, ) idx = self.s.where(atom_type=["Na", "Cl"]) assert_npequal( idx["atom"], [True, True, False, False, False, False, False, False, False, False, False, True, True] )
def test_from_arrays(self): mols = self._make_molecules() system = System.from_arrays( r_array=np.concatenate([m.r_array for m in mols]), type_array=np.concatenate([m.type_array for m in mols]), bonds=np.concatenate([m.bonds + 3 * i for i, m in enumerate(mols)]), maps={ ('atom', 'molecule'): [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], ('bond', 'molecule'): [0, 0, 1, 1, 2, 2, 3, 3] }) self._assert_init(system)
def test_from_arrays(self): mols = self._make_molecules() system = System.from_arrays( r_array=np.concatenate([m.r_array for m in mols]), type_array=np.concatenate([m.type_array for m in mols]), bonds=np.concatenate([m.bonds + 3 * i for i, m in enumerate(mols) ]), maps={ ('atom', 'molecule'): [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], ('bond', 'molecule'): [0, 0, 1, 1, 2, 2, 3, 3] }) self._assert_init(system)
def test_from_arrays(self): mols = self._make_molecules() r_array = np.concatenate([m.r_array for m in mols]) type_array = np.concatenate([m.type_array for m in mols]) mol_indices = [0, 3, 6, 9] bonds = np.concatenate([m.bonds + 3 * i for i, m in enumerate(mols)]) system = System.from_arrays(r_array=r_array, type_array=type_array, mol_indices=mol_indices, bonds=bonds) self._assert_init(system)
def test_from_arrays(self): mols = self._make_molecules() r_array = np.concatenate([m.r_array for m in mols]) type_array = np.concatenate([m.type_array for m in mols]) mol_indices = [0, 3, 6, 9] bonds = np.concatenate([m.bonds + 3*i for i, m in enumerate(mols)]) system = System.from_arrays(r_array=r_array, type_array=type_array, mol_indices=mol_indices, bonds=bonds) self._assert_init(system)
def test_atom_type(self): self.s = System.from_arrays( type_array=['Cl', 'Cl', 'O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H', 'Na', 'Na'], maps= {('atom', 'molecule'): [0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6]}) idx = self.s.where(type_array='O') assert_npequal(idx['atom'], [False, False, True, False, False, True, False, False, True, False, False, False, False]) assert_npequal(idx['molecule'], [False, False, True, True, True, False, False]) self.s = System.from_arrays( type_array=['Cl', 'Cl', 'O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H', 'Na', 'Na'], maps= {('atom', 'molecule'): [0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6]}) idx = self.s.where(type_array=['Na', 'Cl']) assert_npequal(idx['atom'], [True, True, False, False, False, False, False, False, False, False, False, True, True])
def test_system(): wat = Molecule([Atom("O", [-4.99, 2.49, 0.0]), Atom("H", [-4.02, 2.49, 0.0]), Atom("H", [-5.32, 1.98, 1.0])], export={'hello': 1.0}) wat.r_array *= 0.1 # Initialization from empty s = System.empty(4, 4*3) mols = [] for i in xrange(s.n_mol): wat.r_array += 0.1 s.add(wat) m = wat.copy() mols.append(wat.copy()) # Printing just to test if there aren't any exception print "Init from Empty" print "*" * 72 _print_sysinfo(s) print "Init Normal" print "*" * 72 s = System(mols) _print_sysinfo(s) # 3 water molecules r_array = np.random.random((9, 3)) type_array = ['O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H'] mol_indices = [0, 3, 6] mol_n_atoms = [3, 3, 3] s2 = System.from_arrays(r_array=r_array, type_array=type_array, mol_indices=mol_indices, mol_n_atoms=mol_n_atoms) print 'bonds', s2._mol_bonds sub2 = subsystem_from_molecules(s2, np.array([0, 2])) assert sub2.n_mol == 2 sub = subsystem_from_atoms(s2, np.array([True, True, False, False, False, False, False, False, False])) assert sub.n_mol == 1
def test_query(): type_array = ['Cl', 'Cl', 'O', 'H', 'H', 'O', 'H', 'H', 'Na', 'Na'] mol_indices = [0, 1, 2, 5, 8, 9] s = System.from_arrays(type_array = type_array, mol_indices=mol_indices) array_eq_(s.where(type_in=['Na', 'Cl']), [True, True, False, False, False, False, False, False, True, True]) array_eq_(s.where(type='Cl'), [True, True, False, False, False, False, False, False, False, False]) # We move the Cl away cl = s.where(type='Cl') s.r_array[cl.nonzero()[0]] = [1, 0, 0] s.box_vectors = np.diag([3, 3, 3]) array_eq_(s.where(type_in=['H', 'O'], within_of=(0.2, [8, 9])), [False, False, True, True, True, True, True, True, False, False]) array_eq_(s.where(type_in=['H', 'O'], within_of=(0.2, 8)), [False, False, True, True, True, True, True, True, False, False]) array_eq_(s.where(type_in=['H', 'O'], within_of=(0.2, [8])), [False, False, True, True, True, True, True, True, False, False])
def test_query(): type_array = ['Cl', 'Cl', 'O', 'H', 'H', 'O', 'H', 'H', 'Na', 'Na'] maps = {('atom', 'molecule'): [0, 1, 2, 2, 2, 3, 3, 3, 4, 5]} s = System.from_arrays(type_array = type_array, maps=maps) assert_npequal(s.where(type_array=['Na', 'Cl'])['atom'], [True, True, False, False, False, False, False, False, True, True]) assert_npequal(s.where(type_array='Cl')['atom'], [True, True, False, False, False, False, False, False, False, False]) # We move the Cl away cl = s.where(type_array='Cl')['atom'] s.r_array[cl.nonzero()[0]] = [1, 0, 0] s.box_vectors = np.diag([3, 3, 3]) assert_npequal(s.where(type_array=['H', 'O'], within_of=(0.2, [8, 9]))['atom'], [False, False, True, True, True, True, True, True, False, False]) assert_npequal(s.where(type_array=['H', 'O'], within_of=(0.2, 8))['atom'], [False, False, True, True, True, True, True, True, False, False]) assert_npequal(s.where(type_array=['H', 'O'], within_of=(0.2, [8]))['atom'], [False, False, True, True, True, True, True, True, False, False])
def setup(self): self.s = System.from_arrays( type_array=['O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H'], maps={('atom', 'molecule'): [0, 0, 0, 1, 1, 1, 2, 2, 2]})
def setup(self): self.s = System.from_arrays( type_array=["O", "H", "H", "O", "H", "H", "O", "H", "H"], maps={("atom", "molecule"): [0, 0, 0, 1, 1, 1, 2, 2, 2]}, )