Example #1
0
File: xyz.py Project: hornos/pypak
class IO( 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

  def read( self, opts = None ):
    raise NotImplementedError( "read" )
  # end def

  ### begin write
  def write( self, opts = None ):
    line = ["",""]
    self.rewind()
    self.clean()
    # dump header
    self.putline( self.geom.natoms() )
    self.putline( self.geom.name )
    try:
      pt = opts['pt']
    except:
      pt = PT.Cart
    # end try

    # dump coordinates
    for atom in self.geom.atoms:
      s = atom.symbol
      mov = atom.moveable
      line[0] = "  %2s" % s

      # convert
      pos = atom.position
      if self.geom.pt != pt:
        if pt == PT.Cart:
          # D -> C
          opos = atom.position
          pos = self.geom.position_cart( atom.position )
        else:
          # C -> D
          pos = self.geom.position_direct( atom.position )
        # end if
      # end if

      for i in range( 0, 3 ):
        line[0] += "  %20.16f" % pos[i]
      # end for
      self.putline( line[0] )
    # end for
    #
    self.write_buffer()
Example #2
0
File: xyz.py Project: hornos/pypak
 def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ):
   File.__init__( self, path, opts, sysopts )
   self.geom = Geometry( 'xyz' )
   self.species = {}
Example #3
0
File: UPOT.py Project: hornos/pyf3
  def average_upot( self, argv = None ):
    atomlist = argv['atomlist']
    ref      = argv['reference']
    vacancy  = argv['vacancy']
    spec = {}
    avg_cl_shift = 0.000

    # lattice vectors
    try:
      lat_vec = argv['lat_vec']
      self.geom.lat_vec = numpy.reshape( lat_vec, ( 3, 3 ) )
      if ref != None:
        ref.geom.lat_vec = numpy.reshape( lat_vec, ( 3, 3 ) )
      # end if
      msg = PTD[PT.Cart]
      # cart origo?
      try:
        origo_vec = argv['origo_vec']
      except:
        origo_vec = None
      # end try
    # end if
    except:
      lat_vec = None
      msg = PTD[PT.Direct]
    # end try
    if self.verbose:
      print " Coords:",msg
    # end if

    if lat_vec != None:
      avg_xyz = Geometry()
      avg_xyz.lat_vec = self.geom.lat_vec
      avg_xyz.pt = PT.Cart
    # end if

    if self.verbose:
      msg = "\n  %5s %4s %12s %12s %12s %12s" % ("No","Sym","cl_shift","x","y","z")
      if origo_vec != None:
        msg += " %12s" % ("dr")
        if ref != None:
          msg += " %12s" % ("ref dr")
      print msg
    # end if

    # cross check
    if ref != None:
      lookup = l_vacancy( range( 0, ref.geom.ac ), vacancy )
      ref_avg_cl_shift = 0.000
    # end if

    for ai in atomlist:
      atom = self.geom.get( ai )
      s    = atom.symbol
      no   = atom.no
      cls  = atom.cl_shift
      pos  = atom.position
      avg_cl_shift += cls

      # cross check
      if ref != None:
        ref_atom = ref.geom.get( lookup[ai] )
        ref_s    = ref_atom.symbol
        ref_pos  = ref_atom.position
        ref_cls  = ref_atom.cl_shift
        ref_avg_cl_shift += ref_cls
      # end if

      # cart position
      if lat_vec != None:
        # change to cart
        pos = self.geom.position( atom, PT.Cart )
        if ref != None:
          ref_pos = ref.geom.position( ref_atom, PT.Cart )
        avg_xyz.add( AtomPos( s, no, cls, pos ) )
      # end if

      if self.verbose:
        msg  = "  %5d %4s" % (atom.no, s)
        msg += " %12.6f" % cls
        msg += " %12.6f %12.6f %12.6f" % (pos[0], pos[1], pos[2])
        if origo_vec != None and lat_vec != None:
          msg += " %12.6f" % v_dr( origo_vec, pos )
        # end if
        print msg

        if ref != None:
          msg  = "R %5d %4s" % (ref_atom.no, ref_s)
          msg += " %12.6f" % ref_cls
          msg += " %12.6f %12.6f %12.6f" % (ref_pos[0], ref_pos[1], ref_pos[2])
          if origo_vec != None and lat_vec != None:
            msg += " %12.6f" % v_dr( origo_vec, ref_pos )
            msg += " %12.6f" % v_dr( ref_pos, pos )
          # end if
          print msg
        # end if
      # end if

      try:
        spec[s] += 1
      except:
        spec[s] = 1
      # end try
    # end for ai

    if self.verbose:
      print "\n Atoms: ", len( atomlist )
      print " Species: ", spec
      print "\n Orig. Average:", avg_cl_shift, "/", len( atomlist ), \
                        "=", avg_cl_shift / len( atomlist )
      if ref != None:
        print "  Ref. Average:", ref_avg_cl_shift, "/", len( atomlist ), \
                               "=", ref_avg_cl_shift / len( atomlist )
        print "\n Shift:", avg_cl_shift - ref_avg_cl_shift
      # end if

    # end if

    # end if
    if lat_vec != None:
      avg_output = IO( 'atomlist.xyz', 'xyz', 'w+' )
      avg_output.geom( avg_xyz )
      avg_output.write()
    # end if

    avg_cl_shift /= len( atomlist )
    return avg_cl_shift
