def test_from_iodata(): """Test gbasis.wrapper.from_iodata.""" pytest.importorskip("iodata") from iodata import load_one mol = load_one(find_datafile("data_iodata_water_sto3g_hf_g03.fchk")) basis, coord_types = from_iodata(mol) assert coord_types == ["cartesian"] * 5 assert all(isinstance(i, GeneralizedContractionShell) for i in basis) assert basis[0].angmom == 0 assert np.allclose(basis[0].coord, mol.atcoords[0]) assert np.allclose(basis[0].exps, np.array([130.7093214, 23.80886605, 6.443608313])) assert np.allclose( basis[0].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) assert np.allclose(basis[0].norm_cont, 1.0) assert basis[1].angmom == 0 assert np.allclose(basis[1].coord, mol.atcoords[0]) assert np.allclose(basis[1].exps, np.array([5.033151319, 1.169596125, 0.3803889600])) assert np.allclose( basis[1].coeffs, np.array([-0.09996722919, 0.3995128261, 0.7001154689]).reshape(-1, 1)) assert np.allclose(basis[1].norm_cont, 1.0) assert basis[2].angmom == 1 assert np.allclose(basis[2].coord, mol.atcoords[0]) assert np.allclose(basis[2].exps, np.array([5.033151319, 1.169596125, 0.3803889600])) assert np.allclose( basis[2].coeffs, np.array([0.1559162750, 0.6076837186, 0.3919573931]).reshape(-1, 1)) assert np.allclose(basis[2].norm_cont, 1.0) assert basis[3].angmom == 0 assert np.allclose(basis[3].coord, mol.atcoords[1]) assert np.allclose(basis[3].exps, np.array([3.425250914, 0.6239137298, 0.1688554040])) assert np.allclose( basis[3].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) assert np.allclose(basis[3].norm_cont, 1.0) assert basis[4].angmom == 0 assert np.allclose(basis[4].coord, mol.atcoords[2]) assert np.allclose(basis[4].exps, np.array([3.425250914, 0.6239137298, 0.1688554040])) assert np.allclose( basis[4].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) assert np.allclose(basis[4].norm_cont, 1.0) # Artificially change angular momentum. # The following few lines are commented out deliberately. The file # "data_iodata_water_sto3g_hf_g03.fchk" does not contain "spherical" # functions for angular momenta 0 and 1, so there is no need to have # conventions for them. (This is a fairly common pattern in most QC codes.) # -- BEGIN COMMENTED ASSERTS # basis[2].angmom = 0 # assert basis[2].angmom_components_sph == (0,) # basis[2].angmom = 1 # assert basis[2].angmom_components_sph == (1, -1, 0) # -- END COMMENTED ASSERTS basis[2].angmom = 2 assert basis[2].angmom_components_sph == ("c0", "c1", "s1", "c2", "s2") basis[2].angmom = 3 assert basis[2].angmom_components_sph == ("c0", "c1", "s1", "c2", "s2", "c3", "s3") # NOTE: you shouldn't actually change the magnetic quantum number that is not compatible with # the angular momentum, but we do so here to check that user input is accepted mol.obasis.conventions[(0, "p")] = ["c1"] basis, coord_types = from_iodata(mol) basis[2].angmom = 0 assert coord_types == ["cartesian"] * 5 assert basis[2].angmom_components_sph == ("c1", ) assert np.allclose(basis[2].norm_cont, 1.0) mol.obasis.conventions[(1, "p")] = ["c1", "c0", "s1"] basis, coord_types = from_iodata(mol) basis[2].angmom = 1 assert coord_types == ["cartesian"] * 5 assert basis[2].angmom_components_sph == ("c1", "c0", "s1") assert np.allclose(basis[2].norm_cont, 1.0) mol.obasis.conventions[(1, "c")] = ["z", "y", "x"] basis, coord_types = from_iodata(mol) basis[2].angmom = 1 assert np.allclose(np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]]), basis[2].angmom_components_cart) # Test Cartesian convention generation for missing angmom # Needed for cases when only spherical basis is used in basis set del mol.obasis.conventions[(1, "c")] basis, coord_types = from_iodata(mol) basis[2].angmom = 1 assert np.allclose(np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), basis[2].angmom_components_cart) with pytest.raises(ValueError): basis[2].angmom = 10 basis[2].angmom_components_sph with pytest.raises(ValueError): mol.obasis = mol.obasis._replace(primitive_normalization="L1") basis, coord_types = from_iodata(mol)
from gbasis.integrals.point_charge import point_charge_integral from Alchemical_tools import Alchemical_tools import pylibxc from gbasis.evals.density import evaluate_density from gbasis.evals.eval import evaluate_basis from gbasis.integrals.electron_repulsion import electron_repulsion_integral from grid.onedgrid import GaussChebyshev from grid.rtransform import BeckeTF from grid.utils import get_cov_radii from grid.atomic_grid import AtomicGrid from grid.molgrid import MolGrid from iodata import load_one from gbasis.wrappers import from_iodata molecule = load_one("h2o.fchk") basis = from_iodata(molecule) q = np.array([0.1]) R = np.array([[2.574156, -0.181816, -2.453822]]) alc_hf = Alchemical_tools( basis, molecule, point_charge_positions=R, point_charges_values=q, coord_type="cartesian", k="HF", ) r0 = 1e-10 radii = get_cov_radii(molecule.atnums) n_rad_points = 100 deg = 5 onedgrid = GaussChebyshev(n_rad_points)
def test_from_iodata(): """Test gbasis.wrapper.from_iodata.""" pytest.importorskip("iodata") from iodata import load_one mol = load_one(find_datafile("data_iodata_water_sto3g_hf_g03.fchk")) basis = from_iodata(mol) assert all(isinstance(i, GeneralizedContractionShell) for i in basis) assert basis[0].angmom == 0 assert np.allclose(basis[0].coord, mol.atcoords[0]) assert np.allclose(basis[0].exps, np.array([130.7093214, 23.80886605, 6.443608313])) assert np.allclose( basis[0].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) assert basis[1].angmom == 0 assert np.allclose(basis[1].coord, mol.atcoords[0]) assert np.allclose(basis[1].exps, np.array([5.033151319, 1.169596125, 0.3803889600])) assert np.allclose( basis[1].coeffs, np.array([-0.09996722919, 0.3995128261, 0.7001154689]).reshape(-1, 1)) assert basis[2].angmom == 1 assert np.allclose(basis[2].coord, mol.atcoords[0]) assert np.allclose(basis[2].exps, np.array([5.033151319, 1.169596125, 0.3803889600])) assert np.allclose( basis[2].coeffs, np.array([0.1559162750, 0.6076837186, 0.3919573931]).reshape(-1, 1)) assert basis[3].angmom == 0 assert np.allclose(basis[3].coord, mol.atcoords[1]) assert np.allclose(basis[3].exps, np.array([3.425250914, 0.6239137298, 0.1688554040])) assert np.allclose( basis[3].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) assert basis[4].angmom == 0 assert np.allclose(basis[4].coord, mol.atcoords[2]) assert np.allclose(basis[4].exps, np.array([3.425250914, 0.6239137298, 0.1688554040])) assert np.allclose( basis[4].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) with pytest.raises(ValueError): basis[2].angmom_components_sph # pylint: disable=W0104 # artificially change angular momentum basis[2].angmom = 2 assert basis[2].angmom_components_sph == (0, 1, -1, 2, -2) basis[2].angmom = 3 assert basis[2].angmom_components_sph == (0, 1, -1, 2, -2, 3, -3) with pytest.raises(ValueError): mol.obasis = mol.obasis._replace(primitive_normalization="L1") basis = from_iodata(mol)
def get_one_dm(mol, mole, verb_lvl=0): """Function to pass the one-particle density matrix from a molecule IOdata object to the format that PySCF uses. Should work for restricted and unrestricted The MO information is used to generate the density matrix. Parameters ---------- mol IOdata molecule object. mole PySCF mole object with the same or different basis set. verb_lvl Verbosity level integer flag. Returns ------- one_dm_2 One-particle density matrix understood by IOdata projected and formatted in a PySCF-compatible way (single 2d array or list for a,b 2d arrays) coeffs AO coefficient matrix understood by IOdata projected and formatted in a PySCF-compatible way (single 2d array or list for a,b 2d arrays) occs Occupation numbers formatted in a PySCF-compatible way (single 1d array or list for a,b 1d arrays) energies MO energies formatted in a PySCF-compatible way (single 1d array or list for a,b 1d arrays) Raises ------ wrappyscferror If the mol.mo information is not understood. Can be set manually by modifying the mol.mo attribute in the mol object passed from IOData. """ basis_1 = from_iodata(mol) basis_2 = from_pyscf(mole) proj, s_2, s_1 = project(basis_2, basis_1) try: if mol.mo[0] == "unrestricted": one_dm_a_1, one_dm_b_1, one_dm_1 = spin_dm(mol) one_dm_a_2 = proj @ one_dm_a_1 @ np.linalg.inv(proj) if verb_lvl > 0: print("Total number of alpha electrons is {0}".format( checkrdm(one_dm_a_2, s_2))) one_dm_b_2 = proj @ one_dm_b_1 @ np.linalg.inv(proj) if verb_lvl > 0: print("Total number of beta electrons is {0}".format( checkrdm(one_dm_b_2, s_2))) one_dm_2 = one_dm_a_2 + one_dm_b_2 if verb_lvl > 0: print("Total number of electrons is {0}".format( checkrdm(one_dm_2, s_2))) if np.allclose(one_dm_a_2, one_dm_b_2): if verb_lvl > 1: print( "This will be treated a restricted wave function, alpha and beta orbitals are the same." ) coeffs = proj @ mol.mo.coeffsa energies = np.asarray(mol.mo.energies[:mol.mo.norba]) occs = np.asarray(np.around(mol.mo.occsa) * 2) return one_dm_2, coeffs, occs, energies else: if verb_lvl > 2: print("This is a true unrestricted wave function.") energies_a = np.asarray(mol.mo.energies[:mol.mo.norba]) energies_b = np.asarray(mol.mo.energies[mol.mo.norbb:]) occs_a = np.asarray(np.around(mol.mo.occsa)) occs_b = np.asarray(np.around(mol.mo.occsb)) coeffs_a = proj @ mol.mo.coeffsa coeffs_b = proj @ mol.mo.coeffsb return ( np.array((one_dm_a_2, one_dm_b_2)), np.array((coeffs_a, coeffs_b)), np.array((occs_a, occs_b)), np.array((energies_a, energies_b)), ) if mol.mo[0] == "restricted": one_dm_1 = spin_dm(mol)[2] one_dm_2 = proj @ one_dm_1 @ np.linalg.inv(proj) coeffs = np.asarray(proj @ mol.mo.coeffsa) occs = np.asarray(np.around(mol.mo.occsa) * 2) energies = np.asarray(mol.mo.energies) if verb_lvl > 2: print("This is a restricted wave function.") if verb_lvl > 0: print("Total number of electrons is {0}".format( checkrdm(one_dm_2, s_2))) return one_dm_2, coeffs, occs, energies except: raise wrappyscferror( "Could not understand if the passed data is restricted or unrestricted. You may set it manually." )
def test_from_iodata(): """Test gbasis.wrapper.from_iodata.""" pytest.importorskip("iodata") from iodata import load_one mol = load_one(find_datafile("data_iodata_water_sto3g_hf_g03.fchk")) basis = from_iodata(mol) assert all(isinstance(i, GeneralizedContractionShell) for i in basis) assert basis[0].angmom == 0 assert np.allclose(basis[0].coord, mol.atcoords[0]) assert np.allclose(basis[0].exps, np.array([130.7093214, 23.80886605, 6.443608313])) assert np.allclose( basis[0].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) assert basis[1].angmom == 0 assert np.allclose(basis[1].coord, mol.atcoords[0]) assert np.allclose(basis[1].exps, np.array([5.033151319, 1.169596125, 0.3803889600])) assert np.allclose( basis[1].coeffs, np.array([-0.09996722919, 0.3995128261, 0.7001154689]).reshape(-1, 1)) assert basis[2].angmom == 1 assert np.allclose(basis[2].coord, mol.atcoords[0]) assert np.allclose(basis[2].exps, np.array([5.033151319, 1.169596125, 0.3803889600])) assert np.allclose( basis[2].coeffs, np.array([0.1559162750, 0.6076837186, 0.3919573931]).reshape(-1, 1)) assert basis[3].angmom == 0 assert np.allclose(basis[3].coord, mol.atcoords[1]) assert np.allclose(basis[3].exps, np.array([3.425250914, 0.6239137298, 0.1688554040])) assert np.allclose( basis[3].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) assert basis[4].angmom == 0 assert np.allclose(basis[4].coord, mol.atcoords[2]) assert np.allclose(basis[4].exps, np.array([3.425250914, 0.6239137298, 0.1688554040])) assert np.allclose( basis[4].coeffs, np.array([0.1543289673, 0.5353281423, 0.4446345422]).reshape(-1, 1)) # artificially change angular momentum basis[2].angmom = 0 assert basis[2].angmom_components_sph == (0, ) basis[2].angmom = 1 assert basis[2].angmom_components_sph == (1, -1, 0) basis[2].angmom = 2 assert basis[2].angmom_components_sph == (0, 1, -1, 2, -2) basis[2].angmom = 3 assert basis[2].angmom_components_sph == (0, 1, -1, 2, -2, 3, -3) # NOTE: you shouldn't actually change the magnetic quantum number that is not compatible with # the angular momentum, but we do so here to check that user input is accepted mol.obasis.conventions[(0, "p")] = ["sc1"] basis = from_iodata(mol) basis[2].angmom = 0 assert basis[2].angmom_components_sph == (1, ) mol.obasis.conventions[(1, "p")] = ["pc1", "pc0", "ps1"] basis = from_iodata(mol) basis[2].angmom = 1 assert basis[2].angmom_components_sph == (1, 0, -1) with pytest.raises(ValueError): basis[2].angmom = -1 basis[2].angmom_components_sph with pytest.raises(ValueError): mol.obasis = mol.obasis._replace(primitive_normalization="L1") basis = from_iodata(mol)