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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

# coding: utf-8 

# Copyright (c) Pymatgen Development Team. 

# Distributed under the terms of the MIT License. 

 

from __future__ import division, unicode_literals 

 

""" 

This module defines the FeffInputSet abstract base class and a concrete 

implementation for the Materials Project. The basic concept behind an input 

set is to specify a scheme to generate a consistent set of Feff inputs from a 

structure without further user intervention. This ensures comparability across 

runs. 

""" 

 

import six 

 

__author__ = "Alan Dozier" 

__credits__ = "Anubhav Jain, Shyue Ping Ong" 

__copyright__ = "Copyright 2011, The Materials Project" 

__version__ = "1.0.3" 

__maintainer__ = "Alan Dozier" 

__email__ = "adozier@uky.edu" 

__date__ = "April 7, 2013" 

 

import os 

import abc 

 

from monty.serialization import loadfn 

 

from pymatgen.io.feff import FeffAtoms, FeffTags, FeffPot, Header 

 

 

class AbstractFeffInputSet(six.with_metaclass(abc.ABCMeta, object)): 

""" 

Abstract base class representing a set of Feff input parameters. 

The idea is that using a FeffInputSet, a complete set of input files 

(feffPOT,feffXANES, feffEXAFS, ATOMS, feff.inp)set_ 

can be generated in an automated fashion for any structure. 

""" 

 

@abc.abstractmethod 

def get_feff_atoms(self, structure, central_atom): 

""" 

Returns Atoms string from a structure that goes in feff.inp file. 

 

Args: 

structure: pymatgen structure object 

central_atom: atom symbol string for absorbing atom 

 

Returns: 

FeffAtoms object. 

""" 

return 

 

@abc.abstractmethod 

def get_feff_tags(self, calc_type): 

""" 

Returns standard calculation paramters for either an FEFF XANES or 

EXAFS input. 

 

Args: 

calc_type: At this time either 'XANES' or 'EXAFS' string is 

supported for K shell excitation. In the future this will be 

expanded to include other shells and material class 

differentiation. 

""" 

return 

 

@abc.abstractmethod 

def get_feff_pot(self, structure, central_atom): 

""" 

Returns POTENTIAL section used in feff.inp from a structure. 

 

Args: 

structure: pymatgen structure object 

central_atom: atom symbol string for absorbing atom 

""" 

return 

 

@abc.abstractmethod 

def get_header(self, structure, source, comment): 

""" 

Returns header to be used in feff.inp file from a pymatgen structure 

 

Args: 

structure: A pymatgen structure object 

source: Source identifier used to create structure, can be defined 

however user wants to organize structures, calculations, etc. 

example would be Materials Project material ID number. 

""" 

return 

 

def get_all_feff_input(self, structure, calc_type, source, central_atom, 

comment=''): 

""" 

Returns all input files as a dict of {filename: feffio object} 

 

Args: 

structure: Structure object 

calc_type: At this time either 'XANES' or 'EXAFS' string is 

supported for K shell excitation. In the future this will be 

expanded to inlude other shells and material class 

differentiation. 

source: Source identifier used to create structure, can be defined 

however user wants to organize structures, calculations, etc. 

example would be Materials Project material ID number. 

central_atom: Atom symbol string for absorbing atom 

comment: Comment to appear in Header. 

 

Returns: 

dict of objects used to create feff.inp file i.e. Header, FeffTags, 

FeffPot, FeffAtoms 

""" 

 

feff = {"HEADER": self.get_header(structure, source, comment), 

"PARAMETERS": self.get_feff_tags(calc_type), 

"POTENTIALS": self.get_feff_pot(structure, central_atom), 

"ATOMS": self.get_feff_atoms(structure, central_atom)} 

 

return feff 

 

def write_input(self, structure, calc_type, source, central_atom, 

comment='', output_dir=".", make_dir_if_not_present=True): 

""" 

Writes a set of FEFF input to a directory. 

 

Args: 

structure: Structure object 

calc_type: At this time either 'XANES' or 'EXAFS' string is 

supported for K shell excitation. In the future this will be 

expanded to include other shells and material class 

differentiation. 

source: Source identifier used to create structure, can be defined 

however user wants to organize structures, calculations, etc. 

example would be Materials Project material ID number. 

central_atom: Atom symbol string for absorbing atom 

output_dir: Directory to output the FEFF input files 

comment: comment for Header 

make_dir_if_not_present: Set to True if you want the directory ( 

and the whole path) to be created if it is not present. 

""" 

 

