UHLI_REMO

st-pdos.py script

Nov 26th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys, string
  4. from xml.sax import handler, make_parser, saxutils
  5.  
  6.  
  7. ### Functions
  8. def normalize_whitespace( text ):
  9. return string.strip( text )
  10. # end def
  11.  
  12.  
  13. ### Classes
  14. class orbital:
  15. def __init__( self ):
  16. self.orbital_index = 0
  17. self.atom_index = 0
  18. self.species = 'X'
  19. self.position = [0.000, 0.000, 0.000]
  20. self.nlmz = [0, 0, 0, 0]
  21. self.dos = [[],[]]
  22. # end def
  23. # end class
  24.  
  25.  
  26. class pdosData:
  27. def __init__( self ):
  28. self.nspin = 0
  29. self.norbitals = 0
  30. self.natoms = 0
  31. self.energy_values = []
  32. self.orbitals = []
  33. # end def
  34. # end class
  35.  
  36.  
  37. # class pdosHandler
  38. class pdosHandler( handler.ContentHandler ):
  39. def __init__( self, data ):
  40. self.data = data
  41. self.parts = { 'nspin': 0, 'norbitals': 0, 'energy_values': 0, 'orbital': 0, 'data': 0 }
  42. self.line = ""
  43. self.orbidx = 0
  44. self.atmidx = 0
  45. #
  46.  
  47. def startDocument( self ):
  48. print "Reading started"
  49.  
  50. def endDocument( self ):
  51. # print "Reading finished"
  52. self.data.natoms = self.atmidx
  53.  
  54. def startElement( self, name, attrs ):
  55. for key, val in self.parts.iteritems():
  56. if( name == key ):
  57. self.parts[key] = 1;
  58.  
  59. if( self.parts['orbital'] ):
  60. orb = orbital()
  61. ok = 1
  62.  
  63. val = attrs.get( 'index' )
  64. if( val != None ):
  65. val = string.atoi( normalize_whitespace( attrs.get( 'index' ) ) )
  66. self.orbidx = (val - 1)
  67. orb.orbital_index = val
  68. else:
  69. ok = 0
  70.  
  71. val = attrs.get( 'atom_index' )
  72. if( val != None ):
  73. val = string.atoi( normalize_whitespace( attrs.get( 'atom_index' ) ) )
  74. self.atmidx = val
  75. orb.atom_index = val
  76. else:
  77. ok = 0
  78.  
  79. val = attrs.get( 'species' )
  80. if( val != None ):
  81. val = str( normalize_whitespace( attrs.get( 'species' ) ) )
  82. orb.species = val
  83. else:
  84. ok = 0
  85.  
  86. val = attrs.get( 'position' )
  87. if( val != None ):
  88. lin = normalize_whitespace( attrs.get( 'position' ) )
  89. pos = lin.split()
  90. orb.position[0] = string.atof( pos[0] )
  91. orb.position[1] = string.atof( pos[1] )
  92. orb.position[2] = string.atof( pos[2] )
  93. else:
  94. ok = 0
  95.  
  96. val = attrs.get( 'n' )
  97. if( val != None ):
  98. val = string.atoi( normalize_whitespace( attrs.get( 'n' ) ) )
  99. orb.nlmz[0] = val
  100. else:
  101. ok = 0
  102.  
  103. val = attrs.get( 'l' )
  104. if( val != None ):
  105. val = string.atoi( normalize_whitespace( attrs.get( 'l' ) ) )
  106. orb.nlmz[1] = val
  107. else:
  108. ok = 0
  109.  
  110. val = attrs.get( 'm' )
  111. if( val != None ):
  112. val = normalize_whitespace( attrs.get( 'm' ) )
  113. if( val != '*' ):
  114. val = string.atoi( val )
  115.  
  116. orb.nlmz[2] = val
  117. else:
  118. ok = 0
  119.  
  120. val = attrs.get( 'z' )
  121. if( val != None ):
  122. val = string.atoi( normalize_whitespace( attrs.get( 'z' ) ) )
  123. orb.nlmz[3] = val
  124. else:
  125. ok = 0
  126.  
  127. if( ok ):
  128. self.data.orbitals.append( orb )
  129. # end orbital part
  130. # end startElement
  131.  
  132.  
  133. def endElement( self, name ):
  134. for key, val in self.parts.iteritems():
  135. if( name == key ):
  136. self.parts[key] = 0;
  137. #
  138.  
  139. def characters( self, ch ):
  140. if( self.parts['nspin'] ):
  141. self.data.nspin = string.atoi( normalize_whitespace( ch ) )
  142.  
  143. if( self.parts['norbitals'] ):
  144. self.data.norbitals = string.atoi( normalize_whitespace( ch ) )
  145.  
  146. if( self.parts['energy_values'] ):
  147. if( ch != '\n' ):
  148. self.data.energy_values.append( string.atof( normalize_whitespace( ch ) ) )
  149.  
  150. if( self.parts['orbital'] and self.parts['data'] ):
  151. if( ch != '\n' ):
  152. self.line += ch
  153. else:
  154. lin = normalize_whitespace( self.line )
  155. if( self.line != "" ):
  156. dos = lin.split();
  157. alpha = string.atof( dos[0] )
  158. self.data.orbitals[self.orbidx].dos[0].append( alpha )
  159.  
  160. if( self.data.nspin == 2 ):
  161. beta = string.atof( dos[1] )
  162. self.data.orbitals[self.orbidx].dos[1].append( beta )
  163. #
  164. self.line = ""
  165. #
  166. #
  167. #
  168. #
  169. # end class pdosHandler
  170.  
  171. def getqn( nlmz ):
  172. return nlmz[0]
  173.  
  174. def getql( nlmz ):
  175. ql = [ 's', 'p', 'd', 'f' ]
  176. return ql[nlmz[1]]
  177.  
  178. def getqm( nlmz ):
  179. if( nlmz[2] == 0 ):
  180. return 'z'
  181.  
  182. if( nlmz[2] == 1 ):
  183. return 'x'
  184.  
  185. if( nlmz[2] == -1 ):
  186. return 'y'
  187.  
  188. return str( nlmz[2] )
  189.  
  190.  
  191.  
  192. def pdoswrite( idx, data ):
  193. orbitals = []
  194.  
  195. # print data.orbitals[2569].dos[0]
  196.  
  197. i = 0
  198. for orb in data.orbitals:
  199. if( orb.atom_index == idx ):
  200. orbitals.append( i )
  201. i += 1
  202.  
  203. print "Found in: "+str(orbitals)
  204.  
  205. for s in range(data.nspin):
  206. spins = ['up','down']
  207. total = []
  208. output = "pdos_spin_"+spins[s]+"_atom_"+str(idx)+".dat"
  209. fp = open( output, "w")
  210.  
  211. fp.write( "%14s" % "Energy[eV]" )
  212.  
  213.  
  214. for i in orbitals:
  215. fp.write( "\t%s%d%s%s%d" % ("nlmz=", getqn( data.orbitals[i].nlmz ), getql( data.orbitals[i].nlmz ), getqm( data.orbitals[i].nlmz ), data.orbitals[i].nlmz[3]) )
  216.  
  217. fp.write( "\t%s" % "Total" )
  218. fp.write( "\n" )
  219.  
  220. j = 0
  221. for eng in data.energy_values:
  222. total.append( 0.000 )
  223.  
  224. fp.write( "%12.5f" % eng )
  225.  
  226. for i in orbitals:
  227. #print data.orbitals[i].dos[0][0]
  228. fp.write( "\t%9.5f" % data.orbitals[i].dos[s][j] )
  229. total[j] += data.orbitals[i].dos[s][j]
  230. # print orb.dos[0]
  231. # end for i
  232. fp.write( "\t%9.5f" % total[j] )
  233. fp.write( "\n" );
  234. j += 1
  235. # end for eng
  236. fp.close()
  237. # end for s
  238.  
  239. def pdosreader( inp, data ):
  240.  
  241. handler = pdosHandler( data )
  242. parser = make_parser()
  243.  
  244. parser.setContentHandler( handler )
  245. inFile = open( inp, 'r' )
  246.  
  247. parser.parse( inFile )
  248.  
  249. inFile.close()
  250. #
  251.  
  252.  
  253.  
  254. def main():
  255. data = pdosData()
  256.  
  257. args = sys.argv[1:]
  258.  
  259. if len( args ) < 2:
  260. print "usage: "+sys.argv[0]+" input atomno"
  261. sys.exit( -1 )
  262.  
  263. pdosreader( args[0], data )
  264. pdoswrite( string.atoi( args[1] ), data )
  265.  
  266.  
  267. if __name__ == '__main__':
  268. main()
Advertisement
Add Comment
Please, Sign In to add comment