Example #4
0
File: UPOT.py Project: hornos/pyf3
 def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ):
   File.__init__( self, path, opts, sysopts )
   self.geom = Geometry( 'UPOT' )
   self.reference = None
Example #5
0
File: UPOT.py Project: hornos/pyf3
class IO( File ):
  def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ):
    File.__init__( self, path, opts, sysopts )
    self.geom = Geometry( 'UPOT' )
    self.reference = None
  # 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 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:
        self.geom.add( AtomPos( symbol, no, cl_shift, numpy.array( [x, y, z] ) ) )
      except Exception as ex:
        print str( ex )
      # end try
      self.process()
    # end for
  # end def

# PRB 78, 235104-5 (7)
# EPA(D,q) = q * (V_Dq - V_H)
#   shift  =     (inp  - ref)

  def average_upot( self, argv = None ):
    atomlist = argv['atomlist']
    ref      = argv['reference']
    vacancy  = argv['vacancy']
    spec = {}
    avg_cl_shift = 0.000

    # lattice vectors
    try:
      lat_vec = argv['lat_vec']
      self.geom.lat_vec = numpy.reshape( lat_vec, ( 3, 3 ) )
      if ref != None:
        ref.geom.lat_vec = numpy.reshape( lat_vec, ( 3, 3 ) )
      # end if
      msg = PTD[PT.Cart]
      # cart origo?
      try:
        origo_vec = argv['origo_vec']
      except:
        origo_vec = None
      # end try
    # end if
    except:
      lat_vec = None
      msg = PTD[PT.Direct]
    # end try
    if self.verbose:
      print " Coords:",msg
    # end if

    if lat_vec != None:
      avg_xyz = Geometry()
      avg_xyz.lat_vec = self.geom.lat_vec
      avg_xyz.pt = PT.Cart
    # end if

    if self.verbose:
      msg = "\n  %5s %4s %12s %12s %12s %12s" % ("No","Sym","cl_shift","x","y","z")
      if origo_vec != None:
        msg += " %12s" % ("dr")
        if ref != None:
          msg += " %12s" % ("ref dr")
      print msg
    # end if

    # cross check
    if ref != None:
      lookup = l_vacancy( range( 0, ref.geom.ac ), vacancy )
      ref_avg_cl_shift = 0.000
    # end if

    for ai in atomlist:
      atom = self.geom.get( ai )
      s    = atom.symbol
      no   = atom.no
      cls  = atom.cl_shift
      pos  = atom.position
      avg_cl_shift += cls

      # cross check
      if ref != None:
        ref_atom = ref.geom.get( lookup[ai] )
        ref_s    = ref_atom.symbol
        ref_pos  = ref_atom.position
        ref_cls  = ref_atom.cl_shift
        ref_avg_cl_shift += ref_cls
      # end if

      # cart position
      if lat_vec != None:
        # change to cart
        pos = self.geom.position( atom, PT.Cart )
        if ref != None:
          ref_pos = ref.geom.position( ref_atom, PT.Cart )
        avg_xyz.add( AtomPos( s, no, cls, pos ) )
      # end if

      if self.verbose:
        msg  = "  %5d %4s" % (atom.no, s)
        msg += " %12.6f" % cls
        msg += " %12.6f %12.6f %12.6f" % (pos[0], pos[1], pos[2])
        if origo_vec != None and lat_vec != None:
          msg += " %12.6f" % v_dr( origo_vec, pos )
        # end if
        print msg

        if ref != None:
          msg  = "R %5d %4s" % (ref_atom.no, ref_s)
          msg += " %12.6f" % ref_cls
          msg += " %12.6f %12.6f %12.6f" % (ref_pos[0], ref_pos[1], ref_pos[2])
          if origo_vec != None and lat_vec != None:
            msg += " %12.6f" % v_dr( origo_vec, ref_pos )
            msg += " %12.6f" % v_dr( ref_pos, pos )
          # end if
          print msg
        # end if
      # end if

      try:
        spec[s] += 1
      except:
        spec[s] = 1
      # end try
    # end for ai

    if self.verbose:
      print "\n Atoms: ", len( atomlist )
      print " Species: ", spec
      print "\n Orig. Average:", avg_cl_shift, "/", len( atomlist ), \
                        "=", avg_cl_shift / len( atomlist )
      if ref != None:
        print "  Ref. Average:", ref_avg_cl_shift, "/", len( atomlist ), \
                               "=", ref_avg_cl_shift / len( atomlist )
        print "\n Shift:", avg_cl_shift - ref_avg_cl_shift
      # end if

    # end if

    # end if
    if lat_vec != None:
      avg_output = IO( 'atomlist.xyz', 'xyz', 'w+' )
      avg_output.geom( avg_xyz )
      avg_output.write()
    # end if

    avg_cl_shift /= len( atomlist )
    return avg_cl_shift
  # end def

  def compare( self, complist = None, eps = 0.01 ):
    shift = 0.00000
    one  = 1.000000
    zero = 0.000000
    c = 0
    ref = self.geom
    inp = self.geom.geom
    for k,v in complist.iteritems():
      k = int(k)
      v = int(v)
      ratom = ref.get(k)
      rpos  = ratom.position
      rcls  = ratom.cl_shift

      iatom = inp.get(v)
      ipos  = iatom.position
      icls  = iatom.cl_shift

      if ratom.symbol != iatom.symbol:
        raise Warning( "Symbol mismatch!" )

      # G2012-04-12
      # normalize coordinates
      for i in range(0,3):
        if ipos[i] > one:
          ipos[i] -= one
        if abs(one - ipos[i]) < eps:
          ipos[i] = one - ipos[i]

        if rpos[i] > one:
          rpos[i] -= one
        if abs(one - rpos[i]) < eps:
          rpos[i] = one - rpos[i]
      # end for
      print "REF %4d %2s %12.9f %12.9f %12.9f %12.9f" % (ratom.no,ratom.symbol,rpos[0],rpos[1],rpos[2],rcls)
      print "INP %4d %2s %12.9f %12.9f %12.9f %12.9f" % (iatom.no,iatom.symbol,ipos[0],ipos[1],ipos[2],icls)
      print "L2 INP-REF: %12.9f" %(l2norm(ipos-rpos))
      print
      # print ratom.no,ratom.symbol,ratom.position
      # print iatom.no,iatom.symbol,iatom.position
      shift += icls - rcls
      c += 1
    # end for
    print "Shift:",shift/float(c)
    print

  # def

  def average( self, atomlist = None ):
    shift = 0.00000
    c = 0
    ref = self.geom
    inp = self.geom.geom
    for i in atomlist:
      i = int(i)
      ratom = ref.get( i )
      rpos  = ratom.position
      rcls  = ratom.cl_shift

      j = ref.geom_match[i]
      iatom = inp.get( j )
      ipos  = iatom.position
      icls  = iatom.cl_shift

      if ratom.symbol != iatom.symbol:
        raise Warning( "Symbol mismatch!" )

      print "REF %4d %2s %12.9f %12.9f %12.9f %12.9f" % (ratom.no,ratom.symbol,rpos[0],rpos[1],rpos[2],rcls)
      print "INP %4d %2s %12.9f %12.9f %12.9f %12.9f" % (iatom.no,iatom.symbol,ipos[0],ipos[1],ipos[2],icls)

      # print ratom.no,ratom.symbol,ratom.position
      # print iatom.no,iatom.symbol,iatom.position
      shift += icls - rcls
      c += 1
    # end for
    print "Shift:",shift/float(c)
    print
