def init_guess_by_minao(mf, mol=None, ao2sub=None): if mol is None: mol = mf.mol if ao2sub is None: ao2sub = mf.ao2sub dm_ao = hf.init_guess_by_minao(mol) s = hf.get_ovlp(mol) dm_sub = tools.dm_ao2loc(dm_ao, s, ao2sub) return dm_sub
def init_guess_by_minao(mol): '''Generate initial guess density matrix based on ANO basis, then project the density matrix to the basis set defined by ``mol`` Returns: Density matrices, a list of 2D ndarrays for alpha and beta spins ''' dm = hf.init_guess_by_minao(mol) return numpy.array((dm * .5, dm * .5))
def init_guess_by_minao(mol): '''Generate initial guess density matrix based on ANO basis, then project the density matrix to the basis set defined by ``mol`` Returns: Density matrices, a list of 2D ndarrays for alpha and beta spins ''' dm = hf.init_guess_by_minao(mol) return numpy.array((dm*.5,dm*.5))
def get_init_guess(self, key=None): """ Compute an initial guess for the density matrix. ???? """ from pyscf.scf.hf import init_guess_by_minao if hasattr(self, 'mol'): dm = init_guess_by_minao(self.mol) else: dm = self.comp_dm() # the loaded ks orbitals will be used if dm.shape[0:2]==(1,1) and dm.shape[4]==1 : dm = dm.reshape((self.norbs,self.norbs)) return dm
def get_init_guess(self, key=None): """ Compute an initial guess for the density matrix. """ from pyscf.scf.hf import init_guess_by_minao if hasattr(self, 'mol'): dm = init_guess_by_minao(self.mol) else: dm = self.comp_dm() # the loaded ks orbitals will be used if dm.shape[0:2] == (1, 1) and dm.shape[4] == 1: dm = dm.reshape((self.norbs, self.norbs)) return dm
def init_guess_by_minao(mol, breaksym=BREAKSYM): '''Generate initial guess density matrix based on ANO basis, then project the density matrix to the basis set defined by ``mol`` Returns: Density matrices, a list of 2D ndarrays for alpha and beta spins ''' dm = hf.init_guess_by_minao(mol) dma = dmb = dm*.5 if breaksym: dma, dmb = _break_dm_spin_symm(mol, (dma, dmb)) return numpy.array((dma,dmb))
def nn_guess(mol, S, P0=None): """This method returns a guess for the density matrix of a molecule that can be used as input for scf calculations. Until now only the diagonal elements are estimated by the network, while the rest is Args: - atoms <list<str>>: a list of chemical symbols for the atoms in the molecule - S <np.array>: the density matrix of the molecule - P0 <np.array>: a (cheap) initial guess in which the network guess (which calculates only the diagonal) shall be placed. Returns: - <np.array>: a matrix containing the initial guess for the density matrix """ atoms = mol.species sess = tf.Session() #--- acquire model and setup graph --- model_database = normpath("./models") networks = {} for species in list(set(atoms)): model = np.load(join(model_database, species + ".npy"), encoding="bytes") nn = EluFixedValue(*model) nn.setup() networks[species] = nn sess.run(tf.global_variables_initializer()) #--- # if no init guess is given used pyscf default guess if P0 is None: P0 = init_guess_by_minao(mol.get_pyscf_molecule()) dm = [] for atom_index, atom in enumerate(atoms): inputs = [Descriptor.values(S, atoms, atom_index)] dm += (list(networks[atom].run(sess, inputs))) # overwrite the diag of P0 np.fill_diagonal(P0, dm) return P0
def init_guess_by_minao(mol, breaksym=BREAKSYM): '''Generate initial guess density matrix based on ANO basis, then project the density matrix to the basis set defined by ``mol`` Returns: Density matrices, a list of 2D ndarrays for alpha and beta spins ''' dm = hf.init_guess_by_minao(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.aoslice_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 )
def init_guess_by_minao(mol): '''Initial guess in terms of the overlap to minimal basis.''' dm = hf.init_guess_by_minao(mol) return _proj_dmll(mol, dm, mol)
def init_guess_by_minao(mol): dm = hf.init_guess_by_minao(mol) return numpy.array((dm*.5, dm*.5))
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()
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 )
#--- #--- 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") mf = hf.RHF(mol) mf.verbose = 1
def main(sample_index): msg.print_level = 2 msg.info("Hi. Measurements for butadien", 2) #--- fetch dataset and constants --- msg.info("Fetching sample " + str(sample_index) + " from datase", 2) dim = DIM molecules = np.load( "butadien/data/molecules.npy" ) S = np.load( "butadien/data/S.npy") P = np.load( "butadien/data/P.npy") dataset = make_butadien_dataset(molecules, S, P)[0] molecule = molecules[sample_index].get_pyscf_molecule() s = S[sample_index].reshape(dim, dim) s_normed = dataset.inverse_input_transform(s.reshape(1, -1)).reshape(1,-1) p = P[sample_index].reshape(dim, dim) #--- #--- fetch network --- msg.info("Fetching pretained network ", 2) graph = tf.Graph() structure, weights, biases = np.load( "butadien/data/network.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) msg.info("Neural network ", 1) p_nn = network.run(sess, s_normed).reshape(dim, dim) msg.info("McWheenys", 1) p_mcw1 = multi_mc_wheeny(p_nn, s, n_max=1) p_mcw5 = multi_mc_wheeny(p_nn, s, n_max=5) msg.info("Classics", 1) p_sap = hf.init_guess_by_atom(molecule) p_minao = hf.init_guess_by_minao(molecule) p_gwh = hf.init_guess_by_wolfsberg_helmholtz(molecule) #--- #--- Measureing & print --- outfile = "butadien/results/cube_" + str(sample_index) + "_" msg.info("Results Converged ", 1) cubegen.density(molecule, outfile + "converged.cube", p.astype("float64")) msg.info("Results NN: ", 1) cubegen.density(molecule, outfile + "nn.cube", p_nn.astype("float64")) msg.info("Results McWheeny 1: ",1) cubegen.density(molecule, outfile + "mcw1.cube", p_mcw1.astype("float64")) msg.info("Results McWheeny 5: ", 1) cubegen.density(molecule, outfile + "mcw5.cube", p_mcw5.astype("float64")) msg.info("Results SAP: ", 1) cubegen.density(molecule, outfile + "sap.cube", p_sap) msg.info("Results MINAO: ", 1) cubegen.density(molecule, outfile + "minao.cube", p_minao) msg.info("Results GWH: ", 1) cubegen.density(molecule, outfile + "gwh.cube", p_gwh)
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 )