Ejemplo n.º 1
0
def main(argv = None):
   if argv is None:
      argv = sys.argv
   argc = len(argv)
   self = "convert.py"
   if argv[0][-len(self):] != self:
      sys.exit("\n   Rename script to %s\n" % self)
   if not (argc == 5 or (argc > 5 and ((argv[5].lower() == 'units' and argc == 9) or (argv[5].lower() == 'vacuum' and argc == 9) or (argv[5].lower() == 'supercell' and (argc == 7 or argc == 20)) or (argv[5].lower() == 'translate' and argc == 10) or (argv[5].lower() == 'rotate' and (argc == 8 or argc == 9)) or (argv[5].lower() == 'center' and argc == 8) or (argv[5].lower() == 'minbox' and argc == 7)))):
      sys.exit("\n   Usage: %s ifn iff ofn off [action parameters]\n" % self +
               "      ifn = input file name         ofn = output file name\n" +
               "      iff = input file format       off = output file format\n" +
               "   format = mat|paratec|vasp|espresso|siesta|tbpw|xyz|xsf|wien|povray\n" +
               "                                   (wien & povray are only for output)\n\n" +
               "     action  parameters\n" +
               "     ------------------\n" +
               "      units  lattice_constant_units lattice_vector_units atomic_position_units\n" +
               "     vacuum  axes separation_distance separation_distance_units\n" +
               "  supercell  ( sc_file | sc_origin sc_vectors sc_units sc_translation )\n" +
               "  translate  translation_vector translation_vector_units\n" +
               "     rotate  axis ( rotation_angle | adjacent_side opposite_side )\n" +
               "     center  axes center_type\n" +
               "     minbox  axes\n\n" +
               "             axis = x | y | z             rotation_angle in degrees\n" +
               "             axes = x & y & z             *_side in arbitrary cartesian units\n" +
               "   sc_translation = true | false          center_type = geometric | mass\n" +
               "          *_units = bohr | angstrom | latconst | latvec\n")
   ifn = argv[1]
   ift = argv[2].lower()
   ofn = argv[3]
   oft = argv[4].lower()
   if ift == 'mat':
      ierr, mat = mat_read(ifn)
   elif ift == 'paratec':
      ierr, mat = paratec_read(ifn)
   elif ift == 'vasp':
      ierr, mat = vasp_read(ifn)
   elif ift == 'espresso':
      ierr, mat = espresso_read(ifn)
   elif ift == 'siesta':
      ierr, mat = siesta_read(ifn)
   elif ift == 'tbpw':
      ierr, mat = tbpw_read(ifn)
   elif ift == 'xyz':
      ierr, mat = xyz_read(ifn)
   elif ift == 'xsf':
      ierr, mat = xsf_read(ifn)
   else:
      sys.exit("\n   Error: invalid input format %s\n" % ift)
   if ierr != 0:
      sys.exit("\n   Error: unable to read file %s\n" % ifn)
   ierr = mat_check(mat)
   if ierr != 0:
      sys.exit("\n   Error: invalid content in file %s\n" % ifn)
   if oft == 'povray':
      st = 1
      umat = {}
      smin = []
      smax = []
      si = []
   if argc != 5:
      if argv[5].lower() == 'units':
         ierr, fc = format_input(argv[6], 2)
         if ierr != 0:
            sys.exit("\n   Error: invalid lattice_constant_units %s\n" % argv[6])
         ierr, fo = format_input(argv[7], 3)
         if ierr != 0:
            sys.exit("\n   Error: invalid lattice_vector_units %s\n" % argv[7])
         ierr, fp = format_input(argv[8], 4)
         if ierr != 0:
            sys.exit("\n   Error: invalid atomic_position_units %s\n" % argv[8])
         if fo == format_index2mat[3]:
            fv = format_index2mat[2]
         else:
            fv = fo
         mat = mat_format(mat, fc, fo, fv, fp)
      elif argv[5].lower() == 'vacuum':
         axes = axes_input(argv[6])
         try:
            distance = float(argv[7])
         except:
            sys.exit("\n   Error: invalid separation_distance %s\n" % argv[7])
         ierr, format = format_input(argv[8], 3)
         if ierr != 0:
            sys.exit("\n   Error: invalid separation_distance_units %s\n" % argv[8])
         mat = mat_lattice(mat, axes, distance, format)
      elif argv[5].lower() == 'supercell':
         if argc == 7:
            ierr = 0
            try:
               h = open(argv[6], 'r')
            except:
               ierr = 1
            if ierr == 0:
               r = h.readlines()
               h.close()
               s = r[0]
               t = s.split()
               so = [float(t[0]), float(t[1]), float(t[2])]
               sv = []
               for i in range(3):
                  s = r[i + 1]
                  t = s.split()
                  sv.append([float(t[0]), float(t[1]), float(t[2])])
               s = r[4]
               t = s.split()
               jerr, format = format_input(t[0], 4)
               ierr += jerr
               s = r[5]
               t = s.split()
               jerr, st = boolean_input(t[0])
               ierr += jerr
            if ierr != 0:
               sys.exit("\n   Error: unable to read supercell_file %s\n" % argv[6])
         else:
            try:
               so = [float(argv[6]), float(argv[7]), float(argv[8])]
            except:
               sys.exit("\n   Error: invalid supercell_origin %s %s %s\n" % (argv[6], argv[7], argv[8]))
            try:
               sv = [[float(argv[9]), float(argv[10]), float(argv[11])], [float(argv[12]), float(argv[13]), float(argv[14])], [float(argv[15]), float(argv[16]), float(argv[17])]]
            except:
               sys.exit("\n   Error: invalid supercell_vectors %s %s %s %s %s %s %s %s %s\n" % (argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17]))
            ierr, format = format_input(argv[18], 4)
            if ierr != 0:
               sys.exit("\n   Error: invalid supercell_units %s\n" % argv[18])
            ierr, st = boolean_input(argv[19])
            if ierr != 0:
               sys.exit("\n   Error: invalid supercell_translation %s\n" % argv[19])
         if st == 0:
            umat = copy.deepcopy(mat)
         smin, smax, si, mat = mat_replicate(mat, so, sv, format)
      elif argv[5].lower() == 'translate':
         try:
            translation = [float(argv[6]), float(argv[7]), float(argv[8])]
         except:
            sys.exit("\n   Error: invalid translation_vector %s %s %s\n" % (argv[6], argv[7], argv[8]))
         ierr, format = format_input(argv[9], 4)
         if ierr != 0:
            sys.exit("\n   Error: invalid translation_vector_units %s\n" % argv[9])
         mat = mat_translate(mat, translation, format)
      elif argv[5].lower() == 'rotate':
         ierr, axis = axis_input(argv[6])
         if argc == 8:
            try:
               angle = float(argv[7]) * math.pi / 180.0
            except:
               sys.exit("\n   Error: invalid rotation_angle %s\n" % argv[7])
         else:
            try:
               adjacent = float(argv[7])
            except:
               sys.exit("\n   Error: invalid adjacent_side %s\n" % argv[7])
            try:
               opposite = float(argv[8])
            except:
               sys.exit("\n   Error: invalid opposite_side %s\n" % argv[8])
            angle = math.atan2(opposite, adjacent)
         rotation = rotation_matrix(axis, angle)
         mat = mat_rotate(mat, rotation)
      elif argv[5].lower() == 'center':
         axes = axes_input(argv[6])
         ierr, center_type = center_input(argv[7])
         if ierr != 0:
            sys.exit("\n   Error: invalid center_type %s\n" % argv[7])
         format = mat['fp']
         center = []
         if center_type == 0:
            fr, rmin, rmax = mat_range(mat['na'], mat['fc'], mat['lc'], mat['fv'], mat['lv'], mat['fp'], mat['ap'], format)
            if fr != format:
               sys.exit("\n   Error: failed center_type geometric\n")
            for j in range(3):
               center.append((rmin[j] + rmax[j]) / 2)
         else:
            if format == format_index2mat[3]:
               sys.exit("\n   Error: center_type mass incompatible with atomic_position_units %s\n" % format)
            total_mass = 0.0
            for j in range(3):
               center.append(0.0)
            for i in range(mat['na']):
               atomic_mass = periodic_table[index_by_number(mat['as'][i])]['mass']
               total_mass += atomic_mass
               for j in range(3):
                  center[j] += mat['ap'][i][j] * atomic_mass
            if abs(total_mass) > eps9:
               for j in range(3):
                  center[j] /= total_mass
            else:
               sys.exit("\n   Error: failed center_type mass\n")
         translation = []
         for j in range(3):
            translation.append(-center[j] * axes[j])
         mat = mat_translate(mat, translation, format)
      elif argv[5].lower() == 'minbox':
         axes = axes_input(argv[6])
         for i in range(3):
            if axes[i] == 1:
               for j in range(3):
                  if j != i:
                     minwidth = inf9
                     minangle = 0.0
                     for k in range(180):
                        angle = float(k) * math.pi / 180.0
                        rotation = rotation_matrix(j, angle)
                        cmat = copy.deepcopy(mat)
                        rmat = mat_rotate(cmat, rotation)
                        format, rmin, rmax = mat_range(rmat['na'], rmat['fc'], rmat['lc'], rmat['fv'], rmat['lv'], rmat['fp'], rmat['ap'], 'bohr')
                        cmat = []
                        rmat = []
                        width = rmax[i] - rmin[i]
                        if width < minwidth - eps9:
                           minwidth = width
                           minangle = angle
                     rotation = rotation_matrix(j, minangle)
                     cmat = copy.deepcopy(mat)
                     mat = mat_rotate(cmat, rotation)
                     cmat = []
   if oft == 'povray':
      box = 1
      basis = 1
      if st != 0:
         ierr, bonds = povray_bonds_st(mat)
      else:
         ierr, bonds = povray_bonds_ut(mat, umat, smin, smax, si)
      if ierr != 0:
         sys.exit("\n   Error: failed povray_bonds\n")
   if oft == 'mat':
      ierr = mat_write(ofn, mat)
   elif oft == 'paratec':
      ierr = paratec_write(ofn, mat)
   elif oft == 'vasp':
      ierr = vasp_write(ofn, mat)
   elif oft == 'espresso':
      ierr = espresso_write(ofn, mat)
   elif oft == 'siesta':
      ierr = siesta_write(ofn, mat)
   elif oft == 'tbpw':
      ierr = tbpw_write(ofn, mat)
   elif oft == 'xyz':
      ierr = xyz_write(ofn, mat)
   elif oft == 'xsf':
      ierr = xsf_write(ofn, mat)
   elif oft == 'wien':
      ierr = wien_write(ofn, mat)
   elif oft == 'povray':
      ierr = povray_write(ofn, mat, box, basis, bonds)
   else:
      sys.exit("\n   Error: invalid output format %s\n" % oft)
   if ierr != 0:
      sys.exit("\n   Error: unable to write file %s\n" % ofn)
   return 0
