Esempio n. 1
0
def gts_data_and_molden(gtsfile):
    # read gts and prepare Molecule
    molecule = Molecule()
    molecule.set_from_gts(gtsfile)
    molecule.setup()
    # type of PES stationary point?
    nimag = numimag(molecule._ccfreqs)
    mformu = molecule._mform
    # Get ctc and itc
    ctc, itc, ext = gtsfile.split("/")[-1].split(".")
    # tuple
    gts_tuple = (itc, molecule._ch, molecule._mtp, molecule._V0, nimag, mformu,
                 molecule._pgroup)
    # molden file
    if not os.path.exists(PN.DIR5): os.mkdir(PN.DIR5)
    if is_string_valid(ctc, extra="_"):
        molden = PN.get_gtsmolden(ctc, itc)
        if not os.path.exists(molden):
            print "      creating %s" % molden
            bmolden = True
            write_molden(molden,molecule._xcc,molecule._symbols,\
                         molecule._ccfreqs,molecule._ccFevecs)
        else:
            bmolden = False
    else:
        molden = None
        bmolden = False
    return ctc, gts_tuple, molden, bmolden
Esempio n. 2
0
def generate_gts_file(esfile, gtsfile, read_method):
    '''
    Generate gts file (and molden & frozen files)
    from given electronic structure (ES) file;
    The ES file is read using read_method
    '''
    # Extra files that (maybe) will be generated
    file_frozen = gtsfile + ".frozen"  # only if any frozen atom
    file_molden = gtsfile + ".molden"  # molden file
    # read file
    xcc, atonums, ch, mtp, E, gcc, Fcc, masses, clevel = read_method(
        PN.UFOLDER + esfile)[0:9]
    # clevel is not really clevel when using read_gtsfile as read_method
    if read_method == read_gtsfile: clevel = ""
    # symbols and atonums
    symbols, atonums = fncs.symbols_and_atonums(atonums)
    # is masses available?
    if masses is None or len(masses) == 0 or sum(masses) == 0.0:
        masses = atonums2masses(atonums)
    # is Fcc available?
    if len(atonums) != 1 and (Fcc is None or len(Fcc) == 0):
        status = -1
        cache = None
        molec = None
    else:
        # Generate Molecule instance
        molec = Molecule()
        molec.setvar(xcc=xcc, gcc=gcc, Fcc=Fcc)
        molec.setvar(atonums=atonums, masses=masses)
        molec.setvar(ch=ch, mtp=mtp, V0=E)
        molec.prepare()
        # Deal with frozen atoms [atom i is frozen if Fij=0 forall j)
        frozen_xcc, frozen_symbols = molec.remove_frozen()
        # Calculate point group (must be done after remove frozen)
        molec.calc_pgroup(force=True)
        # Deal with Hessian
        molec.setup()
        # write gts file
        molec.genfile_gts(PN.DIR1 + gtsfile, level=clevel)
        status = 1
        # write frozen (if there are frozen atoms)
        RW.write_frozen(PN.DIR1 + file_frozen, frozen_xcc, frozen_symbols)
        # write molden file
        idata = (PN.DIR1 + file_molden, molec._xcc, molec._symbols,
                 molec._ccfreqs, molec._ccFevecs)
        write_molden(*idata)
        # save to cache
        nimag = int(fncs.numimag(molec._ccfreqs))
        pgroup = str(molec._pgroup)
        mform = str(molec._mform)
        cache = [esfile, gtsfile, E, ch, mtp, nimag, mform, pgroup]
    # delete variable, just in case
    del xcc, gcc, Fcc
    del symbols, atonums, masses
    del molec
    # return
    return status, cache
Esempio n. 3
0
 def set_from_gtsfiles(self, gtsfiles):
     '''
       returns status, string
       status = -1 ==> inconsistences between conformers
       status =  0 ==> one or several gts files do not exist
       status =  1 ==> everything is ok
       string is "" except for status = -1
       '''
     self._itcs = []
     self._lpg = []
     lch = []
     lmtp = []
     limag = []
     lmformu = []
     self._lV0 = []
     if len(gtsfiles) == 0: return 0, None
     # save lists
     for gts in gtsfiles:
         ctc, itc, ext = gts.split("/")[-1].split(".")
         self._root = ctc
         if not os.path.exists(gts): return 0, ""
         molecule = Molecule()
         molecule.set_from_gts(gts)
         molecule.setup()
         # save molecule
         self._molecules.append(molecule)
         # complete CTC data
         self._lV0.append(float(molecule._V0))
         self._lpg.append(str(molecule._pgroup))
         self._itcs.append((itc, pgroup2weight(molecule._pgroup)))
         # save some special data of this gts
         lch.append(int(molecule._ch))
         lmtp.append(int(molecule._mtp))
         limag.append(int(numimag(molecule._ccfreqs)))
         lmformu.append(str(molecule._mform))
     # check
     len1 = len(list(set(lch)))
     len2 = len(list(set(lmtp)))
     len3 = len(list(set(limag)))
     len4 = len(list(set(lmformu)))
     if len1 * len2 * len3 * len4 != 1:
         # table head and division
         ml1 = max([len(name) for name in lmformu] + [7])
         ml2 = max([len(name) for name in gtsfiles] + [10])
         line_format = " %%-%is | %%-%is | %%-%is | %%-%is | %%-%is | %%-%is " % (
             5, 6, 3, 7, ml1, ml2)
         thead = line_format % ("itc", "charge", "mtp", "n.imag.",
                                "m.form.", "gts file")
         tdivi = "-" * len(thead)
         # start string
         string = "Main properties of each conformer in '%s'\n" % self._ctc
         string += "   " + tdivi + "\n"
         string += "   " + thead + "\n"
         string += "   " + tdivi + "\n"
         for idx, (itc, weight) in enumerate(self._itcs):
             col1 = itc
             col2 = "%i" % lch[idx]
             col3 = "%i" % lmtp[idx]
             col4 = "%i" % limag[idx]
             col5 = lmformu[idx]
             col6 = gtsfiles[idx]
             ldata = (col1, col2, col3, col4, col5, col6)
             string += "   " + line_format % ldata + "\n"
         string += "   " + tdivi + "\n"
         return -1, string
     else:
         self._mformu = lmformu[0]
         self._ch = lch[0]
         self._mtp = lmtp[0]
         self._type = limag[0]
         self._V0 = min(self._lV0)
         self._es = [(self._mtp, 0.0)]
         return 1, ""
