예제 #1
0
파일: link.py 프로젝트: wu2meng3/GW_spinor
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
예제 #2
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