root / ase / io / turbomole.py @ 15
Historique | Voir | Annoter | Télécharger (2,73 ko)
1 |
from ase.atoms import Atoms |
---|---|
2 |
from ase.units import Bohr |
3 |
|
4 |
|
5 |
def read_turbomole(filename='coord'): |
6 |
"""Method to read turbomole coord file
|
7 |
|
8 |
coords in bohr, atom types in lowercase, format:
|
9 |
$coord
|
10 |
x y z atomtype
|
11 |
x y z atomtype f
|
12 |
$end
|
13 |
Above 'f' means a fixed atom.
|
14 |
"""
|
15 |
from ase import Atoms, Atom |
16 |
from ase.constraints import FixAtoms |
17 |
|
18 |
if isinstance(filename, str): |
19 |
f = open(filename)
|
20 |
|
21 |
lines = f.readlines() |
22 |
atoms_pos = [] |
23 |
atom_symbols = [] |
24 |
dollar_count=0
|
25 |
myconstraints=[] |
26 |
for line in lines: |
27 |
if ('$' in line): |
28 |
dollar_count = dollar_count + 1
|
29 |
if (dollar_count >= 2): |
30 |
break
|
31 |
else:
|
32 |
x, y, z, symbolraw = line.split()[:4]
|
33 |
symbolshort=symbolraw.strip() |
34 |
symbol=symbolshort[0].upper()+symbolshort[1:].lower() |
35 |
#print symbol
|
36 |
atom_symbols.append(symbol) |
37 |
atoms_pos.append([float(x)*Bohr, float(y)*Bohr, float(z)*Bohr]) |
38 |
cols = line.split() |
39 |
if (len(cols) == 5): |
40 |
fixedstr = line.split()[4].strip()
|
41 |
if (fixedstr == "f"): |
42 |
myconstraints.append(True)
|
43 |
else:
|
44 |
myconstraints.append(False)
|
45 |
else:
|
46 |
myconstraints.append(False)
|
47 |
|
48 |
if type(filename) == str: |
49 |
f.close() |
50 |
|
51 |
atoms = Atoms(positions = atoms_pos, symbols = atom_symbols, pbc = False)
|
52 |
# c = FixAtoms(myconstraints)
|
53 |
# atoms.set_constraint(c)
|
54 |
#print c
|
55 |
|
56 |
|
57 |
return atoms
|
58 |
|
59 |
def write_turbomole(filename, atoms): |
60 |
"""Method to write turbomole coord file
|
61 |
"""
|
62 |
|
63 |
import numpy as np |
64 |
from ase.constraints import FixAtoms |
65 |
|
66 |
if isinstance(filename, str): |
67 |
f = open(filename, 'w') |
68 |
else: # Assume it's a 'file-like object' |
69 |
f = filename |
70 |
|
71 |
coord = atoms.get_positions() |
72 |
symbols = atoms.get_chemical_symbols() |
73 |
printfixed = False
|
74 |
|
75 |
if atoms.constraints:
|
76 |
for constr in atoms.constraints: |
77 |
if isinstance(constr, FixAtoms): |
78 |
fix_index=constr.index |
79 |
printfixed=True
|
80 |
#print sflags
|
81 |
|
82 |
if (printfixed):
|
83 |
fix_str=[] |
84 |
for i in fix_index: |
85 |
if i == 1: |
86 |
fix_str.append("f")
|
87 |
else:
|
88 |
fix_str.append(" ")
|
89 |
|
90 |
|
91 |
f.write("$coord\n")
|
92 |
if (printfixed):
|
93 |
for (x, y, z), s, fix in zip(coord,symbols,fix_str): |
94 |
f.write('%20.14f %20.14f %20.14f %2s %2s \n'
|
95 |
% (x/Bohr, y/Bohr, z/Bohr, s.lower(), fix)) |
96 |
|
97 |
else:
|
98 |
for (x, y, z), s in zip(coord,symbols): |
99 |
f.write('%20.14f %20.14f %20.14f %2s \n'
|
100 |
% (x/Bohr, y/Bohr, z/Bohr, s.lower())) |
101 |
f.write("$end\n")
|