Hide keyboard shortcuts

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 

8 

9 

10def read_gpw(filename): 

11 try: 

12 reader = ulm.open(filename) 

13 except ulm.InvalidULMFileError: 

14 return read_old_gpw(filename) 

15 

16 atoms = read_atoms(reader.atoms, _try_except=False) 

17 

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') 

28 

29 if reader.version >= 3: 

30 efermi = reader.wave_functions.fermi_levels.mean() 

31 else: 

32 efermi = reader.occupations.fermilevel 

33 

34 atoms.calc = SinglePointDFTCalculator( 

35 atoms, 

36 efermi=efermi, 

37 ibzkpts=ibzkpts, 

38 bzkpts=bzkpts, 

39 bz2ibz=bz2ibz, 

40 **reader.results.asdict()) 

41 

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 

53 reader.close() 

54 

55 return atoms 

56 

57 

58def read_old_gpw(filename): 

59 from gpaw.io.tar import Reader 

60 r = Reader(filename) 

61 positions = r.get('CartesianPositions') * Bohr 

62 numbers = r.get('AtomicNumbers') 

63 cell = r.get('UnitCell') * Bohr 

64 pbc = r.get('BoundaryConditions') 

65 tags = r.get('Tags') 

66 magmoms = r.get('MagneticMoments') 

67 energy = r.get('PotentialEnergy') * Hartree 

68 

69 if r.has_array('CartesianForces'): 

70 forces = r.get('CartesianForces') * Hartree / Bohr 

71 else: 

72 forces = None 

73 

74 atoms = Atoms(positions=positions, 

75 numbers=numbers, 

76 cell=cell, 

77 pbc=pbc) 

78 if tags.any(): 

79 atoms.set_tags(tags) 

80 

81 if magmoms.any(): 

82 atoms.set_initial_magnetic_moments(magmoms) 

83 magmom = magmoms.sum() 

84 else: 

85 magmoms = None 

86 magmom = None 

87 

88 atoms.calc = SinglePointDFTCalculator(atoms, energy=energy, 

89 forces=forces, 

90 magmoms=magmoms, 

91 magmom=magmom) 

92 kpts = [] 

93 if r.has_array('IBZKPoints'): 

94 for w, kpt, eps_n, f_n in zip(r.get('IBZKPointWeights'), 

95 r.get('IBZKPoints'), 

96 r.get('Eigenvalues'), 

97 r.get('OccupationNumbers')): 

98 kpts.append(SinglePointKPoint(w, kpt[0], kpt[1], 

99 eps_n[0], f_n[0])) 

100 atoms.calc.kpts = kpts 

101 

102 return atoms