Exemple #1
0
def data_target(target, dall, ltemp):
    # convert to list
    target = target2list(target)
    # initialize
    tot_V0 = 0.0
    tot_V1 = 0.0
    tot_Q = [1.0 for T in ltemp]
    tot_ANH = [1.0 for T in ltemp]
    trg_ANH = []
    # everything ok?
    if target is None: return None, None, None, tot_ANH, trg_ANH
    # read data
    for trg in target:
        ctc, itc = PN.name2data(trg)
        # Get key
        if itc is None: key = PN.struckey(ctc, "msho")
        else: key = PN.struckey(ctc, itc)
        # look for data
        try:
            V0, V1, pfns = dall["pfn"][key]
        except:
            raise Exc.NoData
        # add energies to total
        tot_V0 += V0
        tot_V1 += V1
        # update total partition function
        tot_Q = [q_i * q_j for q_i, q_j in zip(tot_Q, pfns)]
        # anharmonicity
        if ctc in dall["anh"].keys():
            anh = dall["anh"][ctc]
            tot_ANH = [r1 * r2 for r1, r2 in zip(tot_ANH, anh)]
            trg_ANH.append(trg)
    # return data
    return tot_V0, tot_V1, tot_Q, tot_ANH, trg_ANH
Exemple #2
0
def get_itargets(targets, dpath, dctc):
    '''
    get individual targets
    '''
    # Targets?
    if (len(targets) == 0) or ("*" in targets): targets = dpath.keys()

    # generate list of individual targets
    itargets = []
    for target in targets:
        ctc, itc = PN.name2data(target)
        # check ctc
        if ctc not in dctc.keys():
            print "   * '%s' not in '%s'" % (target, PN.IFILE1)
            print
            continue
        if ctc not in dpath.keys():
            print "   * '%s' not in '%s'" % (target, PN.IFILE3)
            print
            continue
        # list of itcs for the ctc
        itclist = [itc_i for itc_i, weight_i in dctc[ctc]._itcs]
        # add itc
        if itc is None:
            itargets += [
                PN.struckey(ctc, itc) for itc, weight in dctc[ctc]._itcs
            ]
        elif itc in itclist:
            itargets += [PN.struckey(ctc, itc)]
    return sorted(itargets)
Exemple #3
0
def get_targets(targets, dctc):
    # No targets selected
    if targets == [] or targets == "*":
        targets = [
            PN.struckey(ctc, itc) for ctc in dctc.keys()
            for itc, weight in dctc[ctc]._itcs
        ]
        targets.sort()
        return targets
    # Check targets
    itargets = []
    for target in targets:
        np = target.count(".")
        # a whole ctc is selected
        if np == 0:
            ctc = target
            if ctc not in dctc.keys(): continue
            itargets += [
                PN.struckey(ctc, itc) for itc, weight in dctc[ctc]._itcs
            ]
        # a specific itc
        elif np == 1:
            ctc, itc = target.split(".")
            if ctc not in dctc.keys(): continue
            itcs = [itc_i for itc_i, weight_i in dctc[ctc]._itcs]
            if itc not in itcs: continue
            itargets += [target]
    return itargets
Exemple #4
0
def conformer_contributions(ltemp, dpfn, ctc, itcs):
    # get contributions
    dchi = {}
    for itc, weight in itcs:
        V0i, V1i, Qi = dpfn[PN.struckey(ctc, itc)]
        V0, V1, Q = dpfn[PN.struckey(ctc, "msho")]
        # calculate contribution
        expvec = [np.exp(-(V1i - V1) / KB / T) for T in ltemp]
        chi = weight * np.array(Qi) / np.array(Q) * np.array(expvec)
        # save data
        dchi[itc] = chi
    return dchi