Example #6
0
File: diff.py Project: hornos/pyf3
class IO( File ):
  def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ):
    File.__init__( self, path, opts, sysopts )
    self.geom = Geometry( 'POSCAR' )
    self.species = {}
  # end def

  def read( self, opts = None ):
    self.rewind()
    self.clean()
    self.state( 1,  self.read_name )
    self.state( 2,  self.read_lat_c )
    self.state( 3,  self.read_lat_vec )
    File.run( self, 3 )
    self.state( 6,  self.read_types )
    self.state( 8,  self.comment )
    self.state( 9,  self.read_pt )
    self.state( 10, self.read_positions )
    File.run( self )
    self.geom.gen_species()
    # self.geom.info()
  # end def
  ###  end read

  def read_name( self ):
    self.geom.name = ''.join( self.line() )
  # end def

  ### begin read
  def read_lat_c( self ):
    constant = self.line()[0]
    self.geom.lat_c = string.atof( constant )
    self.process()
  # end def

  # super: read.lat_vec

  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_pt( self ):
    if string.upper( self.line()[0][0] ) == 'D':
      self.geom.pt = PT.Direct
    else:
      self.geom.pt = PT.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, no, symb ) = self.line()
        except:
          ( x, y, z, no, symb ) = self.line()
          xm = ym = zm = 'T'
        # end try
        x = string.atof( x )
        y = string.atof( y )
        z = string.atof( z )
        no = string.atoi( no )

        # without renumbering
        self.geom.add( AtomPos( symbol = symb, no = no, 
                                vec = [ x, y, z ], 
                                moveable = [ xm, ym, zm ] ), False )
        if self.debug:
          print " %05d Process:" % self.lc(), "%4d" % j, "%3d" % i, "%2s" % s, self.line()
        # end if
        j += 1
      # end for
    # end for
  # end def


  ### begin write
  # here we need to convert
  def write( self, opts = None ):
    line = ["",""]
    self.rewind()
    self.clean()
    # dump header
    self.putline( self.geom.name )
    # dump lattice
    self.putline( "  %20.16f" % self.geom.lat_c )
    for i in range( 0, 3 ):
      line[0] = ""
      for j in range( 0, 3 ):
        line[0] += "  %20.16f" % self.geom.lat_vec[i][j]
      # end for
      self.putline( line[0] )
    # end for
    # dump species
    line = ["",""]
    for s in self.geom.species:
      line[0] += "  %4s" % s
      line[1] += "  %4d" % self.geom.species[s]
    # end for
    self.putline( line[0] )
    self.putline( line[1] )
    # dump coordinates
    self.putline( 'Selective dynamics' )
    try:
      pt = opts['pt']
    except:
      pt = PT.Direct
    # end try
    self.putline( PTD[ pt ] )

    if self.verbose:
      print " Conversion:", "%s to %s" % ( PTD[ self.geom.pt ], PTD[ pt ] )
    # end if

    for atom in self.geom.atoms:
      line = ["",""]
      # convert
      pos = atom.position
      if self.geom.pt != pt:
        if pt == PT.Cart:
          # D -> C
          opos = atom.position
          pos = self.geom.position_cart( atom.position )
        else:
          # C -> D
          pos = self.geom.position_direct( atom.position )
        # end if
      # end if

      mov = atom.moveable

      for i in range( 0, 3 ):
        line[0] += "  %20.16f" % pos[i]
        line[1] += "  %2s" % TF( mov[i] )
      # end for
      line[0] += line[1]
      line[0] += " %5d %3s" % ( atom.no, atom.symbol )
      self.putline( line[0] )
    # end for
    #
    self.putline( "" )

    for atom in self.geom.atoms:
      line = ""
      vel = atom.velocity
      for i in range( 0, 3 ):
        line += "  %20.16f" % vel[i]
      # end for
      self.putline( line )
    # end for

    self.write_buffer()
