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( self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coordinates[0], ) atg2 = AtomicGrid( 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([0.5, 0.5]), 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_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 = AtomGrid.from_pruned( self.rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=coordinates[0], ) atg2 = AtomGrid.from_pruned( self.rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=coordinates[1], ) atg3 = AtomGrid.from_pruned( self.rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=coordinates[2], ) becke = BeckeWeights(order=3) mg = MolGrid([atg1, atg2, atg3], becke, 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_get_localgrid_1s(self): """Test local grid for a molecule with one atom.""" nums = np.array([1]) coords = np.array([0.0, 0.0, 0.0]) # initialize MolGrid with atomic grid atg1 = AtomGrid.from_pruned( self.rgrid, 0.5, sectors_r=np.array([]), sectors_degree=np.array([17]), center=coords, ) grid = MolGrid(np.array([1]), [atg1], BeckeWeights(), store=False) fn = np.exp(-2 * np.linalg.norm(grid.points, axis=-1)) assert_allclose(grid.integrate(fn), np.pi) # conventional local grid localgrid = grid.get_localgrid(coords, 12.0) localfn = np.exp(-2 * np.linalg.norm(localgrid.points, axis=-1)) assert localgrid.size < grid.size assert localgrid.size == 10560 assert_allclose(localgrid.integrate(localfn), np.pi) assert_allclose(fn[localgrid.indices], localfn) # "whole" loal grid, useful for debugging code using local grids wholegrid = grid.get_localgrid(coords, np.inf) assert wholegrid.size == grid.size assert_allclose(wholegrid.points, grid.points) assert_allclose(wholegrid.weights, grid.weights) assert_allclose(wholegrid.indices, np.arange(grid.size)) # initialize MolGrid like horton grid = MolGrid.from_size(nums, coords[np.newaxis, :], self.rgrid, 110, BeckeWeights(), store=True) fn = np.exp(-4.0 * np.linalg.norm(grid.points, axis=-1)) assert_allclose(grid.integrate(fn), np.pi / 8) localgrid = grid.get_localgrid(coords, 5.0) localfn = np.exp(-4.0 * np.linalg.norm(localgrid.points, axis=-1)) assert localgrid.size < grid.size assert localgrid.size == 9900 assert_allclose(localgrid.integrate(localfn), np.pi / 8, rtol=1e-5) assert_allclose(fn[localgrid.indices], localfn)
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_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_hirshfeld_weights_single_1s(self): """Test molecular integral in H atom with Hirshfeld weights.""" pts = HortonLinear(100) tf = ExpRTransform(1e-5, 2e1) rgrid = tf.transform_1d_grid(pts) coordinates = np.array([0.0, 0.0, -0.5]) atg1 = AtomGrid.from_pruned( rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=coordinates, ) mg = MolGrid([atg1], HirshfeldWeights(), np.array([7])) 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_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_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 = [ AtomGrid.from_pruned( self.rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=center, ) for center in centers ] becke = BeckeWeights(order=3) mg = MolGrid(atgs, becke, 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_hirshfeld_weights_pair_1s(self): """Test molecular integral in H2.""" coordinates = np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]]) atg1 = AtomGrid.from_pruned( self.rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=coordinates[0], ) atg2 = AtomGrid.from_pruned( self.rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=coordinates[1], ) mg = MolGrid([atg1, atg2], HirshfeldWeights(), np.array([1, 1])) 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 + 1.5 * np.exp(-2 * dist1) / np.pi occupation = mg.integrate(fn) assert_almost_equal(occupation, 2.5, decimal=5)
def test_integrate_hydrogen_pair_1s(self): """Test molecular integral in H2.""" 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], ) mg = MolGrid([atg1, atg2], np.array([1, 1])) 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, 2.0, decimal=6)
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 = AtomGrid.from_pruned( self.rgrid, 0.5, sectors_r=np.array([]), sectors_degree=np.array([17]), center=coordinates[0], ) atg2 = AtomGrid.from_pruned( self.rgrid, 0.5, sectors_r=np.array([]), sectors_degree=np.array([17]), center=coordinates[1], ) # use an array as aim_weights mg = MolGrid(np.array([1, 1]), [atg1, atg2], np.ones(22000)) 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_integrate_hydrogen_pair_1s(self): """Test molecular integral in H2.""" coordinates = np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]], float) atg1 = AtomGrid.from_pruned( self.rgrid, 0.5, sectors_r=np.array([]), sectors_degree=np.array([17]), center=coordinates[0], ) atg2 = AtomGrid.from_pruned( self.rgrid, 0.5, sectors_r=np.array([]), sectors_degree=np.array([17]), center=coordinates[1], ) becke = BeckeWeights(order=3) mg = MolGrid(np.array([1, 1]), [atg1, atg2], becke) 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, 2.0, decimal=6)
ias = (s * len(indices[0][0]) * len(indices[1][0]) + i_c * len(indices[1][s]) + a_c) for t in range(2): for j_c, j in enumerate(indices[0][t]): for b_c, b in enumerate(indices[1][t]): jbt = (t * len(indices[0][0]) * len(indices[1][0]) + j_c * len(indices[1][t]) + b_c) M_HF[ias][jbt] = (M_HF[ias][jbt] + 2 * two_electron_int[i, a, j, b]) M_LDA[ias][jbt] = (M_LDA[ias][jbt] + 2 * two_electron_int[i, a, j, b]) values = (f_xc_values[s + t] * MO_values_conjugated[i] * MO_values[a] * MO_values_conjugated[j] * MO_values[b]) M_LDA[ias][jbt] = M_LDA[ias][ jbt] + 2 * molgrid.integrate(values) if s == t: M_HF[ias][jbt] = (M_HF[ias][jbt] - 2 * two_electron_int[i, a, j, b]) if s == t and i == j and a == b: M_HF[ias][jbt] = (M_HF[ias][jbt] + molecule.mo.energies[a] - molecule.mo.energies[i]) M_LDA[ias][jbt] = (M_LDA[ias][jbt] + molecule.mo.energies[a] - molecule.mo.energies[i]) M_HF_inv = np.linalg.inv(M_HF) M_LDA_inv = np.linalg.inv(M_LDA) def test_Alchemical_tools_density_matrix_variation():
def test_raise_errors(self): """Test molgrid errors raise.""" atg = AtomGrid.from_pruned( self.rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=np.array([0.0, 0.0, 0.0]), ) # errors of aim_weight with self.assertRaises(TypeError): MolGrid([atg], aim_weights="test", atom_nums=np.array([1])) with self.assertRaises(ValueError): MolGrid([atg], aim_weights=np.array(3), atom_nums=np.array([1])) with self.assertRaises(TypeError): MolGrid([atg], aim_weights=[3, 5], atom_nums=np.array([1])) # integrate errors becke = BeckeWeights({1: 0.472_431_53}, order=3) molg = MolGrid([atg], becke, 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(ValueError): molg.get_atomic_grid(-3) molg = MolGrid([atg], becke, np.array([1]), store=True) with self.assertRaises(ValueError): molg.get_atomic_grid(-5) # test make_grid error pts = HortonLinear(70) tf = ExpRTransform(1e-5, 2e1) rgrid = tf.transform_1d_grid(pts) numbers = np.array([1, 1]) becke = BeckeWeights(order=3) # construct molgrid with self.assertRaises(ValueError): MolGrid.make_grid(numbers, np.array([0.0, 0.0, 0.0]), rgrid, "fine", becke) with self.assertRaises(ValueError): MolGrid.make_grid(np.array([1, 1]), np.array([[0.0, 0.0, 0.0]]), rgrid, "fine", becke) with self.assertRaises(ValueError): MolGrid.make_grid(np.array([1, 1]), np.array([[0.0, 0.0, 0.0]]), rgrid, "fine", becke) with self.assertRaises(TypeError): MolGrid.make_grid( np.array([1, 1]), np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]]), {3, 5}, "fine", becke, ) with self.assertRaises(TypeError): MolGrid.make_grid( np.array([1, 1]), np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]]), rgrid, np.array([3, 5]), becke, )