Ejemplo n.º 2
0
def main(argv = None):
   if argv is None:
      argv = sys.argv
   argc = len(argv)
   self = "gsphere.py"
   if argv[0][-len(self):] != self:
      sys.exit("\n   Rename script to %s\n" % self)
   if argc != 3:
      sys.exit("\n   Usage: %s ifn ofn\n" % self +
                "   Description: reads system info from file ifn, generates the G-space\n" +
               "   and writes it to file ofn. ifn contains: lattice vectors (in bohr),\n" +
               "   energy cutoff (in Ry), FFT grid (set to zero to determine automatically)\n" +
               "   flag (true or false) for sorting the G-vectors, and optionally a k- or\n" +
               "   q-point (reduced coords). The number of G-vectors is roughly equal to\n" +
               "   N = V * E^(3/2) / (6 * pi^2) where V is the volume of the unit cell\n" +
               "   (in bohr^3) and E is the energy cutoff (in Ry).\n")
   ifn = argv[1]
   ofn = argv[2]
   ierr = 0
   try:
      h = open(ifn, 'r')
   except:
      ierr = 1
   if ierr == 0:
      a = []
      for i in range(3):
         a.append([])
         s = h.readline()
         t = s.split()
         for j in range(3):
            a[i].append(float(t[j]))
      s = h.readline()
      t = s.split()
      ecut = float(t[0])
      s = h.readline()
      t = s.split()
      nr = [int(t[0]), int(t[1]), int(t[2])]
      s = h.readline()
      t = s.split()
      ierr, fsort = boolean_input(t[0])
      try:
         s = h.readline()
         t = s.split()
         kpt = [float(t[0]), float(t[1]), float(t[2])]
         kpt_given = True
      except:
         kpt = [0.0, 0.0, 0.0]
         kpt_given = False
      h.close()
   if ierr != 0:
      sys.exit("\n   Error: unable to read file %s\n" % ifn)

   al = math.sqrt(a[0][0] * a[0][0] + a[0][1] * a[0][1] + a[0][2] * a[0][2])
   for i in range(3):
      for j in range(3):
         a[i][j] /= al
   vol = 0.0
   vol += a[0][0] * (a[1][1] * a[2][2] - a[2][1] * a[1][2])
   vol -= a[0][1] * (a[1][0] * a[2][2] - a[2][0] * a[1][2])
   vol += a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1])
   bl = 2.0 * math.pi / al
   b = [[], [], []]
   b[0].append((a[1][1] * a[2][2] - a[1][2] * a[2][1]) / vol)
   b[0].append((a[1][2] * a[2][0] - a[1][0] * a[2][2]) / vol)
   b[0].append((a[1][0] * a[2][1] - a[1][1] * a[2][0]) / vol)
   b[1].append((a[2][1] * a[0][2] - a[2][2] * a[0][1]) / vol)
   b[1].append((a[2][2] * a[0][0] - a[2][0] * a[0][2]) / vol)
   b[1].append((a[2][0] * a[0][1] - a[2][1] * a[0][0]) / vol)
   b[2].append((a[0][1] * a[1][2] - a[0][2] * a[1][1]) / vol)
   b[2].append((a[0][2] * a[1][0] - a[0][0] * a[1][2]) / vol)
   b[2].append((a[0][0] * a[1][1] - a[0][1] * a[1][0]) / vol)
   vol = abs(vol) * al * al * al
   gcutm = ecut / (bl * bl)

   try:
      h = open(ofn, 'w')
   except:
      sys.exit("\n   Error: unable to write file %s\n" % ofn)

   h.write('\n')
   for i in range(3):
      s = ' a%1i =' % (i + 1)
      for j in range(3):
         s += ' %13.9f' % (al * a[i][j])
      s += '\n'
      h.write(s)
   h.write('\n')
   for i in range(3):
      s = ' b%1i =' % (i + 1)
      for j in range(3):
         s += ' %13.9f' % (bl * b[i][j])
      s += '\n'
      h.write(s)
   h.write('\n')
   s = ' volume = %.9f\n' % vol
   h.write(s)
   h.write('\n')
   s = ' cutoff = %.9f\n' % ecut
   h.write(s)
   h.write('\n')

   if nr[0] < 1 or nr[1] < 1 or nr[2] < 1:
      for i in range(3):
         nr[i] = 2 * int(math.sqrt(gcutm) * math.sqrt(a[i][0] * a[i][0] + a[i][1] * a[i][1] + a[i][2] * a[i][2])) + 1
   nr6 = []
   for i in range(3):
      nr6.append(nr[i])
      while not check_nr_size(nr6[i], 6):
         nr6[i] += 1
   nr3 = []
   for i in range(3):
      nr3.append(nr[i])
      while not check_nr_size(nr3[i], 3):
         nr3[i] += 1

   s = ' grid = ('
   for i in range(3):
      s += ' %i' % nr[i]
   s += ' ) -- minimal\n'
   h.write(s)
   s = '        ('
   for i in range(3):
      s += ' %i' % nr6[i]
   s += ' ) -- factors 2, 3, 5, 7, 1*11, 1*13\n'
   h.write(s)
   s = '        ('
   for i in range(3):
      s += ' %i' % nr3[i]
   s += ' ) -- factors 2, 3, 5\n'
   h.write(s)
   h.write('\n')

   grange = []
   s = " Max G-vector indices :"
   for idir in range(3):
      grange.append((nr[idir] - 1) / 2)
      s += " %i" % grange[idir]
   h.write(s)
   h.write('\n')

   if kpt_given:
      s = ' k-point = ('
      for i in range(3):
         s += ' %.9f' % kpt[i]
      s += ' )'
      h.write(s)
      h.write('\n')

   ng = 0
   g_cryst = []
   g_cart = []
   e_kin = []
   for i in range(-grange[0], grange[0] + 1):
      for j in range(-grange[1], grange[1] + 1):
         for k in range(-grange[2], grange[2] + 1):
            ifl = float(i) + kpt[0]
            jfl = float(j) + kpt[1]
            kfl = float(k) + kpt[2]
            gx = ifl * b[0][0] + jfl * b[1][0] + kfl * b[2][0]
            gy = ifl * b[0][1] + jfl * b[1][1] + kfl * b[2][1]
            gz = ifl * b[0][2] + jfl * b[1][2] + kfl * b[2][2]
            g2 = gx * gx + gy * gy + gz * gz
            if bl * bl * g2 < ecut:
               ng += 1
               g_cryst.append([i, j, k])
               gx = float(i) * b[0][0] + float(j) * b[1][0] + float(k) * b[2][0]
               gy = float(i) * b[0][1] + float(j) * b[1][1] + float(k) * b[2][1]
               gz = float(i) * b[0][2] + float(j) * b[1][2] + float(k) * b[2][2]
               g_cart.append([bl * gx, bl * gy, bl * gz])
               e_kin.append(bl * bl * g2)
   g_sort = []
   for i in range(ng):
      g_sort.append(i)
   if fsort:
      e_sort = []
      for i in range(ng):
         e_sort.append(e_kin[i])
      e_sort.sort()
      for i in range(ng):
         j = e_sort.index(e_kin[i])
         e_sort[j] = inf9
         g_sort[j] = i

   s = ' ng = %i\n' % ng
   h.write(s)
   h.write('\n')
   h.write('     n    g1    g2    g3     gx        gy        gz        ekin\n')
   h.write(' --------------------------------------------------------------------\n')

   for i in range(ng):
      s = ' %5i' % (i + 1)
      for j in range(3):
         s += ' %5i' % g_cryst[g_sort[i]][j]
      for j in range(3):
         s += ' %9.3f' % g_cart[g_sort[i]][j]
      s += ' %15.9f' % e_kin[g_sort[i]]
      s += '\n'
      h.write(s)
   h.write('\n')

   h.close()
   return 0