Example #7
0
class IO( File ):
  def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ):
    File.__init__( self, path, opts, sysopts )
    self.geom = Geometry( 'ASEPOT' )
    self.reference = None
  # 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_lat_c )
    self.state( 4 + self.types + 4, self.read_lat_vec )
    self.state( 4 + self.types + 8, self.read_ions )
    File.run( self )
    self.geom.gen_species()
  # 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 )
      # VMD indexes
      no = no - 1 
      cl_shift = string.atof( cl_shift )
      x = string.atof( x )
      y = string.atof( y )
      z = string.atof( z )
      try:
        self.geom.add( AtomPos( symbol, no, cl_shift, numpy.array( [x, y, z] ) ) )
      except Exception as ex:
        print str( ex )
      # end try
      self.process()
    # end for
  # end def

  def read_lat_c( self ):
    constant = self.line()[0]
    self.geom.lat_c = string.atof( constant )
    self.process()
  # end def

  # super: read_lat_vec

# PRB 78, 235104-5 (7)
# EPA(D,q) = q * (V_Dq - V_H)
#   shift  =     (inp  - ref)

  def average_upot( self, argv = None ):
    atomlist = argv['atomlist']
    ref      = argv['reference']
    vacancy  = argv['vacancy']
    spec = {}
    avg_cl_shift = 0.000

    # lattice vectors
    try:
      lat_vec = argv['lat_vec']
      self.geom.lat_vec = numpy.reshape( lat_vec, ( 3, 3 ) )
      if ref != None:
        ref.geom.lat_vec = numpy.reshape( lat_vec, ( 3, 3 ) )
      # end if
      msg = PTD[PT.Cart]
      # cart origo?
      try:
        origo_vec = argv['origo_vec']
      except:
        origo_vec = None
      # end try
    # end if
    except:
      lat_vec = None
      msg = PTD[PT.Direct]
    # end try
    if self.verbose:
      print " Coords:",msg
    # end if

    if lat_vec != None:
      avg_xyz = Geometry()
      avg_xyz.lat_vec = self.geom.lat_vec
      avg_xyz.pt = PT.Cart
    # end if

    if self.verbose:
      msg = "\n  %5s %4s %12s %12s %12s %12s" % ("No","Sym","cl_shift","x","y","z")
      if origo_vec != None:
        msg += " %12s" % ("dr")
        if ref != None:
          msg += " %12s" % ("ref dr")
      print msg
    # end if

    # cross check
    if ref != None:
      lookup = l_vacancy( range( 0, ref.geom.ac ), vacancy )
      ref_avg_cl_shift = 0.000
    # end if

    for ai in atomlist:
      atom = self.geom.get( ai )
      s    = atom.symbol
      no   = atom.no
      cls  = atom.cl_shift
      pos  = atom.position
      avg_cl_shift += cls

      # cross check
      if ref != None:
        ref_atom = ref.geom.get( lookup[ai] )
        ref_s    = ref_atom.symbol
        ref_pos  = ref_atom.position
        ref_cls  = ref_atom.cl_shift
        ref_avg_cl_shift += ref_cls
      # end if

      # cart position
      if lat_vec != None:
        # change to cart
        pos = self.geom.position( atom, PT.Cart )
        if ref != None:
          ref_pos = ref.geom.position( ref_atom, PT.Cart )
        avg_xyz.add( AtomPos( s, no, cls, pos ) )
      # end if

      if self.verbose:
        msg  = "  %5d %4s" % (atom.no, s)
        msg += " %12.6f" % cls
        msg += " %12.6f %12.6f %12.6f" % (pos[0], pos[1], pos[2])
        if origo_vec != None and lat_vec != None:
          msg += " %12.6f" % v_dr( origo_vec, pos )
        # end if
        print msg

        if ref != None:
          msg  = "R %5d %4s" % (ref_atom.no, ref_s)
          msg += " %12.6f" % ref_cls
          msg += " %12.6f %12.6f %12.6f" % (ref_pos[0], ref_pos[1], ref_pos[2])
          if origo_vec != None and lat_vec != None:
            msg += " %12.6f" % v_dr( origo_vec, ref_pos )
            msg += " %12.6f" % v_dr( ref_pos, pos )
          # end if
          print msg
        # end if
      # end if

      try:
        spec[s] += 1
      except:
        spec[s] = 1
      # end try
    # end for ai

    if self.verbose:
      print "\n Atoms: ", len( atomlist )
      print " Species: ", spec
      print "\n Orig. Average:", avg_cl_shift, "/", len( atomlist ), \
                        "=", avg_cl_shift / len( atomlist )
      if ref != None:
        print "  Ref. Average:", ref_avg_cl_shift, "/", len( atomlist ), \
                               "=", ref_avg_cl_shift / len( atomlist )
        print "\n Shift:", avg_cl_shift - ref_avg_cl_shift
      # end if

    # end if

    # end if
    if lat_vec != None:
      avg_output = IO( 'atomlist.xyz', 'xyz', 'w+' )
      avg_output.geom( avg_xyz )
      avg_output.write()
    # end if

    avg_cl_shift /= len( atomlist )
    return avg_cl_shift
  # end def

  def compare( self, complist = None, eps = 0.01 ):
    shift = 0.00000
    one  = 1.000000
    zero = 0.000000
    c = 0
    ref = self.geom
    inp = self.geom.geom
    for k,v in complist.iteritems():
      k = int(k)
      v = int(v)
      ratom = ref.get(k)
      rpos  = ratom.position
      rcls  = ratom.cl_shift

      iatom = inp.get(v)
      ipos  = iatom.position
      icls  = iatom.cl_shift

      if ratom.symbol != iatom.symbol:
        raise Warning( "Symbol mismatch!" )

      # G2012-04-12
      # normalize coordinates
      for i in range(0,3):
        if ipos[i] > one:
          ipos[i] -= one
        if abs(one - ipos[i]) < eps:
          ipos[i] = one - ipos[i]

        if rpos[i] > one:
          rpos[i] -= one
        if abs(one - rpos[i]) < eps:
          rpos[i] = one - rpos[i]
      # end for
      print "REF %4d %2s %12.9f %12.9f %12.9f %12.9f" % (ratom.no,ratom.symbol,rpos[0],rpos[1],rpos[2],rcls)
      print "INP %4d %2s %12.9f %12.9f %12.9f %12.9f" % (iatom.no,iatom.symbol,ipos[0],ipos[1],ipos[2],icls)
      print "L2 INP-REF: %12.9f" %(l2norm(ipos-rpos))
      print
      # print ratom.no,ratom.symbol,ratom.position
      # print iatom.no,iatom.symbol,iatom.position
      shift += icls - rcls
      c += 1
    # end for
    print "Shift:",shift/float(c)
    print
  # end def

  def ino( self, no, vl ):
    vl.sort()
    for i in vl:
      if no >= i:
        no = no - 1
    # end for
    return no
  # end def

  def average( self, vaclist = None ):
    shift = 0.00000
    one  = 1.000000
    zero = 0.000000
    c = 0
    inp = self.geom
    ref = self.geom.geom

    # for crop check
    crop = self.geom.clone()

    err_symbol = {}
    err_dist = {}

    for aref in ref.atoms:
      # reference
      rno  = aref.rno
      rpos = aref.position
      rcls = aref.cl_shift

      # input
      ino  = self.ino( rno, vaclist )
      if(ino!=rno):
        print "INDEX SHIFT: %d -> %d" %(rno,ino)
      ainp = inp.get(ino)
      ipos = ainp.position
      icls = ainp.cl_shift
      crop.add(ainp)

      if aref.symbol != ainp.symbol:
        raise Warning( "Symbol mismatch!" )
        err_symbol[rno] = ino
      # end if

      # G2012-04-12
      # normalize coordinates
      ipos = rcnorm( ipos, 0.01 )
      rpos = rcnorm( rpos, 0.01 )
      dist = l2norm(ipos-rpos)
      ps = icls-rcls
      print "REF %4d %2s %12.9f %12.9f %12.9f %12.9f" % (rno,aref.symbol,rpos[0],rpos[1],rpos[2],rcls)
      print "INP %4d %2s %12.9f %12.9f %12.9f %12.9f" % (ainp.no,ainp.symbol,ipos[0],ipos[1],ipos[2],icls)
      if(dist > 0.05):
        print "HIGH DISTANCE"
        err_dist[rno] = ino
      # end if
      print "L2(INP-REF) %12.9f  Shift  %12.9f" %(dist,ps)
      print "ASEDIFF %4d %2s %12.9f %12.9f" %(rno,aref.symbol,aref.dist,ps)
      print
      shift += ps
      c += 1
    # end for

    if len(err_symbol):
      print
      print "Symbol mismatch"
      for k,v in err_symbol.iteritems():
        rno=int(k)
        ino=int(v)
        aref=ref.get(rno)
        ainp=inp.get(ino)
        rpos=aref.position
        ipos=ainp.position
        rcls = aref.cl_shift
        icls = ainp.cl_shift
        print "ERR SYMB REF %4d %2s %12.9f %12.9f %12.9f %12.9f" % (rno,aref.symbol,rpos[0],rpos[1],rpos[2],rcls)
        print "ERR SYMB INP %4d %2s %12.9f %12.9f %12.9f %12.9f" % (ino,ainp.symbol,ipos[0],ipos[1],ipos[2],icls)
        print
      # end for
    # end if

    if len(err_dist):
      print
      print "High distance"
      for k,v in err_symbol.iteritems():
        rno=int(k)
        ino=int(v)
        aref=ref.get(rno)
        ainp=inp.get(ino)
        rpos=aref.position
        ipos=ainp.position
        rcls = aref.cl_shift
        icls = ainp.cl_shift
        print "ERR DIST REF %4d %2s %12.9f %12.9f %12.9f %12.9f" % (rno,aref.symbol,rpos[0],rpos[1],rpos[2],rcls)
        print "ERR DIST INP %4d %2s %12.9f %12.9f %12.9f %12.9f" % (ino,ainp.symbol,ipos[0],ipos[1],ipos[2],icls)
        print
      # end for
    # end if

    print "APS %4d %12.9f" % ( c, shift/float(c) )
    print

    crop.normalize()
    #crop.info()
    return crop
  # end def

