def locate_ref_dmfile(path=None, filename="Densmat_SCF.txt", prop_key="HF_ref", jsonfile="DMfinder.json", ccpfile="CCParser.json"): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if os.path.isfile(os.path.join(path, "AB_MP2", filename)): abfile = os.path.join(path, "AB_MP2", filename) abfol = os.path.join(path, "AB_MP2") elif os.path.isfile(os.path.join(path, "AB_HF", filename)): abfile = os.path.join(path, "AB_HF", filename) abfol = os.path.join(path, "AB_HF") else: abfile = False json_filepath = jsonfile if ut.split_path(jsonfile)[0] else os.path.join(path, jsonfile) old = ut.load_js(json_filepath) if os.path.isfile(json_filepath) else {} new = {} if abfile: if not os.path.isfile(os.path.join(abfol, ccpfile)): find_and_parse(abfol) raw = ut.load_js(os.path.join(abfol, ccpfile)) coords = raw["frag_xyz"][-1][0] if type(coords) == str: coords = ut.vals_from_npz(os.path.join(abfol, coords), "frag_xyz")[-1][0].tolist() elconf, cnt = [], 0 while not elconf: # if empty because "read", get previous elconf = raw["elconf"][-1 - cnt][0] cnt += 1 elconf = elconf[0] if len(elconf) == 1 else elconf[1] bas = find_basfile(abfol) new[prop_key] = [elconf, abfile, coords, bas] if jsonfile: old.update(new) ut.dump_js(old, json_filepath) return new
def get_ref_int(path=None, n=0, rawfile="CCParser.json", linenumbers=True): """ """ if n != 0: raise ValueError("Only Ground-State energy") path = ut.deal_with_type(path, condition=None, to=os.getcwd) return get_non_fdet_int(ut.split_path(path)[0], rawfile=rawfile, linenumbers=linenumbers)
def elst_change_ref(path=None, n=0, rawfile="CCParser.json", linenumbers=True): """ """ if n != 0: raise ValueError("Only Ground-State energy") path = ut.deal_with_type(path, condition=None, to=os.getcwd) return get_elst_ref(path=path, rawfile=rawfile, linenumbers=linenumbers) - get_elst_int_sum_iso(path=path, rawfile=rawfile, linenumbers=linenumbers)
def elst_change_fdet(path=None, n=0, rawfile="CCParser.json", linenumbers=True): """/home/nico/.local/lib/python3.6/site-packages """ if n != 0: raise ValueError("Only Ground-State energy") path = ut.deal_with_type(path, condition=None, to=os.getcwd) return elst_fdet(path=path, rawfile=rawfile, linenumbers=linenumbers) - get_elst_int_sum_iso(path=path, rawfile=rawfile, linenumbers=linenumbers)
def kernel(path=None, n=0, kvar="HF_FDET", knvar="MP_FDET", dmfindfile="DMfinder.json", ccpfile="CCParser.json"): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if n != 0: raise NotImplementedError("Only GS so far!") kernel = get_kernel(path=path, n=n, kvar=kvar, knvar=knvar, dmfindfile=dmfindfile, ccpfile=ccpfile) return sum(kernel.values())
def get_non_fdet_terms(path=None, rawfile="CCParser.json", linenumbers=True): # cached=True is omitted, uses default, we can because other funcs checked """ """ ccdlog = logging.getLogger("ccd") ut.setupLogger() path = ut.deal_with_type(path, condition=None, to=os.getcwd) E = {} # AB if os.path.isdir(os.path.join(path, "AB_MP2")): raw = ut.load_js(os.path.join(path, "AB_MP2", rawfile)) E["HF_AB"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] E["E_2_AB"] =raw["mp_correction"][-1][0] if linenumbers else raw["mp_correction"][-1] elif os.path.isdir(os.path.join(path, "AB_HF")): raw = ut.load_js(os.path.join(path, "AB_HF", rawfile)) E["HF_AB"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] else: ccdlog.error("Could not find AB_MP2 nor AB_HF") # A if os.path.isdir(os.path.join(path, "A_MP2")): raw = ut.load_js(os.path.join(path, "A_MP2", rawfile)) E["HF_A"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] E["E_2_A"] =raw["mp_correction"][-1][0] if linenumbers else raw["mp_correction"][-1] elif os.path.isdir(os.path.join(path, "A_HF")): raw = ut.load_js(os.path.join(path, "A_HF", rawfile)) E["HF_A"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] else: ccdlog.error("Could not find A_MP2 nor A_HF") # A_gh if os.path.isdir(os.path.join(path, "A_MP2_gh")): raw = ut.load_js(os.path.join(path, "A_MP2_gh", rawfile)) E["HF_A_gh"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] E["E_2_A_gh"] =raw["mp_correction"][-1][0] if linenumbers else raw["mp_correction"][-1] elif os.path.isdir(os.path.join(path, "A_HF_gh")): raw = ut.load_js(os.path.join(path, "A_HF_gh", rawfile)) E["HF_A_gh"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] else: ccdlog.error("Could not find A_MP2_gh nor A_HF_gh") # B if os.path.isdir(os.path.join(path, "B_MP2")): raw = ut.load_js(os.path.join(path, "B_MP2", rawfile)) E["HF_B"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] E["E_2_B"] =raw["mp_correction"][-1][0] if linenumbers else raw["mp_correction"][-1] elif os.path.isdir(os.path.join(path, "B_HF")): raw = ut.load_js(os.path.join(path, "B_HF", rawfile)) E["HF_B"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] else: ccdlog.error("Could not find B_MP2 nor B_HF") # B_gh if os.path.isdir(os.path.join(path, "B_MP2_gh")): raw = ut.load_js(os.path.join(path, "B_MP2_gh", rawfile)) E["HF_B_gh"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] E["E_2_B_gh"] =raw["mp_correction"][-1][0] if linenumbers else raw["mp_correction"][-1] elif os.path.isdir(os.path.join(path, "B_HF_gh")): raw = ut.load_js(os.path.join(path, "B_HF_gh", rawfile)) E["HF_B_gh"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] else: ccdlog.error("Could not find B_MP2_gh nor B_HF_gh") return E
def elst_hf(path=None, rawfile="CCParser.json", linenumbers=True): # cached=True is omitted, uses default, we can because other funcs checked """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) raw = ut.load_js(os.path.join(path, rawfile)) J = raw["tot_coulomb"][-1][0] if linenumbers else raw["tot_coulomb"][-1] V_ne = raw["nuc_attr"][-1][0] if linenumbers else raw["nuc_attr"][-1] V_nn = raw["nuc_repu"][-1][0] if linenumbers else raw["nuc_repu"][-1] return J + V_ne + V_nn
def get_fdet_terms(path=None, rawfile="CCParser.json", linenumbers=True, MP=True): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) afol = os.path.join(path, find_emb_A(path=path)) bfol = os.path.join(path, find_emb_B(path=path)) d = get_fdet_terms_A(path=afol, rawfile=rawfile, linenumbers=linenumbers, MP=MP) d.update(**get_energy_B(path=bfol, rawfile=rawfile, linenumbers=linenumbers, MP=MP)) return d
def densdiff(path=None, n=0, k1="HF_FDET", k2="HF_ref", rawfile="DMfinder.json"): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if n != 0: raise NotImplementedError("Only GS so far!") d1, d2, gridpoints, weights = densities_on_gridpoints(path=path, n=n, k1=k1, k2=k2, rawfile=rawfile) return 0.5*np.dot(weights, np.absolute(d2 - d1))
def elst_ref(path=None, rawfile="CCParser.json", linenumbers=True): path = ut.deal_with_type(path, condition=None, to=os.getcwd) if os.path.isdir(os.path.join(path, "AB_MP2")): abfol = os.path.join(path, "AB_MP2") elif os.path.isdir(os.path.join(path, "AB_HF")): abfol = os.path.join(path, "AB_HF") else: raise FileNotFoundError("Could not find AB reference folder") return elst_hf(path=abfol, rawfile=rawfile, linenumbers=linenumbers)
def elst_fdet(path=None, rawfile="CCParser.json", linenumbers=True): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) afol = os.path.join(path, find_emb_A(path=path)) bfol = os.path.join(path, find_emb_B(path=path)) elst_A = elst_hf(path=afol, rawfile=rawfile, linenumbers=linenumbers) elst_B = elst_hf(path=bfol, rawfile=rawfile, linenumbers=linenumbers) fdet = get_fdet_terms_A(path=afol, rawfile=rawfile, linenumbers=linenumbers) elst_int = fdet["J"] + fdet["V_NN"] + fdet["AnucB"] + fdet["BnucA"] return elst_A + elst_B + elst_int
def get_fdet_int(path=None, n=0, rawfile="CCParser.json", linenumbers=True, lin=True, MP=True): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if n != 0: raise ValueError("Only Ground-State energy") E = group_fdet_terms(path=path, rawfile=rawfile, linenumbers=linenumbers, MP=MP) E_int = E["Delta_HF"] + E["elst_int"] E_int += E["nonel,lin"] if lin else E["nonel"] if MP: E_int += E["Delta_E_2"] return E_int
def elst_sum_iso(path=None, rawfile="CCParser.json", linenumbers=True, expansion=None): # cached=True is omitted, uses default, we can because other funcs checked """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) mono = expansion == "ME" if expansion is None: raise ValueError("You must provide the expansion") # A if os.path.isdir(os.path.join(path, "A_MP2" if mono else "A_MP2_gh")): afol = os.path.join(path, "A_MP2" if mono else "A_MP2_gh") elif os.path.isdir(os.path.join(path, "A_HF" if mono else "A_HF_gh")): afol = os.path.join(path, "A_HF" if mono else "A_HF_gh") else: raise FileNotFoundError("Could not find isolated A") # B if os.path.isdir(os.path.join(path, "B_MP2" if mono else "B_MP2_gh")): bfol = os.path.join(path, "B_MP2" if mono else "B_MP2_gh") elif os.path.isdir(os.path.join(path, "B_HF" if mono else "B_HF_gh")): bfol = os.path.join(path, "B_HF" if mono else "B_HF_gh") else: raise FileNotFoundError("Could not find isolated B") elst_A = elst_hf(path=afol, rawfile=rawfile, linenumbers=linenumbers) elst_B = elst_hf(path=bfol, rawfile=rawfile, linenumbers=linenumbers) intfol = False for exp in expansion.upper(), expansion.lower(): suspects = ["FT*{}".format(exp), "FnT*{}".format(exp)] for sus in suspects: globbed = gl.glob(os.path.join(path, sus)) if not globbed: continue for fol in globbed: if os.path.isdir(os.path.join(fol, "cy0")): intfol = os.path.join(fol, "cy0") break # Following part breaks all loops. else: continue break else: continue break if not intfol: raise FileNotFoundError("Could not find electrostatic interaction for sum of isolated fragments.\ \nGenerally this is in the 0-th cycle of freeze and thaw, please insert in \ for A embedded in isolated B") raw = ut.load_js(os.path.join(intfol, rawfile)) J = raw["J_sum_iso"][0] if type(raw["J_sum_iso"][0]) != list else raw["J_sum_iso"][0][0] # not trusting linenumbers on this one AnucB = raw["AnucB_sum_iso"][0] if type(raw["AnucB_sum_iso"][0]) != list else raw["AnucB_sum_iso"][0][0] BnucA = raw["BnucA_sum_iso"][0] if type(raw["BnucA_sum_iso"][0]) != list else raw["BnucA_sum_iso"][0][0] V_NN = raw["V_AB"][0][0] if linenumbers else raw["V_AB"][0] elst_int = J + AnucB + BnucA + V_NN return elst_A + elst_B + elst_int
def find_emb_A(path=None): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if os.path.isdir(os.path.join(path, "MP2_A")): afol = os.path.join(path, "MP2_A") elif os.path.isdir(os.path.join(path, "HF_A")): afol = os.path.join(path, "HF_A") else: cyfols = [i for i in os.listdir(path) if "cy" in i] n_iters = len(cyfols) - 1 afol = os.path.join(path, "cy{}".format(n_iters - n_iters%2)) return afol
def M_value(path=None, n=0, k1="HF_FDET", k2="HF_ref", rawfile="DMfinder.json"): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if n != 0: raise NotImplementedError("Only GS so far!") d1, d2, gridpoints, weights = densities_on_gridpoints(path=path, n=n, k1=k1, k2=k2, b_only=True, rawfile=rawfile) d = d2 - d1 pos = np.where(d > 0) d[pos] = 0 return -np.dot(weights, d)
def get_energy_B(path=None, rawfile="CCParser.json", linenumbers=True, find=False, MP=True): # cached=True is omitted, uses default, we can because other funcs checked """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if find: path = os.path.join(path, find_emb_B(path=path)) raw = ut.load_js(os.path.join(path, rawfile)) d = {} cycles = raw["cycle_energies"][-1][0] if linenumbers else raw["cycle_energies"][-1] if type(cycles) == str and re.match(".+npz", cycles): cycles = ut.vals_from_npz(os.path.join(path, cycles), "cycle_energies") d["HF_B"] = cycles[-1][-1] if MP: d["E_2_B"]= raw["mp_correction"][-1][0] if linenumbers else raw["mp_correction"][-1] return d
def raw_to_complex(path=None, rawfile="CCParser.json", raw_key="", n=0, linenumbers=True, first_only=False, group_values=False): """ Note ---- Simply copies a raw quantity as a complex quantity. Handles change of key, picks required "state" (n-th value of raw_quantity) Parameters ---------- path: str path in which to look for raw quantity rawfile: str the json file with raw quantities. default is ccp raw_key: str the key of the raw quantity in the json dictionary n: int which value to take. Uses fortran/human counting, default is 1 linenumbers: bool Whethere the quantity in the json file will have linenumbers. Default is True. e.g. json_dict[raw_key]=[[val1,line1],[val2,line2], ...] => use True json_dict[raw_key]=[[val1,val2,val3, ...] => use False Returns ------- dictionary item generally a float, potentially str. """ if raw_key == "": raise ValueError("you didn't specify the raw quantity's key") if n < 0: print( "CRITICAL - raw_to_complex: negative index. Most likely you didn't specify that a quantity is in ex_qs" ) raise ValueError #message in print to appear also when in "try" statements if first_only and n > 0: raise ValueError("You want a value only, but are asking for n > 0") path = ut.deal_with_type(path, condition=None, to=os.getcwd) rawfile = os.path.join(path, rawfile) raws = ut.load_js(rawfile) vals = group_values(raws[raw_key]) if group_values else raws[raw_key] to_return = vals[n][0] if linenumbers else vals[n] return to_return
def raw_to_complex(path=None, rawfile="CCParser.json", raw_key="", n=0, linenumbers=True, first_only=False, group_values=False, arr_type="arr"): """ Note ---- Simply copies a raw quantity as a complex quantity. Handles change of key, picks required "state" (n-th value of raw_quantity) Parameters ---------- path: str path in which to look for raw quantity rawfile: str the json file with raw quantities. default is ccp raw_key: str the key of the raw quantity in the json dictionary n: int which value to take. Uses fortran/human counting, default is 1 linenumbers: bool Whethere the quantity in the json file will have linenumbers. Default is True. e.g. json_dict[raw_key]=[[val1,line1],[val2,line2], ...] => use True json_dict[raw_key]=[[val1,val2,val3, ...] => use False Returns ------- dictionary item generally a float, potentially str. """ if not raw_key: raise ValueError("you didn't specify the raw quantity's key") if n < 0: raise ValueError("negative index. Most likely you didn't specify that a quantity is in ex_qs") if first_only and n > 0: raise ValueError("You want a value only, but are asking for n > 0") path = ut.deal_with_type(path, condition=None, to=os.getcwd) rawfile = os.path.join(path, rawfile) raws = ut.load_js(rawfile, cached=True) vals = raws[raw_key] if group_values and np.array([type(i) in [float,np.float32,np.float64] for i in vals]).all(): vals = group_values(raws[raw_key]) val = vals[n][0] if linenumbers else vals[n] if type(val) == str and re.match(".+npz", val): val = ut.vals_from_npz(os.path.join(path, val), raw_key)[n] if type(val) not in [bool, int, float, str]: val = deal_with_array(val, to=arr_type) return val
def densities_on_gridpoints(path=None, n=0, k1="HF_FDET", k2="HF_ref", b_only=False, rawfile="DMfinder.json"): """ """ ccdlog = logging.getLogger("ccd") ut.setupLogger() first_dec = lambda x: min([n for n, i in enumerate(np.format_float_positional(x).split(".")[-1]) if i != "0"]) check_int = lambda x: first_dec(round(x) - x) path = ut.deal_with_type(path, condition=None, to=os.getcwd) expansion = deduce_expansion(path=path) raw = ut.load_js(os.path.join(path, rawfile)) d1, gridpoints, weights = key_to_density(raw, k1, b_only=b_only, expansion=expansion) int1 = np.dot(weights, d1) ccdlog.info("{} integrates to {}".format(k1, int1)) assert check_int(int1) >= 3, "Non-integer integration for {}".format(k1) d2, *_ = key_to_density(raw, k2, gridpoints=gridpoints, weights=weights, b_only=b_only, expansion=expansion) int2 = np.dot(weights, d2) ccdlog.info("{} integrates to {}".format(k2, int2)) assert check_int(int2) >= 3, "Non-integer integration for {}".format(k2) return d1, d2, gridpoints, weights
def get_non_fdet_int(path=None, rawfile="CCParser.json", linenumbers=True): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) d = get_non_fdet_terms(path, rawfile=rawfile, linenumbers=linenumbers) E_int = {} E_int["HF"] = d["HF_AB"] - d["HF_A"] - d["HF_B"] try: E_int["HF_CP"] = d["HF_AB"] - d["HF_A_gh"] - d["HF_B_gh"] except KeyError: ccdlog.info("at least some HF-ghost value unavailable") try: E_int["MP"] = E_int["HF"] + d["E_2_AB"] - d["E_2_A"] - d["E_2_B"] except KeyError: ccdlog.info("at least some MP value unavailable") try: E_int["MP_CP"] = E_int["HF_CP"] + d["E_2_AB"] - d["E_2_A_gh"] - d["E_2_B_gh"] except KeyError: ccdlog.info("at least some MP-ghost value unavailable") return E_int
def get_kernel(path=None, n=0, kvar="HF_FDET", knvar="MP_FDET", dmfindfile="DMfinder.json", ccpfile="CCParser.json"): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) raw = ut.load_js(os.path.join(path, dmfindfile)) expansion = deduce_expansion(path=path) molb, dmb, mola, dma = read_key(raw, kvar, b_only=False) mol = mola + molb if expansion == "ME" else mola grid = get_grid(mol, obj=True) dA, dB = dm_on_grid(mola, dma, grid.coords), dm_on_grid(molb, dmb, grid.coords) molb_,dmb_nvar,mola_,dma_nvar = read_key(raw, knvar, b_only=False) fols = find_emb_A(path=path), find_emb_B(path=path) d = {"fde_Tfunct": "T", "fde_Xfunc": "X", "fde_Cfunc": "C", "fde_XCfunc": "XC"} kernel = {} for n in range(2): ccpdata = ut.load_js(os.path.join(fols[n], ccpfile)) kw = {v: ccpdata[k][-1][0].upper() for k, v in d.items() if k in ccpdata.keys()} kernel.update({"{}_{}".format(k, ["A", "B"][n]): calc_kernel(v, [dma, dmb][n], [dma_nvar, dmb_nvar][n], [dA, dB][n], [dB, dA][n], grid, [mola,molb][n]) for k, v in kw.items()}) if not kernel: raise BaseException("Somehow no kernel obtained!") return kernel
def get_fdet_terms_A(path=None, rawfile="CCParser.json", linenumbers=True, find=False, MP=True): # cached=True is omitted, uses default, we can because other funcs checked """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if find: path = os.path.join(path, find_emb_A(path=path)) raw = ut.load_js(os.path.join(path, rawfile)) d = {} d["J"] = raw["J_int"][0][0] if linenumbers else raw["J_int"][0] d["V_NN"] = raw["V_AB"][0][0] if linenumbers else raw["V_AB"][0] d["AnucB"] = raw["AnucB"][0][0] if linenumbers else raw["AnucB"][0] d["BnucA"] = raw["BnucA"][0][0] if linenumbers else raw["BnucA"][0] d["Exc_nad_upd"] = raw["Exc_nad"][1][0] if linenumbers else raw["Exc_nad"][1] d["Ts_nad_upd"] = raw["Ts_nad"][1][0] if linenumbers else raw["Ts_nad"][1] d["Exc_nad_ref"] = raw["Exc_nad"][0][0] if linenumbers else raw["Exc_nad"][0] d["Ts_nad_ref"] = raw["Ts_nad"][0][0] if linenumbers else raw["Ts_nad"][0] d["Delta_lin"] = raw["fde_delta_lin"][0][0] if linenumbers else raw["fde_delta_lin"][0] d["HF_A"] = raw["scf_energy"][-1][0] if linenumbers else raw["scf_energy"][-1] d["expansion"] = raw["fde_expansion"][-1][0] if linenumbers else raw["fde_expansion"][-1] if MP: d["E_2_A"] = raw["mp_correction"][-1][0] if linenumbers else raw["mp_correction"][-1] return d
def group_fdet_terms(path=None, rawfile="CCParser.json", linenumbers=True, MP=True): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) fdet = get_fdet_terms(path=path, rawfile=rawfile, linenumbers=linenumbers, MP=MP) ref = get_ref_terms(path=path, rawfile=rawfile, linenumbers=linenumbers) E = {} E["Delta_HF_A"] = fdet["HF_A"] - ref["HF_A"] if fdet["expansion"] == "ME" else \ fdet["HF_A"] - ref["HF_A_gh"] E["Delta_HF_B"] = fdet["HF_B"] - ref["HF_B"] if fdet["expansion"] == "ME" else \ fdet["HF_B"] - ref["HF_B_gh"] E["Delta_HF"] = E["Delta_HF_B"] + E["Delta_HF_A"] E["elst_int"] = fdet["V_NN"] + fdet["J"] + fdet["AnucB"] + fdet["BnucA"] E["nonel,lin"] = fdet["Ts_nad_ref"] + fdet["Exc_nad_ref"] + fdet["Delta_lin"] E["nonel"] = fdet["Ts_nad_ref"] + fdet["Exc_nad_ref"] if MP: E["Delta_E_2_A"] = fdet["E_2_A"] - ref["E_2_A"] if fdet["expansion"] == "ME" else \ fdet["E_2_A"] - ref["E_2_A_gh"] E["Delta_E_2_B"] = fdet["E_2_B"] - ref["E_2_B"] if fdet["expansion"] == "ME" else \ fdet["E_2_B"] - ref["E_2_B_gh"] E["Delta_E_2"] = E["Delta_E_2_A"] + E["Delta_E_2_B"] return E
def deduce_expansion(path=None): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) basename = ut.path_basename(path).upper() if "ME" in basename and "SE" not in basename: return "ME" elif "SE" in basename and "ME" not in basename: return "SE" try: grep = str(sp.check_output("grep -i expansion {}".format(os.path.join(path, "*", "*.out")), shell=True)).lower() except: try: grep = str(sp.check_output("grep -i expansion {}".format(os.path.join(path, "*", "*")), shell=True)).lower() except: raise FileNotFoundError("Could not determine expansion") if "me" in grep and "se" not in grep: return "ME" if "se" in grep and "me" not in grep: return "SE" else: raise FileNotFoundError("Could not determine expansion")
def __init__(self, joblist=None, levelnames=["A", "B", "basis", "calc"], parsername=None, parser_file=None, parserdict=None, parserargsdict=None, parserkwargsdict=None, reqsdict=None, funcdict=None, cqlist=None, ex_qs=None, ext="*.out", ignore="slurm*", to_console=True, to_log=False, logname="CCDatabase.log", printlevel=20): self.specs( dict(joblist=joblist, levelnames=levelnames, parser_file=parser_file, parsername=parsername, parserdict=parserdict, parserargsdict=parserargsdict, parserkwargsdict=parserkwargsdict, reqsdict=reqsdict, funcdict=funcdict, cqlist=cqlist, ex_qs=ex_qs, ext=ext, ignore=ignore, to_console=to_console, to_log=to_log, logname=logname, printlevel=printlevel)) if joblist == None: raise ValueError("You must specify joblist!!") else: joblist = ut.deal_with_type(joblist, condition=str, to=ccd.get_joblist) self.joblist = joblist self.levelnames = levelnames if parsername == None: # process then use other args parsername = (''.join([i for i in parsername if i.isalpha() ])).lower() # removes non-aplhabetic self.reqsdict = reqsdict self.funcdict = funcdict self.all_raws = [ r[1] if type(r) in [tuple, list] else r for r in set(ittl.chain.from_iterable(reqsdict.values())) ] self.parserdict = parserdict self.parser_file = parser_file self.parserargsdict = parserargsdict self.parserkwargsdict = parserkwargsdict # TODO: process else: # deduce other args self.reqsdict = { cq: megadict[parsername]["reqs"][cq] for cq in cqlist } self.funcdict = megadict[parsername]["funcdict"] self.all_raws = [ r[1] if type(r) in [tuple, list] else r for r in set(ittl.chain.from_iterable(reqsdict.values())) ] self.parserdict = { rq: megadict[parsername]["parser"] for rq in self.all_raws } #TODO: change self.parser_file = megadict[parsername]["parser_file"] self.parserargsdict = { parsername: [{ rq: megadict[parsername]["parser_args"][rq] for rq in self.all_raws }] } self.parserkwargsdict = { parsername: megadict[parsername]["parser_kwargs"] } self.cqlist = cqlist self.ex_qs = ex_qs if ex_qs else [ cq for cq in cqlist if cq in megadict["ex_qs"] ] self.ext = ext self.ignore = ignore self.to_console = to_console self.to_log = to_log self.logname = logname self.printlevel = printlevel
def locate_iso_dmfiles(path=None, filename="Densmat_SCF.txt", expansion="ME", prop_key="HF_iso_{}", jsonfile="DMfinder.json", ccpfile="CCParser.json"): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) if expansion == "ME": if os.path.isfile(os.path.join(path, "A_MP2", filename)): afile = os.path.join(path, "A_MP2", filename) afol = os.path.join(path, "A_MP2") elif os.path.isfile(os.path.join(path, "A_HF", filename)): afile = os.path.join(path, "A_HF", filename) afol = os.path.join(path, "A_HF") else: afile = False if os.path.isfile(os.path.join(path, "B_MP2", filename)): bfile = os.path.join(path, "B_MP2", filename) bfol = os.path.join(path, "B_MP2") elif os.path.isfile(os.path.join(path, "B_HF", filename)): bfile = os.path.join(path, "B_HF", filename) bfol = os.path.join(path, "B_HF") else: bfile = False elif expansion == "SE": if os.path.isfile(os.path.join(path, "A_MP2_gh", filename)): afile = os.path.join(path, "A_MP2_gh", filename) afol = os.path.join(path, "A_MP2_gh") elif os.path.isfile(os.path.join(path, "A_HF", filename)): afile = os.path.join(path, "A_HF_gh", filename) afol = os.path.join(path, "A_HF_gh") else: afile = False if os.path.isfile(os.path.join(path, "B_MP2_gh", filename)): bfile = os.path.join(path, "B_MP2_gh", filename) bfol = os.path.join(path, "B_MP2_gh") elif os.path.isfile(os.path.join(path, "B_HF_gh", filename)): bfile = os.path.join(path, "B_HF_gh", filename) bfol = os.path.join(path, "B_HF_gh") else: bfile = False else: raise NotImplementedError("Unknown expansion!! Only ME and SE so far!") json_filepath = jsonfile if ut.split_path(jsonfile)[0] else os.path.join(path, jsonfile) old = ut.load_js(json_filepath) if os.path.isfile(json_filepath) else {} new = {} # if "{}" in prop_key: # prop_key = prop_key.format(expansion) if afile: if not os.path.isfile(os.path.join(afol, ccpfile)): find_and_parse(afol) raw = ut.load_js(os.path.join(afol, ccpfile)) coords = raw["frag_xyz"][-1][0] if type(coords) == str: coords = ut.vals_from_npz(os.path.join(afol, coords), "frag_xyz")[-1][0].tolist() coords = [[i[0].replace("@", "X-")]+i[1:] for i in coords] elconf, cnt = [], 0 while not elconf: # if empty because "read", get previous elconf = raw["elconf"][-1 - cnt][0] cnt += 1 elconf = elconf[0] if len(elconf) == 1 else elconf[1] basA = find_basfile(afol) new[prop_key+"_A"] = [elconf, afile, coords, basA] if bfile: if not os.path.isfile(os.path.join(bfol, ccpfile)): find_and_parse(afol) raw = ut.load_js(os.path.join(bfol, ccpfile)) coords = raw["frag_xyz"][-1][0] if type(coords) == str: coords = ut.vals_from_npz(os.path.join(bfol, coords), "frag_xyz")[-1][0].tolist() coords = [[i[0].replace("@", "X-")]+i[1:] for i in coords] elconf, cnt = [], 0 while not elconf: # if empty because "read", get previous elconf = raw["elconf"][-1 - cnt][0] cnt += 1 elconf = elconf[0] if len(elconf) == 1 else elconf[1] basB = find_basfile(bfol) new[prop_key+"_B"] = [elconf, bfile, coords, basB] if (afile or bfile) and jsonfile: old.update(new) ut.dump_js(old, json_filepath) return new
def raw_atomic(path=None, atomstring="", n=0, rawfile="CCParser.json", raw_key="", first_only=True, frag=0, all_frag_avail=True, linenumbers=True, arr_type="arr"): """ Note ---- Returns an atomic quantity. Uses the geometry to deduce value index. Parameters ---------- path: str the folder path atomstring: str a string which determines which atoms to select. "O" => every oxygen "O1" => the first oxygen "O2-O4" => oxygen 2,3,4 "A1", "A2-A4": atom 1, atom 2,3,4 n: int the state (0=GS,1=ES1, ..) rawfile: str the name of the raw quantity json file raw_key: str the desired property's key first_only: bool if the property is only GS frag: int what fragment the counting should be in all_frag_avail: bool whether the property is available only for frag_0 or all linenumbers: bool whether raw quantities include the linenumber or not arr_typ: str/type desired type of array-like objects Returns ------- obj the property. if array-like, in the desired format """ if not raw_key: raise ValueError("you didn't specify the raw quantity's key") if n < 0: raise ValueError("negative index. Most likely you didn't specify that a quantity is in ex_qs") if first_only and n > 0: raise ValueError("You want a value only, but are asking for n > 0") path = ut.deal_with_type(path, condition=None, to=os.getcwd) rawfile = os.path.join(path, rawfile) raws = ut.load_js(rawfile, cached=True) if "frag_xyz" in raws.keys(): geomkey = "frag_xyz" elif "xyz" in raws.keys(): geomkey = "xyz" if frag: raise ValueError("fragment geometries not available") else: raise ValueError("no type of geometry (frag_xyz/xyz) available") geoms = raws[geomkey][0][0] if re.match(".+npz", geoms): npzfile = os.path.join(path,geoms) geoms = ut.vals_from_npz(npzfile, geomkey) else: if geomkey == "frag_xyz": geoms = [np.array(geom, dtype="object") for geom in geoms] else: geoms = np.array(geoms, dtype="object") if geomkey == "frag_xyz": frag_atoms = geoms[frag][:,0] all_atoms = np.vstack(*geoms)[:,0] shift = sum([len(geom) for geom in geoms[:frag]]) # N_atoms in fragments before frag else: frag_atoms = geoms[:,0] all_atoms = frag_atoms all_vals = raws[raw_key] atomlist = all_atoms if all_frag_avail else frag_atoms if len(all_vals)%len(atomlist) != 0: raise AssertionError("The total number of values available is not a multiple of the number of atoms!") idict = get_index_dict(atomstring, atomlist) valsdict = {} for name,idxs in idict.items(): idxs = [idx + n*len(atomlist) + shift for idx in idxs] # adjust for state and previous fragments try: vals = [all_vals[idx][0] if linenumbers else all_vals[idx] for idx in idxs] except: vals = [] for nv, val in enumerate(vals): if type(val) == str and re.match(".+npz", val): vals[nv] = ut.vals_from_npz(os.path.join(path, val), raw_key)[idxs[nv]] if type(vals[nv]) not in [bool, int, float]: vals[nv] = deal_with_array(vals[nv], to=arr_type) valsdict[name] = vals if not list(ittl.chain.from_iterable(valsdict.values())): raise BaseException("No item in atomstring returned a quantity value") to_return = valsdict.copy() for k,v in valsdict.items(): if not v: del to_return[k] return to_return
def get_ref_terms(path=None, rawfile="CCParser.json", linenumbers=True): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) return get_non_fdet_terms(ut.split_path(path)[0], rawfile=rawfile, linenumbers=linenumbers)
def get_elst_int_sum_iso(path=None, rawfile="CCParser.json", linenumbers=True): """ """ path = ut.deal_with_type(path, condition=None, to=os.getcwd) expansion = deduce_expansion(path=path) return elst_sum_iso(path=ut.split_path(path)[0], rawfile=rawfile, linenumbers=linenumbers, expansion=expansion)
def get_elst_ref(path=None, rawfile="CCParser.json", linenumbers=True): path = ut.deal_with_type(path, condition=None, to=os.getcwd) mainfol = ut.split_path(path)[0] return elst_ref(path=mainfol, rawfile=rawfile, linenumbers=linenumbers)