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_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 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 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 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 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))
def save_specs(self, fname): specs = self.specs ut.dump_js(specs, fname)