root / ase / io / dacapo.py @ 13
Historique | Voir | Annoter | Télécharger (2,49 ko)
1 |
import numpy as np |
---|---|
2 |
|
3 |
from ase.calculators.singlepoint import SinglePointCalculator |
4 |
from ase.atom import Atom |
5 |
from ase.atoms import Atoms |
6 |
|
7 |
|
8 |
def read_dacapo_text(fileobj): |
9 |
if isinstance(fileobj, str): |
10 |
fileobj = open(fileobj)
|
11 |
|
12 |
lines = fileobj.readlines() |
13 |
i = lines.index(' Structure: A1 A2 A3\n')
|
14 |
cell = np.array([[float(w) for w in line.split()[2:5]] |
15 |
for line in lines[i + 1:i + 4]]).transpose() |
16 |
i = lines.index(' Structure: >> Ionic positions/velocities ' +
|
17 |
'in cartesian coordinates <<\n')
|
18 |
atoms = [] |
19 |
for line in lines[i + 4:]: |
20 |
words = line.split() |
21 |
if len(words) != 9: |
22 |
break
|
23 |
Z, x, y, z = words[2:6] |
24 |
atoms.append(Atom(int(Z), [float(x), float(y), float(z)])) |
25 |
|
26 |
atoms = Atoms(atoms, cell=cell.tolist()) |
27 |
|
28 |
try:
|
29 |
i = lines.index( |
30 |
' DFT: CPU time Total energy\n')
|
31 |
except ValueError: |
32 |
pass
|
33 |
else:
|
34 |
column = lines[i + 3].split().index('selfcons') - 1 |
35 |
try:
|
36 |
i2 = lines.index(' ANALYSIS PART OF CODE\n', i)
|
37 |
except ValueError: |
38 |
pass
|
39 |
else:
|
40 |
while i2 > i:
|
41 |
if lines[i2].startswith(' DFT:'): |
42 |
break
|
43 |
i2 -= 1
|
44 |
energy = float(lines[i2].split()[column])
|
45 |
atoms.set_calculator(SinglePointCalculator(energy, None, None, |
46 |
None, atoms))
|
47 |
|
48 |
return atoms
|
49 |
|
50 |
|
51 |
|
52 |
def read_dacapo(filename): |
53 |
from ase.io.pupynere import NetCDFFile |
54 |
|
55 |
nc = NetCDFFile(filename) |
56 |
dims = nc.dimensions |
57 |
vars = nc.variables |
58 |
|
59 |
cell = vars['UnitCell'][-1] |
60 |
try:
|
61 |
magmoms = vars['InitialAtomicMagneticMoment'][:] |
62 |
except KeyError: |
63 |
magmoms = None
|
64 |
try:
|
65 |
tags = vars['AtomTags'][:] |
66 |
except KeyError: |
67 |
tags = None
|
68 |
atoms = Atoms(scaled_positions=vars['DynamicAtomPositions'][-1], |
69 |
symbols=[(a + b).strip() |
70 |
for a, b in vars['DynamicAtomSpecies'][:]], |
71 |
cell=cell, |
72 |
magmoms=magmoms, |
73 |
tags=tags, |
74 |
pbc=True)
|
75 |
|
76 |
try:
|
77 |
energy = vars['TotalEnergy'][-1] |
78 |
force = vars['DynamicAtomForces'][-1] |
79 |
except KeyError: |
80 |
energy = None
|
81 |
force = None
|
82 |
calc = SinglePointCalculator(energy,force,None, None, atoms) ### Fixme magmoms |
83 |
atoms.set_calculator(calc) |
84 |
|
85 |
return atoms
|