Exemple #5
0
def highlevel_mep(ctc, itc, keyHL_sp, software, dtesHL, dhighlvl, points):
    # key for dhighlvl
    keyHL_path = "%s.%s.path" % (ctc, itc)
    # initialize dictE
    dictE = {}
    # folder for calculation and file name
    TMP = PN.TMPHLi % PN.struckey(ctc, itc)
    # template and function for calculation
    tes = dtesHL.get(software, {}).get(ctc, None)
    spc_fnc = get_spc_fnc(software)
    # extra-arguments for spc_fnc
    clean = False  # do not delete files
    bhess = False  # do not calculate hessian (no needed actually)
    eargs = (tes, TMP, clean)
    # rst file
    rstfile = PN.get_rst(ctc, itc)
    tpath2, tcommon2, drst = ff.read_rst(rstfile)
    if drst == {}:
        yield None, keyHL_path, (None, None), None, "emptyrst"
        return
    ch, mtp, atonums, masses, mu = tcommon2
    symbols = fncs.atonums2symbols(atonums)
    # define points
    allpoints = sorted_points(drst, hess=False)
    for point in points:
        if point.startswith("auto"):
            auto, nbw, nfw = point.split("_")
            points = auto_points(drst, allpoints, int(nbw), int(nfw))
            break
    # loop in points
    for point_i in points:
        # name for files
        mname = "%s.%s.%s" % (ctc, itc, point_i)
        # get label
        label_i, s_i = find_label_in_rst(point_i, drst)
        # tuple for identify
        tuple_ID = (s_i, label_i)
        # not in drst?
        if label_i is None:
            yield point_i, keyHL_path, tuple_ID, None, "noinrst"
        # already calculated
        elif label_i in dhighlvl.get(keyHL_path, {}).keys():
            yield point_i, keyHL_path, tuple_ID, dhighlvl[keyHL_path][
                label_i], "already"
        # HL-calculation
        else:
            if s_i == 0.0:
                Ehl_sp = dhighlvl.get(keyHL_sp, None)
                if Ehl_sp is not None:
                    yield point_i, keyHL_path, tuple_ID, Ehl_sp, "saddlefromsp"
                    continue
            # get xcc from rst
            xms = drst[label_i][2]
            xcc = fncs.ms2cc_x(xms, masses, mu)
            # calculation
            try:
                out_spc = spc_fnc(xcc, symbols, bhess, mname, eargs)
                yield point_i, keyHL_path, tuple_ID, out_spc[4], "calculated"
            except:
                yield point_i, keyHL_path, tuple_ID, None, "fail"
Exemple #6
0
def get_V0HL_list(ctc, itcs, dhighlvl):
    list_V0HL = []
    for itc, weight in itcs:
        key = PN.struckey(ctc, itc)
        V0HL = dhighlvl.get(key, None)
        list_V0HL.append(V0HL)
    return list_V0HL
Exemple #7
0
def get_TSratios(ctc, itcs, V1TS, QTS, ltemp, dall):
    '''
    ratio = QTS(i)/QTS * exp(-dE(i) * beta)
    '''
    exception = Exc.LostConformer(Exception)
    tst_ratios = {}
    for itc, weight in itcs:
        key = PN.struckey(ctc, itc)
        if key not in dall["pfn"].keys():
            exception._var = key
            raise exception
        V0_i, V1_i, Q_i = dall["pfn"][key]
        ratio = [0.0 for T in ltemp]
        dE = (V1_i - V1TS)
        for idx, T in enumerate(ltemp):
            ratio[idx] = weight * Q_i[idx] / QTS[idx] * np.exp(-dE / KB / T)
        tst_ratios[itc] = ratio
    # get individual data
    return tst_ratios