# end class
Example #8
0
File: UPOT.py Project: hornos/pypak
class IO( File ):
  def __init__( self, path = None, opts = "", sysopts = { "verbose" : False, "debug" : False } ):
    File.__init__( self, path, opts, sysopts )
    self.geom = Geometry( 'UPOT' )
    self.reference = None
  # 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 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:
        self.geom.add( AtomPos( symbol, no, cl_shift, numpy.array( [x, y, z] ) ) )
      except Exception as ex:
        print str( ex )
      # end try
      self.process()
    # end for
  # end def


  def average_upot( self, argv = None ):
    atomlist = argv['atomlist']
    ref      = argv['reference']
    vacancy  = argv['vacancy']
    spec = {}
    avg_cl_shift = 0.000

    # lattice vectors
    try:
      lat_vec = argv['lat_vec']
      self.geom.lat_vec = numpy.reshape( lat_vec, ( 3, 3 ) )
      if ref != None:
        ref.geom.lat_vec = numpy.reshape( lat_vec, ( 3, 3 ) )
      # end if
      msg = PTD[PT.Cart]
      # cart origo?
      try:
        origo_vec = argv['origo_vec']
      except:
        origo_vec = None
      # end try
    # end if
    except:
      lat_vec = None
      msg = PTD[PT.Direct]
    # end try
    if self.verbose:
      print " Coords:",msg
    # end if

    if lat_vec != None:
      avg_xyz = Geometry()
      avg_xyz.lat_vec = self.geom.lat_vec
      avg_xyz.pt = PT.Cart
    # end if

    if self.verbose:
      msg = "\n  %5s %4s %12s %12s %12s %12s" % ("No","Sym","cl_shift","x","y","z")
      if origo_vec != None:
        msg += " %12s" % ("dr")
        if ref != None:
          msg += " %12s" % ("ref dr")
      print msg
    # end if

    # cross check
    if ref != None:
      lookup = l_vacancy( range( 0, ref.geom.ac ), vacancy )
      ref_avg_cl_shift = 0.000
    # end if

    for ai in atomlist:
      atom = self.geom.get( ai )
      s    = atom.symbol
      no   = atom.no
      cls  = atom.cl_shift
      pos  = atom.position
      avg_cl_shift += cls

      # cross check
      if ref != None:
        ref_atom = ref.geom.get( lookup[ai] )
        ref_s    = ref_atom.symbol
        ref_pos  = ref_atom.position
        ref_cls  = ref_atom.cl_shift
        ref_avg_cl_shift += ref_cls
      # end if

      # cart position
      if lat_vec != None:
        # change to cart
        pos = self.geom.position( atom, PT.Cart )
        if ref != None:
          ref_pos = ref.geom.position( ref_atom, PT.Cart )
        avg_xyz.add( AtomPos( s, no, cls, pos ) )
      # end if

      if self.verbose:
        msg  = "  %5d %4s" % (atom.no, s)
        msg += " %12.6f" % cls
        msg += " %12.6f %12.6f %12.6f" % (pos[0], pos[1], pos[2])
        if origo_vec != None and lat_vec != None:
          msg += " %12.6f" % v_dr( origo_vec, pos )
        # end if
        print msg

        if ref != None:
          msg  = "R %5d %4s" % (ref_atom.no, ref_s)
          msg += " %12.6f" % ref_cls
          msg += " %12.6f %12.6f %12.6f" % (ref_pos[0], ref_pos[1], ref_pos[2])
          if origo_vec != None and lat_vec != None:
            msg += " %12.6f" % v_dr( origo_vec, ref_pos )
            msg += " %12.6f" % v_dr( ref_pos, pos )
          # end if
          print msg
        # end if
      # end if

      try:
        spec[s] += 1
      except:
        spec[s] = 1
      # end try
    # end for ai

    if self.verbose:
      print "\n Atoms: ", len( atomlist )
      print " Species: ", spec
      print "\n Orig. Average:", avg_cl_shift, "/", len( atomlist ), \
                        "=", avg_cl_shift / len( atomlist )
      if ref != None:
        print "  Ref. Average:", ref_avg_cl_shift, "/", len( atomlist ), \
                               "=", ref_avg_cl_shift / len( atomlist )
        print "\n Shift:", avg_cl_shift - ref_avg_cl_shift
      # end if

    # end if

    # end if
    if lat_vec != None:
      avg_output = IO( 'atomlist.xyz', 'xyz', 'w+' )
      avg_output.geom( avg_xyz )
      avg_output.write()
    # end if

    avg_cl_shift /= len( atomlist )
    return avg_cl_shift
  # end def



  def average( self, argv = None ):
    return self.average_upot( argv )