Ejemplo 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
Ejemplo n.º 2
0
def add_chem(data, dchem={}):
    set_completer(2)
    try:
        line = " ".join(data)
        if ":" not in line: raise Exception
        rname = line.split(":")[0]
        rformu = line.split(":")[1]
        # strip strings
        rname = rname.strip()
        rformu = rformu.strip()
        if rname == "": raise Exception
        if rformu == "": raise Exception
        if not is_string_valid(rname, extra="_"):
            print(IBLANK1 + "invalid name for the reaction (%s)!" % rname)
            return dchem
        # get reactant(s), TS and product(s)
        reac, ts, prod = rformu.split("-->")
        reac = [R.strip() for R in reac.split("+")]
        ts = ts.strip()
        prod = [P.strip() for P in prod.split("+")]
        if "" in reac or "??" in reac: reac = []
        if "" in prod or "??" in prod: prod = []
        if "" == ts or "??" == ts: ts = None
        dchem[rname] = (reac, ts, prod)
    except:
        print(IBLANK1 + "invalid line...")
    return dchem
Ejemplo n.º 3
0
def convert_udata_to_gts(ff, dtrack, dctc):
    global wrong1
    global wrong2
    global wrong5
    inconsistence = None
    print("         |--> %s" % ff)
    iblank = "         |    "
    # Is ff a folder or a file?
    if os.path.isdir(PN.UFOLDER + ff):
        # Get CTC name & list of files
        ctc = ff[:-1]
        files = sorted(
            [ff + filename for filename in os.listdir(PN.UFOLDER + ff)])
    else:
        # Get CTC name & list of files
        ctc = ff.split(".")[0]
        files = [ff]

    # valid name??
    if not is_string_valid(ctc, extra="_"):
        print(iblank + "invalid name!")
        print(iblank)
        wrong1 = True
        return dtrack, dctc, inconsistence

    # only files with known extension
    files = known_files(files)
    if len(files) == 0:
        print(iblank + "no valid file(s) or empty folder")
        print(iblank)
        wrong5 = True
        return dtrack, dctc, inconsistence

    in_dtrack = [ff in dtrack.keys() for ff in files].count(False) == 0
    if ctc in dctc.keys() and in_dtrack:
        print(iblank + "already registered")
        print(iblank)
        return dtrack, dctc, inconsistence

    # Convert to gts (provisional name)
    count = 1
    gtslist = []
    for ifile in files:
        # already in dtrack
        if ifile in dtrack.keys():
            print(iblank + "|--> %s (already in %s)" %
                  (ifile.split("/")[-1], PN.IFILE0))
            continue
        # provisional gts name
        while True:
            #prov_gts = PN.DIR1+"%s.gts_%i"%(ctc,count)
            prov_gts = PN.DIR1 + "%s.prov%i.gts" % (ctc, count)
            if not os.path.exists(prov_gts): break
            count += 1
        # read file and create gts
        status, E = userfile_to_gtsfile(PN.UFOLDER + ifile, prov_gts)
        if status == 0:
            print(iblank + "|--> %s (sth failed!)" % (ifile.split("/")[-1]))
            wrong2 = True
        elif status == -1:
            print(iblank + "|--> %s (Force Constant Matrix NOT FOUND!)" %
                  (ifile.split("/")[-1]))
            wrong2 = True
        else:
            print(iblank + "|--> %s (new)" % (ifile.split("/")[-1]))
        if E is not None: gtslist.append((E, ifile, prov_gts))
    print(iblank)

    # sort by energy (only those created)
    if len(gtslist) == 0: return dtrack, dctc, inconsistence
    gtslist.sort()

    # Rename gts files and update dtrack
    info, idx = [], 1
    generated_gtsfiles = []
    for E, ifile, prov_gts in gtslist:
        while True:
            gtsfile = ctc + ".%003i.gts" % (idx)
            idx += 1
            if gtsfile in dtrack.values(): continue
            if not os.path.exists(PN.DIR1 + gtsfile): break
        os.rename(prov_gts, PN.DIR1 + gtsfile)
        if os.path.exists(prov_gts + ".frozen"):
            os.rename(prov_gts + ".frozen", PN.DIR1 + gtsfile + ".frozen")
        info.append((E, ifile, gtsfile))
        dtrack[ifile] = gtsfile
        generated_gtsfiles.append(gtsfile)

    # consider all gts files and prepare CTC
    CTC, status, string, gtsfiles = gen_and_check_ctc(ctc, dtrack)
    # save ctc in dctc
    if status == 1: dctc[ctc] = CTC
    # save inconsistence
    if status == -1: inconsistence = (string, generated_gtsfiles)
    return dtrack, dctc, inconsistence
