def test_reduce1(self): atom = Atoms(['C', 'Au']) atom = atom.sub(0) atom = atom.reduce() assert_true(atom[0] == Atom[6]) assert_true(len(atom) == 1) assert_true(len(atom.atom) == 1)
def test_reorder1(self): atom = Atoms(['C', 'Au']) atom = atom.sub(1) atom = atom.reorder() assert_true(atom[0] == Atom['Au']) assert_true(len(atom) == 1) assert_true(len(atom.atom) == 2)
def test_atoms_set(): a1 = Atom(1) a2 = Atom(2) a3 = Atom(3) a = Atoms(a1, 3) assert len(a) == 3 assert len(a.atom) == 1 a[1] = a2 assert len(a) == 3 assert len(a.atom) == 2 # Add the atom, but do not specify any # atoms to have the species a[[]] = a3 assert len(a) == 3 assert len(a.atom) == 3 for atom in a: assert atom in [a1, a2] assert atom != a3 found = 0 for atom, i_s in a.iter(True): if atom == a1: assert len(i_s) == 2 found += 1 elif atom == a2: assert len(i_s) == 1 found += 1 elif atom == a3: assert len(i_s) == 0 found += 1 assert found == 3
def test_create1(self): atom1 = Atoms([self.C, self.C3, self.Au]) atom2 = Atoms(['C', 'C', 'Au']) atom3 = Atoms(['C', 6, 'Au']) atom4 = Atoms(['Au', 6, 'C']) assert_true(atom2 == atom3) assert_true(atom2 == atom4)
def test_remove1(): atom = Atoms(['C', 'Au']) atom = atom.remove(1) atom = atom.reduce() assert atom[0] == Atom[6] assert len(atom) == 1 assert len(atom.atom) == 1
def read_geometry(self, velocity=False, species_Z=False): """ Returns a `Geometry` object from the XV file Parameters ---------- species_Z : bool, optional if ``True`` the atomic numbers are the species indices (useful when reading the ChemicalSpeciesLabel block simultaneously). velocity : bool, optional also return the velocities in the file Returns ------- Geometry velocity : only if `velocity` is true. """ sc = self.read_supercell() # Read number of atoms na = int(self.readline()) xyz = np.empty([na, 3], np.float64) vel = np.empty([na, 3], np.float64) atms = [None] * na sp = np.empty([na], np.int32) for ia in range(na): line = list(map(float, self.readline().split()[:8])) sp[ia] = int(line[0]) if species_Z: atms[ia] = Atom(sp[ia]) else: atms[ia] = Atom(int(line[1])) xyz[ia, :] = line[2:5] vel[ia, :] = line[5:8] xyz *= Bohr2Ang vel *= Bohr2Ang # Ensure correct sorting max_s = sp.max() sp -= 1 # Ensure we can remove the atom after having aligned them atms2 = Atoms(AtomUnknown(1000), na=na) for i in range(max_s): idx = (sp[:] == i).nonzero()[0] if len(idx) == 0: # Always ensure we have "something" for the unoccupied places atms2[idx] = AtomUnknown(1000 + i) else: atms2[idx] = atms[idx[0]] geom = Geometry(xyz, atms2.reduce(), sc=sc) if velocity: return geom, vel return geom
def read_geometry(self): """ Read geometry from the contained file """ # First we read in the geometry sc = self.read_supercell() # Try and go to the first model record in_model, l = self._step_record('MODEL') idx = [] tags = [] xyz = [] Z = [] if in_model: l = self.readline() def is_atom(line): return l.startswith('ATOM') or l.startswith('HETATM') def is_end_model(line): return l.startswith('ENDMDL') or l == '' while not is_end_model(l): if is_atom(l): idx.append(int(l[6:11])) tags.append(l[12:16].strip()) xyz.append( [float(l[30:38]), float(l[38:46]), float(l[46:54])]) Z.append(l[76:78].strip()) l = self.readline() # First sort all atoms according to the idx array idx = np.array(idx) idx = np.argsort(idx) xyz = np.array(xyz)[idx, :] tags = [tags[i] for i in idx] Z = [Z[i] for i in idx] # Create the atom list atoms = Atoms(Atom(Z[0], tag=tags[0]), na=len(Z)) for i, a in enumerate(map(Atom, Z, tags)): try: s = atoms.specie_index(a) except: s = len(atoms.atom) atoms._atom.append(a) atoms._specie[i] = s return Geometry(xyz, atoms, sc=sc)
def test_get1(self): atoms = Atoms(['C', 'C', 'Au']) assert_true(atoms[2] == Atom('Au')) assert_true(atoms[0] == Atom('C')) assert_true(atoms[1] == Atom('C')) assert_true(atoms[0:2] == [Atom('C')] * 2) assert_true(atoms[1:] == [Atom('C'), Atom('Au')])
def test_get1(): atoms = Atoms(['C', 'C', 'Au']) assert atoms[2] == Atom('Au') assert atoms[0] == Atom('C') assert atoms[1] == Atom('C') assert atoms[0:2] == [Atom('C')]*2 assert atoms[1:] == [Atom('C'), Atom('Au')]
def read_basis(self): """ Returns a set of atoms corresponding to the basis-sets in the ORB_INDX file The specie names have a short field in the ORB_INDX file, hence the name may not necessarily be the same as provided in the species block """ # First line contains no no_s line = self.readline().split() no = int(line[0]) self.readline() self.readline() pt = PeriodicTable() def crt_atom(spec, orbs): i = pt.Z(spec) if isinstance(i, int): return Atom(i, orbs) else: return Atom(-1, orbs, tag=spec) # Now we begin by reading the atoms atom = [] orbs = [] specs = [] ia = 1 for _ in range(no): line = self.readline().split() i_a = int(line[1]) if i_a != ia: if i_s not in specs: atom.append(crt_atom(spec, orbs)) specs.append(i_s) ia = i_a orbs = [] i_s = int(line[2]) - 1 if i_s in specs: continue spec = line[3] nlmz = list(map(int, line[5:9])) P = line[9] == 'T' rc = float(line[11]) * Bohr2Ang # Create the orbital o = AtomicOrbital(n=nlmz[0], l=nlmz[1], m=nlmz[2], Z=nlmz[3], P=P, spherical=Orbital(rc)) orbs.append(o) if i_s not in specs: atom.append(crt_atom(spec, orbs)) specs.append(i_s) # Now re-arrange the atoms and create the Atoms object return Atoms([atom[i] for i in specs])
def test_iter1(self): # Add new atoms to the set atom = Atoms(['C', 'C']) for a, idx in atom: assert_true(a == Atom[6]) assert_true(len(idx) == 2) atom = Atoms(['C', 'Au', 'C', 'Au']) for i, aidx in enumerate(atom): a, idx = aidx if i == 0: assert_true(a == Atom[6]) assert_true((idx == [0, 2]).all()) elif i == 1: assert_true(a == Atom['Au']) assert_true((idx == [1, 3]).all()) assert_true(len(idx) == 2)
def test_set1(self): # Add new atoms to the set atom = Atoms(['C', 'C']) assert_true(atom[0] == Atom('C')) assert_true(atom[1] == Atom('C')) atom[1] = Atom('Au') assert_true(atom[0] == Atom('C')) assert_true(atom[1] == Atom('Au'))
def test_set1(): # Add new atoms to the set atom = Atoms(['C', 'C']) assert atom[0] == Atom('C') assert atom[1] == Atom('C') atom[1] = Atom('Au') assert atom[0] == Atom('C') assert atom[1] == Atom('Au')
def test_charge_diff(): o1 = Orbital(1., 1.) o2 = Orbital(1., .5) a1 = Atom(5, [o1, o2, o1, o2]) a2 = Atom(5, [o1, o2, o1, o2, o1, o1]) a = Atoms([a1, a2]) assert len(a.q0) == 2 assert a.q0.sum() == pytest.approx(8) assert np.allclose(a.q0, [3, 5])
def read_atom(self): """ Reads a the atoms and returns an `Atoms` object """ self.readline() _, ns = map(int, self.readline().split()[:2]) species = self.readline().split()[:ns] # species orbs = self.readline().split()[:ns] # orbs per species # Create list of species with correct # of orbitals per specie species = [Atom(s, orbs=int(o)) for s, o in zip(species, orbs)] return Atoms(species)
def test_set2(): # Add new atoms to the set atom = Atoms(['C', 'C']) assert atom[0] == Atom('C') assert atom[1] == Atom('C') assert len(atom.atom) == 1 atom[1] = Atom('Au', [-1] * 2) assert atom[0] == Atom('C') assert atom[1] != Atom('Au') assert atom[1] == Atom('Au', [-1] * 2) assert len(atom.atom) == 2
def test_set2(self): # Add new atoms to the set atom = Atoms(['C', 'C']) assert_true(atom[0] == Atom('C')) assert_true(atom[1] == Atom('C')) assert_true(len(atom.atom) == 1) atom[1] = Atom('Au', orbs=2) assert_true(atom[0] == Atom('C')) assert_false(atom[1] == Atom('Au')) assert_true(atom[1] == Atom('Au', orbs=2)) assert_true(len(atom.atom) == 2)
def test_replace1(): # Add new atoms to the set atom = Atoms(['C'] * 10 + ['B'] * 2) atom[range(1, 4)] = Atom('Au', [-1] * 2) assert atom[0] == Atom('C') for i in range(1, 4): assert atom[i] != Atom('Au') assert atom[i] == Atom('Au', [-1] * 2) assert atom[4] != Atom('Au') assert atom[4] != Atom('Au', [-1] * 2) assert len(atom.atom) == 3 atom.replace(atom[0], Atom('C', [-1] * 2)) assert atom[0] == Atom('C', [-1] * 2) assert len(atom.atom) == 3 assert atom[0] == Atom('C', [-1] * 2) for i in range(4, 10): assert atom[i] == Atom('C', [-1] * 2) for i in range(1, 4): assert atom[i] == Atom('Au', [-1] * 2) for i in range(10, 12): assert atom[i] == Atom('B')
def test_append1(): # Add new atoms to the set atom1 = Atoms(['C', 'C']) assert atom1[0] == Atom('C') assert atom1[1] == Atom('C') atom2 = Atoms([Atom('C', tag='DZ'), Atom[6]]) assert atom2[0] == Atom('C', tag='DZ') assert atom2[1] == Atom('C') atom = atom1.append(atom2) assert atom[0] == Atom('C') assert atom[1] == Atom('C') assert atom[2] == Atom('C', tag='DZ') assert atom[3] == Atom('C') atom = atom1.append(Atom(6, tag='DZ')) assert atom[0] == Atom('C') assert atom[1] == Atom('C') assert atom[2] == Atom('C', tag='DZ') atom = atom1.append([Atom(6, tag='DZ'), Atom[6]]) assert atom[0] == Atom('C') assert atom[1] == Atom('C') assert atom[2] == Atom('C', tag='DZ') assert atom[3] == Atom('C')
def test_set3(): # Add new atoms to the set atom = Atoms(['C'] * 10) atom[range(1, 4)] = Atom('Au', [-1] * 2) assert atom[0] == Atom('C') for i in range(1, 4): assert atom[i] != Atom('Au') assert atom[i] == Atom('Au', [-1] * 2) assert atom[4] != Atom('Au') assert atom[4] != Atom('Au', [-1] * 2) assert len(atom.atom) == 2 atom[1:4] = Atom('C') assert len(atom.atom) == 2
def test_set3(self): # Add new atoms to the set atom = Atoms(['C'] * 10) atom[range(1, 4)] = Atom('Au', orbs=2) assert_true(atom[0] == Atom('C')) for i in range(1, 4): assert_false(atom[i] == Atom('Au')) assert_true(atom[i] == Atom('Au', orbs=2)) assert_false(atom[4] == Atom('Au')) assert_false(atom[4] == Atom('Au', orbs=2)) assert_true(len(atom.atom) == 2) atom[1:4] = Atom('C') assert_true(len(atom.atom) == 2)
def test_create1(setup): atom1 = Atoms([setup.C, setup.C3, setup.Au]) atom2 = Atoms(['C', 'C', 'Au']) atom3 = Atoms(['C', 6, 'Au']) atom4 = Atoms(['Au', 6, 'C']) assert atom2 == atom3 assert atom2 != atom4 assert atom2.hassame(atom4)
def test_reduce1(): atom = Atoms(['C', 'Au']) atom = atom.sub(0) atom1 = atom.reduce() assert atom[0] == Atom[6] assert len(atom) == 1 assert len(atom.atom) == 2 assert atom1[0] == Atom[6] assert atom1[0] != Atom[8] assert len(atom1) == 1 assert len(atom1.atom) == 1 atom.reduce(True) assert atom[0] == Atom[6] assert len(atom) == 1 assert len(atom.atom) == 1
def read_geometry(self): """ Returns Geometry object from a Siesta.nc file """ # Read supercell sc = self.read_supercell() xyz = np.array(self._value('xa'), np.float64) xyz.shape = (-1, 3) if 'BASIS' in self.groups: basis = self.read_basis() species = self.groups['BASIS'].variables['basis'][:] - 1 atom = Atoms([basis[i] for i in species]) else: atom = Atom(1) xyz *= Bohr2Ang # Create and return geometry object geom = Geometry(xyz, atom, sc=sc) return geom
def test_iter1(): # Add new atoms to the set atom = Atoms(['C', 'C']) for a in atom.iter(): assert a == Atom[6] for a, idx in atom.iter(True): assert a == Atom[6] assert len(idx) == 2 atom = Atoms(['C', 'Au', 'C', 'Au']) for i, aidx in enumerate(atom.iter(True)): a, idx = aidx if i == 0: assert a == Atom[6] assert (idx == [0, 2]).all() elif i == 1: assert a == Atom['Au'] assert (idx == [1, 3]).all() assert len(idx) == 2
def test_reorder1(): atom = Atoms(['C', 'Au']) atom = atom.sub(1) atom1 = atom.reorder() # Check we haven't done anything to the original Atoms object assert atom[0] == Atom['Au'] assert atom.specie[0] == 1 assert len(atom) == 1 assert len(atom.atom) == 2 assert atom1[0] == Atom['Au'] assert atom1.specie[0] == 0 assert len(atom1) == 1 assert len(atom1.atom) == 2 # Do in-place atom.reorder(True) assert atom[0] == Atom['Au'] assert atom.specie[0] == 0 assert len(atom) == 1 assert len(atom.atom) == 2
def test_replace2(): # Add new atoms to the set atom = Atoms(['C'] * 10 + ['B'] * 2) atom.replace(range(1, 4), Atom('Au', [-1] * 2)) assert atom[0] == Atom('C') for i in range(1, 4): assert atom[i] != Atom('Au') assert atom[i] == Atom('Au', [-1] * 2) assert atom[4] != Atom('Au') assert atom[4] != Atom('Au', [-1] * 2) assert len(atom.atom) == 3 # Second replace call (equivalent to replace_atom) atom.replace(atom[0], Atom('C', [-1] * 2)) assert atom[0] == Atom('C', [-1] * 2) assert len(atom.atom) == 3 assert atom[0] == Atom('C', [-1] * 2) for i in range(4, 10): assert atom[i] == Atom('C', [-1] * 2) for i in range(1, 4): assert atom[i] == Atom('Au', [-1] * 2) for i in range(10, 12): assert atom[i] == Atom('B')
def test_len(setup): atom = Atoms([setup.C, setup.C3, setup.Au]) assert len(atom) == 3 assert len(atom.q0) == 3
def test_create_map(): atom = Atoms(map(lambda x: x, [1, 2, 3])) assert len(atom) == 3
def test_create2(): atom = Atoms(Atom(6, R=1.45), na=2) atom = Atoms(atom, na=4) assert atom[0].maxR() == 1.45 for ia in range(len(atom)): assert atom.maxR(True)[ia] == 1.45