class FileIO(File): def __init__(self, path=None, opts="", sysopts={"verbose": False, "debug": False}): File.__init__(self, path, opts, sysopts) self.geom = Geometry("SCCOUT") self.species = {} # end def # def read_lattice_vectors( self ): # for i in range( 1, 4 ): # if i > 1: # self.getline() # # end if # line_arr = self.line.split() # for j in range( 0, 3 ): # self.geom.lattice_vectors[i-1][j] = string.atof(line_arr[j]) # # end for # self.process() # # end for # # end def def read_positions(self): i = 1 while True: if i > 1: try: self.getline() except: break # end try # end if (no, x, y, z, r) = self.line() x = string.atof(x) y = string.atof(y) z = string.atof(z) s = self.dict["global_specie"] atom = AtomPosition(Atom(s, i), VectorPosition([x, y, z])) # atom.info() self.geom.add_atom(atom) self.process() i += 1 # end for self.geom.position_type = PositionTypes.Cart # end def def read(self, opts=None): self.rewind() self.clean() self.state(2, self.read_lattice_vectors) File.run(self, 2) # self.state( 25, self.read_positions ) self.state(29, self.read_positions) # self.state( 31, self.read_positions ) File.run(self) self.geom.gen_species()
def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ): File.__init__( self, path, opts, sysopts ) self.geom = Geometry( 'CHGCAR' ) self.species = {} self._natom = 0 self.voldata = {} self.volcol = 5
class FileIO( File ): def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ): File.__init__( self, path, opts, sysopts ) self.geom = Geometry( 'xyz' ) self.species = {} # end def ### begin write def write( self, opts = None ): line1 = "" line2 = "" self.rewind() self.clean() # dump header self.putline( self.geom.number_of_atoms() ) self.putline( self.geom.name ) # dump coordinates for a in self.geom.atoms: s = a.symbol() pos = a.position() mov = a.moveable() line1 = " %2s" % s for i in range( 0, 3 ): line1 += " %20.16f" % pos[i] # end for self.putline( line1 ) # end for # self.buffer_write()
def __init__(self, path=None, opts="", sysopts={"verbose": False, "debug": False}): File.__init__(self, path, opts, sysopts) self.geom = Geometry("SCCOUT") self.species = {}
def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ): File.__init__( self, path, opts, sysopts ) self.geom = Geometry( 'UPOT' ) self.reference_upot = None
class FileIO( File ): def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ): File.__init__( self, path, opts, sysopts ) self.geom = Geometry( 'UPOT' ) self.reference_upot = None # end def def read_types_header( self ): (types,ions) = self.line() self.types = string.atoi( types ) self.ions = string.atoi( ions ) self.process() # end def def read_types( self ): for i in range( 1, self.types + 1 ): if i > 1: self.getline() # end if self.process() # end for # end def def read_ions( self ): for i in range( 1, self.ions + 1 ): if i > 1: self.getline() # end if (no,symbol,cl_shift,x,y,z) = self.line() no = string.atoi( no ) cl_shift = string.atof( cl_shift ) x = string.atof( x ) y = string.atof( y ) z = string.atof( z ) try: atom = Atom( symbol, no, cl_shift ) vpos = VectorPosition( numpy.array( [x, y, z] ) ) apos = AtomPosition( atom, vpos ) self.geom.add_atom( apos ) except Exception as ex: print str( ex ) # self.geom.add_atom( AtomPosition( Atom( symbol, no, cl_shift ), VectorPosition( numpy.array( [x, y, z] ) ) ) ) self.process() # end for # end def def read( self, opts = None ): self.rewind() self.clean() self.state( 1, self.comment ) self.state( 2, self.read_types_header ) self.state( 3, self.comment ) self.state( 4, self.read_types ) File.run( self, 4 ) self.state( 4 + self.types, self.comment ) self.state( 4 + self.types + 2, self.comment ) self.state( 4 + self.types + 3, self.read_ions ) File.run( self ) self.geom.gen_species() # self.geom.check() # end def def reference( self, argv = None ): reference_upot = argv['reference_upot'] self.reference_upot = reference_upot # end def # TODO: generate atomlist with origo rmin rmax def average_upot_on_reference( self, argv = None ): atomlist = argv['atomlist'] if self.verbose: print print ' Average (reference):', atomlist avg_cl_shift = 0.000 nat_cl_shift = 0.000 reference_geom = self.reference_upot.geom() for i in atomlist: reference_atom = reference_geom.get_atom( i ) nearest_atom = self.geom.nearest_atom_on_position( reference_atom.position() ) nat_cl_shift = nearest_atom.cl_shift(); if self.verbose: rp = reference_atom.position() np = nearest_atom.position() dp = np - rp dr = math.sqrt(dp[0]*dp[0]+dp[1]*dp[1]+dp[2]*dp[2]) print print ' Reference:', "%3d" % reference_atom.no(), "%2s" % reference_atom.symbol(), rp print ' Nearest:', "%3d" % nearest_atom.no(), "%2s" % nearest_atom.symbol(), np print ' Distance:', dr print ' cl shift:', nat_cl_shift if reference_atom.symbol() != nearest_atom.symbol(): print ' WARNING: Symbol mismatch' # end if if dr > 0.01: print ' WARNING: Distance to large' # end if # end if avg_cl_shift += nat_cl_shift # end for avg_cl_shift /= len( atomlist ) return avg_cl_shift # end def def average_upot( self, argv = None ): atomlist = argv['atomlist'] origo = argv['origo'] ref = argv['reference'] ono = argv['ono'] lv = argv['lv'] if lv != None: self.geom.lattice_vectors = numpy.reshape(lv,(3,3)) if ref != None: ref.geom.lattice_vectors = numpy.reshape(lv,(3,3)) # end if # dump out atomlist xyz alout = open( './atomlist.xyz', 'w' ) alout.write( str( len(atomlist) ) + "\n" ) alout.write( "\n" ) spec = {} if self.verbose: print print ' Average (self): ', atomlist avg_cl_shift = 0.000 # self print "%5s %4s" % ("no", "sym"), "%12s %12s %12s" % ("x", "y", "z"), "%12s" % "cls", "%12s" % "dr" for i in atomlist: atom = self.geom.get_atom( i, True ) cls = atom.cl_shift() avg_cl_shift += cls s = atom.symbol() if lv != None: pos = self.geom.position( atom, PositionTypes.Cart ) dr = 0.000 for j in range(0,3): dr += (origo[j] - pos[j])**2 # end for dr = math.sqrt(dr) else: pos = atom.position() print "%5d %4s" % (atom.no(), s), "%12.6f %12.6f %12.6f" % (pos[0], pos[1], pos[2]), "%12.6f" % cls, "%12.6f" % dr try: spec[s] += 1 except: spec[s] = 1 mov = atom.moveable() aline = "%2s" % s for j in range( 0, 3 ): aline += " %20.16f" % pos[j] # end for alout.write( aline + "\n" ) # end for alout.close() # reference cross-check if ref != None: ref_avg_cl_shift = 0.000 print "" print "%5s %4s %4s" % ("no","sym","rsym"), "%12s %12s %12s" % ("dr","rdr", "ddr"), "%12s %12s %12s" % ("cls","rcls","dcls") sd_dcls = [] for i in atomlist: atom = self.geom.get_atom( i, True ) ii = i if ono != None: if i >= ono: ii = i + 1 print ' Origo no shift: ',i,ii ref_atom = ref.geom.get_atom( ii, True ) cls = atom.cl_shift() ref_cls = ref_atom.cl_shift() ref_avg_cl_shift += ref_cls s = atom.symbol() ref_s = ref_atom.symbol() sd_dcls.append( cls-ref_cls ) if lv != None: pos = self.geom.position( atom, PositionTypes.Cart ) ref_pos = self.geom.position( ref_atom, PositionTypes.Cart ) dr = 0.000 ref_dr = 0.000 for j in range(0,3): dr += (origo[j] - pos[j])**2 ref_dr += (origo[j] - ref_pos[j])**2 # end for dr = math.sqrt(dr) ref_dr = math.sqrt(ref_dr) else: pos = atom.position() ref_pos = ref_atom.position() print "%5d %4s %4s" % (ref_atom.no(),s,ref_s), "%12.6f %12.6f %12.6f" % (dr,ref_dr,dr-ref_dr), "%12.6f %12.6f %12.6f" % (cls,ref_cls,cls-ref_cls) # end for # end if print "" print " Species: ", spec print "" print " Sum: ", avg_cl_shift," / ", len(atomlist) avg_cl_shift /= len( atomlist ) if ref != None: ref_avg_cl_shift /= len(atomlist) print " Shift Mean: %12.6f" % (avg_cl_shift - ref_avg_cl_shift) print " Shift STD: %12.6f" % numpy.std(sd_dcls) # end if return avg_cl_shift # print ' Average Potential: ' + str( avg_cl_shift ) # end def def average( self, argv = None ): if self.reference_upot != None : return self.average_upot_on_reference( argv ) # end if return self.average_upot( argv )
def main( self ): (options, args) = self.opt_parser.parse_args() input_files_arr = {} input_arr = {} input_files = options.input_files output_name = options.output_name trans_name = options.trans_name direct = options.direct merge_geom = Geometry( 'Merge' ) merge_geom.position_type = PositionTypes.Cart try: for inp in input_files.split( "," ): ( sym, input_file ) = inp.split( ":" ) input_files_arr[sym] = input_file # end for except Exception as ex: print ' Error: ' + str( ex ) return 0 # end try sysopts = { "verbose" : self.verbose, "debug" : self.debug } # read inputs for inp in input_files_arr: sym = inp input_file = input_files_arr[inp] try: input_arr[sym] = FileIO( input_file, 'SCCOUT', "r", sysopts ) except Exception as ex: print ' Error: ' + str( ex ) return 0 # end try input_arr[sym].handler.set( 'global_specie', sym ) input_arr[sym].read() geom = input_arr[sym].geom() geom.name = input_file # geom.check() try: verbose = merge_geom.merge( geom, True, False ) if self.verbose: print verbose except Exception as ex: print ' Error: ' + str( ex ) return 0 # end try # end for position_type = PositionTypes.Cart if direct: position_type = PositionTypes.Direct # end if # # merge check # merge_geom.wignercell() # # write out check geoms merge_geom.order() write_geom( output_name, sysopts, merge_geom, position_type ) try: trans = Transform() trans.build() trans.process( trans_name, merge_geom ) except Exception as ex: print str( ex ) #if self.verbose: # raise # end try write_geom( output_name + ".transform", sysopts, merge_geom, position_type )
class FileIO( File ): def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ): File.__init__( self, path, opts, sysopts ) self.geom = Geometry( 'POSCAR' ) self.species = {} self.voldata = {} # end def ### begin read def read_lattice_constant( self ): constant = self.line()[0] self.geom.lattice_constant = string.atof( constant ) self.process() self.species = {} # end def def read_types( self ): line_arr_1 = self.line() self.process() self.getline() line_arr_2 = self.line() self.process() for i in range( 0, len( line_arr_1 ) ): self.species[line_arr_1[i]] = string.atoi( line_arr_2[i] ) # end for # print self.species # end def def read_position_type( self ): if string.upper( self.line()[0][0] ) == 'D': self.geom.position_type = PositionTypes.Direct else: self.geom.position_type = PositionTypes.Cart # end if self.process() # end def # old version def read_positions( self ): j = 1 for s in self.species: for i in range( 0, self.species[s] ): if j > 1: self.getline() # end if # POSCAR or CHGCAR try: ( x, y, z, xm, ym, zm ) = self.line() except: ( x, y, z ) = self.line() xm = ym = zm = 'T' # end try x = string.atof( x ) y = string.atof( y ) z = string.atof( z ) atom = AtomPosition( Atom( s, j ), VectorPosition( [ x, y, z ], [ xm, ym, zm ] ) ) # atom.info() self.geom.add_atom( atom ) if self.verbose: print " %05d Process:" % self.lc(), "%4d" % j, "%3d" % i, "%2s" % s, self.line() # end if j += 1 # end for # end for # end def # converter version # do not need to convert at read def new_bad_read_positions( self, position_type = PositionTypes.Direct ): j = 1 for s in self.species: for i in range( 0, self.species[s] ): if j > 1: self.getline() # end if try: ( x, y, z, xm, ym, zm ) = self.line() except: ( x, y, z ) = self.line() xm = ym = zm = 'T' # end try x = string.atof( x ) y = string.atof( y ) z = string.atof( z ) pos = numpy.zeros( 3 ) convert = 'O' # convert if self.geom.position_type != position_type: if position_type == PositionTypes.Cart: # D -> C convert = 'D->C' rho = numpy.array( [ x, y, z ] ) pos = self.geom.position_cart( rho ) else: # C -> D convert = 'C->D' r = numpy.array( [ x, y, z ] ) pos = self.geom.position_direct( r ) # end if else: pos = numpy.array( [x, y, z ] ) # end if # atom = AtomPosition( Atom( s, j ), VectorPosition( [ x, y, z ], [ xm, ym, zm ] ) ) atom = AtomPosition( Atom( s, j ), VectorPosition( pos, numpy.array( [ xm, ym, zm ] ) ) ) # print [x,y,z] # print pos # atom.info() self.geom.add_atom( atom ) if self.verbose: print " %05d Process:" % self.lc(), "%4d" % j, "%3d" % i, "%2s" % s, self.line(), "%5s" % convert # end if j += 1 # end for # end for # end def def read( self, opts = None ): self.rewind() self.clean() self.state( 1, self.comment ) self.state( 2, self.read_lattice_constant ) self.state( 3, self.read_lattice_vectors ) File.run( self, 3 ) self.state( 6, self.read_types ) self.state( 8, self.comment ) self.state( 9, self.read_position_type ) # self.state( 8, self.read_position_type ) self.state( 10, self.read_positions ) File.run( self ) self.geom.gen_species() self.geom.check() # end def ### end read ### begin write # here we need to convert def write( self, opts = None ): line1 = "" line2 = "" self.rewind() self.clean() # dump header self.putline( self.geom.name ) # dump lattice self.putline( " %20.16f" % self.geom.lattice_constant ) for i in range( 0, 3 ): line1 = "" for j in range( 0, 3 ): line1 += " %20.16f" % self.geom.lattice_vectors[i][j] # end for self.putline( line1 ) # end for # dump species line1 = "" line2 = "" for s in self.geom.species: line1 += " %4s" % s line2 += " %4d" % self.geom.species[s] # end for self.putline( line1 ) self.putline( line2 ) # dump coordinates self.putline( 'Selective dynamics' ) try: position_type = opts['position_type'] except: position_type = PositionTypes.Direct # end try # self.putline( PositionTypesDict[ self.geom.position_type ] ) self.putline( PositionTypesDict[ position_type ] ) # print self.geom.position_type # print position_type if self.verbose: print " Conversion:", "%s to %s" % ( PositionTypesDict[ self.geom.position_type ], PositionTypesDict[ position_type ] ) # end if for a in self.geom.atoms: line1 = "" line2 = "" # convert if self.geom.position_type != position_type: if position_type == PositionTypes.Cart: # D -> C opos = a.position() pos = self.geom.position_cart( a.position() ) else: # C -> D pos = self.geom.position_direct( a.position() ) # end if else: pos = a.position() # end if mov = a.moveable() for i in range( 0, 3 ): line1 += " %20.16f" % pos[i] line2 += " %2s" % TF( mov[i] ) # end for line1 += line2 self.putline( line1 ) # end for # self.putline( "" ) for a in self.geom.atoms: line1 = "" vel = a.velocity() for i in range( 0, 3 ): line1 += " %20.16f" % vel[i] # end for self.putline( line1 ) # end for # print self.buffer.lines self.buffer_write()
class FileIO( File ): def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ): File.__init__( self, path, opts, sysopts ) self.geom = Geometry( 'CHGCAR' ) self.species = {} self._natom = 0 self.voldata = {} self.volcol = 5 # end def ### begin read def read_lattice_constant( self ): constant = self.line()[0] self.geom.lattice_constant = string.atof( constant ) self.process() self.species = {} self.ngx = 0 self.ngy = 0 self.ngz = 0 # end def def read_types( self ): line_arr_1 = self.line() self.process() self.getline() line_arr_2 = self.line() self.process() self._natom for i in range( 0, len( line_arr_1 ) ): sna = string.atoi( line_arr_2[i] ) self._natom += sna self.species[line_arr_1[i]] = sna # end for # print self.species # end def def read_position_type( self ): if string.upper( self.line()[0][0] ) == 'D': self.geom.position_type = PositionTypes.Direct else: self.geom.position_type = PositionTypes.Cart # end if self.process() # end def def read_gdim( self ): (ngx,ngy,ngz) = self.line() self.ngx = string.atoi( ngx ) self.ngy = string.atoi( ngy ) self.ngz = string.atoi( ngz ) # self.ng = ngx * ngy * ngz if self.verbose: print " %05d Process:" % self.lc(), self.line() # end if # end def def ng( self ): return self.ngx * self.ngy * self.ngz # end def # old version def read_positions( self ): j = 1 for s in self.species: for i in range( 0, self.species[s] ): if j > 1: self.getline() # end if ( x, y, z ) = self.line() x = string.atof( x ) y = string.atof( y ) z = string.atof( z ) atom = AtomPosition( Atom( s, j ), VectorPosition( [ x, y, z ] ) ) # atom.info() self.geom.add_atom( atom ) if self.verbose: print " %05d Process:" % self.lc(), "%4d" % j, "%3d" % i, "%2s" % s, self.line() # end if j += 1 # end for # end for # end def def read_voldata( self ): # allocate # data lines dle = int(math.floor( self.ng() / self.volcol )) + 1 self.voldata['total'] = numpy.zeros(self.ng()) print " L: ",dle," S:",self.ng() j = 1 ik = 0 # dle for i in range(0,dle): if j > 1: self.getline() # endif larr = self.line() for k in range(0,self.volcol): try: self.voldata['total'][ik] = string.atof(larr[k]) except Exception as ex: pass ik += 1 # end for # print " %05d Process:" % self.lc(), ik j += 1 # end for # end def def add( self, argv = {} ): s = argv['s'] a = argv['a'] b = argv['b'] mv = self.voldata['total'] sv = s.voldata() sv = sv['total'] dv = a * mv + b * sv self.voldata['diff'] = dv # end def def read( self, opts = None ): self.rewind() self.clean() self.state( 1, self.comment ) self.state( 2, self.read_lattice_constant ) self.state( 3, self.read_lattice_vectors ) File.run( self, 3 ) self.state( 6, self.read_types ) # self.state( 8, self.comment ) self.state( 8, self.read_position_type ) self.state( 9, self.read_positions ) File.run( self, 9 ) eor = 9 + self._natom + 1 self.state( eor , self.read_gdim ) File.run( self, eor ) self.state( eor + 1 , self.read_voldata ) File.run( self ) self.geom.gen_species() # self.geom.check() # print self.voldata # end def ### end read ### begin write # here we need to convert def write( self, opts = None ): line1 = "" line2 = "" self.rewind() self.clean() # dump header self.putline( self.geom.name ) # dump lattice self.putline( " %20.16f" % self.geom.lattice_constant ) for i in range( 0, 3 ): line1 = "" for j in range( 0, 3 ): line1 += " %20.16f" % self.geom.lattice_vectors[i][j] # end for self.putline( line1 ) # end for # dump species line1 = "" line2 = "" for s in self.geom.species: line1 += " %4s" % s line2 += " %4d" % self.geom.species[s] # end for self.putline( line1 ) self.putline( line2 ) # dump coordinates # self.putline( 'Selective dynamics' ) try: position_type = opts['position_type'] except: position_type = PositionTypes.Direct # end try # self.putline( PositionTypesDict[ self.geom.position_type ] ) self.putline( PositionTypesDict[ position_type ] ) # print self.geom.position_type # print position_type if self.verbose: print " Conversion:", "%s to %s" % ( PositionTypesDict[ self.geom.position_type ], PositionTypesDict[ position_type ] ) # end if for a in self.geom.atoms: line1 = "" line2 = "" # convert if self.geom.position_type != position_type: if position_type == PositionTypes.Cart: # D -> C opos = a.position() pos = self.geom.position_cart( a.position() ) else: # C -> D pos = self.geom.position_direct( a.position() ) # end if else: pos = a.position() # end if mov = a.moveable() for i in range( 0, 3 ): line1 += " %20.16f" % pos[i] # line2 += " %2s" % TF( mov[i] ) # end for #line1 += line2 self.putline( line1 ) # end for # self.putline( "" ) # for a in self.geom.atoms: # line1 = "" # vel = a.velocity() # for i in range( 0, 3 ): # line1 += " %20.16f" % vel[i] # # end for # self.putline( line1 ) # # end for # # print self.buffer.lines self.putline( "%5d %5d %5d" % ( self.ngx, self.ngy, self.ngz ) ) if opts['diff']: vd = self.voldata['diff'] else: vd = self.voldata['total'] # end if line1 = "" vc = 0 for v in vd: vc += 1 line1 += " %20.16f" % v if vc % 5: vc = 1 self.putline( line1 ) line1 = "" # end for if vc > 1: self.putline( line1 ) self.buffer_write()