def test_compute_one_elec_int_generalized_contraction(): """Test _one_elec_int._compute_one_electron_integrals for generalized contractions.""" contr_one = GeneralizedContractionShell(3, np.array([0.5, 1, 1.5]), np.array([[1.0, 2.0], [1.5, 2.5]]), np.array([0.1, 0.25])) contr_two = GeneralizedContractionShell(2, np.array([1.5, 2, 3]), np.array([[3.0, 4.0], [3.5, 4.5]]), np.array([0.02, 0.01])) coord_a = contr_one.coord angmom_a = contr_one.angmom exps_a = contr_one.exps coeffs_a = contr_one.coeffs coord_b = contr_two.coord angmom_b = contr_two.angmom exps_b = contr_two.exps coeffs_b = contr_two.coeffs _compute_one_elec_integrals( np.array([0.0, 0.0, 0.0]), # coord_point boys_func, coord_a, angmom_a, exps_a, coeffs_a, coord_b, angmom_b, exps_b, coeffs_b, )
def test_overlap_construct_array_contraction(): """Test gbasis.integrals.overlap.Overlap.construct_array_contraction.""" test_one = GeneralizedContractionShell( 1, np.array([0.5, 1, 1.5]), np.array([1.0, 2.0]), np.array([0.1, 0.01]) ) test_two = GeneralizedContractionShell( 2, np.array([1.5, 2, 3]), np.array([3.0, 4.0]), np.array([0.2, 0.02]) ) answer = np.array( [ [ _compute_multipole_moment_integrals( np.array([0, 0, 0]), np.array([[0, 0, 0]]), np.array([0.5, 1, 1.5]), np.array([angmom_comp_one]), np.array([0.1, 0.01]), np.array([[1], [2]]), np.array( [ [ (2 * 0.1 / np.pi) ** (3 / 4) * (4 * 0.1) ** (1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_one - 1))), (2 * 0.01 / np.pi) ** (3 / 4) * (4 * 0.01) ** (1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_one - 1))), ] ] ), np.array([1.5, 2, 3]), np.array([angmom_comp_two]), np.array([0.2, 0.02]), np.array([[3], [4]]), np.array( [ [ (2 * 0.2 / np.pi) ** (3 / 4) * (4 * 0.2) ** (2 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_two - 1))), (2 * 0.02 / np.pi) ** (3 / 4) * (4 * 0.02) ** (2 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_two - 1))), ] ] ), ) for angmom_comp_two in test_two.angmom_components_cart ] for angmom_comp_one in test_one.angmom_components_cart ] ) assert np.allclose( np.squeeze(Overlap.construct_array_contraction(test_one, test_two)), np.squeeze(answer) ) with pytest.raises(TypeError): Overlap.construct_array_contraction(test_one, None) with pytest.raises(TypeError): Overlap.construct_array_contraction(None, test_two)
def test_contractions_two(): """Test BaseTwoIndexAsymmetric.constractions_two.""" Test = disable_abstract(BaseTwoIndexAsymmetric) # noqa: N806 cont_one = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) cont_two = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) test = Test([cont_one], [cont_two]) assert test.contractions_two[0] == cont_two
def test_compute_one_elec_int_v_recursion(): """Test vertical recursion in _one_elec_int._compute_one_elec_integrals.""" contr_one = GeneralizedContractionShell(3, np.array([0.5, 1, 1.5]), np.array([1.0, 2.0]), np.array([0.1, 0.25])) contr_two = GeneralizedContractionShell(2, np.array([1.5, 2, 3]), np.array([3.0, 4.0]), np.array([0.02, 0.01])) coord_a = contr_one.coord angmom_a = contr_one.angmom exps_a = contr_one.exps coeffs_a = contr_one.coeffs coord_b = contr_two.coord angmom_b = contr_two.angmom exps_b = contr_two.exps coeffs_b = contr_two.coeffs answer = _compute_one_elec_integrals( np.array([0.0, 0.0, 0.0]).reshape(1, 3), # coord_point boys_func, coord_a, angmom_a, exps_a, coeffs_a, coord_b, angmom_b, exps_b, coeffs_b, ) # Test V(0)(000|000) using hand-calculated values assert np.allclose(0.12077036178347592, answer[0, 0, 0, 0, 0, 0]) # Test vertical recursion for one nonzero index # Base case for recursion assert np.allclose(-0.00869614017728084, answer[1, 0, 0, 0, 0, 0]) # Recursion loop for all a assert np.allclose(0.11662166020358243, answer[2, 0, 0, 0, 0, 0]) assert np.allclose(-0.0020384534940189078, answer[3, 0, 0, 0, 0, 0]) # Test vertical recursion for two nonzero indices # Base case for recursion assert np.allclose(-0.011960055776784264, answer[2, 1, 0, 0, 0, 0]) # Recursion loop for all a assert np.allclose(0.14385092207210784, answer[2, 2, 0, 0, 0, 0]) # Test vertical recursion for three nonzero indices # Base case for recursion assert np.allclose(0.010332151108923077, answer[2, 1, 1, 0, 0, 0]) # Recursion loop for all a assert np.allclose(-0.010379229289999358, answer[2, 1, 2, 0, 0, 0])
def test_contractions(): """Test BaseTwoIndexSymmetric.constractions.""" Test = disable_abstract(BaseTwoIndexSymmetric) # noqa: N806 cont = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) test = Test([cont]) assert test.contractions[0] == cont
def test_construct_array_cartesian(): """Test BaseFourIndexSymmetric.construct_array_cartesian.""" cont_one = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones((1, 1)), np.ones(1)) cont_two = GeneralizedContractionShell(2, np.array([2, 3, 4]), np.ones((1, 1)), 2 * np.ones(1)) Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={ "construct_array_contraction": lambda self, cont1, cont2, cont3, cont4: np.ones( ( cont1.num_seg_cont, cont1.num_cart, cont2.num_seg_cont, cont2.num_cart, cont3.num_seg_cont, cont3.num_cart, cont4.num_seg_cont, cont4.num_cart, ) ) * cont1.exps * cont2.exps * cont3.exps * cont4.exps }, ) test = Test([cont_one, cont_two]) answer = np.zeros( (cont_one.num_cart * cont_one.num_seg_cont + cont_two.num_cart * cont_two.num_seg_cont,) * 4 ) answer[:3, :3, :3, :3] = 1 * 1 * 1 * 1 answer[:3, :3, :3, 3:9] = 1 * 1 * 1 * 2 answer[:3, :3, 3:9, :3] = 1 * 1 * 2 * 1 answer[:3, :3, 3:9, 3:9] = 1 * 1 * 2 * 2 answer[:3, 3:9, :3, :3] = 1 * 2 * 1 * 1 answer[:3, 3:9, :3, 3:9] = 1 * 2 * 1 * 2 answer[:3, 3:9, 3:9, :3] = 1 * 2 * 2 * 1 answer[:3, 3:9, 3:9, 3:9] = 1 * 2 * 2 * 2 answer[3:9, :3, :3, :3] = 2 * 1 * 1 * 1 answer[3:9, :3, :3, 3:9] = 2 * 1 * 1 * 2 answer[3:9, :3, 3:9, :3] = 2 * 1 * 2 * 1 answer[3:9, :3, 3:9, 3:9] = 2 * 1 * 2 * 2 answer[3:9, 3:9, :3, :3] = 2 * 2 * 1 * 1 answer[3:9, 3:9, :3, 3:9] = 2 * 2 * 1 * 2 answer[3:9, 3:9, 3:9, :3] = 2 * 2 * 2 * 1 answer[3:9, 3:9, 3:9, 3:9] = 2 * 2 * 2 * 2 assert np.allclose(test.construct_array_cartesian(), answer)
def test_init(): """Test BaseFourIndexSymmetric.__init__.""" Test = disable_abstract(BaseFourIndexSymmetric) # noqa: N806 test = skip_init(Test) contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) Test.__init__(test, [contractions]) assert test._axes_contractions[0][0] == contractions with pytest.raises(TypeError): Test.__init__(test, [contractions], [contractions])
def test_init(): """Test BaseOneIndex.__init__.""" Test = disable_abstract(BaseOneIndex) # noqa: N806 test = skip_init(Test) contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) Test.__init__(test, [contractions]) assert test._axes_contractions == ((contractions,),) with pytest.raises(TypeError): Test.__init__(test, [contractions], [contractions])
def test_contruct_array_cartesian(): """Test BaseOneIndex.construct_array_cartesian.""" contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) contractions.norm_cont = np.ones((1, 5)) Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={ "construct_array_contraction": lambda self, cont, a=2: np.ones((1, 5)) * a }, ) test = Test([contractions]) assert np.allclose(test.construct_array_cartesian(), np.ones(5) * 2) assert np.allclose(test.construct_array_cartesian(a=3), np.ones(5) * 3) with pytest.raises(TypeError): test.construct_array_cartesian(bad_keyword=3) test = Test([contractions, contractions]) assert np.allclose(test.construct_array_cartesian(), np.ones(10) * 2) assert np.allclose(test.construct_array_cartesian(a=3), np.ones(10) * 3) Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={ "construct_array_contraction": lambda self, cont, a=2: np.ones((2, 5)) * a }, ) contractions.norm_cont = np.ones((2, 5)) test = Test([contractions, contractions]) assert np.allclose(test.construct_array_cartesian(), np.ones(20) * 2) assert np.allclose(test.construct_array_cartesian(a=3), np.ones(20) * 3) Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={ "construct_array_contraction": lambda self, cont, a=2: np.ones((2, 5, 4)) * a }, ) test = Test([contractions, contractions]) assert np.allclose(test.construct_array_cartesian(), np.ones((20, 4)) * 2) assert np.allclose(test.construct_array_cartesian(a=3), np.ones((20, 4)) * 3)
def test_contruct_array_contraction(): """Test BaseOneIndex.construct_array_contraction.""" # enable only the abstract method construct_array_contraction Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={"construct_array_contraction": BaseOneIndex.construct_array_contraction}, ) contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) with pytest.raises(TypeError): Test([contractions])
def test_contruct_array_lincomb(): """Test BaseOneIndex.construct_array_lincomb.""" contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) sph_transform = generate_transformation( 1, contractions.angmom_components_cart, contractions.angmom_components_sph, "left" ) orb_transform = np.random.rand(3, 3) Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={ "construct_array_contraction": lambda self, cont, a=2: np.arange( 9, dtype=float ).reshape(1, 3, 3) * a }, ) test = Test([contractions]) assert np.allclose( test.construct_array_lincomb(orb_transform, "cartesian"), orb_transform.dot(np.arange(9).reshape(3, 3)) * 2, ) assert np.allclose( test.construct_array_lincomb(orb_transform, "spherical"), orb_transform.dot(sph_transform).dot(np.arange(9).reshape(3, 3)) * 2, ) assert np.allclose( test.construct_array_lincomb(orb_transform, "spherical", a=3), orb_transform.dot(sph_transform).dot(np.arange(9).reshape(3, 3)) * 3, ) with pytest.raises(TypeError): test.construct_array_lincomb(orb_transform, "bad") with pytest.raises(TypeError): test.construct_array_lincomb(orb_transform, "spherical", bad_keyword=3) orb_transform = np.random.rand(3, 6) test = Test([contractions, contractions]) assert np.allclose( test.construct_array_lincomb(orb_transform, "spherical"), orb_transform.dot( np.vstack([sph_transform.dot(np.arange(9, dtype=float).reshape(3, 3)) * 2] * 2) ), ) assert np.allclose( test.construct_array_lincomb(orb_transform, ["spherical", "cartesian"]), orb_transform.dot( np.vstack( [ sph_transform.dot(np.arange(9, dtype=float).reshape(3, 3)) * 2, np.arange(9, dtype=float).reshape(3, 3) * 2, ] ) ), )
def tests_init(): """Test GeneralizedContractionShell.__init__.""" test = GeneralizedContractionShell( 1, np.array([0, 1, 2]), np.array([1, 2, 3, 4], dtype=float), np.array([5, 6, 7, 8], dtype=float), ) assert test._angmom == 1 assert np.allclose(test._coord, np.array([0, 1, 2])) assert np.allclose(test._coeffs, np.array([[1], [2], [3], [4]])) assert np.allclose(test._exps, np.array([5, 6, 7, 8]))
def test_contruct_array_lincomb(): """Test base.BaseGaussianRelatedArray.construct_array_lincomb.""" # enable only the abstract method construct_array_lincomb Test = disable_abstract( # noqa: N806 BaseGaussianRelatedArray, dict_overwrite={ "construct_array_lincomb": BaseGaussianRelatedArray.construct_array_lincomb }, ) contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) with pytest.raises(TypeError): Test([contractions])
def test_compute_one_elec_int_multiple_contractions(): """Test _one_elec_int._compute_one_electron_integrals for s-type contractions.""" # GeneralizedContractionShell(angmom, coord, charge, coeffs, exps) contr_one = GeneralizedContractionShell(1, np.array([0.5, 1, 1.5]), np.array([1.0, 2.0]), np.array([0.1, 0.25])) contr_two = GeneralizedContractionShell(1, np.array([1.5, 2, 3]), np.array([3.0, 4.0]), np.array([0.02, 0.01])) coord_a = contr_one.coord angmom_a = contr_one.angmom exps_a = contr_one.exps coeffs_a = contr_one.coeffs coord_b = contr_two.coord angmom_b = contr_two.angmom exps_b = contr_two.exps coeffs_b = contr_two.coeffs answer = _compute_one_elec_integrals( np.array([0.0, 0.0, 0.0]).reshape(1, 3), # coord_point boys_func, coord_a, angmom_a, exps_a, coeffs_a, coord_b, angmom_b, exps_b, coeffs_b, ) # Test output array using hand-calculated values assert np.allclose(answer[1, 0, 0, 1, 0, 0], 1.1725677458858423) assert np.allclose(answer[1, 0, 0, 0, 1, 0], 0.07610607398731435) assert np.allclose(answer[1, 0, 0, 0, 0, 1], 0.11415911098097152) assert np.allclose(answer[0, 1, 0, 1, 0, 0], 0.1525693722167499) assert np.allclose(answer[0, 1, 0, 0, 1, 0], 1.3047296607539662) assert np.allclose(answer[0, 1, 0, 0, 0, 1], 0.2860362946729374) assert np.allclose(answer[0, 0, 1, 1, 0, 0], 0.22885405832512482) assert np.allclose(answer[0, 0, 1, 0, 1, 0], 0.28603629467293734) assert np.allclose(answer[0, 0, 1, 0, 0, 1], 1.5430932396480808)
def from_pyscf(mol): """Return basis set stored within the Mole instance in pyscf. Parameters ---------- mol : pyscf.gto.mole.Mole Mole object in pyscf. Returns ------- basis : tuple of gbasis.contraciton.GeneralizedContractionShell Contractions for each atom. Contractions are ordered by the atom first, then the contractions as ordered in pyscf. Raises ------ ValueError If `mol` is not a pyscf.gto.mole.Mole instance. Notes ----- This function touches the internal components of pyscf, which may or may not be documented. This function will break as soon as the internal components change. If so, please raise an issue at https://github.com/theochem/gbasis. It is supported for, at least, pyscf version 1.6.1. """ # pylint: disable=W0212 if not (mol.__class__.__name__ == "Mole" and hasattr(mol, "_basis")): raise ValueError("`mol` must be a pyscf.gto.mole.Mole instance.") basis = [] for atom, coord in mol._atom: basis_info = mol._basis[atom] for shell in basis_info: angmom = shell[0] exps_coeffs = np.vstack(shell[1:]) exps = exps_coeffs[:, 0] coeffs = exps_coeffs[:, 1:] basis.append( GeneralizedContractionShell( angmom, np.array(coord), np.array(coeffs), np.array(exps) ) ) return tuple(basis)
def make_contractions(basis_dict, atoms, coords): """Return the contractions that correspond to the given atoms for the given basis. Parameters ---------- basis_dict : dict of str to list of 3-tuple of (int, np.ndarray, np.ndarray) Output of the parsers from gbasis.parsers. atoms : N-list/tuple of str Atoms at which the contractions are centered. coords : np.ndarray(N, 3) Coordinates of each atom. Returns ------- basis : tuple of GeneralizedContractionShell Contractions for each atom. Contractions are ordered in the same order as in the values of `basis_dict`. Raises ------ TypeError If `atoms` is not a list or tuple of strings. If `coords` is not a two-dimensional `numpy` array with 3 columns. ValueError If the length of atoms is not equal to the number of rows of `coords`. """ if not (isinstance(atoms, (list, tuple)) and all(isinstance(i, str) for i in atoms)): raise TypeError("Atoms must be provided as a list or tuple.") if not (isinstance(coords, np.ndarray) and coords.ndim == 2 and coords.shape[1] == 3): raise TypeError( "Coordinates must be provided as a two-dimensional `numpy` array with three columns." ) if len(atoms) != coords.shape[0]: raise ValueError( "Number of atoms must be equal to the number of rows in the coordinates." ) basis = [] for atom, coord in zip(atoms, coords): for angmom, exps, coeffs in basis_dict[atom]: basis.append( GeneralizedContractionShell(angmom, coord, coeffs, exps)) return tuple(basis)
def test_init(): """Test base.BaseGaussianRelatedArray.""" Test = disable_abstract(BaseGaussianRelatedArray) # noqa: N806 test = skip_init(Test) contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) assert not hasattr(test, "_axes_contractions") with pytest.raises(TypeError): Test.__init__(test, set([contractions])) with pytest.raises(ValueError): Test.__init__(test, []) with pytest.raises(TypeError): Test.__init__(test, [1]) with pytest.raises(TypeError): Test.__init__(test, [contractions.__dict__]) Test.__init__(test, [contractions]) assert test._axes_contractions == ((contractions, ), ) Test.__init__(test, [contractions, contractions]) assert test._axes_contractions == ((contractions, contractions), ) Test.__init__(test, [contractions, contractions], [contractions]) assert test._axes_contractions == ((contractions, contractions), (contractions, ))
def test_evaluate_construct_array_contraction(): """Test gbasis.evals.eval.Eval.construct_array_contraction.""" test = GeneralizedContractionShell(1, np.array([0.5, 1, 1.5]), np.array([1.0, 2.0]), np.array([0.1, 0.01])) answer = np.array([ _eval_deriv_contractions( np.array([[2, 3, 4]]), np.array([0, 0, 0]), np.array([0.5, 1, 1.5]), np.array([angmom_comp]), np.array([0.1, 0.01]), np.array([1, 2]), np.array([[ (2 * 0.1 / np.pi)**(3 / 4) * (4 * 0.1)**(1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp - 1))), (2 * 0.01 / np.pi)**(3 / 4) * (4 * 0.01)**(1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp - 1))), ]]), ) for angmom_comp in test.angmom_components_cart ]).reshape(3, 1) assert np.allclose( Eval.construct_array_contraction(points=np.array([[2, 3, 4]]), contractions=test), answer) with pytest.raises(TypeError): Eval.construct_array_contraction(points=np.array([[2, 3, 4]]), contractions=None) with pytest.raises(TypeError): Eval.construct_array_contraction(points=np.array([[2, 3, 4]]), contractions={1: 2}) with pytest.raises(TypeError): Eval.construct_array_contraction(points=np.array([2, 3, 4]), contractions=test) with pytest.raises(TypeError): Eval.construct_array_contraction(points=np.array([[3, 4]]), contractions=test)
def test_construct_array_mix(): """Test BaseFourIndex.construct_array_mix.""" contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={ "construct_array_contraction": ( lambda self, cont_one, cont_two, cont_three, cont_four, a=2: np.arange( 81, dtype=float ).reshape(1, 3, 1, 3, 1, 3, 1, 3) * a ) }, ) test = Test([contractions]) assert np.allclose(test.construct_array_spherical(), test.construct_array_mix(["spherical"])) assert np.allclose( test.construct_array_spherical(a=3), test.construct_array_mix(["spherical"], a=3) ) assert np.allclose(test.construct_array_cartesian(), test.construct_array_mix(["cartesian"])) assert np.allclose( test.construct_array_cartesian(a=3), test.construct_array_mix(["cartesian"], a=3) ) cont_one = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) cont_two = GeneralizedContractionShell(2, np.array([1, 2, 3]), np.ones(1), np.ones(1)) Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={ "construct_array_contraction": lambda self, cont_one, cont_two, cont_three, cont_four: ( np.arange( cont_one.num_cart * cont_two.num_cart * cont_three.num_cart * cont_four.num_cart * 2, dtype=float, ).reshape( 1, cont_one.num_cart, 1, cont_two.num_cart, 1, cont_three.num_cart, 1, cont_four.num_cart, 2, ) ) }, ) test = Test([cont_one, cont_two]) assert np.allclose( test.construct_array_spherical(), test.construct_array_mix(["spherical"] * 2) ) assert np.allclose( test.construct_array_cartesian(), test.construct_array_mix(["cartesian"] * 2) ) Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={ "construct_array_contraction": lambda self, cont_one, cont_two, cont_three, cont_four: ( np.arange( 2 * cont_one.num_cart * 2 * cont_two.num_cart * 2 * cont_three.num_cart * 2 * cont_four.num_cart * 2, dtype=float, ).reshape( 2, cont_one.num_cart, 2, cont_two.num_cart, 2, cont_three.num_cart, 2, cont_four.num_cart, 2, ) ) }, ) cont_one.norm_cont = np.ones((2, cont_one.num_cart)) cont_two.norm_cont = np.ones((2, cont_two.num_cart)) test = Test([cont_one, cont_two]) assert np.allclose( test.construct_array_spherical(), test.construct_array_mix(["spherical"] * 2) ) assert np.allclose( test.construct_array_cartesian(), test.construct_array_mix(["cartesian"] * 2) ) # check coord_types type with pytest.raises(TypeError): test.construct_array_mix(np.array(["cartesian"] * 2), a=3), # check coord_types content with pytest.raises(ValueError): test.construct_array_mix(["cartesian", "something"], a=3), # check coord_types length with pytest.raises(ValueError): test.construct_array_mix(["cartesian"] * 3, a=3),
def test_assign_norm_cont(): """Test GeneralizedContractionShell.assign_norm_cont.""" test = GeneralizedContractionShell(0, np.array([0, 0, 0]), np.array([1.0]), np.array([0.25])) test.assign_norm_cont() assert np.allclose(test.norm_cont, 1) test = GeneralizedContractionShell(1, np.array([0, 0, 0]), np.array([1.0]), np.array([0.25])) test.assign_norm_cont() assert np.allclose(test.norm_cont, 1) test = GeneralizedContractionShell(2, np.array([0, 0, 0]), np.array([1.0]), np.array([0.25])) test.assign_norm_cont() assert np.allclose(test.norm_cont, 1)
def test_norm_prim_cart(): """Test GeneralizedContractionShell.norm_prim_cart.""" test = GeneralizedContractionShell(0, np.array([0, 0, 0]), np.array([1.0]), np.array([0.25])) assert np.isclose(test.norm_prim_cart, 0.2519794355383807303479140) test = GeneralizedContractionShell(3, np.array([0, 0, 0]), np.array([1.0]), np.array([0.5])) assert np.isclose(test.norm_prim_cart[7], 0.6920252830162908851679097)
def test_contruct_array_cartesian(): """Test BaseTwoIndexSymmetric.construct_array_cartesian.""" contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) Test = disable_abstract( # noqa: N806 BaseTwoIndexSymmetric, dict_overwrite={ "construct_array_contraction": lambda self, cont1, cont2, a=2: np.ones((1, 2, 1, 2)) * a }, ) contractions.norm_cont = np.ones((1, 2)) test = Test([contractions]) assert np.allclose(test.construct_array_cartesian(), np.ones((2, 2)) * 2) assert np.allclose(test.construct_array_cartesian(a=3), np.ones((2, 2)) * 3) with pytest.raises(TypeError): test.construct_array_cartesian(bad_keyword=3) test = Test([contractions, contractions]) assert np.allclose(test.construct_array_cartesian(), np.ones((4, 4)) * 2) assert np.allclose(test.construct_array_cartesian(a=3), np.ones((4, 4)) * 3) cont_one = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) cont_two = GeneralizedContractionShell(2, np.array([1, 2, 3]), np.ones(1), np.ones(1)) Test = disable_abstract( # noqa: N806 BaseTwoIndexSymmetric, dict_overwrite={ "construct_array_contraction": lambda self, cont_one, cont_two, a=2: ( np.arange(cont_one.num_cart * cont_two.num_cart, dtype=float).reshape( 1, cont_one.num_cart, 1, cont_two.num_cart ) * a ) }, ) cont_one.norm_cont = np.ones((1, cont_one.num_cart)) cont_two.norm_cont = np.ones((1, cont_two.num_cart)) test = Test([cont_one, cont_two]) assert np.allclose( test.construct_array_cartesian(), np.vstack([ np.hstack([ np.arange(9).reshape(3, 3).T * 2, np.arange(18).reshape(3, 6) * 2 ]), np.hstack([ np.arange(18).reshape(3, 6).T * 2, np.arange(36).reshape(6, 6).T * 2 ]), ]), ) assert np.allclose( test.construct_array_cartesian(a=3), np.vstack([ np.hstack([ np.arange(9).reshape(3, 3).T * 3, np.arange(18).reshape(3, 6) * 3 ]), np.hstack([ np.arange(18).reshape(3, 6).T * 3, np.arange(36).reshape(6, 6).T * 3 ]), ]), ) Test = disable_abstract( # noqa: N806 BaseTwoIndexSymmetric, dict_overwrite={ "construct_array_contraction": lambda self, cont_one, cont_two, a=2: ( np.arange(cont_one.num_cart * cont_two.num_cart * 2, dtype=float).reshape( 1, cont_one.num_cart, 1, cont_two.num_cart, 2 ) * a ) }, ) test = Test([cont_one, cont_two]) assert np.allclose( test.construct_array_cartesian(), np.vstack([ np.hstack([ np.swapaxes(np.arange(18).reshape(3, 3, 2), 0, 1) * 2, np.arange(36).reshape(3, 6, 2) * 2, ]), np.hstack([ np.swapaxes(np.arange(36).reshape(3, 6, 2), 0, 1) * 2, np.swapaxes(np.arange(72).reshape(6, 6, 2), 0, 1) * 2, ]), ]), ) assert np.allclose( test.construct_array_cartesian(a=3), np.concatenate( [ np.concatenate( [ np.swapaxes(np.arange(18).reshape(3, 3, 2), 0, 1) * 3, np.arange(36).reshape(3, 6, 2) * 3, ], axis=1, ), np.concatenate( [ np.swapaxes(np.arange(36).reshape(3, 6, 2), 0, 1) * 3, np.swapaxes(np.arange(72).reshape(6, 6, 2), 0, 1) * 3, ], axis=1, ), ], axis=0, ), ) Test = disable_abstract( # noqa: N806 BaseTwoIndexSymmetric, dict_overwrite={ # NOTE: assume that cont_one and cont_two will always be cont_one and cont_two defined # above "construct_array_contraction": lambda self, cont_one, cont_two, a=2: np.arange( 2 * cont_one.num_cart * 2 * cont_two.num_cart, dtype=float ).reshape(2, cont_one.num_cart, 2, cont_two.num_cart) * a }, ) cont_one.norm_cont = np.ones((2, cont_one.num_cart)) cont_two.norm_cont = np.ones((2, cont_two.num_cart)) test = Test([cont_one, cont_two]) matrix_11 = np.arange(2 * cont_one.num_cart * 2 * cont_one.num_cart).reshape(2, cont_one.num_cart, 2, cont_one.num_cart) matrix_12 = np.arange(2 * cont_one.num_cart * 2 * cont_two.num_cart).reshape(2, cont_one.num_cart, 2, cont_two.num_cart) matrix_22 = np.arange(2 * cont_two.num_cart * 2 * cont_two.num_cart).reshape(2, cont_two.num_cart, 2, cont_two.num_cart) assert np.allclose( test.construct_array_cartesian(), np.vstack([ np.hstack([ np.vstack([ np.hstack([matrix_11[0, :, 0, :], matrix_11[0, :, 1, :]]), np.hstack([matrix_11[1, :, 0, :], matrix_11[1, :, 1, :]]), ]).T, np.vstack([ np.hstack([matrix_12[0, :, 0, :], matrix_12[0, :, 1, :]]), np.hstack([matrix_12[1, :, 0, :], matrix_12[1, :, 1, :]]), ]), ]), np.hstack([ np.vstack([ np.hstack([matrix_12[0, :, 0, :], matrix_12[0, :, 1, :]]), np.hstack([matrix_12[1, :, 0, :], matrix_12[1, :, 1, :]]), ]).T, np.vstack([ np.hstack([matrix_22[0, :, 0, :], matrix_22[0, :, 1, :]]), np.hstack([matrix_22[1, :, 0, :], matrix_22[1, :, 1, :]]), ]).T, ]), ]) * 2, )
def test_construct_array_spherical(): """Test BaseFourIndexSymmetric.construct_array_spherical.""" contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) transform = generate_transformation( 1, contractions.angmom_components_cart, contractions.angmom_components_sph, "left" ) # make symmetric array = np.arange(81, dtype=float).reshape(3, 3, 3, 3) array += np.einsum("ijkl->jikl", array) array += np.einsum("ijkl->ijlk", array) array += np.einsum("ijkl->klij", array) Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={ "construct_array_contraction": ( lambda self, cont_one, cont_two, cont_three, cont_four, a=2: array.reshape( 1, 3, 1, 3, 1, 3, 1, 3 ) * a ) }, ) contractions.norm_cont = np.ones((1, 3)) test = Test([contractions]) assert np.allclose( test.construct_array_spherical(), np.einsum("ijkl,ai,bj,ck,dl->abcd", array, transform, transform, transform, transform) * 2, ) assert np.allclose( test.construct_array_spherical(a=3), np.einsum("ijkl,ai,bj,ck,dl->abcd", array, transform, transform, transform, transform) * 3, ) with pytest.raises(TypeError): test.construct_array_spherical(bad_keyword=3) cont_one = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) cont_two = GeneralizedContractionShell(2, np.array([1, 2, 3]), np.ones(1), np.ones(1)) transform_one = generate_transformation( 1, cont_one.angmom_components_cart, cont_one.angmom_components_sph, "left" ) transform_two = generate_transformation( 2, cont_two.angmom_components_cart, cont_two.angmom_components_sph, "left" ) Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={ "construct_array_contraction": lambda self, cont_one, cont_two, cont_three, cont_four: ( np.arange( cont_one.num_cart * cont_two.num_cart * cont_three.num_cart * cont_four.num_cart * 2, dtype=float, ).reshape( 1, cont_one.num_cart, 1, cont_two.num_cart, 1, cont_three.num_cart, 1, cont_four.num_cart, 2, ) ) }, ) cont_one.norm_cont = np.ones((1, cont_one.num_cart)) cont_two.norm_cont = np.ones((1, cont_two.num_cart)) test = Test([cont_one, cont_two]) # NOTE: since the test subarray (output of construct_array_contraction) does not satisfy the # symmetries of the two electron integral, only the last permutation is used. If this output # satisfies the symmetries of two electron integrals, then all these permutations should result # in the same array. # FIXME: not a good test assert np.allclose( test.construct_array_spherical()[:3, :3, :3, :3], np.einsum( "ijklm->lkjim", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 3 * 3 * 3 * 2).reshape(3, 3, 3, 3, 2), transform_one, transform_one, transform_one, transform_one, ), ), ) assert np.allclose( test.construct_array_spherical()[:3, :3, :3, 3:], np.einsum( "ijklm->jiklm", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 3 * 3 * 6 * 2).reshape(3, 3, 3, 6, 2), transform_one, transform_one, transform_one, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[:3, :3, 3:, :3], np.einsum( "ijklm->jilkm", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 3 * 3 * 6 * 2).reshape(3, 3, 3, 6, 2), transform_one, transform_one, transform_one, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[:3, :3, 3:, 3:], np.einsum( "ijklm->jilkm", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 3 * 6 * 6 * 2).reshape(3, 3, 6, 6, 2), transform_one, transform_one, transform_two, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[:3, 3:, :3, :3], np.einsum( "ijklm->kljim", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 3 * 3 * 6 * 2).reshape(3, 3, 3, 6, 2), transform_one, transform_one, transform_one, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[:3, 3:, :3, 3:], np.einsum( "ijklm->klijm", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 6 * 3 * 6 * 2).reshape(3, 6, 3, 6, 2), transform_one, transform_two, transform_one, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[:3, 3:, 3:, :3], np.einsum( "ijklm->kljim", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 6 * 3 * 6 * 2).reshape(3, 6, 3, 6, 2), transform_one, transform_two, transform_one, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[:3, 3:, 3:, 3:], np.einsum( "ijklm->ijlkm", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 6 * 6 * 6 * 2).reshape(3, 6, 6, 6, 2), transform_one, transform_two, transform_two, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[3:, :3, :3, :3], np.einsum( "ijklm->lkjim", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 3 * 3 * 6 * 2).reshape(3, 3, 3, 6, 2), transform_one, transform_one, transform_one, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[3:, :3, :3, 3:], np.einsum( "ijklm->lkijm", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 6 * 3 * 6 * 2).reshape(3, 6, 3, 6, 2), transform_one, transform_two, transform_one, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[3:, :3, 3:, :3], np.einsum( "ijklm->lkjim", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 6 * 3 * 6 * 2).reshape(3, 6, 3, 6, 2), transform_one, transform_two, transform_one, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[3:, :3, 3:, 3:], np.einsum( "ijklm->jilkm", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 6 * 6 * 6 * 2).reshape(3, 6, 6, 6, 2), transform_one, transform_two, transform_two, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[3:, 3:, :3, :3], np.einsum( "ijklm->lkjim", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 3 * 6 * 6 * 2).reshape(3, 3, 6, 6, 2), transform_one, transform_one, transform_two, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[3:, 3:, :3, 3:], np.einsum( "ijklm->lkijm", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 6 * 6 * 6 * 2).reshape(3, 6, 6, 6, 2), transform_one, transform_two, transform_two, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[3:, 3:, 3:, :3], np.einsum( "ijklm->lkjim", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(3 * 6 * 6 * 6 * 2).reshape(3, 6, 6, 6, 2), transform_one, transform_two, transform_two, transform_two, ), ), ) assert np.allclose( test.construct_array_spherical()[3:, 3:, 3:, 3:], np.einsum( "ijklm->lkjim", np.einsum( "ijklm,ai,bj,ck,dl->abcdm", np.arange(6 * 6 * 6 * 6 * 2).reshape(6, 6, 6, 6, 2), transform_two, transform_two, transform_two, transform_two, ), ), )
def test_construct_array_lincomb(): """Test BaseFourIndexSymmetric.construct_array_lincomb.""" contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) sph_transform = generate_transformation( 1, contractions.angmom_components_cart, contractions.angmom_components_sph, "left" ) orb_transform = np.random.rand(3, 3) Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={ "construct_array_contraction": ( lambda self, cont_one, cont_two, cont_three, cont_four, a=2: np.arange( 81, dtype=float ).reshape(1, 3, 1, 3, 1, 3, 1, 3) * a ) }, ) contractions.norm_cont = np.ones((1, 3)) test = Test([contractions]) assert np.allclose( test.construct_array_lincomb(orb_transform, "cartesian"), np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.einsum("ijkl->lkji", np.arange(81).reshape(3, 3, 3, 3)) * 2, orb_transform, orb_transform, orb_transform, orb_transform, ), ) assert np.allclose( test.construct_array_lincomb(orb_transform, "spherical"), np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.einsum("ijkl->lkji", np.arange(81).reshape(3, 3, 3, 3)) * 2, sph_transform, sph_transform, sph_transform, sph_transform, ), orb_transform, orb_transform, orb_transform, orb_transform, ), ) with pytest.raises(TypeError): test.construct_array_lincomb(orb_transform, "spherical", bad_keyword=3) with pytest.raises(TypeError): test.construct_array_lincomb(orb_transform, "bad", keyword=3) Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={ "construct_array_contraction": lambda self, cont_one, cont_two, cont_three, cont_four: ( np.arange( cont_one.num_cart * cont_two.num_cart * cont_three.num_cart * cont_four.num_cart, dtype=float, ).reshape( 1, cont_one.num_cart, 1, cont_two.num_cart, 1, cont_three.num_cart, 1, cont_four.num_cart, ) ) }, ) cont_one = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) cont_two = GeneralizedContractionShell(2, np.array([1, 2, 3]), np.ones(1), np.ones(1)) cont_one.norm_cont = np.ones((1, cont_one.num_cart)) cont_two.norm_cont = np.ones((1, cont_two.num_cart)) test = Test([cont_one, cont_two]) sph_transform_one = generate_transformation( 1, cont_one.angmom_components_cart, cont_one.angmom_components_sph, "left" ) sph_transform_two = generate_transformation( 2, cont_two.angmom_components_cart, cont_two.angmom_components_sph, "left" ) orb_transform = np.random.rand(8, 8) # NOTE: since the test subarray (output of construct_array_contraction) does not satisfy the # symmetries of the two electron integral, only the last permutation is used. If this output # satisfies the symmetries of two electron integrals, then all these permutations should result # in the same array. # FIXME: not a good test sph_array = np.concatenate( [ np.concatenate( [ np.concatenate( [ np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3), sph_transform_one, sph_transform_one, sph_transform_one, sph_transform_one, ), ), np.einsum( "ijkl->jikl", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 3 * 3 * 6).reshape(3, 3, 3, 6), sph_transform_one, sph_transform_one, sph_transform_one, sph_transform_two, ), ), ], axis=3, ), np.concatenate( [ np.einsum( "ijkl->jilk", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 3 * 3 * 6).reshape(3, 3, 3, 6), sph_transform_one, sph_transform_one, sph_transform_one, sph_transform_two, ), ), np.einsum( "ijkl->jilk", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 3 * 6 * 6).reshape(3, 3, 6, 6), sph_transform_one, sph_transform_one, sph_transform_two, sph_transform_two, ), ), ], axis=3, ), ], axis=2, ), np.concatenate( [ np.concatenate( [ np.einsum( "ijkl->klji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 3 * 3 * 6).reshape(3, 3, 3, 6), sph_transform_one, sph_transform_one, sph_transform_one, sph_transform_two, ), ), np.einsum( "ijkl->klij", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 6 * 3 * 6).reshape(3, 6, 3, 6), sph_transform_one, sph_transform_two, sph_transform_one, sph_transform_two, ), ), ], axis=3, ), np.concatenate( [ np.einsum( "ijkl->klji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 6 * 3 * 6).reshape(3, 6, 3, 6), sph_transform_one, sph_transform_two, sph_transform_one, sph_transform_two, ), ), np.einsum( "ijkl->ijlk", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 6 * 6 * 6).reshape(3, 6, 6, 6), sph_transform_one, sph_transform_two, sph_transform_two, sph_transform_two, ), ), ], axis=3, ), ], axis=2, ), ], axis=1, ), np.concatenate( [ np.concatenate( [ np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 3 * 3 * 6).reshape(3, 3, 3, 6), sph_transform_one, sph_transform_one, sph_transform_one, sph_transform_two, ), ), np.einsum( "ijkl->lkij", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 6 * 3 * 6).reshape(3, 6, 3, 6), sph_transform_one, sph_transform_two, sph_transform_one, sph_transform_two, ), ), ], axis=3, ), np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 6 * 3 * 6).reshape(3, 6, 3, 6), sph_transform_one, sph_transform_two, sph_transform_one, sph_transform_two, ), ), np.einsum( "ijkl->jilk", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 6 * 6 * 6).reshape(3, 6, 6, 6), sph_transform_one, sph_transform_two, sph_transform_two, sph_transform_two, ), ), ], axis=3, ), ], axis=2, ), np.concatenate( [ np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 3 * 6 * 6).reshape(3, 3, 6, 6), sph_transform_one, sph_transform_one, sph_transform_two, sph_transform_two, ), ), np.einsum( "ijkl->lkij", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 6 * 6 * 6).reshape(3, 6, 6, 6), sph_transform_one, sph_transform_two, sph_transform_two, sph_transform_two, ), ), ], axis=3, ), np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 6 * 6 * 6).reshape(3, 6, 6, 6), sph_transform_one, sph_transform_two, sph_transform_two, sph_transform_two, ), ), np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(6 * 6 * 6 * 6).reshape(6, 6, 6, 6), sph_transform_two, sph_transform_two, sph_transform_two, sph_transform_two, ), ), ], axis=3, ), ], axis=2, ), ], axis=1, ), ] ) assert np.allclose( test.construct_array_lincomb(orb_transform, "spherical"), np.einsum( "ijkl,ai,bj,ck,dl->abcd", sph_array, orb_transform, orb_transform, orb_transform, orb_transform, ), ) orb_transform = np.random.rand(9, 9) sph_cart_array = np.concatenate( [ np.concatenate( [ np.concatenate( [ np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj,ck,dl->abcd", np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3), sph_transform_one, sph_transform_one, sph_transform_one, sph_transform_one, ), ), np.einsum( "ijkl->jikl", np.einsum( "ijkl,ai,bj,ck->abcl", np.arange(3 * 3 * 3 * 6).reshape(3, 3, 3, 6), sph_transform_one, sph_transform_one, sph_transform_one, ), ), ], axis=3, ), np.concatenate( [ np.einsum( "ijkl->jilk", np.einsum( "ijkl,ai,bj,ck->abcl", np.arange(3 * 3 * 3 * 6).reshape(3, 3, 3, 6), sph_transform_one, sph_transform_one, sph_transform_one, ), ), np.einsum( "ijkl->jilk", np.einsum( "ijkl,ai,bj->abkl", np.arange(3 * 3 * 6 * 6).reshape(3, 3, 6, 6), sph_transform_one, sph_transform_one, ), ), ], axis=3, ), ], axis=2, ), np.concatenate( [ np.concatenate( [ np.einsum( "ijkl->klji", np.einsum( "ijkl,ai,bj,ck->abcl", np.arange(3 * 3 * 3 * 6).reshape(3, 3, 3, 6), sph_transform_one, sph_transform_one, sph_transform_one, ), ), np.einsum( "ijkl->klij", np.einsum( "ijkl,ai,ck->ajcl", np.arange(3 * 6 * 3 * 6).reshape(3, 6, 3, 6), sph_transform_one, sph_transform_one, ), ), ], axis=3, ), np.concatenate( [ np.einsum( "ijkl->klji", np.einsum( "ijkl,ai,ck->ajcl", np.arange(3 * 6 * 3 * 6).reshape(3, 6, 3, 6), sph_transform_one, sph_transform_one, ), ), np.einsum( "ijkl->ijlk", np.einsum( "ijkl,ai->ajkl", np.arange(3 * 6 * 6 * 6).reshape(3, 6, 6, 6), sph_transform_one, ), ), ], axis=3, ), ], axis=2, ), ], axis=1, ), np.concatenate( [ np.concatenate( [ np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj,ck->abcl", np.arange(3 * 3 * 3 * 6).reshape(3, 3, 3, 6), sph_transform_one, sph_transform_one, sph_transform_one, ), ), np.einsum( "ijkl->lkij", np.einsum( "ijkl,ai,ck->ajcl", np.arange(3 * 6 * 3 * 6).reshape(3, 6, 3, 6), sph_transform_one, sph_transform_one, ), ), ], axis=3, ), np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,ck->ajcl", np.arange(3 * 6 * 3 * 6).reshape(3, 6, 3, 6), sph_transform_one, sph_transform_one, ), ), np.einsum( "ijkl->jilk", np.einsum( "ijkl,ai->ajkl", np.arange(3 * 6 * 6 * 6).reshape(3, 6, 6, 6), sph_transform_one, ), ), ], axis=3, ), ], axis=2, ), np.concatenate( [ np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai,bj->abkl", np.arange(3 * 3 * 6 * 6).reshape(3, 3, 6, 6), sph_transform_one, sph_transform_one, ), ), np.einsum( "ijkl->lkij", np.einsum( "ijkl,ai->ajkl", np.arange(3 * 6 * 6 * 6).reshape(3, 6, 6, 6), sph_transform_one, ), ), ], axis=3, ), np.concatenate( [ np.einsum( "ijkl->lkji", np.einsum( "ijkl,ai->ajkl", np.arange(3 * 6 * 6 * 6).reshape(3, 6, 6, 6), sph_transform_one, ), ), np.einsum( "ijkl->lkji", np.arange(6 * 6 * 6 * 6).reshape(6, 6, 6, 6) ), ], axis=3, ), ], axis=2, ), ], axis=1, ), ] ) assert np.allclose( test.construct_array_lincomb(orb_transform, ("spherical", "cartesian")), np.einsum( "ijkl,ai,bj,ck,dl->abcd", sph_cart_array, orb_transform, orb_transform, orb_transform, orb_transform, ), )
def test_construct_array_mix_with_both_cartesian_and_spherical(): r"""Test construct_array_mix with both a P-Type Cartesian and D-Type Spherical contractions.""" num_pts, num_seg_shell = 1, 1 # Define the coefficients used to seperate which contraction block it is, # Put it in a dictionary to avoid doing so many nested if-statements. coeff_p_p_p_p_type = 2 coeff_p_p_p_d_type = 4 coeff_p_p_d_d_type = 5 coeff_p_d_p_p_type = 6 coeff_p_d_p_d_type = 8 coeff_p_d_d_d_type = 10 coeff_d_d_p_p_type = 12 coeff_d_d_p_d_type = 14 coeff_d_d_d_d_type = 16 coeff_dict = { "1111": coeff_p_p_p_p_type, "1112": coeff_p_p_p_d_type, "1122": coeff_p_p_d_d_type, "1211": coeff_p_d_p_p_type, "1212": coeff_p_d_p_d_type, "1222": coeff_p_d_d_d_type, "2211": coeff_d_d_p_p_type, "2212": coeff_d_d_p_d_type, "2222": coeff_d_d_d_d_type, } def construct_array_cont(self, cont_one, cont_two, cont_three, cont_four): output = np.ones( cont_one.num_cart * cont_two.num_cart * cont_three.num_cart * cont_four.num_cart * num_pts, dtype=float, ).reshape( num_seg_shell, cont_one.num_cart, num_seg_shell, cont_two.num_cart, num_seg_shell, cont_three.num_cart, num_seg_shell, cont_four.num_cart, num_pts, ) identifier = ( str(cont_one.angmom) + str(cont_two.angmom) + str(cont_three.angmom) + str(cont_four.angmom) ) return output * coeff_dict[identifier] Test = disable_abstract( # noqa: N806 BaseFourIndexSymmetric, dict_overwrite={"construct_array_contraction": construct_array_cont}, ) cont_one = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) cont_two = GeneralizedContractionShell(2, np.array([1, 2, 3]), np.ones(1), np.ones(1)) # Remove the dependence on norm constants. cont_one.norm_cont = np.ones((1, cont_one.num_cart)) cont_two.norm_cont = np.ones((1, cont_two.num_cart)) test = Test([cont_one, cont_two]) # Should have shape (3 + 5, 3 + 5, NUM_PTS), due to the following: # 3-> Number of P-type, 5->Number of Spherical D-type. actual = test.construct_array_mix(["cartesian", "spherical"])[:, :, :, :, 0] # Test P-type to P-type to P-Type To P-type i.e. (P, P, P, P) assert np.allclose(actual[:3, :3, :3, :3], np.ones((3, 3)) * coeff_p_p_p_p_type) # Test (P, P, P, D) # Transformation matrix from normalized Cartesian to normalized Spherical, # Transfers [xx, xy, xz, yy, yz, zz] to [S_{22}, S_{21}, C_{20}, C_{21}, C_{22}] # Obtained form iodata website or can find it in Helgeker's book. generate_transformation_array = np.array( [ [0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0], [-0.5, 0, 0, -0.5, 0, 1], [0, 0, 1, 0, 0, 0], [np.sqrt(3.0) / 2.0, 0, 0, -np.sqrt(3.0) / 2.0, 0, 0], ] ) assert np.allclose( actual[:3, :3, :3, 3:], np.ones((3, 3, 3, 6)).dot(generate_transformation_array.T) * coeff_p_p_p_d_type, ) assert np.allclose( actual[:3, :3, 3:, :3], np.einsum("ij,mnjl->mnil", generate_transformation_array, np.ones((3, 3, 6, 3))) * coeff_p_p_p_d_type, ) # Test (P, P, D, D), (D, D, P, P) assert np.allclose( actual[:3, :3, 3:, 3:], np.einsum( "ij,mnjl,pl->mnip", generate_transformation_array, np.ones((3, 3, 6, 6)), generate_transformation_array, ) * coeff_p_p_d_d_type, ) assert np.allclose(actual[3:, 3:, :3, :3], actual[:3, :3, 3:, 3:].T) # Symmetry # Test (P, D, P, D) assert np.allclose( actual[:3, 3:, :3, 3:], np.einsum( "ij,mjnl,pl->minp", generate_transformation_array, np.ones((3, 6, 3, 6)), generate_transformation_array, ) * coeff_p_d_p_d_type, ) # Test (P, D, D, D), & (D, D, P, D) assert np.allclose( actual[:3, 3:, 3:, 3:], np.einsum( "in,mnjl,pl,oj->miop", generate_transformation_array, np.ones((3, 6, 6, 6)), generate_transformation_array, generate_transformation_array, ) * coeff_p_d_d_d_type, ) assert np.allclose(actual[3:, 3:, :3, 3:], np.einsum("ijkl->klij", actual[:3, 3:, 3:, 3:])) # Test (D, D, D, D) assert np.allclose( actual[3:, 3:, 3:, 3:], np.einsum( "dm,in,mnjl,pl,oj->diop", generate_transformation_array, generate_transformation_array, np.ones((6, 6, 6, 6)), generate_transformation_array, generate_transformation_array, ) * coeff_d_d_d_d_type, )
def test_kinetic_energy_construct_array_contraction(): """Test gbasis.integrals.kinetic_energy.KineticEnergyIntegral.construct_array_contraction.""" test_one = GeneralizedContractionShell(1, np.array([0.5, 1, 1.5]), np.array([1.0, 2.0]), np.array([0.1, 0.01])) test_two = GeneralizedContractionShell(2, np.array([1.5, 2, 3]), np.array([3.0, 4.0]), np.array([0.2, 0.02])) answer = np.array([[ _compute_differential_operator_integrals( np.array([[2, 0, 0]]), np.array([0.5, 1, 1.5]), np.array([angmom_comp_one]), np.array([0.1, 0.01]), np.array([[1], [2]]), np.array([[ (2 * 0.1 / np.pi)**(3 / 4) * (4 * 0.1)**(1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_one - 1))), (2 * 0.01 / np.pi)**(3 / 4) * (4 * 0.01)**(1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_one - 1))), ]]), np.array([1.5, 2, 3]), np.array([angmom_comp_two]), np.array([0.2, 0.02]), np.array([[3], [4]]), np.array([[ (2 * 0.2 / np.pi)**(3 / 4) * (4 * 0.2)**(2 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_two - 1))), (2 * 0.02 / np.pi)**(3 / 4) * (4 * 0.02)**(2 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_two - 1))), ]]), ) for angmom_comp_two in test_two.angmom_components_cart ] for angmom_comp_one in test_one.angmom_components_cart]) answer += np.array([[ _compute_differential_operator_integrals( np.array([[0, 2, 0]]), np.array([0.5, 1, 1.5]), np.array([angmom_comp_one]), np.array([0.1, 0.01]), np.array([[1], [2]]), np.array([[ (2 * 0.1 / np.pi)**(3 / 4) * (4 * 0.1)**(1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_one - 1))), (2 * 0.01 / np.pi)**(3 / 4) * (4 * 0.01)**(1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_one - 1))), ]]), np.array([1.5, 2, 3]), np.array([angmom_comp_two]), np.array([0.2, 0.02]), np.array([[3], [4]]), np.array([[ (2 * 0.2 / np.pi)**(3 / 4) * (4 * 0.2)**(2 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_two - 1))), (2 * 0.02 / np.pi)**(3 / 4) * (4 * 0.02)**(2 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_two - 1))), ]]), ) for angmom_comp_two in test_two.angmom_components_cart ] for angmom_comp_one in test_one.angmom_components_cart]) answer += np.array([[ _compute_differential_operator_integrals( np.array([[0, 0, 2]]), np.array([0.5, 1, 1.5]), np.array([angmom_comp_one]), np.array([0.1, 0.01]), np.array([[1], [2]]), np.array([[ (2 * 0.1 / np.pi)**(3 / 4) * (4 * 0.1)**(1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_one - 1))), (2 * 0.01 / np.pi)**(3 / 4) * (4 * 0.01)**(1 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_one - 1))), ]]), np.array([1.5, 2, 3]), np.array([angmom_comp_two]), np.array([0.2, 0.02]), np.array([[3], [4]]), np.array([[ (2 * 0.2 / np.pi)**(3 / 4) * (4 * 0.2)**(2 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_two - 1))), (2 * 0.02 / np.pi)**(3 / 4) * (4 * 0.02)**(2 / 2) / np.sqrt(np.prod(factorial2(2 * angmom_comp_two - 1))), ]]), ) for angmom_comp_two in test_two.angmom_components_cart ] for angmom_comp_one in test_one.angmom_components_cart]) assert np.allclose( np.squeeze( KineticEnergyIntegral.construct_array_contraction( test_one, test_two)), np.squeeze(-0.5 * answer), ) with pytest.raises(TypeError): KineticEnergyIntegral.construct_array_contraction(test_one, None) with pytest.raises(TypeError): KineticEnergyIntegral.construct_array_contraction(None, test_two)
def test_contruct_array_mix(): """Test BaseOneIndex.construct_array_mix.""" contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={ "construct_array_contraction": lambda self, cont, a=2: np.arange( 9, dtype=float ).reshape(1, 3, 3) * a }, ) test = Test([contractions]) assert np.allclose(test.construct_array_spherical(), test.construct_array_mix(["spherical"])) assert np.allclose( test.construct_array_spherical(a=3), test.construct_array_mix(["spherical"], a=3) ) assert np.allclose(test.construct_array_cartesian(), test.construct_array_mix(["cartesian"])) assert np.allclose( test.construct_array_cartesian(a=3), test.construct_array_mix(["cartesian"], a=3) ) test = Test([contractions, contractions]) assert np.allclose( test.construct_array_spherical(), test.construct_array_mix(["spherical"] * 2) ) assert np.allclose( test.construct_array_spherical(a=3), test.construct_array_mix(["spherical"] * 2, a=3) ) assert np.allclose( test.construct_array_cartesian(), test.construct_array_mix(["cartesian"] * 2) ) assert np.allclose( test.construct_array_cartesian(a=3), test.construct_array_mix(["cartesian"] * 2, a=3) ) contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones((1, 2)), np.ones(1)) Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={ "construct_array_contraction": ( lambda self, cont, a=2: np.arange(18, dtype=float).reshape(2, 3, 3) * a ) }, ) test = Test([contractions]) assert np.allclose(test.construct_array_spherical(), test.construct_array_mix(["spherical"])) assert np.allclose( test.construct_array_spherical(a=3), test.construct_array_mix(["spherical"], a=3) ) assert np.allclose(test.construct_array_cartesian(), test.construct_array_mix(["cartesian"])) assert np.allclose( test.construct_array_cartesian(a=3), test.construct_array_mix(["cartesian"], a=3) ) test = Test([contractions, contractions]) assert np.allclose( test.construct_array_spherical(), test.construct_array_mix(["spherical"] * 2) ) assert np.allclose( test.construct_array_spherical(a=3), test.construct_array_mix(["spherical"] * 2, a=3) ) assert np.allclose( test.construct_array_cartesian(), test.construct_array_mix(["cartesian"] * 2) ) assert np.allclose( test.construct_array_cartesian(a=3), test.construct_array_mix(["cartesian"] * 2, a=3) ) # check coord_types type with pytest.raises(TypeError): test.construct_array_mix(np.array(["cartesian"] * 2), a=3), # check coord_types content with pytest.raises(ValueError): test.construct_array_mix(["cartesian", "something"], a=3), # check coord_types length with pytest.raises(ValueError): test.construct_array_mix(["cartesian"] * 3, a=3),
def test_compute_one_elec_int_s_type(): """Test _one_elec_int._compute_one_electron_integrals for s-type primitives.""" # GeneralizedContractionShell(angmom, coord, charge, coeffs, exps) s_type_one = GeneralizedContractionShell(1, np.array([0.5, 1, 1.5]), np.array([1.0]), np.array([0.1])) s_type_two = GeneralizedContractionShell(1, np.array([1.5, 2, 3]), np.array([3.0]), np.array([0.02])) coord_a = s_type_one.coord angmom_a = s_type_one.angmom exps_a = s_type_one.exps coeffs_a = s_type_one.coeffs coord_b = s_type_two.coord angmom_b = s_type_two.angmom exps_b = s_type_two.exps coeffs_b = s_type_two.coeffs s_test = _compute_one_elec_integrals( np.array([0.0, 0.0, 0.0]).reshape(1, 3), # coord_point boys_func, coord_a, angmom_a, exps_a, coeffs_a, coord_b, angmom_b, exps_b, coeffs_b, ) # Test output array using hand-calculated values p = 0.12 x_pa = 1 / 6 y_pa = 1 / 6 z_pa = 1 / 4 x_ab = -1 y_ab = -1 z_ab = -1.5 x_pc = 2 / 3 y_pc = 14 / 12 z_pc = 7 / 4 coeff = 3.0 norm_1_a = (2 * exps_a / np.pi)**(3 / 4) * (4 * exps_a)**(angmom_a / 2) norm_1_b = (2 * exps_b / np.pi)**(3 / 4) * (4 * exps_b)**(angmom_b / 2) norm_coeff = coeff * norm_1_a * norm_1_b # Norm_2 is always 1 and thus is omitted from calculations v0 = (2 * np.pi / p) * np.exp(-17 / 240) * hyp1f1(1 / 2, 3 / 2, -701 / 1200) v1 = ((2 * np.pi / p) * np.exp(-17 / 240) * hyp1f1(1 + 1 / 2, 1 + 3 / 2, -701 / 1200) / (2 * 1 + 1)) v2 = ((2 * np.pi / p) * np.exp(-17 / 240) * hyp1f1(2 + 1 / 2, 2 + 3 / 2, -701 / 1200) / (2 * 2 + 1)) v_100 = x_pa * v0 - x_pc * v1 v1_100 = x_pa * v1 - x_pc * v2 v_200 = x_pa * v_100 - x_pc * v1_100 + (1 / (2 * p)) * (v0 - v1) v_010 = y_pa * v0 - y_pc * v1 v1_010 = y_pa * v1 - y_pc * v2 v_020 = y_pa * v_010 - y_pc * v1_010 + (1 / (2 * p)) * (v0 - v1) v_001 = z_pa * v0 - z_pc * v1 v1_001 = z_pa * v1 - z_pc * v2 v_002 = z_pa * v_001 - z_pc * v1_001 + (1 / (2 * p)) * (v0 - v1) v_011 = z_pa * v_010 - z_pc * v1_010 v_110 = y_pa * v_100 - y_pc * v1_100 v_101 = z_pa * v_100 - z_pc * v1_100 assert np.allclose(s_test[1, 0, 0, 1, 0, 0], norm_coeff * v_200 + x_ab * norm_coeff * v_100) assert np.allclose(s_test[1, 0, 0, 0, 1, 0], norm_coeff * v_110 + y_ab * norm_coeff * v_100) assert np.allclose(s_test[1, 0, 0, 0, 0, 1], norm_coeff * v_101 + z_ab * norm_coeff * v_100) assert np.allclose(s_test[0, 1, 0, 1, 0, 0], norm_coeff * v_110 + x_ab * norm_coeff * v_010) assert np.allclose(s_test[0, 1, 0, 0, 1, 0], norm_coeff * v_020 + y_ab * norm_coeff * v_010) assert np.allclose(s_test[0, 1, 0, 0, 0, 1], norm_coeff * v_011 + z_ab * norm_coeff * v_010) assert np.allclose(s_test[0, 0, 1, 1, 0, 0], norm_coeff * v_101 + x_ab * norm_coeff * v_001) assert np.allclose(s_test[0, 0, 1, 0, 1, 0], norm_coeff * v_011 + y_ab * norm_coeff * v_001) assert np.allclose(s_test[0, 0, 1, 0, 0, 1], norm_coeff * v_002 + z_ab * norm_coeff * v_001)
def test_compare_two_asymm(): """Test BaseTwoIndexSymmetric by comparing it against BaseTwoIndexAsymmetric.""" cont_one = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) cont_two = GeneralizedContractionShell(2, np.array([1, 2, 3]), np.ones(1), np.ones(1)) sph_orb_transform = np.random.rand(8, 8) cart_orb_transform = np.random.rand(9, 9) def construct_array_contraction(self, cont_one, cont_two, a=2): """Temporary symmetric function for testing.""" one_size = cont_one.num_cart two_size = cont_two.num_cart output = (np.arange(one_size)[None, :, None, None, None] + np.arange(two_size)[None, None, None, :, None] + np.arange(5, 10)[None, None, None, None, :]).astype(float) return output * a TestSymmetric = disable_abstract( # noqa: N806 BaseTwoIndexSymmetric, dict_overwrite={ "construct_array_contraction": construct_array_contraction }, ) TestAsymmetric = disable_abstract( # noqa: N806 BaseTwoIndexAsymmetric, dict_overwrite={ "construct_array_contraction": construct_array_contraction }, ) cont_one.norm_cont = np.ones((1, cont_one.num_cart)) cont_two.norm_cont = np.ones((1, cont_two.num_cart)) test_symm = TestSymmetric([cont_one, cont_two]) test_asymm = TestAsymmetric([cont_one, cont_two], [cont_one, cont_two]) assert np.allclose( test_symm.construct_array_contraction(cont_one, cont_one), test_asymm.construct_array_contraction(cont_one, cont_one), ) assert np.allclose( test_symm.construct_array_contraction(cont_one, cont_two), test_asymm.construct_array_contraction(cont_one, cont_two), ) assert np.allclose( test_symm.construct_array_contraction(cont_two, cont_one), test_asymm.construct_array_contraction(cont_two, cont_one), ) assert np.allclose( test_symm.construct_array_contraction(cont_two, cont_two), test_asymm.construct_array_contraction(cont_two, cont_two), ) assert np.allclose(test_symm.construct_array_cartesian(), test_asymm.construct_array_cartesian()) assert np.allclose(test_symm.construct_array_spherical(), test_asymm.construct_array_spherical()) assert np.allclose( test_symm.construct_array_lincomb(sph_orb_transform, "spherical"), test_asymm.construct_array_lincomb(sph_orb_transform, sph_orb_transform, "spherical", "spherical"), ) assert np.allclose( test_symm.construct_array_lincomb(cart_orb_transform, "cartesian"), test_asymm.construct_array_lincomb(cart_orb_transform, cart_orb_transform, "cartesian", "cartesian"), )
def test_contruct_array_spherical(): """Test BaseOneIndex.construct_array_spherical.""" contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones(1), np.ones(1)) transform = generate_transformation( 1, contractions.angmom_components_cart, contractions.angmom_components_sph, "left" ) Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={ "construct_array_contraction": lambda self, cont, a=2: np.arange( 9, dtype=float ).reshape(1, 3, 3) * a }, ) test = Test([contractions]) assert np.allclose( test.construct_array_spherical(), transform.dot(np.arange(9).reshape(3, 3)) * 2 ) assert np.allclose( test.construct_array_spherical(a=3), transform.dot(np.arange(9).reshape(3, 3)) * 3 ) with pytest.raises(TypeError): test.construct_array_spherical(bad_keyword=3) test = Test([contractions, contractions]) assert np.allclose( test.construct_array_spherical(), np.vstack([transform.dot(np.arange(9).reshape(3, 3)) * 2] * 2), ) contractions = GeneralizedContractionShell(1, np.array([1, 2, 3]), np.ones((1, 2)), np.ones(1)) Test = disable_abstract( # noqa: N806 BaseOneIndex, dict_overwrite={ "construct_array_contraction": ( lambda self, cont, a=2: np.arange(18, dtype=float).reshape(2, 3, 3) * a ) }, ) test = Test([contractions]) assert np.allclose( test.construct_array_spherical(), np.vstack( [ transform.dot(np.arange(9).reshape(3, 3)), transform.dot(np.arange(9, 18).reshape(3, 3)), ] ) * 2, ) assert np.allclose( test.construct_array_spherical(a=3), np.vstack( [ transform.dot(np.arange(9).reshape(3, 3)), transform.dot(np.arange(9, 18).reshape(3, 3)), ] ) * 3, ) test = Test([contractions, contractions]) assert np.allclose( test.construct_array_spherical(), np.vstack( [ transform.dot(np.arange(9).reshape(3, 3)), transform.dot(np.arange(9, 18).reshape(3, 3)), ] * 2 ) * 2, )