1from Siesta.Interface import Atom, Crystal
2
3#
4def ReadStruct(filename,species_map):
5    """Return "Crystal" object read from Siesta STRUCT-file.
6       species_map is a dictionary mapping the species index to
7       atomic symbols. For example:
8
9          species_map = { "1" : "Mg",
10                          "2" : "C" ,
11                          "3" : "O" }
12
13	In a forthcoming version this information could be obtained
14        directly from the atomic number in the .STRUCT_{IN,OUT} file.
15
16     """
17    f = open(filename)
18#
19#   First read unit cell
20#
21    cell = []
22    for i in range(3):
23      vector = f.readline()
24      x, y, z = vector.split()
25      cell.append([ float(x), float(y), float(z) ])
26#
27#   Now the atoms
28#
29#   Robust code to allow for broken lines.
30#   Accumulate all the data in a big list...
31
32    natoms = int(f.readline())
33    strings = []
34    while 1:
35       line = f.readline()
36       if not line: break
37       strings = strings + line.split()
38
39#   ... and extract the information for each atom in turn
40#
41    crystal = Crystal([])
42    pos = 0
43    for a in range(natoms):
44        sublist = strings[pos:pos+5]
45        spindex, z , x, y, z = sublist
46        symbol = species_map[spindex]
47        crystal.append(Atom(symbol, [float(x), float(y), float(z)]))
48        pos = pos + 5
49
50    crystal.SetUnitCell(cell)
51
52    return crystal
53#
54
55