def test_condense_quadratic_from_molecule_fmr_mbis_ch4_fchk(): # expected populations of CH4 computed with HORTON with path('chemtools.data', 'ch4_uhf_ccpvdz.fchk') as fname: mol = Molecule.from_file(fname) expected = np.array( [6.46038055, 0.88489494, 0.88492901, 0.88493897, 0.88492396]) # check from_molecule model = CondensedConceptualDFT.from_molecule(mol, "quadratic", "FMR", "mbis") check_condensed_reactivity(model, "quadratic", expected, None, None, 10, 0.736396) # check from_molecule given as a list model = CondensedConceptualDFT.from_molecule([mol], "quadratic", "FMR", "mbis") check_condensed_reactivity(model, "quadratic", expected, None, None, 10, 0.736396) # check from_molecule & passing grid grid = MolecularGrid(mol.coordinates, mol.numbers, mol.pseudo_numbesr, 'insane', 3, False) model = CondensedConceptualDFT.from_molecule(mol, "quadratic", "FMR", "mbis", grid=grid) check_condensed_reactivity(model, "quadratic", expected, None, None, 10, 0.736396) # check from_molecule given as a list & passing grid model = CondensedConceptualDFT.from_molecule([mol], "quadratic", "FMR", "mbis", grid=grid) check_condensed_reactivity(model, "quadratic", expected, None, None, 10, 0.736396)
def from_molecule(cls, mol, scheme=None, grid=None, spin="ab", **kwargs): """Initialize class given a Molecule instance. Parameters ---------- mol : Molecule Instance of Molecule class. scheme : str Type of atoms-in-molecule partitioning scheme. grid : MolecularGrid Instance of MolecularGrid numerical integration grid. spin : str Type of occupied spin orbitals; choose either "a" (for alpha), "b" (for beta), and "ab" (for alpha + beta). """ if grid is None: grid = MolecularGrid(mol.coordinates, mol.numbers, mol.pseudo_numbers, specs="fine", rotate=False, k=3) else: check_molecule_grid(mol, grid) # compute molecular electron density dens = mol.compute_density(grid.points, spin=spin) if mol.pesudo_numbers is None: pesudo_numbers = mol.numbers else: pesudo_numbers = mol.pesudo_numbers return cls(mol.coordinates, mol.numbers, pesudo_numbers, dens, grid, scheme, **kwargs)
def test_condense_linear_from_molecule_fmr_h_ch4_wfn(): # expected populations of CH4 computed with HORTON with path('chemtools.data', 'ch4_uhf_ccpvdz.wfn') as fname: mol = Molecule.from_file(fname) expected = np.array( [6.11301651, 0.97175462, 0.97175263, 0.9717521, 0.97174353]) # check from_molecule model = CondensedConceptualDFT.from_molecule(mol, "linear", "FMR", "h") check_condensed_reactivity(model, "linear", expected, None, None, 10, 0.736396) # check from_molecule given as a list model = CondensedConceptualDFT.from_molecule([mol], "linear", "FMR", "h") check_condensed_reactivity(model, "linear", expected, None, None, 10, 0.736396) # check from_molecule & passing grid grid = MolecularGrid(mol.coordinates, mol.numbers, mol.pseudo_numbesr, 'insane', 3, False) model = CondensedConceptualDFT.from_molecule(mol, "linear", "FMR", "h", grid=grid) check_condensed_reactivity(model, "linear", expected, None, None, 10, 0.736396) # check from_molecule given as a list & passing grid model = CondensedConceptualDFT.from_molecule([mol], "linear", "FMR", "h", grid=grid) check_condensed_reactivity(model, "linear", expected, None, None, 10, 0.736396)
def test_condense_linear_from_file_fmr_mbis_ch4_fchk(): # expected populations of CH4 computed with HORTON with path('chemtools.data', 'ch4_uhf_ccpvdz.fchk') as fname: model1 = CondensedConceptualDFT.from_file(fname, "linear", "FMR", "mbis") model2 = CondensedConceptualDFT.from_file([fname], "linear", "FMR", "mbis") mol = Molecule.from_file(fname) grid = MolecularGrid(mol.coordinates, mol.numbers, mol.pseudo_numbesr, 'insane', 3, False) model3 = CondensedConceptualDFT.from_file(fname, "linear", "FMR", "mbis", grid=grid) model4 = CondensedConceptualDFT.from_file([fname], "linear", "FMR", "mbis", grid=grid) expected = np.array( [6.46038055, 0.88489494, 0.88492901, 0.88493897, 0.88492396]) # check using fname given as a string check_condensed_reactivity(model1, "linear", expected, None, None, 10, 0.736396) # check using fname given as a list check_condensed_reactivity(model2, "linear", expected, None, None, 10, 0.736396) # check using fname as a string & passing grid check_condensed_reactivity(model3, "linear", expected, None, None, 10, 0.736396) # check using fname as a list & passing grid check_condensed_reactivity(model4, "linear", expected, None, None, 10, 0.736396)
def test_condense_linear_from_file_fmr_h_ch4_fchk(): # expected populations of CH4 computed with HORTON with path('chemtools.data', 'ch4_uhf_ccpvdz.fchk') as fname: model1 = CondensedConceptualDFT.from_file(fname, "linear", "FMR", "h") model2 = CondensedConceptualDFT.from_file([fname], "linear", "FMR", "h") mol = Molecule.from_file(fname) grid = MolecularGrid.from_molecule(mol, specs='insane', rotate=False) model3 = CondensedConceptualDFT.from_file(fname, "linear", "FMR", "h", grid=grid) model4 = CondensedConceptualDFT.from_file([fname], "linear", "FMR", "h", grid=grid) expected = np.array( [6.11301651, 0.97175462, 0.97175263, 0.9717521, 0.97174353]) # check using fname given as a string check_condensed_reactivity(model1, "linear", expected, None, None, 10, 0.736396) # check using fname given as a list check_condensed_reactivity(model2, "linear", expected, None, None, 10, 0.736396) # check using fname as a string & passing grid check_condensed_reactivity(model3, "linear", expected, None, None, 10, 0.736396) # check using fname as a list & passing grid check_condensed_reactivity(model4, "linear", expected, None, None, 10, 0.736396)
def test_condense_quadratic_from_molecule_fmr_mbis_ch4(): # expected populations of CH4 computed with HORTON with path('chemtools.data', 'ch4_uhf_ccpvdz.wfn') as fname: molecule = Molecule.from_file(fname) grid = MolecularGrid.from_molecule(molecule, 'fine') part = DensPart.from_molecule(molecule, scheme='mbis', grid=grid) expected = np.array( [6.46038055, 0.88489494, 0.88492901, 0.88493897, 0.88492396]) computed = part.numbers - part.charges assert np.all(abs(expected - computed) < 1.e-2) assert np.all(abs(part.condense_to_atoms(part.density) - computed) < 1.e-2)
def test_wrapper_grid_ch4(): with path('chemtools.data', 'ch4_uhf_ccpvdz.fchk') as fpath: mol = Molecule.from_file(fpath) grid = MolecularGrid(mol.coordinates, mol.numbers, mol.pseudo_numbers, 'exp:1e-5:25:80:230') assert grid.weights.ndim == 1 assert grid.points.shape[0] == grid.weights.shape[0] assert grid.points.shape == (grid.npoints, 3) # check grid basics assert_allclose(mol.coordinates, grid.coordinates, rtol=0., atol=1.e-7) assert_allclose(mol.numbers, grid.numbers, rtol=0., atol=1.e-7) assert_allclose(mol.pseudo_numbers, grid.pseudo_numbers, rtol=0., atol=1.e-7) # check integrate assert_allclose(10., grid.integrate(mol.compute_density(grid.points)), rtol=0., atol=1.e-4)
def test_wrapper_grid_from_file_o2(): with path('chemtools.data', 'o2_uhf.wfn') as fpath: grid = MolecularGrid.from_file(fpath, 'veryfine') mol = Molecule.from_file(fpath) assert grid.weights.ndim == 1 assert grid.points.shape[0] == grid.weights.shape[0] assert grid.points.shape == (grid.npoints, 3) # check integrate assert_allclose(16., grid.integrate(mol.compute_density(grid.points)), rtol=0., atol=1.e-4)
def test_local_linear_from_molecule_fmo_ch4_uhf_ccpvdz_wfn(): # atomic coordinates and numbers of CH4 coord, nums = get_data_ch4() with path('chemtools.data', 'ch4_uhf_ccpvdz.wfn') as file_path: molecule = Molecule.from_file(file_path) grid = MolecularGrid(coord, nums, nums, specs='insane', rotate=False) # check from_molecule passing a grid model = LocalConceptualDFT.from_molecule(molecule, "linear", grid.points) check_local_reactivity(model, "linear", grid, 10) # check from_molecule given as a list passing a grid model = LocalConceptualDFT.from_molecule([molecule], "linear", grid.points) check_local_reactivity(model, "linear", grid, 10)
def get_molecular_grid(molecule, grid=None): r"""Return molecular grid or check that given grid is consistent with molecule. Parameters ---------- molecule : Molecule or Sequence of Molecule Instance of Molecule class, or sequence of Molecule class instances. grid : MolecularGrid, optional Instance or MolecularGrid. If `None`, a default `MolecularGrid` is returned. """ # check grid or make grid if grid is not None and isinstance(molecule, Molecule): # check atomic numbers & coordinates of grid and molecule match ref, numbers = grid.numbers, molecule.numbers if ref.shape != numbers.shape or not np.max( abs(ref - numbers)) < 1.e-6: raise ValueError( "Atomic numbers of grid and molecule do not match!") ref, coord = grid.centers, molecule.coordinates if ref.shape != coord.shape or not np.max(abs(ref - coord)) < 1.e-4: raise ValueError("Coordinates of grid and molecule do not match!") elif grid is not None and all( [isinstance(mol, Molecule) for mol in molecule]): for index, mol in enumerate(molecule): # check atomic numbers of grid and molecules match ref, numbers = grid.numbers, mol.numbers if ref.shape != numbers.shape or not np.max( abs(ref - numbers)) < 1.e-6: raise ValueError( "Atomic number of grid & molecule {0} do not match!". format(index)) # check coordinates of grid and molecules match ref, coord = grid.centers, mol.coordinates if ref.shape != coord.shape or not np.max( abs(ref - coord)) < 1.e-4: raise ValueError( "Coordinates of grid & molecule {0} do not match!".format( index)) else: # make default grid number = get_matching_attr(molecule, "numbers", 1.e-8) pseudo = get_matching_attr(molecule, "pseudo_numbers", 1.e-8) coords = get_matching_attr(molecule, "coordinates", 1.e-4) grid = MolecularGrid(coords, number, pseudo, specs="insane", rotate=False) return grid
def test_local_quadratic_from_file_fmo_ch4_uhf_ccpvdz_wfn(): # atomic coordinates and numbers of CH4 coord, nums = get_data_ch4() # ip = -E(h**o) & ea = E(lumo) & eta = ip - ea # eta = -(-5.43101269E-01) - (-1.93295185E-01) grid = MolecularGrid(coord, nums, nums, specs='insane', rotate=False) with path('chemtools.data', 'ch4_uhf_ccpvdz.wfn') as file_path: # check from_file passing grid model1 = LocalConceptualDFT.from_file(file_path, "quadratic", grid.points) # check from_file given as a list passing grid model2 = LocalConceptualDFT.from_file([file_path], "quadratic", grid.points) check_local_reactivity(model1, "quadratic", grid, 10) check_local_reactivity(model2, "quadratic", grid, 10)
def test_wrapper_grid_raises(): with path('chemtools.data', 'ch4_uhf_ccpvdz.fchk') as fpath: assert_raises(TypeError, MolecularGrid.from_molecule, fpath, 'exp:1e-5:20:40:50') assert_raises(ValueError, MolecularGrid.from_file, fpath, 'ex:1e-5:20:40:50') assert_raises(ValueError, MolecularGrid.from_file, fpath, 'exp:1e-5:-20:40:50') assert_raises(ValueError, MolecularGrid.from_file, fpath, 'exp:1e-5:20:40:10') assert_raises(ValueError, MolecularGrid.from_file, fpath, 'pow:1e-5:20:40:50') assert_raises(ValueError, MolecularGrid.from_file, fpath, 'veryfin') with path('chemtools.data', 'ch4_uhf_ccpvdz.fchk') as fpath: grid = MolecularGrid.from_file(fpath) assert_raises(ValueError, grid.integrate, np.array([[1., 2., 3.]])) assert_raises(NotImplementedError, grid.compute_spherical_average, None)
def _get_eos(filename, scheme): # build proatom database atoms = glob.glob('chemtools/data/atom_0*') proatomdb = ProAtomDB.from_files(atoms, "power:5e-8:20:40:146") # load molecule & make grid, denspart, and eos instances with path('chemtools.data', filename) as file_path: mol = Molecule.from_file(file_path) grid = MolecularGrid.from_molecule(mol, specs='power:5e-8:20:40:146', k=4, rotate=False) part = DensPart.from_molecule(mol, scheme=scheme, grid=grid, local=False, proatomdb=proatomdb) return EOS(mol, part)