Пример #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
Пример #2
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    argc = len(argv)
    self = "link.py"
    if argv[0][-len(self) :] != self:
        sys.exit("\n   Rename script to %s\n" % self)
    if argc != 15:
        sys.exit(
            "\n   Usage: %s fnc ffc fns ffs fna ffa icb icd isb isd acs facs rcs frcs\n" % self
            + "   Description: links the side chain read from file fns to the core structure\n"
            + "   read from file fnc and writes the obtained adduct to file fna. Variables\n"
            + "   ffc, ffs, ffa set to mat|paratec|vasp|espresso|siesta|tbpw|xyz|xsf specify\n"
            + "   the file formats. The core and side atoms with indices icb and isb are\n"
            + "   bonded together, while the core and side atoms with indices icd and isd\n"
            + "   are detached. The side chain is oriented in such a way that bonds icb-icd,\n"
            + "   isd-isb, and icb-isb are all collinear. Variable acs gives the angle by\n"
            + "   which the side chain is rotated around the icb-isb axis. Variable rcs\n"
            + "   gives the distance between icb and isb atoms. Variables facs and frcs set\n"
            + "   to radian or degree and bohr or angstrom define the units of acs and rcs.\n"
        )
    fnc = argv[1]
    ffc = argv[2].lower()
    fns = argv[3]
    ffs = argv[4].lower()
    fna = argv[5]
    ffa = argv[6].lower()
    try:
        icb = int(argv[7]) - 1
    except:
        icb = -1
    try:
        icd = int(argv[8]) - 1
    except:
        icd = -1
    try:
        isb = int(argv[9]) - 1
    except:
        isb = -1
    try:
        isd = int(argv[10]) - 1
    except:
        isd = -1
    try:
        acs = float(argv[11])
    except:
        sys.exit("\n   Error: acs %s\n" % argv[11])
    facs = argv[12].lower()
    if facs == "r" or facs == "rad" or facs == "radian":
        pass
    elif facs == "d" or facs == "deg" or facs == "degree":
        acs *= math.pi / 180.0
    else:
        sys.exit("\n   Error: facs %s\n" % argv[12])
    try:
        rcs = float(argv[13])
    except:
        rsc = -inf9
    if rcs < eps9:
        sys.exit("\n   Error: rcs %s\n" % argv[13])
    frcs = argv[14].lower()
    if frcs == "b" or frcs == "bohr":
        format = format_index2mat[0]
    elif frcs == "a" or frcs == "ang" or frcs == "angstrom":
        format = format_index2mat[1]
    else:
        sys.exit("\n   Error: frcs %s\n" % argv[14])
    if ffc == "mat":
        ierr, matc = mat_read(fnc)
    elif ffc == "paratec":
        ierr, matc = paratec_read(fnc)
    elif ffc == "vasp":
        ierr, matc = vasp_read(fnc)
    elif ffc == "espresso":
        ierr, matc = espresso_read(fnc)
    elif ffc == "siesta":
        ierr, matc = siesta_read(fnc)
    elif ffc == "tbpw":
        ierr, matc = tbpw_read(fnc)
    elif ffc == "xyz":
        ierr, matc = xyz_read(fnc)
    elif ffc == "xsf":
        ierr, matc = xsf_read(fnc)
    else:
        sys.exit("\n   Error: invalid input format %s\n" % ffc)
    if ierr != 0:
        sys.exit("\n   Error: unable to read file %s\n" % fnc)
    ierr = mat_check(matc)
    if ierr != 0:
        sys.exit("\n   Error: invalid content in file %s\n" % fnc)
    matc = mat_format(matc, format, format, format, format)
    if matc["fc"] != format or matc["fo"] != format or matc["fv"] != format or matc["fp"] != format:
        sys.exit("\n   Error: unable to change format in file %s\n" % fnc)
    if icb < 0 or icb > matc["na"] - 1:
        sys.exit("\n   Error: icb %s\n" % argv[7])
    if icd < 0 or icd > matc["na"] - 1 or icd == icb:
        sys.exit("\n   Error: icd %s\n" % argv[8])
    vc = []
    for j in range(3):
        vc.append(matc["ap"][icd][j] - matc["ap"][icb][j])
    rc = 0.0
    for j in range(3):
        rc += math.pow(vc[j], 2)
    rc = math.sqrt(rc)
    if rc < eps9:
        sys.exit("\n   Error: atomic positions in file %s\n" % fnc)
    if ffs == "mat":
        ierr, mats = mat_read(fns)
    elif ffs == "paratec":
        ierr, mats = paratec_read(fns)
    elif ffs == "vasp":
        ierr, mats = vasp_read(fns)
    elif ffs == "espresso":
        ierr, mats = espresso_read(fns)
    elif ffs == "siesta":
        ierr, mats = siesta_read(fns)
    elif ffs == "tbpw":
        ierr, mats = tbpw_read(fns)
    elif ffs == "xyz":
        ierr, mats = xyz_read(fns)
    elif ffs == "xsf":
        ierr, mats = xsf_read(fns)
    else:
        sys.exit("\n   Error: invalid input format %s\n" % ffs)
    if ierr != 0:
        sys.exit("\n   Error: unable to read file %s\n" % fns)
    ierr = mat_check(matc)
    if ierr != 0:
        sys.exit("\n   Error: invalid content in file %s\n" % fns)
    mats = mat_format(mats, format, format, format, format)
    if mats["fc"] != format or mats["fo"] != format or mats["fv"] != format or mats["fp"] != format:
        sys.exit("\n   Error: unable to change format in file %s\n" % fns)
    if isb < 0 or isb > mats["na"] - 1:
        sys.exit("\n   Error: isb %s\n" % argv[9])
    if isd < 0 or isd > mats["na"] - 1 or isd == isb:
        sys.exit("\n   Error: isd %s\n" % argv[10])
    vs = []
    for j in range(3):
        vs.append(mats["ap"][isb][j] - mats["ap"][isd][j])
    rs = 0.0
    for j in range(3):
        rs += math.pow(vs[j], 2)
    rs = math.sqrt(rs)
    if rs < eps9:
        sys.exit("\n   Error: atomic positions in file %s\n" % fns)
    numberc, axesc, anglesc = align_vector(vc)
    numbers, axess, angless = align_vector(vs)
    number = numbers + 1 + numberc
    axes = []
    angles = []
    for i in range(numbers):
        axes.append(axess[i])
        angles.append(angless[i])
    axes.append(2)
    angles.append(acs)
    for i in range(numberc - 1, -1, -1):
        axes.append(axesc[i])
        angles.append(-anglesc[i])
    rotation = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
    for i in range(number):
        rotation = matrix_dot_matrix(rotation_matrix(axes[i], angles[i]), rotation)
    mats = mat_rotate(mats, rotation)
    translation = [0.0, 0.0, 0.0]
    for j in range(3):
        translation[j] += matc["ap"][icb][j] - mats["ap"][isb][j] + vc[j] * rcs / rc
    mats = mat_translate(mats, translation, format)
    matc = mat_remove(matc, icd)
    mats = mat_remove(mats, isd)
    mata = mat_merge(matc, mats)
    if ffa == "mat":
        ierr = mat_write(fna, mata)
    elif ffa == "paratec":
        ierr = paratec_write(fna, mata)
    elif ffa == "vasp":
        ierr = vasp_write(fna, mata)
    elif ffa == "espresso":
        ierr = espresso_write(fna, mata)
    elif ffa == "siesta":
        ierr = siesta_write(fna, mata)
    elif ffa == "tbpw":
        ierr = tbpw_write(fna, mata)
    elif ffa == "xyz":
        ierr = xyz_write(fna, mata)
    elif ffa == "xsf":
        ierr = xsf_write(fna, mata)
    else:
        sys.exit("\n   Error: invalid output format %s\n" % ffa)
    if ierr != 0:
        sys.exit("\n   Error: unable to write file %s\n" % fna)
    return 0
Пример #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