def test_electron_repulsion_cartesian_horton_custom_hhe(): """Test electron_repulsion.electron_repulsion_cartesian against horton results. The test case is diatomic with H and He separated by 0.8 angstroms with basis set ANO-RCC modified. The basis set was modified to remove large exponent components to avoid overflow and some contractions for faster test. This test is also slow. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) coords = np.array([[0, 0, 0], [0.8, 0, 0]]) basis = make_contractions(basis_dict, ["H", "He"], coords) basis = [HortonContractions(i.angmom, i.coord, i.coeffs[:, 0], i.exps) for i in basis[:8]] basis[0] = HortonContractions( basis[0].angmom, basis[0].coord, basis[0].coeffs[3:], basis[0].exps[3:] ) basis[4] = HortonContractions( basis[4].angmom, basis[4].coord, basis[4].coeffs[4:], basis[4].exps[4:] ) basis.pop(3) basis.pop(2) horton_elec_repulsion = np.load(find_datafile("data_horton_hhe_cart_elec_repulsion.npy")) assert np.allclose( horton_elec_repulsion, electron_repulsion_integral(basis, coord_type="cartesian") )
def test_evaluate_general_kinetic_energy_density_horton(): """Test evaluate_general_kinetic_energy_density against results from HORTON. The test case is diatomic with H and He separated by 0.8 angstroms with basis set ANO-RCC. It's actually just testing posdef part. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) # NOTE: used HORTON's conversion factor for angstroms to bohr points = np.array([[0, 0, 0], [0.8 * 1.0 / 0.5291772083, 0, 0]]) basis = make_contractions(basis_dict, ["H", "He"], points) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] horton_density_kinetic_density = np.load( find_datafile("data_horton_hhe_sph_posdef_kinetic_density.npy")) grid_1d = np.linspace(-2, 2, num=10) grid_x, grid_y, grid_z = np.meshgrid(grid_1d, grid_1d, grid_1d) grid_3d = np.vstack([grid_x.ravel(), grid_y.ravel(), grid_z.ravel()]).T assert np.allclose( evaluate_general_kinetic_energy_density(np.identity(88), basis, grid_3d, 0, np.identity(88)), horton_density_kinetic_density, )
def test_evaluate_hessian_deriv_horton(): """Test gbasis.evals.density.evaluate_density_hessian against result from HORTON. The test case is diatomic with H and He separated by 0.8 angstroms with basis set ANO-RCC. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) # NOTE: used HORTON's conversion factor for angstroms to bohr points = np.array([[0, 0, 0], [0.8 * 1.0 / 0.5291772083, 0, 0]]) basis = make_contractions(basis_dict, ["H", "He"], points) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] horton_density_hessian = np.zeros((10**3, 3, 3)) horton_density_hessian[:, [0, 0, 0, 1, 1, 2], [0, 1, 2, 1, 2, 2]] = np.load( find_datafile( "data_horton_hhe_sph_density_hessian.npy")) horton_density_hessian[:, [1, 2, 2], [0, 0, 1]] = horton_density_hessian[:, [0, 0, 1], [1, 2, 2]] grid_1d = np.linspace(-2, 2, num=10) grid_x, grid_y, grid_z = np.meshgrid(grid_1d, grid_1d, grid_1d) grid_3d = np.vstack([grid_x.ravel(), grid_y.ravel(), grid_z.ravel()]).T assert np.allclose( evaluate_density_hessian(np.identity(88), basis, grid_3d, np.identity(88)), horton_density_hessian, )
def test_electrostatic_potential_spherical(): """Test gbasis.evals.electrostatic_potential.electorstatic_potential_spherical. The test case is diatomic with H and He separated by 0.8 angstroms with basis set ANO-RCC. Density matrix is an identity matrix. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) # NOTE: used HORTON's conversion factor for angstroms to bohr coords = np.array([[0, 0, 0], [0.8 * 1.0 / 0.5291772083, 0, 0]]) basis = make_contractions(basis_dict, ["H", "He"], coords) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] grid_1d = np.linspace(-2, 2, num=5) grid_x, grid_y, grid_z = np.meshgrid(grid_1d, grid_1d, grid_1d) grid_3d = np.vstack([grid_x.ravel(), grid_y.ravel(), grid_z.ravel()]).T horton_nucattract = np.load(find_datafile("data_horton_hhe_sph_esp.npy")) assert np.allclose( electrostatic_potential(basis, np.identity(88), grid_3d, coords, np.array([1, 2]), coord_type="spherical"), horton_nucattract, )
def test_overlap_horton_anorcc_bec(): """Test gbasis.integrals.overlap.overlap_cartesian against HORTON's overlap matrix. The test case is diatomic with Be and C separated by 1.0 angstroms with basis set ANO-RCC. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) # NOTE: used HORTON's conversion factor for angstroms to bohr basis = make_contractions( basis_dict, ["Be", "C"], np.array([[0, 0, 0], [1.0 * 1.0 / 0.5291772083, 0, 0]]) ) basis = [HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis] horton_overlap = np.load(find_datafile("data_horton_bec_cart_overlap.npy")) assert np.allclose(overlap_integral(basis, coord_type="cartesian"), horton_overlap)
def test_electron_repulsion_cartesian_horton_sto6g_bec(): """Test electron_repulsion.electron_repulsion_cartesian against horton results. The test case is diatomic with Be and C separated by 1.0 angstroms with basis set STO-6G. Note that ano-rcc was not used because it results in overflow in the _compute_two_electron_integrals. """ basis_dict = parse_nwchem(find_datafile("data_sto6g.nwchem")) coords = np.array([[0, 0, 0], [1.0, 0, 0]]) basis = make_contractions(basis_dict, ["Be", "C"], coords) basis = [HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis] horton_elec_repulsion = np.load(find_datafile("data_horton_bec_cart_elec_repulsion.npy")) assert np.allclose( horton_elec_repulsion, electron_repulsion_integral(basis, coord_type="cartesian") )
def test_overlap_integral_asymmetric_compare(): """Test overlap_asymm.overlap_integral_asymmetric against overlap.overlap_integral.""" basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) basis = make_contractions(basis_dict, ["Kr", "Kr"], np.array([[0, 0, 0], [1.0, 0, 0]])) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] assert np.allclose( overlap_integral(basis, coord_type="cartesian"), overlap_integral_asymmetric(basis, basis, coord_type_one="cartesian", coord_type_two="cartesian"), ) assert np.allclose( overlap_integral(basis, coord_type="spherical"), overlap_integral_asymmetric(basis, basis, coord_type_one="spherical", coord_type_two="spherical"), ) assert np.allclose( overlap_integral(basis, transform=np.identity(218), coord_type="spherical"), overlap_integral_asymmetric( basis, basis, transform_one=np.identity(218), transform_two=np.identity(218), coord_type_one="spherical", coord_type_two="spherical", ), ) assert np.allclose( overlap_integral(basis, coord_type=["spherical"] * 9 + ["cartesian"]), overlap_integral_asymmetric( basis, basis, coord_type_one=["spherical"] * 9 + ["cartesian"], coord_type_two=["spherical"] * 9 + ["cartesian"], ), )
def test_kinetic_energy_integral_horton_anorcc_hhe(): """Test gbasis.integrals.kinetic_energy.kinetic_energy_integral_cartesian against HORTON's results. The test case is diatomic with H and He separated by 0.8 angstroms with basis set ANO-RCC. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) # NOTE: used HORTON's conversion factor for angstroms to bohr basis = make_contractions( basis_dict, ["H", "He"], np.array([[0, 0, 0], [0.8 * 1.0 / 0.5291772083, 0, 0]])) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] horton_kinetic_energy_integral = np.load( find_datafile("data_horton_hhe_cart_kinetic_energy_integral.npy")) assert np.allclose(kinetic_energy_integral(basis, coord_type="cartesian"), horton_kinetic_energy_integral)
def test_evaluate_basis_horton(): """Test gbasis.evals.eval.evaluate_basis against horton results.""" basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) points = np.array([[0, 0, 0], [0.8, 0, 0]]) basis = make_contractions(basis_dict, ["H", "He"], points) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] horton_eval_cart = np.load(find_datafile("data_horton_hhe_cart_eval.npy")) horton_eval_sph = np.load(find_datafile("data_horton_hhe_sph_eval.npy")) grid_1d = np.linspace(-2, 2, num=5) grid_x, grid_y, grid_z = np.meshgrid(grid_1d, grid_1d, grid_1d) grid_3d = np.vstack([grid_x.ravel(), grid_y.ravel(), grid_z.ravel()]).T assert np.allclose(evaluate_basis(basis, grid_3d, coord_type="cartesian"), horton_eval_cart.T) assert np.allclose(evaluate_basis(basis, grid_3d, coord_type="spherical"), horton_eval_sph.T)
def test_nuclear_electron_attraction_horton_anorcc_hhe(): """Test nuclear_electron_attraciton.nuclear_electron_attraction_cartesian with HORTON. The test case is diatomic with H and He separated by 0.8 angstroms with basis set ANO-RCC. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) # NOTE: used HORTON's conversion factor for angstroms to bohr coords = np.array([[0, 0, 0], [0.8 * 1.0 / 0.5291772083, 0, 0]]) basis = make_contractions(basis_dict, ["H", "He"], coords) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] horton_nucattract = np.load( find_datafile("data_horton_hhe_cart_nucattract.npy")) assert np.allclose( nuclear_electron_attraction_integral(basis, coords, np.array([1, 2]), coord_type="cartesian"), horton_nucattract, )
def test_overlap_integral_asymmetric_horton_anorcc_hhe(): """Test gbasis.integrals.overlap_asymm.overlap_integral_asymmetric against HORTON's overlap matrix. The test case is diatomic with H and He separated by 0.8 angstroms with basis set ANO-RCC. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) # NOTE: used HORTON's conversion factor for angstroms to bohr basis = make_contractions( basis_dict, ["H", "He"], np.array([[0, 0, 0], [0.8 * 1.0 / 0.5291772083, 0, 0]])) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] horton_overlap_integral_asymmetric = np.load( find_datafile("data_horton_hhe_cart_overlap.npy")) assert np.allclose( overlap_integral_asymmetric(basis, basis, coord_type_two="cartesian", coord_type_one="cartesian"), horton_overlap_integral_asymmetric, )
def test_evaluate_ehrenfest_hessian(): """Test gbasis.evals.stress_tensor.evaluate_ehrenfest_hessian.""" basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) coords = np.array([[0, 0, 0]]) basis = make_contractions(basis_dict, ["H"], coords) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] points = np.random.rand(10, 3) with pytest.raises(TypeError): evaluate_ehrenfest_hessian(np.identity(40), basis, points, np.array(0), 0, np.identity(40)) with pytest.raises(TypeError): evaluate_ehrenfest_hessian(np.identity(40), basis, points, 1, None, np.identity(40)) with pytest.raises(TypeError): evaluate_ehrenfest_hessian(np.identity(40), basis, points, 1, 0j, np.identity(40)) test_a = evaluate_ehrenfest_hessian(np.identity(40), basis, points, 0, 0, np.identity(40)) test_b = evaluate_ehrenfest_hessian(np.identity(40), basis, points, 1, 0, np.identity(40)) test_c = evaluate_ehrenfest_hessian(np.identity(40), basis, points, 0.5, 0, np.identity(40)) test_d = evaluate_ehrenfest_hessian(np.identity(40), basis, points, 0, 2, np.identity(40)) for j in range(3): for k in range(3): ref_a = np.zeros(points.shape[0]) ref_b = np.zeros(points.shape[0]) ref_c = np.zeros(points.shape[0]) ref_d = np.zeros(points.shape[0]) orders_j = np.array([0, 0, 0]) orders_j[j] += 1 orders_k = np.array([0, 0, 0]) orders_k[k] += 1 for i in range(3): orders_i = np.array([0, 0, 0]) orders_i[i] += 1 temp1 = evaluate_deriv_reduced_density_matrix( 2 * orders_i + orders_k, orders_j, np.identity(40), basis, points, np.identity(40), ) temp2 = evaluate_deriv_reduced_density_matrix( 2 * orders_i, orders_j + orders_k, np.identity(40), basis, points, np.identity(40), ) temp3 = evaluate_deriv_reduced_density_matrix( orders_i + orders_k, orders_i + orders_j, np.identity(40), basis, points, np.identity(40), ) temp4 = evaluate_deriv_reduced_density_matrix( orders_i, orders_i + orders_j + orders_k, np.identity(40), basis, points, np.identity(40), ) temp5 = evaluate_deriv_reduced_density_matrix( 2 * orders_i + orders_j + orders_k, np.array([0, 0, 0]), np.identity(40), basis, points, np.identity(40), ) temp6 = evaluate_deriv_reduced_density_matrix( 2 * orders_i + orders_j, orders_k, np.identity(40), basis, points, np.identity(40), ) temp7 = evaluate_deriv_reduced_density_matrix( orders_i + orders_j + orders_k, orders_i, np.identity(40), basis, points, np.identity(40), ) temp8 = evaluate_deriv_reduced_density_matrix( orders_i + orders_j, orders_i + orders_k, np.identity(40), basis, points, np.identity(40), ) temp9 = evaluate_deriv_density( 2 * orders_i + orders_j + orders_k, np.identity(40), basis, points, np.identity(40), ) ref_a += temp5 + temp6 + temp7 + temp8 ref_b += -temp1 - temp2 - temp3 - temp4 ref_c += (-0.5 * temp1 - 0.5 * temp2 - 0.5 * temp3 - 0.5 * temp4 + 0.5 * temp5 + 0.5 * temp6 + 0.5 * temp7 + 0.5 * temp8) ref_d += temp3 + temp4 + temp5 + temp6 - temp9 assert np.allclose(test_a[:, j, k], ref_a) assert np.allclose(test_b[:, j, k], ref_b) assert np.allclose(test_c[:, j, k], ref_c) assert np.allclose(test_d[:, j, k], ref_d) assert np.allclose( evaluate_ehrenfest_hessian(np.identity(40), basis, points, 0, 0, np.identity(40), symmetric=True), (evaluate_ehrenfest_hessian(np.identity(40), basis, points, 0, 0, np.identity(40)) + np.swapaxes( evaluate_ehrenfest_hessian(np.identity(40), basis, points, 0, 0, np.identity(40)), 1, 2, )) / 2, )
def test_electrostatic_potential(): """Test gbasis.evals.electrostatic_potential.electorstatic_potential. Tested by using point_charge.point_charge_cartesian. """ basis_dict = parse_nwchem(find_datafile("data_anorcc.nwchem")) coords = np.array([[0, 0, 0], [0.8 * 1.0 / 0.5291772083, 0, 0]]) basis = make_contractions(basis_dict, ["H", "He"], coords) basis = [ HortonContractions(i.angmom, i.coord, i.coeffs, i.exps) for i in basis ] # check density_matrix type with pytest.raises(TypeError): electrostatic_potential( basis, np.identity(103).tolist(), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type="cartesian", ) with pytest.raises(TypeError): electrostatic_potential( basis, np.identity(103).flatten(), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type="cartesian", ) # check nuclear_coords with pytest.raises(TypeError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 3).tolist(), np.array([1, 2]), coord_type="cartesian", ) with pytest.raises(TypeError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 4), np.array([1, 2]), coord_type="cartesian", ) # check nuclear charges with pytest.raises(TypeError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]).reshape(1, 2), coord_type="cartesian", ) with pytest.raises(TypeError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]).tolist(), coord_type="cartesian", ) # check density_matrix symmetry with pytest.raises(ValueError): electrostatic_potential( basis, np.eye(103, 102), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type="cartesian", ) # check nuclear_coords and nuclear_charges shapes with pytest.raises(ValueError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(3, 3), np.array([1, 2]), coord_type="cartesian", ) # check threshold_dist types with pytest.raises(TypeError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type="cartesian", threshold_dist=None, ) # check threshold_dist value with pytest.raises(ValueError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type="cartesian", threshold_dist=-0.1, ) # check coord_types type with pytest.raises(TypeError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type="bad", ) with pytest.raises(ValueError): electrostatic_potential( basis, np.identity(88), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type="cartesian", ) with pytest.raises(ValueError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type="spherical", ) with pytest.raises(ValueError): electrostatic_potential( basis, np.identity(103), np.random.rand(10, 3), np.random.rand(2, 3), np.array([1, 2]), coord_type=["spherical"] * 9, )