Ejemplo n.º 3
0
def main(argv = None):
   if argv is None:
      argv = sys.argv
   argc = len(argv)
   self = "volume.py"
   if argv[0][-len(self):] != self:
      sys.exit("\n   Rename script to %s\n" % self)
   if not (argc == 9 or argc == 10):
      sys.exit("\n   Usage: %s imfn imff ivfn ivff ovfn ovff phas cplx [hole]\n" % self +
               "   imfn = input matter file name\n" +
               "   imff = input matter file format\n" +
               "          (mat|paratec|vasp|espresso|siesta|tbpw|xyz|xsf)\n" +
               "   ivfn = input volumetric file name\n" +
               "   ivff = input volumetric file format (a3dr)\n" +
               "   ovfn = output volumetric file name\n" +
               "   ovff = output volumetric file format (cube|xsf)\n" +
               "   phas = remove wavefunction phase (true|false)\n" +
               "   cplx = complex wavefunction (re|im|abs|abs2)\n" +
               "   hole = plot hole (true|false)\n")
   imfn = argv[1]
   imff = argv[2].lower()
   ivfn = argv[3]
   ivff = argv[4].lower()
   ovfn = argv[5]
   ovff = argv[6].lower()
   if imff == 'mat':
      ierr, mat = mat_read(imfn)
   elif imff == 'paratec':
      ierr, mat = paratec_read(imfn)
   elif imff == 'vasp':
      ierr, mat = vasp_read(imfn)
   elif imff == 'espresso':
      ierr, mat = espresso_read(imfn)
   elif imff == 'siesta':
      ierr, mat = siesta_read(imfn)
   elif imff == 'tbpw':
      ierr, mat = tbpw_read(imfn)
   elif imff == 'xyz':
      ierr, mat = xyz_read(imfn)
   elif imff == 'xsf':
      ierr, mat = xsf_read(imfn)
   else:
      sys.exit("\n   Error: invalid input format %s\n" % imff)
   if ierr != 0:
      sys.exit("\n   Error: unable to read file %s\n" % imfn)
   ierr = mat_check(mat)
   if ierr != 0:
      sys.exit("\n   Error: invalid content in file %s\n" % imfn)
   if ivff == 'a3dr':
      ierr, state, energy, holpos, latvec, grid, psi = a3dr_read(ivfn)
   else:
      sys.exit("\n   Error: invalid input format %s\n" % ivff)
   if ierr != 0:
      sys.exit("\n   Error: unable to read file %s\n" % ivfn)
   format = format_index2mat[0]
   so = [0.0, 0.0, 0.0]
