Esempio n. 1
0
def init_guess_by_1e(mol, breaksym=True):
    dm = hf.init_guess_by_1e(mol)
    dma = dmb = dm*.5
    if breaksym:
        #remove off-diagonal part of beta DM
        dmb = numpy.zeros_like(dma)
        for b0, b1, p0, p1 in mol.offset_nr_by_atom():
            dmb[p0:p1,p0:p1] = dma[p0:p1,p0:p1]
    return numpy.array((dma,dmb))
def do_analysis(dataset, molecules, s_raw, log_file):

    #--- calculate guesses ---
    msg.info("Calculating guesses ...",2)

    p_1e = np.array([
        hf.init_guess_by_1e(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_sap = np.array([
        hf.init_guess_by_atom(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_minao = np.array([
        hf.init_guess_by_minao(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_gwh = np.array([
        hf.init_guess_by_wolfsberg_helmholtz(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    #--- 

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ H_Core +++++\n")
    msg.info("Results H_Core: ", 1)
    measure_and_display(
        p_1e.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ SAP +++++\n")
    msg.info("Results SAP: ", 1)
    measure_and_display(
        p_sap.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ MINAO +++++\n")
    msg.info("Results MINAO: ", 1)
    measure_and_display(
        p_minao.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ GWH +++++\n")
    msg.info("Results GWH: ", 1)
    measure_and_display(
        p_gwh.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )
Esempio n. 3
0
def not_used():
    msg.info("Netowrk Analysis!", 2)

    #--- fetching the molecules ---
    msg.info("Fetching the molecules", 2)

    def grep_molecule(input_file):
        import re

        with open(input_file) as f:

            molecule = re.search(r"\$molecule.*\$end", f.read(), re.DOTALL)
            if molecule is None:
                raise ValueError("No molecule found in " + f.name)
            else:
                molecule = molecule.group(0)

                # cut out geometries
                geometries = molecule.splitlines()[2:-1]

        # from geometries take the species and positions
        species, positions = [], []
        for line in geometries:
            splits = line.split()
            species.append(splits[0])
            positions.append(splits[1:])

        return species, positions

    def fetch_molecules(folder):

        files = [file for file in listdir(folder) if ".inp" in file]

        for i, file in enumerate(files):

            msg.info("Fetching: " + str(i + 1) + "/" + str(len(files)))

            mol = Molecule(*grep_molecule(join(folder, file)))

            mol.basis = "sto-3g"

            yield mol

    molecules = list(fetch_molecules("butadien/data"))
    #---

    #--- do scf ---
    msg.info("Running the SCF calculations", 2)
    iterations = []
    for i, molecule in enumerate(molecules):

        mol = molecule.get_pyscf_molecule()

        msg.info("Calculating: " + str(i + 1) + "/200.")

        # assemble pyscf initial guesses
        P_1e = hf.init_guess_by_1e(mol)
        P_atom = hf.init_guess_by_atom(mol)
        P_minao = hf.init_guess_by_minao(mol)

        # nn guess
        S = hf.get_ovlp(mol).reshape(1, dim**2)
        P_NN = network.run(sess, S).reshape(dim, dim)

        iterations_molecule = []
        for guess in [P_1e, P_atom, P_minao, P_NN]:  #, P_NN]:

            mf = hf.RHF(mol)
            mf.verbose = 1
            mf.kernel(dm0=guess)
            iterations_molecule.append(mf.iterations)

        iterations.append(iterations_molecule)

    iterations = np.array(iterations)
    #---

    #--- statistics ---
    fig, axes = plt.subplots(2, 2)

    bins = 1  # todo hier kann man auch ein array angeben

    for i, name in enumerate(['1e', 'atom', 'P_minao']):

        hist, bins = np.histogram(iterations[:, i])
        center = (bins[:-1] + bins[1:]) / 2
        axes[i].bar(center, hist, label=name)

    plt.legend()
    plt.show()
Esempio n. 4
0
def init_guess_by_1e(mol):
    dm = hf.init_guess_by_1e(mol)
    return numpy.array((dm*.5,dm*.5))
Esempio n. 5
0
File: uhf.py Progetto: armunoz/pyscf
def init_guess_by_1e(mol):
    dm = hf.init_guess_by_1e(mol)
    return numpy.array((dm * .5, dm * .5))
Esempio n. 6
0
def do_analysis(network_path, dataset, molecules, s_raw, log_file):

    #--- fetch network ---
    msg.info("Fetching pretained network ", 2)
    graph = tf.Graph()
    structure, weights, biases = np.load(
        network_path, 
        encoding="latin1"
    )[:3]

    with graph.as_default():
        sess = tf.Session()
        network = EluFixedValue(structure, weights, biases)
        network.setup()
        sess.run(tf.global_variables_initializer())
    #---

    #--- calculate guesses ---
    msg.info("Calculating guesses ...",2)
    
    msg.info("Neural network ", 1)
    p_nn = network.run(sess, dataset.testing[0])

    msg.info("McWheenys", 1)
    p_batch = make_matrix_batch(p_nn, DIM, True)
    p_mcw1 = np.array(list(map(lambda x: multi_mc_wheeny(x[0], x[1], n_max=1), zip(p_batch, s_raw))))
    p_mcw5 = np.array(list(map(lambda x: multi_mc_wheeny(x[0], x[1], n_max=5), zip  (p_batch, s_raw))))

    msg.info("Classics", 1)
    p_1e = np.array([
        hf.init_guess_by_1e(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_sap = np.array([
        hf.init_guess_by_atom(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_minao = np.array([
        hf.init_guess_by_minao(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_gwh = np.array([
        hf.init_guess_by_wolfsberg_helmholtz(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    #--- 

    msg.info("Results NN: ", 1)
    with open(log_file, "a+") as f:
        f.write("\n+++++ Plain NN +++++\n")
    measure_and_display(
        p_nn, dataset, molecules, True, log_file, s=s_raw
    )
    
    with open(log_file, "a+") as f:
        f.write("\n\n+++++ McW 1 +++++\n")
    msg.info("Results McWheeny 1: ",1)
    measure_and_display(
        p_mcw1.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )
    
    with open(log_file, "a+") as f:
        f.write("\n\n+++++ McW 5 +++++\n")
    msg.info("Results McWheeny 5: ", 1)
    measure_and_display(
        p_mcw5.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ H_Core +++++\n")
    msg.info("Results H_Core: ", 1)
    measure_and_display(
        p_1e.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ SAP +++++\n")
    msg.info("Results SAP: ", 1)
    measure_and_display(
        p_sap.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ MINAO +++++\n")
    msg.info("Results MINAO: ", 1)
    measure_and_display(
        p_minao.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ GWH +++++\n")
    msg.info("Results GWH: ", 1)
    measure_and_display(
        p_gwh.reshape(-1, DIM**2), dataset, molecules, False, log_file, s=s_raw
    )
Esempio n. 7
0
def main(molecule_type):

    msg.print_level = 2

    msg.info("Hi. Measurements for " + molecule_type, 2)

    #--- fetch dataset and constants ---
    msg.info("Fetching dataset", 2)
    dataset_triu, molecules = fetch_dataset(molecule_type, True)
    dataset, _ = fetch_dataset(molecule_type, False)
    dim = DIM[molecule_type]
    #---

    #--- fetch network ---
    msg.info("Fetching pretained network ", 2)
    graph = tf.Graph()
    structure, weights, biases = np.load(
        "cc2ai/" + molecule_type + "/network_" + molecule_type + ".npy", 
        encoding="latin1"
    )
    #---

    with graph.as_default():
        sess = tf.Session()
        network = EluFixedValue(structure, weights, biases)
        network.setup()
        sess.run(tf.global_variables_initializer())
    #---

    #--- calculate guesses ---
    msg.info("Calculating guesses ...",2)
    s_raw = make_matrix_batch(dataset_triu.inverse_input_transform(dataset_triu.testing[0]), dim, True)
    
    msg.info("Neural network ", 1)
    p_nn = network.run(sess, dataset_triu.testing[0])

    msg.info("McWheenys", 1)
    p_batch = make_matrix_batch(p_nn, dim, True)
    p_mcw1 = np.array(list(map(lambda x: multi_mc_wheeny(x[0], x[1], n_max=1), zip(p_batch, s_raw))))
    p_mcw5 = np.array(list(map(lambda x: multi_mc_wheeny(x[0], x[1], n_max=5), zip  (p_batch, s_raw))))

    msg.info("Classics", 1)
    p_1e = np.array([
        hf.init_guess_by_1e(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_sap = np.array([
        hf.init_guess_by_atom(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_minao = np.array([
        hf.init_guess_by_minao(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    p_gwh = np.array([
        hf.init_guess_by_wolfsberg_helmholtz(mol.get_pyscf_molecule()) for mol in molecules[1]
    ])
    #--- 

    #--- Measureing & print ---
    log_file = "cc2ai/" + molecule_type + "/pretrained_" + str(date.today()) + ".log"
    with open(log_file, "a+") as f:
        f.write("##### Analysis of " + str(datetime.now()) + " #####\n")
    msg.info("Results NN: ", 1)
    with open(log_file, "a+") as f:
        f.write("\n+++++ Plain NN +++++\n")
    measure_and_display(
        p_nn, dataset_triu, molecules, molecule_type, True, log_file
    )
    
    with open(log_file, "a+") as f:
        f.write("\n\n+++++ McW 1 +++++\n")
    msg.info("Results McWheeny 1: ",1)
    measure_and_display(
        p_mcw1.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file
    )
    
    with open(log_file, "a+") as f:
        f.write("\n\n+++++ McW 5 +++++\n")
    msg.info("Results McWheeny 5: ", 1)
    measure_and_display(
        p_mcw5.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ H_Core +++++\n")
    msg.info("Results H_Core: ", 1)
    measure_and_display(
        p_1e.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ SAP +++++\n")
    msg.info("Results SAP: ", 1)
    measure_and_display(
        p_sap.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ MINAO +++++\n")
    msg.info("Results MINAO: ", 1)
    measure_and_display(
        p_minao.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file
    )

    with open(log_file, "a+") as f:
        f.write("\n\n+++++ GWH +++++\n")
    msg.info("Results GWH: ", 1)
    measure_and_display(
        p_gwh.reshape(-1, dim**2), dataset, molecules, molecule_type, False, log_file
    )
             
molecules = fetch_molecules("butadien/data")
#---

#--- do scf ---
msg.info("Running the SCF calculations", 2)
iterations = []
for i, molecule in enumerate(molecules):
    
    mol = molecule.get_pyscf_molecule()
    
    
    msg.info("Calculating: " + str(i + 1) + "/200.")
    
    # assemble pyscf initial guesses
    P_1e = hf.init_guess_by_1e(mol)
    P_atom = hf.init_guess_by_atom(mol)
    P_minao = hf.init_guess_by_minao(mol)
    
    print("die normalen sind fertig")

    # nn guess
    S = hf.get_ovlp(mol).reshape(1, dim**2)
    P_NN = network.run(sess, S).reshape(dim, dim).astype('float64')


    print("P_NN fertig")
    
    iterations_molecule = []
    for guess in [P_1e, P_atom, P_minao, P_NN]:#, P_NN]:
        print("SCF")