Coverage for /builds/kinetik161/ase/ase/io/gpw.py: 11.11%
63 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
1"""Read gpw-file from GPAW."""
2import ase.io.ulm as ulm
3from ase import Atoms
4from ase.calculators.singlepoint import (SinglePointDFTCalculator,
5 SinglePointKPoint, all_properties)
6from ase.io.trajectory import read_atoms
7from ase.units import Bohr, Hartree
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 # New gpw-files may have "non_collinear_magmom(s)" which ASE
41 # doesn't know:
42 **{property: value
43 for property, value in reader.results.asdict().items()
44 if property in all_properties})
46 if kpts is not None:
47 atoms.calc.kpts = []
48 spin = 0
49 for eps_kn, f_kn in zip(wfs.eigenvalues, wfs.occupations):
50 kpt = 0
51 for weight, eps_n, f_n in zip(kpts.weights, eps_kn, f_kn):
52 atoms.calc.kpts.append(
53 SinglePointKPoint(weight, spin, kpt, eps_n, f_n))
54 kpt += 1
55 spin += 1
57 reader.close()
59 return atoms
62def read_old_gpw(filename):
63 from gpaw.io.tar import Reader
64 r = Reader(filename)
65 positions = r.get('CartesianPositions') * Bohr
66 numbers = r.get('AtomicNumbers')
67 cell = r.get('UnitCell') * Bohr
68 pbc = r.get('BoundaryConditions')
69 tags = r.get('Tags')
70 magmoms = r.get('MagneticMoments')
71 energy = r.get('PotentialEnergy') * Hartree
73 if r.has_array('CartesianForces'):
74 forces = r.get('CartesianForces') * Hartree / Bohr
75 else:
76 forces = None
78 atoms = Atoms(positions=positions,
79 numbers=numbers,
80 cell=cell,
81 pbc=pbc)
82 if tags.any():
83 atoms.set_tags(tags)
85 if magmoms.any():
86 atoms.set_initial_magnetic_moments(magmoms)
87 magmom = magmoms.sum()
88 else:
89 magmoms = None
90 magmom = None
92 atoms.calc = SinglePointDFTCalculator(atoms, energy=energy,
93 forces=forces,
94 magmoms=magmoms,
95 magmom=magmom)
96 kpts = []
97 if r.has_array('IBZKPoints'):
98 for w, kpt, eps_n, f_n in zip(r.get('IBZKPointWeights'),
99 r.get('IBZKPoints'),
100 r.get('Eigenvalues'),
101 r.get('OccupationNumbers')):
102 kpts.append(SinglePointKPoint(w, kpt[0], kpt[1],
103 eps_n[0], f_n[0]))
104 atoms.calc.kpts = kpts
106 return atoms