Exemple #8
0
def get_corrfactors(ltemp, ctc, itcs, dall, tst_ratios):
    '''
    k^(x) = 1/(hbeta) * QTS/QR * exp(dE*beta) * GAMMA^(x)
    GAMMA^(x) = sum_i weight_i * QTS_i/QTS * exp(dE_i*beta) * Gamma_i =
              = sum_i ratioTST_i * Gamma_i
    '''
    cf = {}
    cf["cvt"] = {}
    cf["cvtzct"] = {}
    cf["cvtsct"] = {}
    cf["tstzct"] = {}
    cf["tstsct"] = {}
    for itc, weight in itcs:
        tsname = PN.struckey(ctc, itc)
        # individual coefs
        cvt = dall.get("cvt", {}).get(tsname, None)
        zct = dall.get("zct", {}).get(tsname, None)
        sct = dall.get("sct", {}).get(tsname, None)
        cagtst = dall.get("cagtst", {}).get(tsname, None)
        cagcvt = dall.get("cagcvt", {}).get(tsname, None)
        # correction
        try:
            cf_cvt = np.array(prod_list((cvt, )))
        except:
            cf_cvt = None
        try:
            cf_cvtzct = np.array(prod_list((zct, cvt, cagcvt)))
        except:
            cf_cvtzct = None
        try:
            cf_cvtsct = np.array(prod_list((sct, cvt, cagcvt)))
        except:
            cf_cvtsct = None
        try:
            cf_tstzct = np.array(prod_list((zct, cagtst)))
        except:
            cf_tstzct = None
        try:
            cf_tstsct = np.array(prod_list((sct, cagtst)))
        except:
            cf_tstsct = None
        # save
        cf["cvt"][itc] = cf_cvt
        cf["cvtzct"][itc] = cf_cvtzct
        cf["cvtsct"][itc] = cf_cvtsct
        cf["tstzct"][itc] = cf_tstzct
        cf["tstsct"][itc] = cf_tstsct
    # get total
    for rc in cf.keys():
        tot_cf = np.array([0.0 for T in ltemp])
        fail = False
        for itc, weight in itcs:
            tst_ratio = tst_ratios[itc]
            cf_i = cf[rc][itc]
            if cf_i is None:
                fail = True
                continue
            tot_cf += tst_ratio * cf_i
        if fail: cf[rc]["total"] = None
        else: cf[rc]["total"] = tot_cf
    return cf
Exemple #9
0
    if fstatus == -1: exit()
    # expand case
    (dof, hlf, plotfile), dlevel, software = case
    #-------------------------------------------------------#

    print "   Selected software: %s" % software
    print

    targets = get_targets(targets, dctc)
    # Print targets
    if len(targets) != 0:
        print "   High-level (HL) calculations will be carried out for:"
        ml = max([len(target) for target in targets] + [1])
        for target in targets:
            ctc, itc = PN.name2data(target)
            TMP = PN.TMPHLi % PN.struckey(ctc, itc)
            print "       %s (TMP folder: %s)" % ("%%-%is" % ml % target, TMP)
        print
    else:
        print "   No valid target(s) for High-level (HL) calculations"
        print
        return

    # read high-level output file
    dhighlvl = RW.read_highlevelfile(hlf)

    # loop over each target
    print "   Carrying out HL-calculations:"
    print
    ml = max([len(target) for target in targets])
    cols = ("target", "point identifier", "s value (bohr)", "E_HL (hartree)")
Exemple #10
0
def get_reaction_energies(TS, dchem, dof):
    '''
    * checks the reactions which involved the target transition
      state (TS) in order to extract V0 and V1 for reactants
      and products 
    '''
    # initialize variables
    reaction = None
    Eref = None
    V0R = None
    V0P = None
    V1R = None
    V1P = None
    GibbsR = None
    # select reaction
    ctc, itc = PN.name2data(TS)
    for rname in dchem.keys():
        Rs, ts, Ps = dchem[rname]
        ctc2, itc2 = PN.name2data(ts)
        if ctc == ctc2:
            reaction = rname
            if itc == itc2: break
    # no reaction?
    if reaction is None: return reaction, V0R, V0P, V1R, V1P, GibbsR
    # read dof
    dall = RW.read_alldata(dof)[0]
    # get energy from reaction
    Rs = dchem[reaction][0]
    Ps = dchem[reaction][2]
    # reactants
    if len(Rs) != 0:
        V0R, V1R = 0.0, 0.0
        for R in Rs:
            ctc, itc = PN.name2data(R)
            if itc is None: key = PN.struckey(ctc, "msho")
            else: key = R
            data = dall["pfn"].get(key, None)
            if data is None:
                V0R, V1R = None, None
                break
            V0, V1, pfns = data
            V0R += V0
            V1R += V1
            # Gibbs energy
            if key in dall["gibbs"].keys():
                gibbs = dall["gibbs"][key]
                if GibbsR is None: GibbsR = gibbs
                else: GibbsR = [gi + gj for gi, gj in zip(GibbsR, gibbs)]
    # products
    if len(Ps) != 0:
        V0P, V1P = 0.0, 0.0
        for P in Ps:
            ctc, itc = PN.name2data(P)
            if itc is None: key = PN.struckey(ctc, "msho")
            else: key = P
            data = dall["pfn"].get(key, None)
            if data is None:
                V0P, V1P = None, None
                break
            V0, V1, pfns = data
            V0P += V0
            V1P += V1
    # Output
    return reaction, V0R, V0P, V1R, V1P, GibbsR