# temporary hack for a3dr format
# lattice vectors in a3dr file are written in %.6f format
# in paratec benzene example, lattice vectors read from a3dr file
# differ from lattice vectors read from paratec input file
# this results in identical atoms written to rho.cube
# (total of 16 atoms instead of 12 atoms)
# this hack computes the supercell dimensions and
# replaces the lattice vectors read from a3dr file
# with the lattice vectors read from the input file
# multiplied by the supercell dimensions
   mat = mat_format(mat, mat['fc'], mat['fo'], format, mat['fp'])
   a1 = mat['lv'][0]
   a2 = mat['lv'][1]
   a3 = mat['lv'][2]
   b1 = latvec[0]
   b2 = latvec[1]
   b3 = latvec[2]
   n1 = int(round((a1[0] * b1[0] + a1[1] * b1[1] + a1[2] * b1[2]) / (a1[0]**2 + a1[1]**2 + a1[2]**2)))
   n2 = int(round((a2[0] * b2[0] + a2[1] * b2[1] + a2[2] * b2[2]) / (a2[0]**2 + a2[1]**2 + a2[2]**2)))
   n3 = int(round((a3[0] * b3[0] + a3[1] * b3[1] + a3[2] * b3[2]) / (a3[0]**2 + a3[1]**2 + a3[2]**2)))
   b1 = [n1 * a1[0], n1 * a1[1], n1 * a1[2]]
   b2 = [n2 * a2[0], n2 * a2[1], n2 * a2[2]]
   b3 = [n3 * a3[0], n3 * a3[1], n3 * a3[2]]
   latvec = [b1, b2, b3]