Ejemplo 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
Ejemplo n.º 5
0
def deal_with_file(esfile, dtrack, dctc):
    '''
    deal with an electronic structure (es) file inside UDATA/
    that is not inside a subfolder (i.e. a system with a single conformer)
    '''
    # Return data
    print("  - Reading file: %s" % (PN.UFOLDER + esfile))
    # Get CTC
    ctc = ".".join(esfile.split(".")[:-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

    # previously assignated?
    if esfile in dtrack:
        gtsfile = dtrack[esfile]
        print("    * gts file already assignated in %s! (%s)" %
              (PN.IFILE0, PN.DIR1 + gtsfile))
        if os.path.exists(PN.DIR1 + gtsfile):
            print("      - gts file already exists! Skipping generation\n")
            return dtrack, dctc
    else:
        gtsfile = None

    # GTS generation
    prov_gts, __ = temporal_gtsname(ctc)
    status, cache = userfile_to_gtsfile(esfile, prov_gts)
    remove = False
    if status == 0:
        print("    * [reading failed]")
        remove, WRONG[3] = True, True
    elif status == -1:
        print("    * [Fcc NOT found] ")
        remove, WRONG[4] = True, True
    elif cache[5] not in (0, 1):
        print("    * Incorrect number of imaginary frequencies in this CTC!")
        print("      n.imag. = %i" % cache[5])
        remove, WRONG[6] = True, True
    print()
    # Remove generated files
    if remove:
        remove_provisionals([prov_gts])
        return dtrack, dctc
    # rename files
    new_gts, itc = rename_files(ctc, prov_gts, 0, gtsfile)
    # Create CTC instance
    CTC = ClusterConf(ctc)
    V0, ch, mtp, nifreq, mformu, pg = cache[2:]
    CTC.setvar("ch", ch, "int")
    CTC.setvar("mtp", mtp, "int")
    CTC.setvar("type", nifreq, "int")
    CTC.setvar("mformu", mformu, "str")
    CTC._es = [(mtp, 0.0)]
    CTC.add_itc(itc, V0, pg)
    # update dictionaries
    dtrack[esfile] = new_gts
    dctc[ctc] = CTC
    print_ctc_info(CTC)
    return dtrack, dctc
Ejemplo n.º 6
0
def analyze_filefolder(ff, dtrack):

    global sthwrong
    global wrong1
    global wrong2

    iblank = "         "

    # Is ff a folder or a file?
    if os.path.isdir(PN.UFOLDER + ff):
        # Get CTC name & list of files
        ctc_name = ff[:-1]
        files = sorted(
            [ff + filename for filename in os.listdir(PN.UFOLDER + ff)])
        print "         |--> %s" % ff
        iblank += "     "
    else:
        # Get CTC name & list of files
        ctc_name = ff.split(".")[0]
        files = [ff]

    # valid name??
    if not is_string_valid(ctc_name, extra="_"):
        print iblank + "|--> %s (invalid name!)" % ff
        sthwrong = True
        wrong1 = True
        return None

    # only files with known extension
    files = known_files(files)

    # Convert to gts (provisional name)
    count = 1
    gtslist = []
    for ifile in files:
        # read file
        data = data_from_file(PN.UFOLDER + ifile)
        # print info
        if ifile in dtrack.keys():
            print iblank + "|--> %s (in %s)" % (ifile.split("/")[-1],
                                                PN.IFILE0)
            continue
        elif data is None:
            print iblank + "|--> %s (reading fails!)" % (ifile.split("/")[-1])
            sthwrong = True
            wrong2 = True
            continue
        else:
            print iblank + "|--> %s (new)" % (ifile.split("/")[-1])
        while True:
            prov_gts = PN.DIR1 + "%s.gts_%i" % (ctc_name, count)
            count += 1
            if not os.path.exists(prov_gts): break
        # expand data
        xcc, atonums, ch, mtp, E, gcc, Fcc, masses, clevel = data
        # get point group
        pgroup, rotsigma = get_pgs(atonums, masses, xcc)
        # write gts (provisional)
        if not os.path.exists(PN.DIR1): os.mkdir(PN.DIR1)
        write_gtsfile(xcc,
                      atonums,
                      ch,
                      mtp,
                      E,
                      pgroup,
                      rotsigma,
                      gcc,
                      Fcc,
                      prov_gts,
                      level=clevel)
        gtslist.append((E, ifile, prov_gts))

    # sort by energy (only those created)
    gtslist.sort()

    # Rename gts files
    info, idx = [], 1
    for E, ifile, prov_gts in gtslist:
        while True:
            ofile = ctc_name + ".%003i.gts" % (idx)
            idx += 1
            if ofile in dtrack.values(): continue
            if not os.path.exists(PN.DIR1 + ofile): break

        os.rename(prov_gts, PN.DIR1 + ofile)
        info.append((E, ifile, ofile))
    return info
Ejemplo n.º 7
0
def main(idata, status):

    global sthwrong
    global wrong1
    global wrong2
    global wrong3
    global wrong4
    sthwrong = False
    wrong1 = False
    wrong2 = False
    wrong3 = False
    wrong4 = False

    #---------------------------------------#
    # Read tracking and pif.ctc (if exist)  #
    #---------------------------------------#
    dtrack, (fname1, status1) = RW.read_track()
    (dctc, dimasses), (fname2, status2) = RW.read_ctc()
    if status1 == 1: print "  - File '%s' exists and is not empty\n" % fname1
    if status2 == 1: print "  - File '%s' exists and is not empty\n" % fname2

    #---------------------#
    # add data to imasses #
    #---------------------#
    if dimasses == {}: dimasses = dpt_im

    #---------------------------------------#
    # Read user data and generate gts files #
    #---------------------------------------#
    # files and folders
    if os.path.exists(PN.UFOLDER):
        files = [
            ff for ff in os.listdir(PN.UFOLDER)
            if not os.path.isdir(PN.UFOLDER + ff)
        ]
        folders = [
            ff + "/" for ff in os.listdir(PN.UFOLDER)
            if os.path.isdir(PN.UFOLDER + ff)
        ]
        # only files with known extension
        files = known_files(files)
    else:
        files = []
        folders = []

    if files + folders != []:
        print "  - Reading user's input data from %s" % PN.UFOLDER
        print
        print "    * Number of files inside %s:" % PN.UFOLDER
        print "      num_files   = %i " % (len(files))
        print "      num_folders = %i " % (len(folders))
        print

        print "      %s" % (PN.UFOLDER)
        for ff in sorted(files) + sorted(folders):
            thedata = analyze_filefolder(ff, dtrack)
            if thedata is None: continue
            for E, ifile, ffile in thedata:
                dtrack[ifile] = ffile
        print

        #--------------------------------#
        # Write tracking and check files #
        #--------------------------------#
        print "    * Writing/Updating file: %s" % PN.IFILE0
        print
        RW.write_track(dtrack)
        print "    * Checking existency of gts files listed in '%s'" % (
            PN.IFILE0)
        for ifile, ffile in dtrack.items():
            if not os.path.exists(PN.DIR1 + ffile):
                print "      '%s' NOT FOUND! (from '%s')" % (ffile, ifile)
                sthwrong = True
                wrong3 = True
                continue
        print
    else:
        print "  - Folder '%s' does not exist or is empty" % PN.UFOLDER
    print

    #--------------------------------#
    # READ gts files                 #
    #--------------------------------#
    try:
        gtsfiles = [
            PN.DIR1 + gts for gts in os.listdir(PN.DIR1)
            if gts.endswith(".gts")
        ]
    except:
        gtsfiles = []
    # folder exists?
    if ffchecking([PN.DIR1], []) == -1 or len(gtsfiles) == 0:
        print "  - Folder '%s' does not exist or is empty" % PN.DIR1
        print "    * Is '%s' empty?" % PN.UFOLDER
        print "    * Does an old '%s' file exist?" % PN.IFILE0
        print
        exit()

    # List gts files
    print "  - Reading gts files in %s" % PN.DIR1
    print
    print "    * num(gts) = %i\n" % len(gtsfiles)

    #--------------------------------------#
    # Analyze them and create molden files #
    #--------------------------------------#
    print "    * molden files will be generated at %s" % PN.DIR5
    data_gts = {}
    num_old, num_new = 0, 0
    for gtsfile in gtsfiles:
        ctc, gts_tuple, molden, bmolden = gts_data_and_molden(gtsfile)
        # check name
        if not is_string_valid(ctc, extra="_"):
            print "      * invalid name (%s)!! Ignoring data..." % ctc
            continue
        # save data
        data_gts[ctc] = data_gts.get(ctc, []) + [gts_tuple]
        # molden created?
        if bmolden: num_new += 1
        else: num_old += 1
    print
    print "      # of old molden files: %i" % num_old
    print "      # of new molden files: %i" % num_new
    print
    print

    #---------------------#
    # Info about each CTC #
    #---------------------#
    print "  - Listing Structures:"
    print
    ml = max([len(ctc) for ctc in data_gts.keys()])
    for ctc in sorted(data_gts.keys()):
        # data for this CTC
        ctc_itcs   = sorted([ (itc,pgroup2weight(pgroup)) \
                        for (itc,ch,mtp,E,nimag,mformu,pgroup)\
                        in   data_gts[ctc]])
        ctc_ch = data_gts[ctc][0][1]
        ctc_mtp = data_gts[ctc][0][2]
        ctc_type = data_gts[ctc][0][4]
        ctc_mformu = data_gts[ctc][0][5]
        ctc_fscal = 1.0
        ctc_es = [(ctc_mtp, 0.0)]  # electronic states
        ctc_dics = {}
        ctc_diso = {}
        ctc_anh = None
        # already exists?
        if ctc in dctc.keys():
            print("    * %%-%is (already in %%s)" % ml) % (ctc, PN.IFILE1)
            ctc_mformu = dctc[ctc]._mformu
            itcs = sorted(dctc[ctc]._itcs)
            ctc_ch = dctc[ctc]._ch
            ctc_mtp = dctc[ctc]._mtp
            ctc_es = dctc[ctc]._es
            ctc_type = dctc[ctc]._type
            ctc_fscal = dctc[ctc]._fscal
            ctc_dics = dctc[ctc]._dics
            ctc_anh = dctc[ctc]._anh
            ctc_diso = dctc[ctc]._diso
            # to add and to del
            itcs1 = [itc for itc, weight in ctc_itcs]
            itcs2 = [itc for itc, weight in itcs]
            to_add = [itc for itc in itcs1 if itc not in itcs2]
            to_del = [itc for itc in itcs2 if itc not in itcs1]
            for itc in to_add:
                print "      - new itc will be added  : %s" % itc
            for itc in to_del:
                print "      - old itc will be removed: %s" % itc
            # skip if nothing to do
            #if to_add == [] and to_del == []: continue
            # final list
            final_itcs = []
            for itc1, weight1 in ctc_itcs:
                append = True
                for itc2, weight2 in itcs:
                    if itc1 == itc2:
                        final_itcs.append([itc2, weight2])
                        append = False
                        break
                if append: final_itcs.append([itc1, weight1])
            ctc_itcs = final_itcs
        else:
            print("    * %%-%is" % ml) % ctc
        # checking
        isitok = check_gtss(ctc, data_gts)
        if isitok is False:
            sthwrong = True
            wrong4 = True
            continue
        # save data
        CTC = ClusterConf(ctc)
        CTC._mformu = ctc_mformu
        CTC._itcs = ctc_itcs
        CTC._ch = ctc_ch
        CTC._mtp = ctc_mtp
        CTC._es = ctc_es
        CTC._type = ctc_type
        CTC._fscal = ctc_fscal
        CTC._anh = ctc_anh
        CTC._diso = ctc_diso
        CTC._dics = ctc_dics
        dctc[ctc] = CTC
    print

    # check all ctc has gts files
    for ctc in dctc.keys():
        if ctc in data_gts.keys(): continue
        print "  - No gts file found for '%s'" % ctc
        answer = raw_input("    remove it from %s? (Y/n) " % PN.IFILE1).strip()
        print
        if answer.lower() in ["", "y", "yes"]: dctc.pop(ctc)

    print "  - Summary table of structures:"
    ls_struc(dctc)

    #----------------#
    # Write ctc file #
    #----------------#
    print "  - Writing/Updating information in %s" % PN.IFILE1
    RW.write_ctc(dctc, dimasses)
    print

    #----------------#
    # EVERYTHING OK? #
    #----------------#
    if sthwrong:
        print "  - ERROR: It seems that something did not go well"
        if wrong1: print "           * invalid name for some file/folder(s)!"
        if wrong2:
            print "           * reading process failed for a/some file(s) in %s!" % PN.UFOLDER
        if wrong3: print "           * gts file(s) not found!"
        if wrong4:
            print "           * inconsistences in conformational cluster(s)!"
    else:
        print "  - Everything seems to be OK"