root / ase / io / dftb.py @ 14
Historique | Voir | Annoter | Télécharger (3,05 ko)
| 1 |
from ase.atoms import Atoms |
|---|---|
| 2 |
from ase.units import Bohr |
| 3 |
|
| 4 |
|
| 5 |
def read_dftb(filename='dftb_in.hsd'): |
| 6 |
"""Method to read coordinates form DFTB+ input file dftb_in.hsd
|
| 7 |
additionally read information about fixed atoms
|
| 8 |
and periodic boundary condition
|
| 9 |
"""
|
| 10 |
from ase import Atoms, Atom |
| 11 |
from ase.constraints import FixAtoms |
| 12 |
import sys, string |
| 13 |
|
| 14 |
if isinstance(filename, str): |
| 15 |
f = open(filename)
|
| 16 |
|
| 17 |
lines = f.readlines() |
| 18 |
atoms_pos = [] |
| 19 |
atom_symbols = [] |
| 20 |
type_names = [] |
| 21 |
my_pbc = False
|
| 22 |
myconstraints=[] |
| 23 |
moved_atoms_found = False
|
| 24 |
range_found = False
|
| 25 |
moved_atoms = [] |
| 26 |
|
| 27 |
for line in lines: |
| 28 |
if (line.strip().startswith('#')): |
| 29 |
pass
|
| 30 |
else:
|
| 31 |
if ('TypeNames' in line): |
| 32 |
col=line.split() |
| 33 |
for i in range(3,len(col)-1): |
| 34 |
type_names.append(col[i].strip("\""))
|
| 35 |
elif ('Periodic' in line): |
| 36 |
if ('Yes' in line): |
| 37 |
my_pbc = True
|
| 38 |
else:
|
| 39 |
pass
|
| 40 |
|
| 41 |
start_reading_coords=False
|
| 42 |
stop_reading_coords=False
|
| 43 |
for line in lines: |
| 44 |
if (line.strip().startswith('#')): |
| 45 |
pass
|
| 46 |
else:
|
| 47 |
if ('TypesAndCoordinates' in line): |
| 48 |
start_reading_coords=True
|
| 49 |
if start_reading_coords:
|
| 50 |
if ('}' in line): |
| 51 |
stop_reading_coords=True
|
| 52 |
if (start_reading_coords and not(stop_reading_coords) |
| 53 |
and not 'TypesAndCoordinates' in line): |
| 54 |
typeindexstr, x, y, z = line.split()[:4]
|
| 55 |
typeindex=int(typeindexstr)
|
| 56 |
symbol=type_names[typeindex-1]
|
| 57 |
atom_symbols.append(symbol) |
| 58 |
atoms_pos.append([float(x), float(y), float(z)]) |
| 59 |
|
| 60 |
|
| 61 |
if type(filename) == str: |
| 62 |
f.close() |
| 63 |
|
| 64 |
atoms = Atoms(positions = atoms_pos, symbols = atom_symbols, pbc = my_pbc) |
| 65 |
|
| 66 |
|
| 67 |
return atoms
|
| 68 |
|
| 69 |
|
| 70 |
def write_dftb(filename,atoms): |
| 71 |
"""Method to write coordinates in DFTB+ format
|
| 72 |
"""
|
| 73 |
|
| 74 |
import numpy as np |
| 75 |
from ase.constraints import FixAtoms |
| 76 |
|
| 77 |
if isinstance(filename, str): |
| 78 |
f = open(filename)
|
| 79 |
|
| 80 |
lines = f.readlines() |
| 81 |
|
| 82 |
if type(filename) == str: |
| 83 |
f.close() |
| 84 |
|
| 85 |
if isinstance(filename, str): |
| 86 |
f = open(filename, 'w') |
| 87 |
else: # Assume it's a 'file-like object' |
| 88 |
f = filename |
| 89 |
|
| 90 |
coord = atoms.get_positions() |
| 91 |
symbols = atoms.get_chemical_symbols() |
| 92 |
|
| 93 |
|
| 94 |
start_writing_coords = False
|
| 95 |
stop_writing_coords = False
|
| 96 |
i=0
|
| 97 |
for line in lines: |
| 98 |
if ('TypesAndCoordinates' in line): |
| 99 |
start_writing_coords=True
|
| 100 |
if (start_writing_coords and not(stop_writing_coords)): |
| 101 |
if ('}' in line): |
| 102 |
stop_writing_coords = True
|
| 103 |
if (start_writing_coords and not(stop_writing_coords)and |
| 104 |
not ('TypesAndCoordinates' in line)): |
| 105 |
atom_type_index = line.split()[0]
|
| 106 |
f.write('%6s %20.14f %20.14f %20.14f\n'
|
| 107 |
% (atom_type_index,coord[i][0],coord[i][1],coord[i][2])) |
| 108 |
i=i+1
|
| 109 |
else:
|
| 110 |
f.write(line) |