def __update_many_mol2(inp, suffix, wfn_suffix, g09_suffix, input_type, charges): """ updates many mol2 files at the same time """ from a2mdio.qm import WaveFunction, GaussianLog from a2mdio.molecules import UNITS_TABLE if input_type == 'json': with open(inp) as f: input_contents = json.load(f) elif input_type == 'csv': with open(inp) as f: input_contents = [i.strip() for i in f.readlines()] else: print("unknown format. use either csv or json") sys.exit() for name in input_contents: mm = Mol2(name + '.mol2') wfn = name + wfn_suffix gaussian_log = name + g09_suffix wfn_instance = WaveFunction(file=wfn, prefetch_dm=False, verbose=False) glog = GaussianLog(file=gaussian_log, method='', charges=charges, verbose=False) gdict = glog.read() mm.coordinates = wfn_instance.get_coordinates() * UNITS_TABLE['au']['angstrom'] mm.charges = np.array(gdict['charges'], dtype='float64') mm.write(name + suffix)
def __getitem__(self, item): wfn = WaveFunction(file=self.idx[item], verbose=False) centers = wfn.cent.tolist() sym = wfn.sym.tolist() exp = wfn.exp.tolist() r = torch.tensor(wfn.get_coordinates(), dtype=torch.float) dm = torch.tensor(wfn.density_matrix, dtype=torch.float) return r, centers, exp, sym, dm
def __init__(self, file, verbose=False, prefetch_dm=False, batch_size=10000): WaveFunction.__init__(self, file=file, verbose=verbose, prefetch_dm=True, batch_size=batch_size) if prefetch_dm: self.calculate_density_matrix()
def admin_sources(mm, reference, reference_type): if reference_type == 'wfn': reference_d = WaveFunction(verbose=False, file=reference, batch_size=10000) elif reference_type == 'a2md': reference_d = a2md_from_mol(mm) with open(reference) as f: reference_d.read(json.load(f)) else: logger.error("use either wfn or a2md as reference format") sys.exit() return reference_d
def __update_mol2(name, wfn, gaussian_log, output, charges): """ updates a mol2 file using npa charges from gaussian output and coordinates of a wfn """ from a2mdio.qm import WaveFunction, GaussianLog from a2mdio.molecules import UNITS_TABLE mm = Mol2(name) wfn_instance = WaveFunction(file=wfn, prefetch_dm=False, verbose=False) glog = GaussianLog(file=gaussian_log, method='', charges=charges, verbose=False) gdict = glog.read() mm.coordinates = wfn_instance.get_coordinates() * UNITS_TABLE['au']['angstrom'] mm.charges = np.array(gdict['charges'], dtype='float64') mm.write(output)
def __store_wfn(name, out, save_dm, save_coeff, program): wfn = WaveFunction.from_file(name, program=program) f = WaveFunctionHDF5(out, mode='w-') f.add(name.replace('.g09', ''), wfn, save_dm=save_dm, save_coeff=save_coeff) f.close()
def dx_add_wfn_charge(name, dx, charge, output): wfn = WaveFunction(file=name, verbose=False, batch_size=1000000) convert2au = lambda x: x*UNITS_TABLE['angstrom']['au'] r3 = UNITS_TABLE['angstrom']['au'] ** 3 fun = lambda x : -wfn.eval(convert2au(x))*r3 dx1 = Volume(filename=dx) dx1.read() dx1.eval(fun) dx2 = Volume(filename=dx) dx2.read() q = dx2._Volume__dx.sum() + charge qt = dx1._Volume__dx.sum() * (dx1.get_basis()[0, 0] ** 3) dx1._Volume__dx = dx1._Volume__dx * (q/ qt) dx1._Volume__dx = -dx1._Volume__dx + dx2._Volume__dx dx1.write(output) return
def admin_sources(mm, reference, reference_type): if reference_type == 'wfn': reference_d = WaveFunction.from_file(filename=reference, program='g09', prefetch_dm=True) elif reference_type == 'a2md': reference_d = a2md_from_mol(mm) with open(reference) as f: reference_d.read(json.load(f)) else: logger.error("use either wfn or a2md as reference format") sys.exit() return reference_d
def __many_store_wfn(name, out, save_dm, save_coeff, program): with open(name) as f: contents = [i.strip() for i in f.readlines()] g = WaveFunctionHDF5(out, mode='w-') for wfn_name in contents: print(".. {:s}".format(wfn_name)) wfn = WaveFunction.from_file(wfn_name, program=program) g.add(wfn_name.replace('.wfn', ''), wfn, save_dm=save_dm, save_coeff=save_coeff) f.close()
def __update_many_mol2(inp, suffix, wfn_suffix, g09_suffix, input_type, charges): """ updates many mol2 files at the same time """ from a2mdio.qm import WaveFunction, GaussianLog from a2mdio.molecules import UNITS_TABLE if input_type == 'json': with open(inp) as f: input_contents = json.load(f) elif input_type == 'txt': with open(inp) as f: input_contents = [i.strip() for i in f.readlines()] else: print("unknown format. use either csv or json") sys.exit() for i, name in enumerate(input_contents): mm = Mol2(name + '.mol2') wfn = name + wfn_suffix gaussian_log = name + g09_suffix wfn_instance = WaveFunction.from_file(filename=wfn, program='g09', prefetch_dm=False) glog = GaussianLog(file=gaussian_log, method='', charges=charges, verbose=False) mm.coordinates = wfn_instance.get_coordinates( ) * UNITS_TABLE['au']['angstrom'] try: gdict = glog.read() mm.charges = np.array(gdict['charges'], dtype='float64') except RuntimeError: print('error : cant read file {:s}'.format(name + g09_suffix)) print('-- skipping charges for {:s}'.format(name + '.mol2')) mm.write(name + suffix)
import torch import time import os from a2mdnet.modules import QMDensityFun from a2mdio.qm import WaveFunction, WaveFunctionHDF5 from a2mdtest.a2mdtests import methane, benzene if __name__ == '__main__': print("---") print("-- qm density fun test") device = torch.device('cuda:0') start = time.time() benzene_wfn = WaveFunction.from_file(benzene.wfn, program='g09') methane_wfn = WaveFunction.from_file(methane.wfn, program='g09') wfnh5 = WaveFunctionHDF5('.wfn.h5py', mode='w') wfnh5.add(key='benzene', wfn=benzene_wfn) wfnh5.add(key='methane', wfn=methane_wfn) wfnh5.close() end = time.time() print("-- {:12.4f}".format(end - start)) print("-- done!") load_qm = lambda cwfn: QMDensityFun( group=cwfn, dtype=torch.float, device=device) wfnh5 = WaveFunctionHDF5('.wfn.h5py', mode='r', wfn_init=load_qm) for _, torch_wfn in wfnh5.iterall(): for i in range(10): start = time.time()
from a2mdio.qm import GaussianLog from a2mdio.parsers import forces from a2mdtest.a2mdtests import aca from a2mdio.qm import WaveFunction from a2mdtest.a2mdtests import ammonium from a2mdio.wfx import WaveFunctionX from a2mdio.utils import eval_volume import numpy as np if __name__ == '__main__': sample = np.loadtxt(ammonium.surfaces[0], delimiter=',', skiprows=1) r = sample[:, :3] p = sample[:, 3] wfn = WaveFunction.from_file(ammonium.wfn, program='g09', prefetch_dm=True) pr = wfn.eval(r) l2 = ((p - pr)**2).sum() print("-- l2 = {:6.4f}".format(l2)) print("done!") # gl = GaussianLog( # file='urea_c_000968.g09.output', method='dft-B3LYP', # charges='MK', ep=False # ) # c, f = gl.seek(forces) # for (x, y, z) in f: # print("{:8.4f} {:8.4f} {:8.4f}".format(x, y, z)) # print("DONE!") # out_dict = gl.read() # # gl = GaussianLog( # file=aca.out, method='MP2', # charges='NPA', ep=False
from a2md.integrate import integrate_density_functional_gradient, dkl_gradient_functional, kullback_leibler_functional from a2md.integrate import integrate_density_functional from a2md.models import a2md_from_mol from a2md.utils import RBFSymmetryCluster from a2mdio.molecules import Mol2 from a2mdio.qm import WaveFunction from a2mdtest.a2mdtests import water from a2md.utils import project from scipy.optimize import minimize if __name__ == '__main__': water_mol2 = Mol2(water.mol2) water_a2md = a2md_from_mol(water_mol2) water_wfn = WaveFunction(file=water.wfn) water_density_sample = np.loadtxt(water.surfaces[1], skiprows=1, delimiter=',') cm = RBFSymmetryCluster(verbose=False) water_a2md.parametrize() water_a2md.clustering(cm.cluster) water_a2md.optimize( training_coordinates=water_density_sample[:, :3], training_density=water_density_sample[:, 3], optimization_mode='restricted' ) n = water_a2md.get_number_optimizable_functions() x = water_a2md.get_unfrozen_integrals() q = np.array(water_a2md.atom_charges).sum() - water_a2md.get_frozen_integrals().sum()
for fx in split_space(water_molecule, fun): integral += pi_lebedev( fun=fx, r_max=15.0, radial_res=100, grid='tight' ) print(integral) print("done") print("TE = {:8.4f}".format(time.time() - START)) START = time.time() water_molecule = Mol2(water.mol2) water_wfn = WaveFunction(file=water.path / "gdb_000003_sto3g.wfn", batch_size=20000) fun = lambda x: water_wfn.eval(x) integral = 0 for fx in split_space(water_molecule, fun): integral += pi_lebedev( fun=fx, r_max=15.0, radial_res=100, grid='medium' ) print(integral) print("done") print("TE = {:8.4f}".format(time.time() - START))