Esempio n. 4
0
def deal_with_folder(folder, dtrack, dctc):
    '''
    deal with an electronic structure (es) file of a subfolder inside UDATA/
    '''

    print("  - Reading files in: %s" % (PN.UFOLDER + folder))

    # Get CTC
    ctc = folder[:-1]

    # Check CTC name
    if not fncs.is_string_valid(ctc, extra="_"):
        print("    '%s' is an invalid name for a CTC!\n" % ctc)
        WRONG[1] = True
        return dtrack, dctc

    # Files for each conformer of the CTC
    esfiles = sorted([folder+esfile for esfile in os.listdir(PN.UFOLDER+folder) \
                                    if  esfile.split(".")[-1].lower() in EXTENSIONS])
    if len(esfiles) == 0:
        print("    empty folder...\n")
        WRONG[2] = True
        return dtrack, dctc

    # initialize CTC
    CTC = ClusterConf(ctc)
    ctc_vars = None

    # GTS generation
    count = 0
    cache = []
    remove = False
    for idx, esfile in enumerate(esfiles):

        # previously assignated?
        gtsfile = dtrack.get(esfile, None)
        if gtsfile is not None and os.path.exists(PN.DIR1 + gtsfile):
            # read file
            print("    %s [gts exists: %s]" % (esfile, PN.DIR1 + gtsfile))
            molecule = Molecule()
            molecule.set_from_gts(PN.DIR1 + gtsfile)
            molecule.setup()
            status = 2
            # add data to cache
            V0 = molecule._V0
            ch = molecule._ch
            mtp = molecule._mtp
            nimag = int(fncs.numimag(molecule._ccfreqs))
            mform = molecule._mform
            pgroup = molecule._pgroup
            cache_i = [esfile, gtsfile, V0, ch, mtp, nimag, mform, pgroup]
        else:
            prov_gts, count = temporal_gtsname(ctc, count)
            status, cache_i = userfile_to_gtsfile(esfile, prov_gts)
        # save cache_i
        cache.append(cache_i)

        # use first conformer to update ctc_vars
        if ctc_vars is None: ctc_vars = cache_i[3:7]

        # Check reading and consistence within CTC
        if status == 0:
            print("    %s [reading failed]" % esfile)
            print("    -- STOPPED --")
            WRONG[3], remove = True, True
        elif status == -1:
            print("    %s [Fcc NOT found] " % esfile)
            print("    -- STOPPED --")
            WRONG[4], remove = True, True
        elif ctc_vars != cache_i[3:7]:
            print("    %s [inconsistence found] " % esfile)
            print("    -- STOPPED --")
            WRONG[5], remove = True, True
            print_table_ufiles(cache)
        elif ctc_vars[2] not in (0, 1):
            print("    %s [unexpected number of imag. freqs.] " % esfile)
            print("    -- STOPPED --")
            WRONG[6], remove = True, True
            print_table_ufiles(cache)
        elif status == 1:
            print("    %s [ok]            " % esfile)
        # sth wrong so we have to remove?
        if remove: break
    print()

    # Remove files if sth wrong
    if remove:
        remove_provisionals([cache_i[1] for cache_i in cache])
        return dtrack, dctc

    # sort cache by energy
    cache.sort(key=lambda x: x[2])
    # already in dctc
    if ctc in dctc: CTC0 = dctc.pop(ctc)
    else: CTC0 = None
    # Update CTC instance
    CTC.setvar("ch", ctc_vars[0], "int")
    CTC.setvar("mtp", ctc_vars[1], "int")
    CTC.setvar("type", ctc_vars[2], "int")
    CTC.setvar("mformu", ctc_vars[3], "str")
    CTC._es = [(ctc_vars[1], 0.0)]
    # Rename gtsfiles
    itc = 0
    for idx, cache_i in enumerate(cache):
        prov_gts = cache_i[1]
        if is_provisional(prov_gts):
            new_gts, itc = rename_files(ctc, prov_gts, itc)
            itc_i = itc
            # update dtrack
            dtrack[cache_i[0]] = new_gts
            # update gtsfile in cache
            cache[idx][1] = new_gts
        else:
            itc_i = prov_gts.split(".")[-2]
        # weight
        weight = 1
        if CTC0 is not None: weight = max(CTC0.get_weight(itc_i), weight)
        # update CTC
        CTC.add_itc(itc_i, cache_i[2], cache_i[7], weight)
    CTC.find_minV0()
    # update with info of CTC0
    if CTC0 is not None:
        CTC._es = CTC0._es
        CTC._fscal = CTC0._fscal
        CTC._dics = CTC0._dics
        CTC._dicsfw = CTC0._dicsfw
        CTC._dicsbw = CTC0._dicsbw
        CTC._diso = CTC0._diso
        CTC._anh = CTC0._anh

    # update dctc
    dctc[ctc] = CTC
    # Print info of this CTC
    print_ctc_info(CTC)
    # Delete cache
    del cache
    # Return data
    return dtrack, dctc