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_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_all(fname, filename="Densmat_SCF.txt", ref="HF_ref", iso="HF_iso", FDET="HF_FDET", jsonfile="DMfinder.json", ccpfile="CCParser.json"): path = ut.split_path(fname)[0] 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 {} old.update(find_fdet_dmfiles(fname, filename=filename, prop_key=FDET, jsonfile="", ccpfile=ccpfile)) # jsonfile="" because we only write at the end old.update(find_ref_dmfile(fname, filename=filename, prop_key=ref, jsonfile="", ccpfile=ccpfile)) old.update(find_iso_dmfiles(fname, filename=filename, prop_key=iso, jsonfile="", ccpfile=ccpfile)) ut.dump_js(old, json_filepath)
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 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 find_matrix(fname, wildcardlist=[], jsonfile="CCParser.json", prop_key="ext_DM"): """ """ path = ut.split_path(fname)[0] if not wildcardlist: wildcardlist = ["Densmat_SCF.txt","Densmat_MP"] # TODO add other options elif type(wildcardlist) not in [tuple, list]: wildcardlist = [wildcardlist] 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 {} matching = [ut.split_path(fp)[1] for wc in wildcardlist for fp in gl.glob(os.path.join(path,wc))] old[prop_key] = matching # always overwrites values because if file is no longer there, pointer is useless ut.dump_js(old, json_filepath) return matching
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 read_and_reorder(dmf, coords, bas): """ """ dm = read_density(dmf) try: corr_path = os.environ.get("BAS_CORRESP") except: raise FileNotFoundError("Could not determine \"BAS_CORRESP\" environment variable.\ Please set it in your .bashrc and place there basis correspondence files.") corr_file = os.path.join(corr_path, ut.path_basename(bas).replace(".nwchem",".corr")) d = ut.load_js(corr_file, cached=False) if d["source"] == "qchem" and d["destination"] == "pyscf": order = bset.get_order(corr_file) elif d["source"] == "destination" and d["qchem"] == "pyscf": order = bset.get_rev_order(corr_file) else: raise ValueError("{} to {} is not a qchem <=> pyscf reordering!".format(d["source"], d["destination"])) atomlist = [i[0].replace("X-","") for i in coords] sort_arr = bset.get_sort_arr(atomlist, order) return bset.reorder(dm, sort_arr)
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 from_file(fname): specs = ut.load_js(fname) return DataCollector(**specs)
def find_fdet_dmfiles(fname, filename="Densmat_SCF.txt", prop_key="HF_FDET", jsonfile="DMfinder.json", ccpfile="CCParser.json"): """ """ path = ut.split_path(fname)[0] expansion = deduce_expansion(path=path) afol, bfol = find_emb_A(path=path), find_emb_B(path=path) if os.path.isfile(os.path.join(afol, filename)): afile = os.path.join(afol, filename) else: afile = False if os.path.isfile(os.path.join(bfol, filename)): bfile = os.path.join(bfol, filename) else: bfile = 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 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: npz = os.path.join(afol, coords) coords = ut.vals_from_npz(npz, "frag_xyz")[-1][0].tolist() if expansion == "SE": ghost = ut.vals_from_npz(npz, "frag_xyz")[-1][1] ghost[:,0] = "X-" + ghost[:,0] coords.extend(ghost.tolist()) elif expansion == "SE": ghost = raw["frag_xyz"][-1][1] coords.extend([["X-"+i[0]]+i[1:] for i in ghost]) 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, cnt = "read", 0 while coords == "read": # if empty because "read", get previous coords = raw["frag_xyz"][-1 - cnt][0] cnt += 1 if type(coords) == str: npz = os.path.join(bfol, coords) coords, cnt = "read", 0 while coords == "read": # if empty because "read", get previous coords = ut.vals_from_npz(npz, "frag_xyz")[-1 - cnt][0].tolist() cnt += 1 if expansion == "SE": frag_xyz = ut.vals_from_npz(npz, "frag_xyz") if frag_xyz.shape[1] == 1: # single fragment! probably B_gh frag_xyz = frag_xyz[0, 0] v_repl = np.vectorize(lambda x: x.replace("@", "X-")) frag_xyz[:,0] = v_repl(frag_xyz[:,0]) coords = frag_xyz.tolist() else: ghost = ut.vals_from_npz(npz, "frag_xyz")[-1][1] ghost[:,0] = "X-" + ghost[:,0] coords.extend(ghost.tolist()) elif expansion == "SE": frag_xyz = raw["frag_xyz"][-1] if len(frag_xyz[-1]) == 1: frag_xyz = frag_xyz[0, 0] coords = [[i[0].replace("@", "X-")]+i[1:] if "@" in i[0] else i for i in frag_xyz] else: ghost = raw["frag_xyz"][-1][1] coords.extend([["X-"+i[0]]+i[1:] for i in ghost]) 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 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 elst_int_sum_iso(file, jsfile="CCParser.json", with_ccp=True, linenumbers=True): mainfol = ut.split_path(file)[0] if ut.split_path(file)[0] else os.getcwd() # Get potentials if os.path.isfile(os.path.join(mainfol, "v_coul.txt")): v_j_file = os.path.join(mainfol, "v_coul.txt") expansion = False elif os.path.isfile(os.path.join(mainfol, "v_coulomb_ME.txt")): v_j_file = os.path.join(mainfol, "v_coulomb_ME.txt") expansion = "ME" elif os.path.isfile(os.path.join(mainfol, "v_coulomb_SE.txt")): v_j_file = os.path.join(mainfol, "v_coulomb_SE.txt") expansion = "SE" else: raise FileNotFoundError("Cannot find the Coulomb potential file") v_j = np.loadtxt(v_j_file) if os.path.isfile(os.path.join(mainfol, "v_nucA.txt")): v_a = np.loadtxt(os.path.join(mainfol, "v_nucA.txt")) elif os.path.isfile(os.path.join(mainfol, "v_nuc_1.txt")): v_a = np.loadtxt(os.path.join(mainfol, "v_nuc_1.txt")) else: raise FileNotFoundError("Cannot find file for v_A") if os.path.isfile(os.path.join(mainfol, "v_nucB.txt")): v_b = np.loadtxt(os.path.join(mainfol, "v_nucB.txt")) elif os.path.isfile(os.path.join(mainfol, "v_nuc_0.txt")): v_b = np.loadtxt(os.path.join(mainfol, "v_nuc_0.txt")) else: raise FileNotFoundError("Cannot find file for v_B") # Get expansion jsdata = ut.load_js(os.path.join(mainfol, jsfile)) if os.path.isfile( os.path.join(mainfol, jsfile)) else {} if "fde_expansion" in jsdata.keys(): if expansion: assert expansion == jsdata["fde_expansion"][0][0] or jsdata[ "fde_expansion"][0] # linenumber or not else: expansion = jsdata["fde_expansion"][0] if type(expansion) == list: if not linenumbers: warnings.warn( "get_elst_int_sum: You are adding values without \"linenumbers\"\ in a json file which has them. This can lead to issues in reading data.\ Consider passing \"linenumbers=True\", dummy linenumbers will be added" ) expansion = expansion[0] elif linenumbers: warnings.warn( "get_elst_int_sum: You are adding values with \"linenumbers\"\ in a json file which does not have them. This can lead to issues in reading data.\ Consider passing \"linenumbers=False\"") elif with_ccp: import CCParser as ccp parsed = ccp.Parser(file, to_json=True, json_file=jsfile, to_console=False, overwrite_file=False, overwrite_vals=False) # V_NN = parsed.results.V_AB.get_last() # check if expansion: assert expansion == parsed.results.fde_expansion[0] else: expansion = parsed.results.fde_expansion[0] else: try: s = str(sp.check_output( "grep -i expansion {}".format(file))).upper() expansion = "ME" if "ME" in s else "SE" except: raise FileNotFoundError("Could not determine expansion") # s = str(sp.check_output("grep \"Nuc_A <-> Nuc_B\" {}".format(file))) # V_NN = float(re.search("-?\d+\.\d+",s).group()) # # if "V_AB" in jsdata.keys: # V_NN = jsdata["V_AB"][0] # if type(V_NN) == list: # value and linenumber # if not linenumbers: # warnings.warn("get_elst_int_sum: You are adding values without \"linenumbers\"\ # in a json file which has them. This can lead to issues in reading data.\ # Consider passing \"linenumbers=True\", dummy linenumbers will be added") # V_NN = V_NN[0] # elif linenumbers: # warnings.warn("get_elst_int_sum: You are adding values with \"linenumbers\"\ # in a json file which does not have them. This can lead to issues in reading data.\ # Consider passing \"linenumbers=False\"") # # Get DMs if os.path.isfile(os.path.join(mainfol, "Densmat_A.txt")): dmf_A = os.path.join(mainfol, "Densmat_A.txt") elif os.path.isfile( os.path.join(mainfol, "frag_1_HF_{}.txt".format(expansion))): dmf_A = os.path.join(mainfol, "frag_1_HF_{}.txt".format(expansion)) if os.path.isfile(os.path.join(mainfol, "Densmat_B.txt")): dmf_B = os.path.join(mainfol, "Densmat_B.txt") elif os.path.isfile( os.path.join(mainfol, "frag_0_HF_{}.txt".format(expansion))): dmf_B = os.path.join(mainfol, "frag_0_HF_{}.txt".format(expansion)) dm_A = np.loadtxt(dmf_A, dtype=np.float64, skiprows=1 if has_header(file) else 0) dm_B = np.loadtxt(dmf_B, dtype=np.float64, skiprows=1 if has_header(file) else 0) lA, lB = dm_A.shape[0], dm_B.shape[0] if is_square(lA): nbasA = int(np.sqrt(lA)) dm_A = 2 * dm_A.reshape([nbasA, nbasA]) # NB supposes only alpha!!! elif is_square(lA / 2): nbasA = int(np.sqrt(lA / 2)) dm_A = dm_A.reshape([2, nbasA, nbasA]).sum(axis=0) if is_square(lB): nbasB = int(np.sqrt(lB)) dm_B = 2 * dm_B.reshape([nbasB, nbasB]) # NB supposes only alpha!!! elif is_square(lB / 2): nbasB = int(np.sqrt(lB / 2)) dm_B = dm_B.reshape([2, nbasB, nbasB]).sum(axis=0) v_j = v_j.reshape([nbasA, nbasA]) v_b = v_b.reshape([nbasA, nbasA]) v_a = v_a.reshape([nbasB, nbasB]) J = np.einsum('ab,ba', v_j, dm_A) AnucB = np.einsum('ab,ba', v_b, dm_A) BnucA = np.einsum('ab,ba', v_a, dm_B) if linenumbers: J = [J, -1] AnucB = [AnucB, -1] BnucA = [BnucA, -1] jsdata.update( dict(J_sum_iso=[J], AnucB_sum_iso=[AnucB], BnucA_sum_iso=[BnucA])) # Using same structure as ccp ut.dump_js(jsdata, os.path.join(mainfol, jsfile))