if make_dir_if_not_present and not os.path.exists(output_dir): 

os.makedirs(output_dir) 

 

feff = self.get_all_feff_input(structure, calc_type, source, 

central_atom, comment) 

 

feff_input = "\n\n".join(str(feff[f]) for f in ["HEADER", "PARAMETERS", 

"POTENTIALS", "ATOMS"]) 

 

for k, v in six.iteritems(feff): 

with open(os.path.join(output_dir, k), "w") as f: 

f.write(str(v)) 

 

with open(os.path.join(output_dir, "feff.inp"), "w") as f: 

f.write(feff_input) 

f.close() 

 

def as_dict(self, structure, calc_type, source, central_atom, 

comment=''): 

"""Creates a feff.inp dictionary as a string""" 

 

feff = self.get_all_feff_input(structure, calc_type, source, 

central_atom, comment) 

feff_input = "\n\n".join(str(feff[f]) for f in ["HEADER", "PARAMETERS", 

"POTENTIALS", "ATOMS"]) 

return {'@module': self.__class__.__module__, 

'@class': self.__class__.__name__, 

'feff.inp': feff_input} 

 

@staticmethod 

def from_dict(d): 

"""Return feff.inp from a dictionary string representation""" 

return d['feff.inp'] 

 

 

class FeffInputSet(AbstractFeffInputSet): 

""" 

Standard implementation of FeffInputSet, which can be extended by specific 

implementations. 

 

Args: 

name: The name of a grouping of input parameter sets such as 

"MaterialsProject". 

""" 

 

def __init__(self, name): 

self.name = name 

module_dir = os.path.dirname(os.path.abspath(__file__)) 

config = loadfn(os.path.join(module_dir, "FeffInputSets.yaml")) 

self.xanes_settings = config[self.name + "feffXANES"] 

self.exafs_settings = config[self.name + "feffEXAFS"] 

 

def get_header(self, structure, source='', comment=''): 

""" 

Creates header string from structure object 

 

Args: 

structure: A pymatgen structure object 

source: Source identifier used to create structure, can be defined 

however user wants to organize structures, calculations, etc. 

example would be Materials Project material ID number. 

comment: comment to include in header 

 

Returns: 

Header object to be used in feff.inp file from a pymatgen structure 

""" 

return Header(structure, source, comment) 

 

def get_feff_tags(self, calc_type): 

""" 

Reads standard parameters for XANES or EXAFS calculation 

from FeffInputSets.yaml file. 

 

Args: 

calc_type: At this time either 'XANES' or 'EXAFS' string is 

supported for K shell excitation. In the future this will be 

expanded to include other shells and material class 

differentiation. 

 

Returns: 

FeffTags object 

""" 

 

if calc_type.upper() == "XANES": 

fefftags = FeffTags(self.xanes_settings) 

elif calc_type.upper() == "EXAFS": 

fefftags = FeffTags(self.exafs_settings) 

else: 

raise ValueError("{} is not a valid calculation type" 

.format(calc_type)) 

 

return fefftags 

 

def get_feff_pot(self, structure, central_atom): 

""" 

Creates string representation of potentials used in POTENTIAL file and 

feff.inp. 

 

Args: 

structure: pymatgen structure object 

central_atom: atom symbol string for absorbing atom 

 

Returns: 

FeffPot object 

""" 

return FeffPot(structure, central_atom) 

 

def get_feff_atoms(self, structure, central_atom): 

""" 

Creates string representation of atomic shell coordinates using in 

ATOMS file and feff.inp. 

 

Args: 

structure: pymatgen structure object 

central_atom: atom symbol string for absorbing atom 

 

Returns: 

FeffAtoms object 

""" 

return FeffAtoms(structure, central_atom) 

 

def __str__(self): 

output = [self.name] 

section_names = ["XANES", "EXAFS"] 

for ns in section_names: 

for d in [self.xanes_settings, self.exafs_settings]: 

output.append(ns) 

for k, v in six.iteritems(d): 

output.append("%s = %s" % (k, str(v))) 

output.append("") 

 

return "\n".join(output) 

 

 

class MaterialsProjectFeffInputSet(FeffInputSet): 

""" 

Implementation of FeffInputSet utilizing parameters in the public 

Materials Project. 

""" 

def __init__(self): 

super(MaterialsProjectFeffInputSet, self).__init__("MaterialsProject")