root / ase / io / bader.py @ 14
Historique | Voir | Annoter | Télécharger (1,7 ko)
| 1 |
import numpy as np |
|---|---|
| 2 |
from ase.units import Bohr |
| 3 |
|
| 4 |
def attach_charges(atoms, fileobj='ACF.dat', displacement=1e-4): |
| 5 |
"""Attach the charges from the fileobj to the Atoms."""
|
| 6 |
if isinstance(fileobj, str): |
| 7 |
fileobj = open(fileobj)
|
| 8 |
|
| 9 |
sep = '---------------'
|
| 10 |
i = 0 # Counter for the lines |
| 11 |
k = 0 # Counter of sep |
| 12 |
assume6columns = False
|
| 13 |
for line in fileobj: |
| 14 |
if line[0] == '\n': # check if there is an empty line in the |
| 15 |
i -= 1 # head of ACF.dat file |
| 16 |
if i == 0: |
| 17 |
headings = line |
| 18 |
if 'BADER' in headings.split(): |
| 19 |
j = headings.split().index('BADER')
|
| 20 |
elif 'CHARGE' in headings.split(): |
| 21 |
j = headings.split().index('CHARGE')
|
| 22 |
else:
|
| 23 |
print 'Can\'t find keyword "BADER" or "CHARGE".' \ |
| 24 |
+' Assuming the ACF.dat file has 6 columns.'
|
| 25 |
j = 4
|
| 26 |
assume6columns = True
|
| 27 |
if sep in line: # Stop at last seperator line |
| 28 |
if k == 1: |
| 29 |
break
|
| 30 |
k += 1
|
| 31 |
if not i > 1: |
| 32 |
pass
|
| 33 |
else:
|
| 34 |
words = line.split() |
| 35 |
if assume6columns is True: |
| 36 |
if len(words) != 6: |
| 37 |
raise IOError('Number of columns in ACF file incorrect!\n' |
| 38 |
'Check that Bader program version >= 0.25')
|
| 39 |
|
| 40 |
atom = atoms[int(words[0]) - 1] |
| 41 |
atom.charge = atom.number - float(words[j])
|
| 42 |
if displacement is not None: # check if the atom positions match |
| 43 |
xyz = np.array([float(w) for w in words[1:4]]) * Bohr |
| 44 |
assert np.linalg.norm(atom.position - xyz) < displacement
|
| 45 |
i += 1
|
| 46 |
|