def test_from_batch(self): mols = self._make_molecules() system = System() with system.batch() as batch: [batch.append(mol) for mol in mols] 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_reorder_molecules(self): mols = self._make_molecules() system = System(mols) system.bonds = np.array([[0, 1], [3, 5]]) # Reordering system.reorder_molecules([1, 0, 2, 3]) assert_eqbonds(system.bonds, [[0, 2], [3, 4]])
def test_remove_atoms(self): # This will remove the first and last molecules mols = self._make_molecules() system = System(mols) system.remove_atoms([0, 1, 11]) assert_eqbonds(system.bonds, [[0, 1], [0, 2], [3, 4], [3, 5]]) assert_npequal(system.type_array, np.array(["O", "H", "H", "O", "H", "H"], dtype="object"))
def test_remove_atoms(self): # This will remove the first and last molecules mols = self._make_molecules() system = System(mols) system.remove_atoms([0, 1, 11]) assert_eqbonds(system.bonds, [[0, 1], [0, 2], [3, 4], [3, 5]]) assert_npequal( system.type_array, np.array(['O', 'H', 'H', 'O', 'H', 'H'], dtype='object'))
def test_bond_orders(): # Get a molecule with some bonds wat = _make_water() wat_o = wat.copy() # 0,1 0,2 assert_npequal(wat.bond_orders, np.array([0, 0])) # Remove a bond wat.bonds = np.array([[0, 1]]) assert_npequal(wat.bond_orders, np.array([0])) wat.bond_orders = np.array([2]) # Try with a system s = System() s.add(wat_o) s.add(wat) assert_npequal(s.bond_orders, np.array([0, 0, 2])) s.reorder_molecules([1, 0]) # Bonds get sorted accordingly assert_npequal(s.bond_orders, np.array([2, 0, 0])) s.bonds = np.array([[0, 1], [0, 2], [3, 4], [3, 5]]) assert_npequal(s.bond_orders, np.array([2, 0, 0, 0]))
def test_bond_guessing(): from chemlab.db import ChemlabDB, CirDB from chemlab.graphics import display_molecule from chemlab.io import datafile mol = datafile('tests/data/3ZJE.pdb').read('molecule') print(mol.r_array) mol.guess_bonds() assert mol.bonds.size > 0 # We should find the bond guessing also for systems # System Made of two benzenes bz = datafile("tests/data/benzene.mol").read('molecule') bzbonds = bz.bonds bz.bonds = np.array([]) # Separating the benzenes by large amount bz2 = bz.copy() bz2.r_array += 2.0 s = System([bz, bz2]) s.guess_bonds() assert_eqbonds(s.bonds, np.concatenate((bzbonds, bzbonds + 6))) # Separating benzenes by small amount bz2 = bz.copy() bz2.r_array += 0.15 s = System([bz, bz2]) s.guess_bonds() assert_eqbonds(s.bonds, np.concatenate((bzbonds, bzbonds + 6)))
def test_bonds(): # TODO: deprecate this shit from chemlab.io import datafile bz = datafile("tests/data/benzene.mol").read('molecule') na = Molecule([ Atom('O', [0.0, 0.0, 0.0]), Atom('H', [0.0, 0.0, 0.0]), Atom('H', [0.0, 0.0, 0.0]), ]) # Adding bonds s = System() with s.batch() as b: b.append(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.type_array, ['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']) eq_(s.dimensions['atom'], 12) assert_npequal(s.bonds, np.concatenate((bz.bonds, bz.bonds + 7))) # Reordering s.bonds = np.array([[0, 1], [6, 8]]) s.reorder_molecules([1, 0]) assert_eqbonds(s.bonds, np.array([[6, 7], [0, 2]])) # Selection ss = subsystem_from_molecules(s, [1]) assert_npequal(ss.bonds, np.array([[0, 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])]) s2 = System.empty(2, 2) s2.add(na) s2.add(na) s2.get_bond_array() s = System.empty(2, 2*bz.n_atoms) s.add(bz) s.add(bz) print s.get_bond_array()
def get_system(self): self.coordinates = self.interaction_manager.get_coordinates() atoms = [] for index in range(len(self.interaction_manager.atoms)): atom = self.interaction_manager.atoms[index] type = atom.atom_type if type.upper() == "C": if atom.get_free_valency()!=0: type = "Ne" pos = 3.5 * self.coordinates[index] atoms.append(Atom(type,pos)) mol = Molecule(atoms, self.interaction_manager.get_bonds_array()) system = System([mol]) system.bonds = np.array(self.interaction_manager.get_bonds_array()) return system
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_bond_orders(): # Get a molecule with some bonds wat = _make_water() wat_o = wat.copy() # 0,1 0,2 assert_npequal(wat.bond_orders, np.array([1, 1])) # Remove a bond wat.bonds = np.array([[0, 1]]) assert_npequal(wat.bond_orders, np.array([1])) wat.bond_orders = np.array([2]) # Try with a system s = System.empty(2, 6) s.add(wat_o) s.add(wat) assert_npequal(s.bond_orders , np.array([1, 1, 2])) s.reorder_molecules([1, 0]) # We don't actually sort bonds again assert_npequal(s.bond_orders , np.array([1, 1, 2])) s.bonds = np.array([[0, 1], [0, 2], [3, 4], [3, 5]]) assert_npequal(s.bond_orders, np.array([1, 1, 2, 1]))
def test_merge_system(): # take a protein from chemlab.io import datafile from chemlab.graphics import display_system from chemlab.db import ChemlabDB water = ChemlabDB().get("molecule", "example.water") prot = datafile("tests/data/3ZJE.pdb").read("system") # Take a box of water NWAT = 50000 bsize = 20.0 pos = np.random.random((NWAT, 3)) * bsize wat = water.copy() s = System.empty(NWAT, NWAT*3, box_vectors=np.eye(3)*bsize) for i in range(NWAT): wat.move_to(pos[i]) s.add(wat) prot.r_array += 10 s = merge_systems(s, prot, 0.5) display_system(s, 'ball-and-stick')
def test_merge_system(): # take a protein from chemlab.io import datafile from chemlab.graphics import display_system from chemlab.db import ChemlabDB water = ChemlabDB().get("molecule", "example.water") prot = datafile("tests/data/3ZJE.pdb").read("system") # Take a box of water NWAT = 50000 bsize = 20.0 pos = np.random.random((NWAT, 3)) * bsize wat = water.copy() s = System.empty(NWAT, NWAT * 3, box_vectors=np.eye(3) * bsize) for i in range(NWAT): wat.move_to(pos[i]) s.add(wat) prot.r_array += 10 s = merge_systems(s, prot, 0.5) display_system(s, 'ball-and-stick')
def test_write_gromacs(): water = Molecule( [ Atom("O", [0.0, 0.0, 0.0], export={"grotype": "OW"}), Atom("H", [0.1, 0.0, 0.0], export={"grotype": "HW1"}), Atom("H", [-0.03333, 0.09428, 0.0], export={"grotype": "HW2"}), ], export={"groname": "SOL"}, ) sys = System.empty(200, 3 * 200, box_vectors=np.eye(3) * 2.0) for i in range(200): sys.add(water.copy()) df = datafile("/tmp/dummy.gro", mode="w") df.write("system", sys) with assert_raises(Exception): df = datafile("/tmp/dummy.gro") df.write("system", sys) df = datafile("/tmp/dummy.gro") sread = df.read("system") assert all(sread.type_array == sys.type_array)
def test_bond_orders(): # Get a molecule with some bonds wat = _make_water() wat_o = wat.copy() # 0,1 0,2 assert_npequal(wat.bond_orders, np.array([1, 1])) # Remove a bond wat.bonds = np.array([[0, 1]]) assert_npequal(wat.bond_orders, np.array([1])) wat.bond_orders = np.array([2]) # Try with a system s = System.empty(2, 6) s.add(wat_o) s.add(wat) assert_npequal(s.bond_orders, np.array([1, 1, 2])) s.reorder_molecules([1, 0]) # We don't actually sort bonds again assert_npequal(s.bond_orders, np.array([1, 1, 2])) s.bonds = np.array([[0, 1], [0, 2], [3, 4], [3, 5]]) assert_npequal(s.bond_orders, np.array([1, 1, 2, 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_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 load_molecule(name, format=None): '''Read a `~chemlab.core.Molecule` from a file. .. seealso:: `chemlab.io.datafile` ''' mol = datafile(name, format=format).read('molecule') display_system(System([mol]))
def test_subsystem_from_atoms(self): mols = self._make_molecules() system = System(mols) sub = subsystem_from_atoms( system, np.array( [True, True, False, False, False, False, False, False, False])) assert_equals(sub.n_mol, 1)
def test_bonds(): # TODO: deprecate this shit from chemlab.io import datafile bz = datafile("tests/data/benzene.mol").read('molecule') na = Molecule([Atom('O', [0.0, 0.0, 0.0]), Atom('H', [0.0, 0.0, 0.0]), Atom('H', [0.0, 0.0, 0.0]), ]) # Adding bonds s = System() with s.batch() as b: b.append(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.type_array, ['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']) eq_(s.dimensions['atom'], 12) assert_npequal(s.bonds, np.concatenate((bz.bonds, bz.bonds + 7))) # Reordering s.bonds = np.array([[0, 1], [6, 8]]) s.reorder_molecules([1, 0]) assert_eqbonds(s.bonds, np.array([[6, 7], [0, 2]])) # Selection ss = subsystem_from_molecules(s, [1]) assert_npequal(ss.bonds, np.array([[0, 1]]))
def display_molecule(mol, autozoom=True): '''Display a `~chemlab.core.Molecule` instance in the viewer. This function wraps the molecule in a system before displaying it. ''' s = System([mol]) display_system(s, autozoom=True)
def test_local(): db = ChemlabDB() bz = db.get("molecule", "example.norbornene") pre_string = bz.tojson() db = LocalDB("/tmp/testdb/") db.store("molecule", 'norbornene', bz, nowarn=True) post_string = db.get('molecule', 'norbornene').tojson() assert pre_string == post_string # Do the same thing for a system of 3 norbornenes s = System([bz.copy() for i in range(3)]) pre_string = s.tojson() db.store("system", 'norbornene-3', s, nowarn=True) post_string = db.get('system', 'norbornene-3').tojson() assert pre_string == post_string
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_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_local(): db = ChemlabDB() bz = db.get("molecule", "example.norbornene") pre_dict = bz.to_dict() db = LocalDB("/tmp/testdb/") db.store("molecule", 'norbornene', bz, nowarn=True) post_dict = db.get('molecule', 'norbornene').to_dict() npeq_(pre_dict['r_array'], post_dict['r_array']) # Do the same thing for a system of 3 norbornenes s = System([bz.copy() for i in range(3)]) pre_dict = s.to_dict() db.store("system", 'norbornene-3', s, nowarn=True) post_dict = db.get('system', 'norbornene-3').to_dict() npeq_(pre_dict['r_array'], post_dict['r_array'])
def test_residue(): m = Molecule.from_arrays(type_array=['H', 'H', 'H', 'O', 'O'], residue_name=['VAL', 'ALA'], maps={('atom', 'residue'): [0, 0, 0, 1, 1]}) assert_npequal(m.sub(residue_index=0).type_array, ['H', 'H', 'H']) s = System([m, m]) assert_npequal(s.maps['atom', 'residue'].value, [0, 0, 0, 1, 1, 2, 2, 2, 3, 3])
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_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 _make_molecules(self): wat = _make_water() wat.r_array *= 0.1 # Initialization from empty s = System.empty(4, 4*3) mols = [] # Array to be compared for _ in range(s.n_mol): wat.r_array += 0.1 mols.append(wat.copy()) return mols
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_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_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 _make_molecules(self): wat = _make_water() wat.r_array *= 0.1 # Initialization from empty s = System.empty(4, 4 * 3) mols = [] # Array to be compared for _ in range(s.n_mol): wat.r_array += 0.1 mols.append(wat.copy()) return mols
def test_write_pdb(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'grotype': 'OW'}), Atom('H', [0.1, 0.0, 0.0], export={'grotype': 'HW1'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'grotype': 'HW2'})], export={'groname': 'SOL'}) sys = System.empty(200, 3*200, box_vectors = np.eye(3) * 2.0) for i in range(200): sys.add(water.copy()) df = datafile('/tmp/dummy.gro', mode="w") df.write("system", sys)
def test_write_pdb(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'pdb.type': 'O'}), Atom('H', [0.1, 0.0, 0.0], export={'pdb.type': 'H'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'pdb.type': 'H'})], export={'groname': 'SOL'}) sys = System.empty(200, 3*200, box_vectors = np.eye(3) * 2.0) for i in range(200): water.r_array += 0.1 sys.add(water.copy()) df = datafile('/tmp/dummy.pdb', mode="w") df.write("system", sys)
def test_serialization(): cl = Molecule([Atom.from_fields(type='Cl', r=[0.0, 0.0, 0.0])]) jsonstr = cl.tojson() assert Molecule.from_json(jsonstr).tojson() == jsonstr na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0],[0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[3,3,3]) jsonstr = tsys.tojson() assert System.from_json(jsonstr).tojson() == jsonstr
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_write_pdb(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'pdb.type': 'O'}), Atom('H', [0.1, 0.0, 0.0], export={'pdb.type': 'H'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'pdb.type': 'H'})], export={'groname': 'SOL'}) sys = System.empty() with sys.batch() as b: for i in range(200): water.r_array += 0.1 b.append(water.copy()) df = datafile('/tmp/dummy.pdb', mode="w") df.write("system", sys)
def test_serialization(): cl = Molecule([Atom.from_fields(type="Cl", r=[0.0, 0.0, 0.0])]) jsonstr = cl.to_json() m = Molecule.from_json(jsonstr) eq_(Molecule.from_json(jsonstr).to_json(), jsonstr) na = Molecule([Atom("Na", [0.0, 0.0, 0.0])]) cl = Molecule([Atom("Cl", [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[3, 3, 3]) jsonstr = tsys.to_json() eq_(System.from_json(jsonstr).to_json(), jsonstr)
def test_serialization(): cl = Molecule([Atom.from_fields(type='Cl', r=[0.0, 0.0, 0.0])]) jsonstr = cl.tojson() assert Molecule.from_json(jsonstr).tojson() == jsonstr na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[3, 3, 3]) jsonstr = tsys.tojson() assert System.from_json(jsonstr).tojson() == jsonstr
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_serialization(): cl = Molecule([Atom.from_fields(type='Cl', r=[0.0, 0.0, 0.0])]) jsonstr = cl.to_json() m = Molecule.from_json(jsonstr) eq_(Molecule.from_json(jsonstr).to_json(), jsonstr) na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[3, 3, 3]) jsonstr = tsys.to_json() npeq_(System.from_json(jsonstr).r_array, tsys.r_array)
def test_write_pdb(): water = Molecule( [ Atom("O", [0.0, 0.0, 0.0], export={"pdb.type": "O"}), Atom("H", [0.1, 0.0, 0.0], export={"pdb.type": "H"}), Atom("H", [-0.03333, 0.09428, 0.0], export={"pdb.type": "H"}), ], export={"groname": "SOL"}, ) sys = System.empty(200, 3 * 200, box_vectors=np.eye(3) * 2.0) for i in range(200): water.r_array += 0.1 sys.add(water.copy()) df = datafile("/tmp/dummy.pdb", mode="w") df.write("system", sys)
def test_write_gromacs(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'grotype': 'OW'}), Atom('H', [0.1, 0.0, 0.0], export={'grotype': 'HW1'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'grotype': 'HW2'})], export={'groname': 'SOL'}) sys = System.empty(200, 3*200, box_vectors = np.eye(3)*2.0) for i in range(200): sys.add(water.copy()) df = datafile('/tmp/dummy.gro', mode="w") df.write('system', sys) df = datafile('/tmp/dummy.gro') sread = df.read('system') assert all(sread.type_array == sys.type_array)
def test_write_gromacs(): water = Molecule([ Atom('O', [0.0, 0.0, 0.0], export={'grotype': 'OW'}), Atom('H', [0.1, 0.0, 0.0], export={'grotype': 'HW1'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'grotype': 'HW2'}) ], export={'groname': 'SOL'}) sys = System.empty(200, 3 * 200, box_vectors=np.eye(3) * 2.0) for i in range(200): sys.add(water.copy()) df = datafile('/tmp/dummy.gro', mode="w") df.write('system', sys) df = datafile('/tmp/dummy.gro') sread = df.read('system') assert all(sread.type_array == sys.type_array)
def test_write_gromacs(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], name="OW"), Atom('H', [0.1, 0.0, 0.0], name='HW1'), Atom('H', [-0.03333, 0.09428, 0.0], name='HW2')], name='SOL') sys = System.empty() with sys.batch() as b: for i in range(200): b.append(water.copy()) sys.box_vectors = [[2, 0, 0], [0, 2, 0], [0, 0, 2]] df = datafile('/tmp/dummy.gro', mode="w") df.write('system', sys) with assert_raises(Exception): df = datafile('/tmp/dummy.gro') df.write('system', sys) df = datafile('/tmp/dummy.gro') sread = df.read('system') assert all(sread.type_array == sys.type_array)
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_subsystem_from_molecules(self): mols = self._make_molecules() system = System(mols) subsystem = subsystem_from_molecules(system, np.array([0, 2])) assert_equals(subsystem.n_mol, 2)