def test_total_atomic_grid(self): """Normal initialization test.""" radial_pts = np.arange(0.1, 1.1, 0.1) radial_wts = np.ones(10) * 0.1 radial_grid = RadialGrid(radial_pts, radial_wts) rad = 0.5 scales = np.array([0.5, 1, 1.5]) degs = np.array([6, 14, 14, 6]) # generate a proper instance without failing. ag_ob = AtomicGrid.special_init(radial_grid, radius=rad, scales=scales, degs=degs) assert isinstance(ag_ob, AtomicGrid) assert len(ag_ob.indices) == 11 assert ag_ob.l_max == 15 ag_ob = AtomicGrid.special_init(radial_grid, radius=rad, scales=np.array([]), degs=np.array([6])) assert isinstance(ag_ob, AtomicGrid) assert len(ag_ob.indices) == 11 ag_ob = AtomicGrid(radial_grid, nums=[110]) assert ag_ob.l_max == 17 assert_array_equal(ag_ob._rad_degs, np.ones(10) * 17) assert ag_ob.size == 110 * 10 # new init AtomicGrid ag_ob2 = AtomicGrid(radial_grid, degs=[17]) assert ag_ob2.l_max == 17 assert_array_equal(ag_ob2._rad_degs, np.ones(10) * 17) assert ag_ob2.size == 110 * 10
def test_error_raises(self): """Tests for error raises.""" with self.assertRaises(TypeError): AtomicGrid.special_init(np.arange(3), 1.0, scales=np.arange(2), degs=np.arange(3)) with self.assertRaises(ValueError): AtomicGrid.special_init( RadialGrid(np.arange(3), np.arange(3)), radius=1.0, scales=np.arange(2), degs=np.arange(0), ) with self.assertRaises(ValueError): AtomicGrid.special_init( RadialGrid(np.arange(3), np.arange(3)), radius=1.0, scales=np.arange(2), degs=np.arange(4), ) with self.assertRaises(ValueError): AtomicGrid._generate_atomic_grid( RadialGrid(np.arange(3), np.arange(3)), np.arange(2)) with self.assertRaises(TypeError): AtomicGrid.special_init( RadialGrid(np.arange(3), np.arange(3)), radius=1.0, scales=np.array([0.3, 0.5, 0.7]), degs=np.array([3, 5, 7, 5]), center=(0, 0, 0), ) with self.assertRaises(ValueError): AtomicGrid.special_init( RadialGrid(np.arange(3), np.arange(3)), radius=1.0, scales=np.array([0.3, 0.5, 0.7]), degs=np.array([3, 5, 7, 5]), center=np.array([0, 0, 0, 0]), ) with self.assertRaises(TypeError): AtomicGrid(RadialGrid(np.arange(3), np.arange(3)), nums=110) with self.assertRaises(TypeError): AtomicGrid(RadialGrid(np.arange(3), np.arange(3)), degs=17) with self.assertRaises(ValueError): AtomicGrid(RadialGrid(np.arange(3), np.arange(3)), degs=[17], rotate=-1) with self.assertRaises(ValueError): AtomicGrid(RadialGrid(np.arange(3), np.arange(3)), degs=[17], rotate="asdfaf")
def test_horton_molgrid(self): """Test horton style grid.""" coors = np.array([[0, 0, -0.5], [0, 0, 0.5]]) nums = [1, 1] mol_grid = MolGrid.horton_molgrid(coors, nums, self.rgrid, 110) atg1 = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coors[0] ) atg2 = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coors[1] ) ref_grid = MolGrid([atg1, atg2], nums, store=True) assert_allclose(ref_grid.points, mol_grid.points) assert_allclose(ref_grid.weights, mol_grid.weights)
def test_spline_with_sph_harms(self): """Test spline projection the same as spherical harmonics.""" rad = IdentityRTransform().transform_grid(HortonLinear(10)) rad._points += 1 atgrid = AtomicGrid.special_init(rad, 1, scales=[], degs=[7]) sph_coor = atgrid.convert_cart_to_sph() values = self.helper_func_power(atgrid.points) l_max = atgrid.l_max // 2 r_sph = generate_real_sph_harms(l_max, sph_coor[:, 0], sph_coor[:, 1]) result = spline_with_sph_harms( r_sph, values, atgrid.weights, atgrid.indices, rad.points ) # generate ref # for shell in range(1, 11): for shell in range(1, 11): sh_grid = atgrid.get_shell_grid(shell - 1, r_sq=False) r = np.linalg.norm(sh_grid._points, axis=1) theta = np.arctan2(sh_grid._points[:, 1], sh_grid._points[:, 0]) phi = np.arccos(sh_grid._points[:, 2] / r) r_sph = generate_real_sph_harms(l_max, theta, phi) r_sph_proj = np.sum( r_sph * self.helper_func_power(sh_grid.points) * sh_grid.weights, axis=-1, ) assert_allclose(r_sph_proj, result(shell), atol=1e-10)
def test_raise_errors(self): """Test molgrid errors raise.""" atg = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=np.array([0.0, 0.0, 0.0]), ) # initilize errors with self.assertRaises(NotImplementedError): MolGrid([atg], np.array([1]), aim_weights="test") with self.assertRaises(ValueError): MolGrid([atg], np.array([1]), aim_weights=np.array(3)) with self.assertRaises(TypeError): MolGrid([atg], np.array([1]), aim_weights=[3, 5]) # integrate errors molg = MolGrid([atg], np.array([1])) with self.assertRaises(ValueError): molg.integrate() with self.assertRaises(TypeError): molg.integrate(1) with self.assertRaises(ValueError): molg.integrate(np.array([3, 5])) with self.assertRaises(NotImplementedError): molg.get_atomic_grid(0) with self.assertRaises(ValueError): molg.get_aim_weights(-3) molg = MolGrid([atg], np.array([1]), store=True) with self.assertRaises(ValueError): molg.get_atomic_grid(-5)
def test_molgrid_attrs_subgrid(self): """Test sub atomic grid attributes.""" # numbers = np.array([6, 8], int) coordinates = np.array([[0.0, 0.2, -0.5], [0.1, 0.0, 0.5]], float) atg1 = AtomicGrid.special_init( self.rgrid, 1.228, scales=np.array([]), degs=np.array([17]), center=coordinates[0], ) atg2 = AtomicGrid.special_init( self.rgrid, 0.945, scales=np.array([]), degs=np.array([17]), center=coordinates[1], ) mg = MolGrid([atg1, atg2], np.array([6, 8]), store=True) # mg = BeckeMolGrid(coordinates, numbers, None, (rgrid, 110), mode='keep') assert mg.size == 2 * 110 * 100 assert mg.points.shape == (mg.size, 3) assert mg.weights.shape == (mg.size,) assert mg.aim_weights.shape == (mg.size,) assert len(mg._indices) == 2 + 1 # assert mg.k == 3 # assert mg.random_rotate for i in range(2): atgrid = mg[i] assert isinstance(atgrid, AtomicGrid) assert atgrid.size == 100 * 110 assert atgrid.points.shape == (100 * 110, 3) assert atgrid.weights.shape == (100 * 110,) assert (atgrid.center == coordinates[i]).all() mg = MolGrid([atg1, atg2], np.array([6, 8])) for i in range(2): atgrid = mg[i] assert isinstance(atgrid, SimpleAtomicGrid) assert_allclose(atgrid.center, mg._coors[i])
def test_integrate_hydrogen_trimer_1s(self): """Test molecular integral in H3.""" coordinates = np.array( [[0.0, 0.0, -0.5], [0.0, 0.0, 0.5], [0.0, 0.5, 0.0]], float ) atg1 = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coordinates[0], ) atg2 = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coordinates[1], ) atg3 = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coordinates[2], ) mg = MolGrid([atg1, atg2, atg3], np.array([1, 1, 1])) dist0 = np.sqrt(((coordinates[0] - mg.points) ** 2).sum(axis=1)) dist1 = np.sqrt(((coordinates[1] - mg.points) ** 2).sum(axis=1)) dist2 = np.sqrt(((coordinates[2] - mg.points) ** 2).sum(axis=1)) fn = ( np.exp(-2 * dist0) / np.pi + np.exp(-2 * dist1) / np.pi + np.exp(-2 * dist2) / np.pi ) occupation = mg.integrate(fn) assert_almost_equal(occupation, 3.0, decimal=4)
def test_different_aim_weights_h2(self): """Test different aim_weights for molgrid.""" coordinates = np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]], float) atg1 = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coordinates[0], ) atg2 = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coordinates[1], ) aim_weights = np.ones(22000) mg = MolGrid([atg1, atg2], np.array([1, 1]), aim_weights=aim_weights) dist0 = np.sqrt(((coordinates[0] - mg.points) ** 2).sum(axis=1)) dist1 = np.sqrt(((coordinates[1] - mg.points) ** 2).sum(axis=1)) fn = np.exp(-2 * dist0) / np.pi + np.exp(-2 * dist1) / np.pi occupation = mg.integrate(fn) assert_almost_equal(occupation, 4.0, decimal=4)
def test_molgrid_attrs(self): """Test MolGrid attributes.""" # numbers = np.array([6, 8], int) coordinates = np.array([[0.0, 0.2, -0.5], [0.1, 0.0, 0.5]], float) atg1 = AtomicGrid.special_init( self.rgrid, 1.228, scales=np.array([]), degs=np.array([17]), center=coordinates[0], ) atg2 = AtomicGrid.special_init( self.rgrid, 0.945, scales=np.array([]), degs=np.array([17]), center=coordinates[1], ) mg = MolGrid([atg1, atg2], np.array([6, 8]), store=True) assert mg.size == 2 * 110 * 100 assert mg.points.shape == (mg.size, 3) assert mg.weights.shape == (mg.size,) assert mg.aim_weights.shape == (mg.size,) assert mg.get_atomic_grid(0) is atg1 assert mg.get_atomic_grid(1) is atg2 simple_ag1 = mg.get_simple_atomic_grid(0) simple_ag2 = mg.get_simple_atomic_grid(1) assert_allclose(simple_ag1.points, atg1.points) assert_allclose(simple_ag2.points, atg2.points) aim_at1 = mg.get_aim_weights(0) aim_at2 = mg.get_aim_weights(1) assert_allclose(simple_ag1.weights, atg1.weights * aim_at1) assert_allclose(simple_ag2.weights, atg2.weights * aim_at2)
def test_integrate_hydrogen_8_1s(self): """Test molecular integral in H2.""" x, y, z = np.meshgrid(*(3 * [[-0.5, 0.5]])) centers = np.stack([x.ravel(), y.ravel(), z.ravel()], axis=1) atgs = [ AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=center ) for center in centers ] mg = MolGrid(atgs, np.array([1] * len(centers))) fn = 0 for center in centers: dist = np.linalg.norm(center - mg.points, axis=1) fn += np.exp(-2 * dist) / np.pi occupation = mg.integrate(fn) assert_almost_equal(occupation, len(centers), decimal=2)
def test_integrate_hydrogen_single_1s(self): """Test molecular integral in H atom.""" # numbers = np.array([1], int) coordinates = np.array([0.0, 0.0, -0.5], float) # rgrid = BeckeTF.transform_grid(oned, 0.001, 0.5)[0] # rtf = ExpRTransform(1e-3, 1e1, 100) # rgrid = RadialGrid(rtf) atg1 = AtomicGrid.special_init( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coordinates, ) mg = MolGrid([atg1], np.array([1])) # mg = BeckeMolGrid(coordinates, numbers, None, (rgrid, 110), random_rotate=False) dist0 = np.sqrt(((coordinates - mg.points) ** 2).sum(axis=1)) fn = np.exp(-2 * dist0) / np.pi occupation = mg.integrate(fn) assert_almost_equal(occupation, 1.0, decimal=6)
def test_cubicspline_and_deriv(self): """Test spline for derivation.""" rad = IdentityRTransform().transform_grid(HortonLinear(10)) rad._points += 1 atgrid = AtomicGrid.special_init(rad, 1, scales=[], degs=[7]) sph_coor = atgrid.convert_cart_to_sph() values = self.helper_func_power(atgrid.points) l_max = atgrid.l_max // 2 r_sph = generate_real_sph_harms(l_max, sph_coor[:, 0], sph_coor[:, 1]) result = spline_with_sph_harms( r_sph, values, atgrid.weights, atgrid.indices, rad.points ) semi_sph_c = sph_coor[atgrid.indices[5] : atgrid.indices[6]] interp = interpolate(result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1], deriv=1) # same result from points and interpolation ref_deriv = self.helper_func_power_deriv( atgrid.points[atgrid.indices[5] : atgrid.indices[6]] ) assert_allclose(interp, ref_deriv) # test random x, y, z with fd xyz = np.random.rand(1, 3) xyz /= np.linalg.norm(xyz, axis=-1)[:, None] rad = np.random.normal() * np.random.randint(1, 11) xyz *= rad ref_value = self.helper_func_power_deriv(xyz) r = np.linalg.norm(xyz, axis=-1) theta = np.arctan2(xyz[:, 1], xyz[:, 0]) phi = np.arccos(xyz[:, 2] / r) interp = interpolate(result, np.abs(rad), theta, phi, deriv=1) assert_allclose(interp, ref_value) with self.assertRaises(ValueError): interp = interpolate(result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1], deriv=4) with self.assertRaises(ValueError): interp = interpolate( result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1], deriv=-1 )
def test_cubicspline_and_interp(self): """Test cubicspline interpolation values.""" rad = IdentityRTransform().transform_grid(HortonLinear(10)) rad._points += 1 atgrid = AtomicGrid.special_init(rad, 1, scales=[], degs=[7]) sph_coor = atgrid.convert_cart_to_sph() values = self.helper_func_power(atgrid.points) l_max = atgrid.l_max // 2 r_sph = generate_real_sph_harms(l_max, sph_coor[:, 0], sph_coor[:, 1]) result = spline_with_sph_harms( r_sph, values, atgrid.weights, atgrid.indices, rad.points ) semi_sph_c = sph_coor[atgrid.indices[5] : atgrid.indices[6]] interp = interpolate(result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1]) # same result from points and interpolation assert_allclose(interp, values[atgrid.indices[5] : atgrid.indices[6]]) # random multiple interpolation test for _ in range(100): indices = np.random.randint(1, 11, np.random.randint(1, 10)) interp = interpolate(result, indices, semi_sph_c[:, 0], semi_sph_c[:, 1]) for i, j in enumerate(indices): assert_allclose( interp[i], values[atgrid.indices[j - 1] : atgrid.indices[j]] ) # test random x, y, z xyz = np.random.rand(10, 3) xyz /= np.linalg.norm(xyz, axis=-1)[:, None] rad = np.random.normal() * np.random.randint(1, 11) xyz *= rad ref_value = self.helper_func_power(xyz) r = np.linalg.norm(xyz, axis=-1) theta = np.arctan2(xyz[:, 1], xyz[:, 0]) phi = np.arccos(xyz[:, 2] / r) interp = interpolate(result, np.abs(rad), theta, phi) assert_allclose(interp, ref_value)
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) molecule_at_grid = [] for i in range(len(molecule.atnums)): Rad = BeckeTF.find_parameter(onedgrid.points, r0, radii[i]) rad_grid = BeckeTF(r0, Rad).transform_grid(onedgrid) molecule_at_grid = molecule_at_grid + [ AtomicGrid.special_init(rad_grid, radii[i], degs=[deg], scales=[]) ] molgrid = MolGrid(molecule_at_grid, molecule.atnums) alc_lda = Alchemical_tools( basis, molecule, point_charge_positions=R, point_charges_values=q, coord_type="cartesian", k="lda_x", molgrid=molgrid, ) two_electron_int = electron_repulsion_integral(basis, transform=molecule.mo.coeffs.T, coord_type="cartesian", notation="chemist")