Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Read gpw-file from GPAW."""
2from ase import Atoms
3from ase.calculators.singlepoint import (SinglePointDFTCalculator,
4 SinglePointKPoint)
5from ase.units import Bohr, Hartree
6import ase.io.ulm as ulm
7from ase.io.trajectory import read_atoms
10def read_gpw(filename):
11 try:
12 reader = ulm.open(filename)
13 except ulm.InvalidULMFileError:
14 return read_old_gpw(filename)
16 atoms = read_atoms(reader.atoms, _try_except=False)
18 wfs = reader.wave_functions
19 kpts = wfs.get('kpts')
20 if kpts is None:
21 ibzkpts = None
22 bzkpts = None
23 bz2ibz = None
24 else:
25 ibzkpts = kpts.ibzkpts
26 bzkpts = kpts.get('bzkpts')
27 bz2ibz = kpts.get('bz2ibz')
29 if reader.version >= 3:
30 efermi = reader.wave_functions.fermi_levels.mean()
31 else:
32 efermi = reader.occupations.fermilevel
34 atoms.calc = SinglePointDFTCalculator(
35 atoms,
36 efermi=efermi,
37 ibzkpts=ibzkpts,
38 bzkpts=bzkpts,
39 bz2ibz=bz2ibz,
40 **reader.results.asdict())
42 if kpts is not None:
43 atoms.calc.kpts = []
44 spin = 0
45 for eps_kn, f_kn in zip(wfs.eigenvalues, wfs.occupations):
46 kpt = 0
47 for weight, eps_n, f_n in zip(kpts.weights, eps_kn, f_kn):
48 atoms.calc.kpts.append(
49 SinglePointKPoint(weight, spin, kpt, eps_n, f_n))
50 kpt += 1
51 spin += 1
52 return atoms
55def read_old_gpw(filename):
56 from gpaw.io.tar import Reader
57 r = Reader(filename)
58 positions = r.get('CartesianPositions') * Bohr
59 numbers = r.get('AtomicNumbers')
60 cell = r.get('UnitCell') * Bohr
61 pbc = r.get('BoundaryConditions')
62 tags = r.get('Tags')
63 magmoms = r.get('MagneticMoments')
64 energy = r.get('PotentialEnergy') * Hartree
66 if r.has_array('CartesianForces'):
67 forces = r.get('CartesianForces') * Hartree / Bohr
68 else:
69 forces = None
71 atoms = Atoms(positions=positions,
72 numbers=numbers,
73 cell=cell,
74 pbc=pbc)
75 if tags.any():
76 atoms.set_tags(tags)
78 if magmoms.any():
79 atoms.set_initial_magnetic_moments(magmoms)
80 magmom = magmoms.sum()
81 else:
82 magmoms = None
83 magmom = None
85 atoms.calc = SinglePointDFTCalculator(atoms, energy=energy,
86 forces=forces,
87 magmoms=magmoms,
88 magmom=magmom)
89 kpts = []
90 if r.has_array('IBZKPoints'):
91 for w, kpt, eps_n, f_n in zip(r.get('IBZKPointWeights'),
92 r.get('IBZKPoints'),
93 r.get('Eigenvalues'),
94 r.get('OccupationNumbers')):
95 kpts.append(SinglePointKPoint(w, kpt[0], kpt[1],
96 eps_n[0], f_n[0]))
97 atoms.calc.kpts = kpts
99 return atoms