# end of hack
   sv = latvec
   smin, smax, si, mat = mat_replicate(mat, so, sv, format)
   if mat['fo'] != format and mat['fv'] != format and mat['fp'] != format:
      sys.exit("\n   Error: unable to make supercell\n")
   if argc > 9 and holpos != []:
      ierr, hole = boolean_input(argv[9])
      if ierr != 0:
         sys.exit("\n   Error: invalid hole %s\n" % argv[9])
      if hole != 0:
         mat['as'].append(0)
         mat['ap'].append(holpos)
         mat['na'] += 1
         mat['ns'] = len(mat_species(mat['na'], mat['as']))
   ierr, phas = boolean_input(argv[7])
   if ierr != 0:
      sys.exit("\n   Error: invalid phase %s\n" % argv[7])
   if phas != 0:
      rmax = 0.0
      for i in range(grid[0]):
         for j in range(grid[1]):
            for k in range(grid[2]):
               r = math.sqrt(math.pow(psi[k][j][i][0], 2) + math.pow(psi[k][j][i][1], 2))
               if r > rmax:
                  rmax = r
                  phase = math.atan2(psi[k][j][i][1], psi[k][j][i][0])
      cs = math.cos(phase)
      sn = math.sin(phase)
      for i in range(grid[0]):
         for j in range(grid[1]):
            for k in range(grid[2]):
               x = psi[k][j][i][0]
               y = psi[k][j][i][1]
               psi[k][j][i][0] = cs * x + sn * y
               psi[k][j][i][1] = -sn * x + cs * y
   ierr, cplx = cplx_input(argv[8])
   if ierr != 0:
      sys.exit("\n   Error: invalid cplx %s\n" % argv[8])
   phi = []
   for i in range(grid[0]):
      phi.append([])
      for j in range(grid[1]):
         phi[i].append([])
         for k in range(grid[2]):
            if cplx == 0:
               chi = psi[k][j][i][0]
            elif cplx == 1:
               chi = psi[k][j][i][1]
            elif cplx == 2:
               chi = math.sqrt(math.pow(psi[k][j][i][0], 2) + math.pow(psi[k][j][i][1], 2))
            elif cplx == 3:
               chi = math.pow(psi[k][j][i][0], 2) + math.pow(psi[k][j][i][1], 2)
            else:
               chi = 0.0
            if phas != 0 and (cplx == 2 or cplx == 3) and psi[k][j][i][0] < 0.0:
               chi = -chi
            phi[i][j].append(chi)
   if ovff == 'cube':
      i = 0
   elif ovff == 'xsf':
      i = 1
   else:
      sys.exit("\n   Error: invalid output format %s\n" % ovff)
   fc = format_index2mat[i]
   fo = format_index2mat[i]
   fv = format_index2mat[i]
   fp = format_index2mat[i]
   mat = mat_format(mat, fc, fo, fv, fp)
   if mat['fc'] != fc or mat['fo'] != fo or mat['fv'] != fv or mat['fp'] != fp:
      sys.exit("\n   Error: invalid content in file %s\n" % imfn)
   na = mat['na']
   aspecies = []
   for i in range(na):
      aspecies.append(mat['as'][i])
   acharge = []
   for i in range(na):
      acharge.append(float(mat['as'][i]))
   aposition = []
   for i in range(na):
      aposition.append([])
      for j in range(3):
         aposition[i].append(mat['ap'][i][j])
   origin = []
   for j in range(3):
      origin.append(mat['lo'][j])
   latvec = []
   for i in range(3):
      latvec.append([])
      for j in range(3):
         latvec[i].append(mat['lv'][i][j])
   step = []
   for i in range(3):
      step.append([])
      for j in range(3):
         step[i].append(latvec[i][j] / float(grid[i]))
   if ovff == 'cube':
      ierr = cub_write(ovfn, state, energy, na, aspecies, acharge, aposition, grid, origin, step, phi)
   elif ovff == 'xsf':
      ierr = xsf_write(ovfn, state, energy, na, aspecies, aposition, grid, origin, latvec, phi)
   else:
      sys.exit("\n   Error: invalid output format %s\n" % ovff)
   if ierr != 0:
      sys.exit("\n   Error: unable to write file %s\n" % ovfn)
   return 0