Exemple #11
0
def calculate_QMSHO_for_ctc(Cluster, ltemp, dimasses={}, tupleHL=None):
    # expand data
    ctc = Cluster._ctc
    itcs = Cluster._itcs
    eslist = Cluster._es
    sptype = Cluster._type
    fscal = Cluster._fscal
    diso = Cluster._diso
    if tupleHL is None: dlevel, dhighlvl = False, {}
    else: dlevel, dhighlvl = tupleHL
    # get list of gts files and check existency
    gtsfiles = Cluster.gtsfiles()
    exception = Exc.LackOfGts(Exception)
    for gts in gtsfiles:
        if not os.path.exists(gts):
            exception._var = gts
            raise exception
    # DLEVEL
    if dlevel: V0HLs = get_V0HL_list(Cluster._root, itcs, dhighlvl)
    else: V0HLs = [None for itc, weight in itcs]
    # Generate Molecule instances
    molecules = []
    mass = None
    V0LLs = []
    for idx, gts in enumerate(gtsfiles):
        itc, weight = itcs[idx]
        # tuple with isotopic
        if itc in diso.keys(): iso = diso[itc]
        elif "*" in diso.keys(): iso = diso["*"]
        else: iso = None
        tiso = (iso, dimasses)
        # prepare and add molecule
        molecule = molecule_prep(gts, eslist, tiso, fscal)
        # save low-level
        V0LLs.append(float(molecule._V0))
        # apply dlevel
        if V0HLs[idx] is not None:
            molecule._V0 = V0HLs[idx]
            molecule._ccV1 = molecule._V0 + molecule._cczpe
        # save molecule
        molecules.append(molecule)
    # Get min of V0 and V1
    V0 = min([molecule._V0 for molecule in molecules])
    V1 = min([molecule._ccV1 for molecule in molecules])
    # String
    string = ""
    if dlevel:
        string += "    keyword --dlevel activated: applying High-Level energies to this STRUC\n"
        string += "\n"
        string += "        * Low-level (LL) and high-level (HL) energies (in hartree):\n"
        string += "\n"
        string += "          ---------------------------------------------------\n"
        string += "           conformer |     LL energy     |     HL energy     \n"
        string += "          ---------------------------------------------------\n"
        for idx, (itc, weight) in enumerate(itcs):
            if V0HLs[idx] is None: V0HL = "        -        "
            else: V0HL = "%+17.8f" % V0HLs[idx]
            string += "           %-9s | %+17.8f | %s \n" % (itc, V0LLs[idx],
                                                             V0HL)
        string += "          ---------------------------------------------------\n"
        string += "\n"
        if None in V0HLs:
            string += "        * FAIL!! Not enough data... DUAL-LEVEL will be omitted!\n"
            string += "\n"
            global WARNINGS
            WARNINGS.append("Dual-level was not applied to %s..." % ctc)
    string += add_iblank(PS.getstring_ctc(sptype, molecules, itcs, V0, V1), 4)
    # Compare masses
    for molecule in molecules:
        if mass is None: mass = molecule._mass
        # compare masses
        if abs(mass - molecule._mass) * AMU > EPS_AMU:
            global WARNINGS
            WARNINGS.append("Mass inconsistences in %s" % ctc)
            exception = Exc.DiffMassConf(Exception)
            exception._var = string
            raise exception
    # get partition functions for each molecule (and for CTC)
    dpfn = {}
    dgibbs = {}
    pfn_tot = [0.0 for T in ltemp]
    pfn_gibbs = [0.0 for T in ltemp]  # goes without sigma_rot
    for idx, molecule in enumerate(molecules):
        itc, weight = itcs[idx]
        V0_i, V1_i, pf_i, tuple_i = molecule_pfns(molecule, ltemp, sptype)
        key = PN.struckey(ctc, itc)
        # save individual pfn and gibbs free energy
        rotsigma = molecule._rotsigma
        gibbs_i = [
            V1_i - KB * T * np.log(q_j * rotsigma)
            for T, q_j in zip(ltemp, pf_i)
        ]
        dpfn[key] = (V0_i, V1_i, pf_i)
        dgibbs[key] = gibbs_i
        # update MSHO values
        rel_V1 = V1_i - V1
        for idx, T in enumerate(ltemp):
            beta = 1.0 / (T * KB)
            pfn_tot[idx] += weight * pf_i[idx] * np.exp(-rel_V1 * beta)
            pfn_gibbs[idx] += weight * pf_i[idx] * np.exp(
                -rel_V1 * beta) * rotsigma
        # add to string
        pf_PIB, pf_RR, pf_HO, pf_ele = tuple_i
        string += "    Conformation: %s\n" % itc
        string += add_iblank(molecule.info_string(), 7)
        string += "       partition functions (with regards electronic energy + ZPE):\n"
        string += add_iblank(
            PS.getstring_pfn1(ltemp, pf_i, pf_PIB, pf_RR, pf_HO, pf_ele), 7)
    # save Q_MSHO
    dpfn[PN.struckey(ctc, "msho")] = (V0, V1, pfn_tot)
    # save Gibbs_MSHO
    gibbs_tot = [V1 - KB * T * np.log(q_j) for T, q_j in zip(ltemp, pfn_gibbs)]
    dgibbs[PN.struckey(ctc, "msho")] = gibbs_tot
    # Add to string
    nconfs = sum([weight for name, weight in itcs])
    if nconfs > 1:
        dchi = conformer_contributions(ltemp, dpfn, ctc, itcs)
        string += "    MSHO partition function (QMSHO) with regard to min(V1):\n"
        string += "\n"
        string += add_iblank(PS.getstring_pfn2(ltemp, pfn_tot), 10)
        string += "    Individual contributions to QMSHO:\n"
        string += "\n"
        string += add_iblank(PS.string_contributions(itcs, ltemp, dchi), 10)
    else:
        dchi = {}
    return dpfn, dgibbs, string, dchi
Exemple #12
0
     global WARNINGS
     WARNINGS.append("Problem with %s" % ctc)
     if type(exception) == Exc.DiffMassConf:
         print_string(exception._var, NBS)
         continue
     if type(exception) == Exc.LackOfGts:
         error_line = "ERROR: Unable to find '%s'" % exception._var
         print "       " + error_line
         print
         continue
     raise exception
 # print info
 print_string(string, NBS)
 # anharmonicity??
 anhfile = dctc[ctc]._anh
 V0, V1, QMSHO = dpfn_i[PN.struckey(ctc, "msho")]
 ZPEMSHO = V1 - V0
 RATIO, string = deal_with_anh(anhfile, ltemp, ZPEMSHO, QMSHO)
 if string != "":
     print_string(string, 7)
     if RATIO is None:
         global WARNINGS
         WARNINGS.append("Anharmonicity IGNORED for %s" % ctc)
 # add data of weights to plot
 if plotfile is not None and dchi != {}:
     plotdata = {}
     plotdata.update(manage_data_for_plot_weights(ctc, ltemp, dchi))
     write_plotfile(plotfile, plotdata)
 # update dictionary
 dpfn.update(dpfn_i)
 dgibbs.update(dgibbs_i)