def test_pdms(): h2o = psi4.geometry(""" H H 1 0.7 """) psi4.set_options({"opdm": True, "num_roots": 3}) wfn = psi4.energy("fci/cc-pvdz", return_wfn=True)[1] # Okay, I need three matrix comparisons... ref_opdm = psi4.core.Matrix.from_array([ np.array([[9.84317436e-01, 4.69564137e-03, 3.68310242e-03], [4.69564137e-03, 2.71595295e-03, -8.89586171e-04], [3.68310242e-03, -8.89586171e-04, 4.24202798e-04]]), np.zeros((0, 0)), np.array([[7.6893825e-05]]), np.array([[7.6893825e-05]]), np.zeros((0, 0)), np.array([[0.00416892, 0.00440128, 0.00073837], [0.00440128, 0.00473526, 0.00083457], [0.00073837, 0.00083457, 0.00017527]]), np.array([[0.00165458]]), np.array([[0.00165458]]) ]) # Test OPDM assert psi4.compare_matrices(ref_opdm, wfn.get_opdm(-1, -1, "A", True), 6, "OPDM") ref_tdm = psi4.core.Matrix.from_array([ np.array([[1.11543287e-02, 6.66837005e-02, -9.01185592e-03], [-3.95119873e-02, 2.57584925e-02, 4.69185262e-02], [-1.44471817e-03, 7.47375607e-05, 4.47155049e-04]]), np.zeros((0, 0)), np.array([[-1.17823927e-05]]), np.array([[-1.17823927e-05]]), np.zeros((0, 0)), np.array([[-0.0436292, -0.01418167, -0.00124078], [0.04298612, 0.00671799, 0.00043999], [0.01168194, 0.0019797, 0.0001121]]), np.array([[-0.00026865]]), np.array([[-0.00026865]]) ]) # Test transition matrix assert psi4.compare_matrices(ref_tdm, wfn.get_opdm(1, 2, "A", True), 6, "TDM", equal_phase=True) # Test the swapping the bra and the key produces the transpose matrix. assert psi4.compare_matrices(wfn.get_opdm(2, 1, "A", True), wfn.get_opdm(1, 2, "A", True).transpose(), 6, "TDM SWAP")
def test_gdma(): """gdma1""" #! Water RHF/cc-pVTZ distributed multipole analysis ref_energy = -76.0571685433842219 ref_dma_mat = psi4.core.Matrix(3, 9) ref_dma_mat.name = 'Reference DMA values' ref_dma_arr = [ [ -0.43406697290168, -0.18762673939633, 0.00000000000000, 0.00000000000000, 0.03206686487531, 0.00000000000000, -0.00000000000000, -0.53123477172696, 0.00000000000000 ], [ 0.21703348903257, -0.06422316619952, 0.00000000000000, -0.11648289410022, 0.01844320206227, 0.00000000000000, 0.07409226544133, -0.07115302332866, 0.00000000000000 ], [ 0.21703348903257, -0.06422316619952, 0.00000000000000, 0.11648289410022, 0.01844320206227, 0.00000000000000, -0.07409226544133, -0.07115302332866, 0.00000000000000 ] ] for i in range(3): for j in range(9): ref_dma_mat.set(i, j, ref_dma_arr[i][j]) ref_tot_mat = psi4.core.Matrix(1, 9) ref_tot_mat.name = "Reference total values" ref_tot_arr = [ 0.00000000516346, -0.79665315928128, 0.00000000000000, 0.00000000000000, 0.10813259329390, 0.00000000000000, 0.00000000000000, -2.01989585894142, 0.00000000000000 ] for i in range(9): ref_tot_mat.set(0, i, ref_tot_arr[i]) # noreorient/nocom are not needed, but are used here to guarantee that the # GDMA origin placement defined below is at the O atom. water = psi4.geometry(""" O 0.000000 0.000000 0.117176 H -0.000000 -0.756950 -0.468706 H -0.000000 0.756950 -0.468706 noreorient nocom """) psi4.set_options({"scf_type": "pk", "basis": "cc-pvtz", "d_convergence": 10, "gdma_switch": 0, "gdma_radius": [ "H", 0.65 ], "gdma_limit": 2, "gdma_origin": [ 0.000000, 0.000000, 0.117176 ]}) energy, wfn = psi4.energy('scf', return_wfn=True) psi4.gdma(wfn) dmavals = psi4.core.variable("DMA DISTRIBUTED MULTIPOLES") totvals = psi4.core.variable("DMA TOTAL MULTIPOLES") assert psi4.compare_values(ref_energy, energy, 8, "SCF Energy") assert psi4.compare_matrices(dmavals, ref_dma_mat, 6, "DMA Distributed Multipoles") assert psi4.compare_matrices(totvals, ref_tot_mat, 6, "DMA Total Multipoles")
def test_triplet_with_transpose(): A = psi4.core.Matrix.from_array([ np.random.rand(5, 10000), np.random.rand(10000, 5), np.random.rand(5, 10000) ]) B = psi4.core.Matrix.from_array([ np.random.rand(5, 10000), np.random.rand(10000, 5), np.random.rand(5, 10000) ]) C = psi4.core.Matrix.from_array([ np.random.rand(10000, 5), np.random.rand(5, 10000), np.random.rand(10000, 5) ]) start = time.time() R = psi4.core.triplet(A, B, C, True, False, False) time1 = time.time() - start start = time.time() S = psi4.core.doublet(A, B, True, False) S = psi4.core.doublet(S, C, False, False) time2 = time.time() - start if int(os.environ["PYTEST_XDIST_WORKER_COUNT"]) == 1: assert time1 < time2 assert psi4.compare_matrices(R, S, 8, "linalg::triplet transpose test")
def test_triplet_normal_case(): a = np.random.rand(500, 500) b = np.random.rand(500, 500) c = np.random.rand(500, 500) A = psi4.core.Matrix.from_array(a) B = psi4.core.Matrix.from_array(b) C = psi4.core.Matrix.from_array(c) R = psi4.core.triplet(A, B, C, False, False, False) S = psi4.core.doublet(A, B, False, False) S = psi4.core.doublet(S, C, False, False) assert psi4.compare_matrices(R, S, 8, "linalg::triplet avg_case test")
def test_triplet_with_transpose(): A = psi4.core.Matrix.from_array([np.random.rand(5, 10000), np.random.rand(10000, 5), np.random.rand(5, 10000)]) B = psi4.core.Matrix.from_array([np.random.rand(5, 10000), np.random.rand(10000, 5), np.random.rand(5, 10000)]) C = psi4.core.Matrix.from_array([np.random.rand(10000, 5), np.random.rand(5, 10000), np.random.rand(10000, 5)]) start = time.time() R = psi4.core.triplet(A, B, C, True, False, False) time1 = time.time() - start start = time.time() S = psi4.core.doublet(A, B, True, False) S = psi4.core.doublet(S, C, False, False) time2 = time.time() - start assert (time1 < time2) assert psi4.compare_matrices(R, S, 8, "linalg::triplet transpose test")
def test_triplet_speedup(): a = np.random.rand(10000, 10) b = np.random.rand(10, 10000) c = np.random.rand(10000, 10) A = psi4.core.Matrix.from_array(a) B = psi4.core.Matrix.from_array(b) C = psi4.core.Matrix.from_array(c) start = time.time() R = psi4.core.triplet(A, B, C, False, False, False) time1 = time.time() - start start = time.time() S = psi4.core.doublet(A, B, False, False) S = psi4.core.doublet(S, C, False, False) time2 = time.time() - start assert (time1 < time2) assert psi4.compare_matrices(R, S, 8, "linalg::triplet speedup test")
def test_triplet_speedup(): a = np.random.rand(10000, 10) b = np.random.rand(10, 10000) c = np.random.rand(10000, 10) A = psi4.core.Matrix.from_array(a) B = psi4.core.Matrix.from_array(b) C = psi4.core.Matrix.from_array(c) start = time.time() R = psi4.core.triplet(A, B, C, False, False, False) time1 = time.time() - start start = time.time() S = psi4.core.doublet(A, B, False, False) S = psi4.core.doublet(S, C, False, False) time2 = time.time() - start if int(os.environ["PYTEST_XDIST_WORKER_COUNT"]) == 1: assert time1 < time2 assert psi4.compare_matrices(R, S, 8, "linalg::triplet speedup test")
def test_export_ao_elec_dip_deriv(): h2o = psi4.geometry(""" O H 1 1.0 H 1 1.0 2 101.5 symmetry c1 """) rhf_e, wfn = psi4.energy('SCF/cc-pVDZ', molecule=h2o, return_wfn=True) mints = psi4.core.MintsHelper(wfn.basisset()) natoms = h2o.natom() cart = ['_X', '_Y', '_Z'] D = wfn.Da() D.add(wfn.Db()) D_np = np.asarray(D) deriv1_mat = {} deriv1_np = {} MU_Gradient = np.zeros((3 * natoms, 3)) for atom in range(natoms): deriv1_mat["MU_" + str(atom)] = mints.ao_elec_dip_deriv1(atom) for mu_cart in range(3): for atom_cart in range(3): map_key = "MU" + cart[mu_cart] + "_" + str( atom) + cart[atom_cart] deriv1_np[map_key] = np.asarray( deriv1_mat["MU_" + str(atom)][3 * mu_cart + atom_cart]) MU_Gradient[3 * atom + atom_cart, mu_cart] += np.einsum("uv,uv->", deriv1_np[map_key], D_np) PSI4_MU_Grad = mints.dipole_grad(D) G_python_MU_mat = psi4.core.Matrix.from_array(MU_Gradient) assert psi4.compare_matrices(PSI4_MU_Grad, G_python_MU_mat, 10, "DIPOLE_GRADIENT_TEST") # TEST
# PIS4's overlap_grad, kinetic_grad and potential_grad PSI4_Grad = {} D = wfn.Da() D.add(wfn.Db()) PSI4_Grad["S"] = mints.overlap_grad(D) PSI4_Grad["T"] = mints.kinetic_grad(D) PSI4_Grad["V"] = mints.potential_grad(D) #Convert np array into PSI4 Matrix G_python_S_mat = psi4.core.Matrix.from_array(Gradient["S'"]) G_python_T_mat = psi4.core.Matrix.from_array(Gradient["T"]) G_python_V_mat = psi4.core.Matrix.from_array(Gradient["V"]) # Test OEI gradients with that of PSI4 psi4.compare_matrices(PSI4_Grad["S"], G_python_S_mat, 10, "OVERLAP_GRADIENT_TEST") # TEST psi4.compare_matrices(PSI4_Grad["T"], G_python_T_mat, 10, "KINETIC_GRADIENT_TEST") # TEST psi4.compare_matrices(PSI4_Grad["V"], G_python_V_mat, 10, "POTENTIAL_GRADIENT_TEST") # TEST # PSI4's Total Gradient Total_G_psi4 = psi4.core.Matrix.from_list([ [ 0.000000000000, -0.000000000000, -0.097441440379], [-0.000000000000, -0.086300100260, 0.048720720189], [-0.000000000000, 0.086300100260, 0.048720720189] ]) G_python_total_mat = psi4.core.Matrix.from_array(Gradient["Total"]) psi4.compare_matrices(Total_G_psi4, G_python_total_mat, 10, "RHF_TOTAL_GRADIENT_TEST") # TEST
PSI4_Grad = {} D = wfn.Da() D.add(wfn.Db()) PSI4_Grad["S"] = mints.overlap_grad(D) PSI4_Grad["T"] = mints.kinetic_grad(D) PSI4_Grad["V"] = mints.potential_grad(D) #Convert np array into PSI4 Matrix G_python_S_mat = psi4.core.Matrix.from_array(Gradient["S'"]) G_python_T_mat = psi4.core.Matrix.from_array(Gradient["T"]) G_python_V_mat = psi4.core.Matrix.from_array(Gradient["V"]) # Test OEI gradients with that of PSI4 psi4.compare_matrices(PSI4_Grad["S"], G_python_S_mat, 10, "OVERLAP_GRADIENT_TEST") # TEST psi4.compare_matrices(PSI4_Grad["T"], G_python_T_mat, 10, "KINETIC_GRADIENT_TEST") # TEST psi4.compare_matrices(PSI4_Grad["V"], G_python_V_mat, 10, "POTENTIAL_GRADIENT_TEST") # TEST # PSI4's Total Gradient Total_G_psi4 = psi4.core.Matrix.from_list( [[0.000000000000, -0.000000000000, -0.097441440379], [-0.000000000000, -0.086300100260, 0.048720720189], [-0.000000000000, 0.086300100260, 0.048720720189]]) G_python_total_mat = psi4.core.Matrix.from_array(Gradient["Total"]) psi4.compare_matrices(Total_G_psi4, G_python_total_mat, 10, "RHF_TOTAL_GRADIENT_TEST") # TEST
Hes["R"][r][c] += 0.5 * 1.0 * np.einsum('pqrs,tp,tqrs->', dPpqrs[key2], U[key1], npERI, optimize=True) Hes["R"][r][c] += 0.5 * 1.0 * np.einsum('pqrs,tq,ptrs->', dPpqrs[key2], U[key1], npERI, optimize=True) Hes["R"][r][c] += 0.5 * 1.0 * np.einsum('pqrs,tr,pqts->', dPpqrs[key2], U[key1], npERI, optimize=True) Hes["R"][r][c] += 0.5 * 1.0 * np.einsum('pqrs,ts,pqrt->', dPpqrs[key2], U[key1], npERI, optimize=True) # Build Total Hessian for key in Hes: Hessian += Hes[key] # Symmetrize Hessian Hessian = (Hessian + Hessian.T)/2 Mat = psi4.core.Matrix.from_array(Hessian) Mat.name = " TOTAL HESSIAN" Mat.print_out() H_psi4 = psi4.core.Matrix.from_list([ [-3.6130997071, 0.0000000000, -0.0000000000, 1.8065498535, -0.0000000000, -0.0000000000, 1.8065498535, 0.0000000000, 0.0000000000], [ 0.0000000000, 8.4484978101, 0.0000000000, -0.0000000000, -4.2242489050, -4.7117763859, 0.0000000000, -4.2242489050, 4.7117763859], [-0.0000000000, 0.0000000000, 3.7558758529, -0.0000000000, -4.3809411905, -1.8779379264, 0.0000000000, 4.3809411905, -1.8779379264], [ 1.8065498535, -0.0000000000, -0.0000000000, -1.8756582110, 0.0000000000, 0.0000000000, 0.0691083574, 0.0000000000, 0.0000000000], [-0.0000000000, -4.2242489050, -4.3809411906, 0.0000000000, 4.3722044693, 4.5463587882, -0.0000000000, -0.1479555642, -0.1654175976], [-0.0000000000, -4.7117763860, -1.8779379264, 0.0000000000, 4.5463587882, 1.8072072616, 0.0000000000, 0.1654175977, 0.0707306648], [ 1.8065498535, 0.0000000000, 0.0000000000, 0.0691083574, -0.0000000000, 0.0000000000, -1.8756582110, -0.0000000000, -0.0000000000], [ 0.0000000000, -4.2242489050, 4.3809411906, 0.0000000000, -0.1479555642, 0.1654175976, -0.0000000000, 4.3722044693, -4.5463587882], [ 0.0000000000, 4.7117763860, -1.8779379264, 0.0000000000, -0.1654175977, 0.0707306648, -0.0000000000, -4.5463587882, 1.8072072616] ]) H_python_mat = psi4.core.Matrix.from_array(Hessian) psi4.compare_matrices(H_psi4, H_python_mat, 9, "RHF-HESSIAN-TEST")
def test_geometric_hessian_rhf_outside_solver_chemists(): psi4.core.set_output_file("output2.dat", False) mol = molecules.molecule_physicists_water_sto3g() mol.reset_point_group("c1") mol.update_geometry() psi4.core.set_active_molecule(mol) options = { "BASIS": "STO-3G", "SCF_TYPE": "PK", "E_CONVERGENCE": 1e-10, "D_CONVERGENCE": 1e-10, } psi4.set_options(options) _, wfn = psi4.energy("hf", return_wfn=True) norb = wfn.nmo() nocc = wfn.nalpha() nvir = norb - nocc o = slice(0, nocc) v = slice(nocc, norb) C = wfn.Ca_subset("AO", "ALL") npC = np.asarray(C) mints = psi4.core.MintsHelper(wfn) T = np.asarray(mints.ao_kinetic()) V = np.asarray(mints.ao_potential()) H_ao = T + V H = np.einsum("up,vq,uv->pq", npC, npC, H_ao) MO = np.asarray(mints.mo_eri(C, C, C, C)) F = H + 2.0 * np.einsum("pqii->pq", MO[:, :, o, o]) F -= np.einsum("piqi->pq", MO[:, o, :, o]) F_ref = np.load(os.path.join(datadir, "F.npy")) np.testing.assert_allclose(F, F_ref, rtol=0, atol=1.0e-10) natoms = mol.natom() cart = ["_X", "_Y", "_Z"] oei_dict = {"S": "OVERLAP", "T": "KINETIC", "V": "POTENTIAL"} deriv1_mat = {} deriv1 = {} # 1st Derivative of OEIs for atom in range(natoms): for key in oei_dict: deriv1_mat[key + str(atom)] = mints.mo_oei_deriv1( oei_dict[key], atom, C, C) for p in range(3): map_key = key + str(atom) + cart[p] deriv1[map_key] = np.asarray(deriv1_mat[key + str(atom)][p]) deriv1_ref = np.load(os.path.join(datadir, f"{map_key}.npy")) np.testing.assert_allclose(deriv1[map_key], deriv1_ref, rtol=0, atol=1.0e-10) # 1st Derivative of TEIs for atom in range(natoms): string = "TEI" + str(atom) deriv1_mat[string] = mints.mo_tei_deriv1(atom, C, C, C, C) for p in range(3): map_key = string + cart[p] deriv1[map_key] = np.asarray(deriv1_mat[string][p]) deriv1_ref = np.load(os.path.join(datadir, f"{map_key}.npy")) np.testing.assert_allclose(deriv1[map_key], deriv1_ref, rtol=0, atol=1.0e-10) Hes = {} deriv2_mat = {} deriv2 = {} Hes["S"] = np.zeros((3 * natoms, 3 * natoms)) Hes["V"] = np.zeros((3 * natoms, 3 * natoms)) Hes["T"] = np.zeros((3 * natoms, 3 * natoms)) Hes["N"] = np.zeros((3 * natoms, 3 * natoms)) Hes["J"] = np.zeros((3 * natoms, 3 * natoms)) Hes["K"] = np.zeros((3 * natoms, 3 * natoms)) Hes["R"] = np.zeros((3 * natoms, 3 * natoms)) Hessian = np.zeros((3 * natoms, 3 * natoms)) Hes["N"] = np.asarray(mol.nuclear_repulsion_energy_deriv2()) psi4.core.print_out("\n\n") Mat = psi4.core.Matrix.from_array(Hes["N"]) Mat.name = "NUCLEAR HESSIAN" Mat.print_out() # 2nd Derivative of OEIs for atom1 in range(natoms): for atom2 in range(atom1 + 1): for key in oei_dict: string = key + str(atom1) + str(atom2) deriv2_mat[string] = mints.mo_oei_deriv2( oei_dict[key], atom1, atom2, C, C) pq = 0 for p in range(3): for q in range(3): map_key = string + cart[p] + cart[q] deriv2[map_key] = np.asarray(deriv2_mat[string][pq]) deriv2_ref = np.load( os.path.join(datadir, f"{map_key}.npy")) np.testing.assert_allclose(deriv2[map_key], deriv2_ref, rtol=0, atol=1.0e-10) pq = pq + 1 row = 3 * atom1 + p col = 3 * atom2 + q if key == "S": Hes[key][row][col] = -2.0 * np.einsum( "ii,ii->", F[o, o], deriv2[map_key][o, o]) else: Hes[key][row][col] = 2.0 * np.einsum( "ii->", deriv2[map_key][o, o]) Hes[key][col][row] = Hes[key][row][col] Hes[key][col][row] = Hes[key][row][col] Hes_ref = np.load( os.path.join(datadir, f"Hes_{map_key}.npy")) np.testing.assert_allclose(Hes[key], Hes_ref, rtol=0, atol=1.0e-10) for key in Hes: Mat = psi4.core.Matrix.from_array(Hes[key]) if key in oei_dict: Mat.name = oei_dict[key] + " HESSIAN" Mat.print_out() psi4.core.print_out("\n") # 2nd Derivative of TEIs for atom1 in range(natoms): for atom2 in range(atom1 + 1): string = "TEI" + str(atom1) + str(atom2) deriv2_mat[string] = mints.mo_tei_deriv2(atom1, atom2, C, C, C, C) pq = 0 for p in range(3): for q in range(3): map_key = string + cart[p] + cart[q] deriv2[map_key] = np.asarray(deriv2_mat[string][pq]) deriv2_ref = np.load( os.path.join(datadir, f"{map_key}.npy")) np.testing.assert_allclose(deriv2[map_key], deriv2_ref, rtol=0, atol=1.0e-10) pq = pq + 1 row = 3 * atom1 + p col = 3 * atom2 + q Hes["J"][row][col] = 2.0 * np.einsum( "iijj->", deriv2[map_key][o, o, o, o]) Hes["K"][row][col] = -1.0 * np.einsum( "ijij->", deriv2[map_key][o, o, o, o]) Hes["J"][col][row] = Hes["J"][row][col] Hes["K"][col][row] = Hes["K"][row][col] for map_key in ("J", "K"): Hes_ref = np.load(os.path.join(datadir, f"Hes_{map_key}.npy")) np.testing.assert_allclose(Hes[map_key], Hes_ref, rtol=0, atol=1.0e-10) JMat = psi4.core.Matrix.from_array(Hes["J"]) KMat = psi4.core.Matrix.from_array(Hes["K"]) JMat.name = " COULOMB HESSIAN" KMat.name = " EXCHANGE HESSIAN" JMat.print_out() KMat.print_out() # Solve the CPHF equations here, G_iajb U_jb^x = B_ia^x (Einstein summation), # where G is the electronic hessian, # G_iajb = delta_ij * delta_ab * epsilon_ij * epsilon_ab + 4(ia|jb) - (ij|ab) - (ib|ja), # where epsilon_ij = epsilon_i - epsilon_j, (epsilon -> orbital energies), # x refers to the perturbation, U_jb^x are the corresponsing CPHF coefficients # and B_ia^x = S_ia^x * epsilon_ii - F_ia^x + S_mn^x * [2(ia|mn) - (in|ma)], # where S^x = del(S)/del(x), F^x = del(F)/del(x). I_occ = np.diag(np.ones(nocc)) I_vir = np.diag(np.ones(nvir)) epsilon = np.asarray(wfn.epsilon_a()) eps_diag = epsilon[v].reshape(-1, 1) - epsilon[o] # Build the electronic hessian G G = 4 * MO[o, v, o, v] G -= MO[o, o, v, v].swapaxes(1, 2) G -= MO[o, v, o, v].swapaxes(1, 3) G += np.einsum("ai,ij,ab->iajb", eps_diag, I_occ, I_vir) G_ref = np.load(os.path.join(datadir, "G.npy")) np.testing.assert_allclose(G, G_ref, rtol=0, atol=1.0e-10) # Inverse of G Ginv = np.linalg.inv(G.reshape(nocc * nvir, -1)) Ginv = Ginv.reshape(nocc, nvir, nocc, nvir) B = {} F_grad = {} U = {} # Build F_pq^x now for atom in range(natoms): for p in range(3): key = str(atom) + cart[p] F_grad[key] = deriv1["T" + key] F_grad[key] += deriv1["V" + key] F_grad[key] += 2.0 * np.einsum("pqmm->pq", deriv1["TEI" + key][:, :, o, o]) F_grad[key] -= 1.0 * np.einsum("pmmq->pq", deriv1["TEI" + key][:, o, o, :]) F_grad_ref = np.load(os.path.join(datadir, f"F_grad_{key}.npy")) np.testing.assert_allclose(F_grad[key], F_grad_ref, rtol=0, atol=1.0e-10) psi4.core.print_out("\n\n CPHF Coefficients:\n") # Build B_ia^x now for atom in range(natoms): for p in range(3): key = str(atom) + cart[p] B[key] = np.einsum("ia,ii->ia", deriv1["S" + key][o, v], F[o, o]) B[key] -= F_grad[key][o, v] B[key] += 2.0 * np.einsum("iamn,mn->ia", MO[o, v, o, o], deriv1["S" + key][o, o]) B[key] += -1.0 * np.einsum("inma,mn->ia", MO[o, o, o, v], deriv1["S" + key][o, o]) print(f"B[{key}]") print(B[key]) B_ref = np.load(os.path.join(datadir, f"B_{key}.npy")) np.testing.assert_allclose(B[key], B_ref.T, rtol=0, atol=1.0e-10) # Compute U^x now: U_ia^x = G^(-1)_iajb * B_jb^x U[key] = np.einsum("iajb,jb->ia", Ginv, B[key]) psi4.core.print_out("\n") UMat = psi4.core.Matrix.from_array(U[key]) UMat.name = key UMat.print_out() U_ref = np.load(os.path.join(datadir, f"U_{key}.npy")) np.testing.assert_allclose(U[key], U_ref.T, rtol=0, atol=1.0e-10) # Build the response Hessian for atom1 in range(natoms): for atom2 in range(atom1 + 1): for p in range(3): for q in range(3): key1 = str(atom1) + cart[p] key2 = str(atom2) + cart[q] key1S = "S" + key1 key2S = "S" + key2 r = 3 * atom1 + p c = 3 * atom2 + q Hes["R"][r][c] = -2.0 * np.einsum( "ij,ij->", deriv1[key1S][o, o], F_grad[key2][o, o]) Hes["R"][r][c] -= 2.0 * np.einsum( "ij,ij->", deriv1[key2S][o, o], F_grad[key1][o, o]) Hes["R"][r][c] += 4.0 * np.einsum("ii,mi,mi->", F[o, o], deriv1[key2S][o, o], deriv1[key1S][o, o]) Hes["R"][r][c] += 4.0 * np.einsum( "ij,mn,ijmn->", deriv1[key1S][o, o], deriv1[key2S][o, o], MO[o, o, o, o], ) Hes["R"][r][c] -= 2.0 * np.einsum( "ij,mn,inmj->", deriv1[key1S][o, o], deriv1[key2S][o, o], MO[o, o, o, o], ) Hes["R"][r][c] -= 4.0 * np.einsum("ia,ia->", U[key2], B[key1]) Hes["R"][c][r] = Hes["R"][r][c] Hes_ref = np.load(os.path.join(datadir, "Hes_R.npy")) np.testing.assert_allclose(Hes["R"], Hes_ref, rtol=0, atol=1.0e-10) Mat = psi4.core.Matrix.from_array(Hes["R"]) Mat.name = " RESPONSE HESSIAN" Mat.print_out() for key in Hes: Hessian += Hes[key] # print('deriv1_mat') # print(deriv1_mat.keys()) # print('deriv1') # print(deriv1.keys()) # print('deriv2_mat') # print(deriv2_mat.keys()) # print('deriv2') # print(deriv2.keys()) # print('B') # print(B.keys()) # print('F_grad') # print(F_grad.keys()) # print('U') # print(U.keys()) # print('Hes') # print(Hes.keys()) Mat = psi4.core.Matrix.from_array(Hessian) Mat.name = " TOTAL HESSIAN" Mat.print_out() # pylint: disable=bad-whitespace H_psi4 = psi4.core.Matrix.from_list([ [ 7.613952269164418751e-02, 0.000000000000000000e00, 0.000000000000000000e00, -3.806976134297335168e-02, 0.000000000000000000e00, 0.000000000000000000e00, -3.806976134297410108e-02, 0.000000000000000000e00, 0.000000000000000000e00, ], [ 0.000000000000000000e00, 4.829053723748517601e-01, 0.000000000000000000e00, 0.000000000000000000e00, -2.414526861845633920e-01, 1.589001558536450587e-01, 0.000000000000000000e00, -2.414526861845646133e-01, -1.589001558536444758e-01, ], [ 0.000000000000000000e00, 0.000000000000000000e00, 4.373449597848400039e-01, 0.000000000000000000e00, 7.344233774055003439e-02, -2.186724798895446076e-01, 0.000000000000000000e00, -7.344233774054893804e-02, -2.186724798895475219e-01, ], [ -3.806976134297335168e-02, 0.000000000000000000e00, 0.000000000000000000e00, 4.537741758645107149e-02, 0.000000000000000000e00, 0.000000000000000000e00, -7.307656243475501093e-03, 0.000000000000000000e00, 0.000000000000000000e00, ], [ 0.000000000000000000e00, -2.414526861845633920e-01, 7.344233774055003439e-02, 0.000000000000000000e00, 2.578650065921952450e-01, -1.161712467970963669e-01, 0.000000000000000000e00, -1.641232040762596878e-02, 4.272890905654690846e-02, ], [ 0.000000000000000000e00, 1.589001558536450587e-01, -2.186724798895446076e-01, 0.000000000000000000e00, -1.161712467970963669e-01, 1.977519807685419462e-01, 0.000000000000000000e00, -4.272890905654720684e-02, 2.092049912100946499e-02, ], [ -3.806976134297410108e-02, 0.000000000000000000e00, 0.000000000000000000e00, -7.307656243475501093e-03, 0.000000000000000000e00, 0.000000000000000000e00, 4.537741758645268131e-02, 0.000000000000000000e00, 0.000000000000000000e00, ], [ 0.000000000000000000e00, -2.414526861845646133e-01, -7.344233774054893804e-02, 0.000000000000000000e00, -1.641232040762596878e-02, -4.272890905654720684e-02, 0.000000000000000000e00, 2.578650065921969103e-01, 1.161712467970957008e-01, ], [ 0.000000000000000000e00, -1.589001558536444758e-01, -2.186724798895475219e-01, 0.000000000000000000e00, 4.272890905654690846e-02, 2.092049912100946499e-02, 0.000000000000000000e00, 1.161712467970957008e-01, 1.977519807685442221e-01, ], ]) H_python_mat = psi4.core.Matrix.from_array(Hessian) psi4.compare_matrices(H_psi4, H_python_mat, 10, "RHF-HESSIAN-TEST") # TEST return
def test_geometric_hessian_rhf_outside_solver_psi4numpy(): psi4.core.set_output_file("output.dat", False) mol = psi4.geometry(""" O H 1 1.1 H 1 1.1 2 104 symmetry c1 """) psi4.core.set_active_molecule(mol) options = { "BASIS": "STO-3G", "SCF_TYPE": "PK", "E_CONVERGENCE": 1e-10, "D_CONVERGENCE": 1e-10, } psi4.set_options(options) _, wfn = psi4.energy("SCF", return_wfn=True) # Assuming C1 symmetry occ = wfn.doccpi()[0] nmo = wfn.nmo() vir = nmo - occ C = wfn.Ca_subset("AO", "ALL") npC = np.asarray(C) mints = psi4.core.MintsHelper(wfn.basisset()) H_ao = np.asarray(mints.ao_kinetic()) + np.asarray(mints.ao_potential()) # Update H, transform to MO basis H = np.einsum("uj,vi,uv", npC, npC, H_ao) # Integral generation from Psi4's MintsHelper MO = np.asarray(mints.mo_eri(C, C, C, C)) # Physicist notation MO = MO.swapaxes(1, 2) F = H + 2.0 * np.einsum("pmqm->pq", MO[:, :occ, :, :occ]) F -= np.einsum("pmmq->pq", MO[:, :occ, :occ, :]) # Uncomment every `np.save` call to regenerate reference data. # np.save(os.path.join(datadir, 'F.npy'), F) F_ref = np.load(os.path.join(datadir, "F.npy")) np.testing.assert_allclose(F, F_ref, rtol=0, atol=1.0e-10) natoms = mol.natom() cart = ["_X", "_Y", "_Z"] oei_dict = {"S": "OVERLAP", "T": "KINETIC", "V": "POTENTIAL"} deriv1_mat = {} deriv1 = {} # 1st Derivative of OEIs for atom in range(natoms): for key in oei_dict: deriv1_mat[key + str(atom)] = mints.mo_oei_deriv1( oei_dict[key], atom, C, C) for p in range(3): map_key = key + str(atom) + cart[p] deriv1[map_key] = np.asarray(deriv1_mat[key + str(atom)][p]) # np.save(os.path.join(datadir, f'{map_key}.npy'), deriv1[map_key]) deriv1_ref = np.load(os.path.join(datadir, f"{map_key}.npy")) np.testing.assert_allclose(deriv1[map_key], deriv1_ref, rtol=0, atol=1.0e-10) # 1st Derivative of TEIs for atom in range(natoms): string = "TEI" + str(atom) deriv1_mat[string] = mints.mo_tei_deriv1(atom, C, C, C, C) for p in range(3): map_key = string + cart[p] deriv1[map_key] = np.asarray(deriv1_mat[string][p]) # np.save(os.path.join(datadir, f'{map_key}.npy'), deriv1[map_key]) deriv1_ref = np.load(os.path.join(datadir, f"{map_key}.npy")) np.testing.assert_allclose(deriv1[map_key], deriv1_ref, rtol=0, atol=1.0e-10) Hes = {} deriv2_mat = {} deriv2 = {} Hes["S"] = np.zeros((3 * natoms, 3 * natoms)) Hes["V"] = np.zeros((3 * natoms, 3 * natoms)) Hes["T"] = np.zeros((3 * natoms, 3 * natoms)) Hes["N"] = np.zeros((3 * natoms, 3 * natoms)) Hes["J"] = np.zeros((3 * natoms, 3 * natoms)) Hes["K"] = np.zeros((3 * natoms, 3 * natoms)) Hes["R"] = np.zeros((3 * natoms, 3 * natoms)) Hessian = np.zeros((3 * natoms, 3 * natoms)) Hes["N"] = np.asarray(mol.nuclear_repulsion_energy_deriv2()) psi4.core.print_out("\n\n") Mat = psi4.core.Matrix.from_array(Hes["N"]) Mat.name = "NUCLEAR HESSIAN" Mat.print_out() # 2nd Derivative of OEIs for atom1 in range(natoms): for atom2 in range(atom1 + 1): for key in oei_dict: string = key + str(atom1) + str(atom2) deriv2_mat[string] = mints.mo_oei_deriv2( oei_dict[key], atom1, atom2, C, C) pq = 0 for p in range(3): for q in range(3): map_key = string + cart[p] + cart[q] deriv2[map_key] = np.asarray(deriv2_mat[string][pq]) # np.save(os.path.join(datadir, f'{map_key}.npy'), deriv2[map_key]) deriv2_ref = np.load( os.path.join(datadir, f"{map_key}.npy")) np.testing.assert_allclose(deriv2[map_key], deriv2_ref, rtol=0, atol=1.0e-10) pq = pq + 1 row = 3 * atom1 + p col = 3 * atom2 + q if key == "S": Hes[key][row][col] = -2.0 * np.einsum( "ii,ii->", F[:occ, :occ], deriv2[map_key][:occ, :occ]) else: Hes[key][row][col] = 2.0 * np.einsum( "ii->", deriv2[map_key][:occ, :occ]) Hes[key][col][row] = Hes[key][row][col] Hes[key][col][row] = Hes[key][row][col] # np.save(os.path.join(datadir, f'Hes_{map_key}.npy'), Hes[key]) Hes_ref = np.load( os.path.join(datadir, f"Hes_{map_key}.npy")) np.testing.assert_allclose(Hes[key], Hes_ref, rtol=0, atol=1.0e-10) for key in Hes: Mat = psi4.core.Matrix.from_array(Hes[key]) if key in oei_dict: Mat.name = oei_dict[key] + " HESSIAN" Mat.print_out() psi4.core.print_out("\n") # 2nd Derivative of TEIs for atom1 in range(natoms): for atom2 in range(atom1 + 1): string = "TEI" + str(atom1) + str(atom2) deriv2_mat[string] = mints.mo_tei_deriv2(atom1, atom2, C, C, C, C) pq = 0 for p in range(3): for q in range(3): map_key = string + cart[p] + cart[q] deriv2[map_key] = np.asarray(deriv2_mat[string][pq]) # np.save(os.path.join(datadir, f'{map_key}.npy'), deriv2[map_key]) deriv2_ref = np.load( os.path.join(datadir, f"{map_key}.npy")) np.testing.assert_allclose(deriv2[map_key], deriv2_ref, rtol=0, atol=1.0e-10) pq = pq + 1 row = 3 * atom1 + p col = 3 * atom2 + q Hes["J"][row][col] = 2.0 * np.einsum( "iijj->", deriv2[map_key][:occ, :occ, :occ, :occ]) Hes["K"][row][col] = -1.0 * np.einsum( "ijij->", deriv2[map_key][:occ, :occ, :occ, :occ]) Hes["J"][col][row] = Hes["J"][row][col] Hes["K"][col][row] = Hes["K"][row][col] for map_key in ("J", "K"): # np.save(os.path.join(datadir, f'Hes_{map_key}.npy'), Hes[map_key]) Hes_ref = np.load(os.path.join(datadir, f"Hes_{map_key}.npy")) np.testing.assert_allclose(Hes[map_key], Hes_ref, rtol=0, atol=1.0e-10) JMat = psi4.core.Matrix.from_array(Hes["J"]) KMat = psi4.core.Matrix.from_array(Hes["K"]) JMat.name = " COULOMB HESSIAN" KMat.name = " EXCHANGE HESSIAN" JMat.print_out() KMat.print_out() # Solve the CPHF equations here, G_aibj Ubj^x = Bai^x (Einstein summation), # where G is the electronic hessian, # G_aibj = delta_ij * delta_ab * epsilon_ij * epsilon_ab + 4 <ij|ab> - <ij|ba> - <ia|jb>, # where epsilon_ij = epsilon_i - epsilon_j, (epsilon -> orbital energies), # x refers to the perturbation, Ubj^x are the corresponsing CPHF coefficients # and Bai^x = Sai^x * epsilon_ii - Fai^x + Smn^x * (2<am|in> - <am|ni>), # where, S^x = del(S)/del(x), F^x = del(F)/del(x). I_occ = np.diag(np.ones(occ)) I_vir = np.diag(np.ones(vir)) epsilon = np.asarray(wfn.epsilon_a()) eps_diag = epsilon[occ:].reshape(-1, 1) - epsilon[:occ] # Build the electronic hessian G G = 4 * MO[:occ, :occ, occ:, occ:] G -= MO[:occ, :occ, occ:, occ:].swapaxes(2, 3) G -= MO[:occ, occ:, :occ, occ:].swapaxes(1, 2) G = G.swapaxes(1, 2) G += np.einsum("ai,ij,ab->iajb", eps_diag, I_occ, I_vir) # np.save(os.path.join(datadir, 'G.npy'), G) G_ref = np.load(os.path.join(datadir, "G.npy")) np.testing.assert_allclose(G, G_ref, rtol=0, atol=1.0e-10) # Inverse of G Ginv = np.linalg.inv(G.reshape(occ * vir, -1)) Ginv = Ginv.reshape(occ, vir, occ, vir) B = {} F_grad = {} U = {} # Build Fpq^x now for atom in range(natoms): for p in range(3): key = str(atom) + cart[p] F_grad[key] = deriv1["T" + key] F_grad[key] += deriv1["V" + key] F_grad[key] += 2.0 * np.einsum( "pqmm->pq", deriv1["TEI" + key][:, :, :occ, :occ]) F_grad[key] -= 1.0 * np.einsum( "pmmq->pq", deriv1["TEI" + key][:, :occ, :occ, :]) # np.save(os.path.join(datadir, f'F_grad_{key}.npy'), F_grad[key]) F_grad_ref = np.load(os.path.join(datadir, f"F_grad_{key}.npy")) np.testing.assert_allclose(F_grad[key], F_grad_ref, rtol=0, atol=1.0e-10) psi4.core.print_out("\n\n CPHF Coefficients:\n") # Build Bai^x now for atom in range(natoms): for p in range(3): key = str(atom) + cart[p] B[key] = np.einsum("ai,ii->ai", deriv1["S" + key][occ:, :occ], F[:occ, :occ]) B[key] -= F_grad[key][occ:, :occ] B[key] += 2.0 * np.einsum("amin,mn->ai", MO[occ:, :occ, :occ, :occ], deriv1["S" + key][:occ, :occ]) B[key] += -1.0 * np.einsum("amni,mn->ai", MO[occ:, :occ, :occ, :occ], deriv1["S" + key][:occ, :occ]) print(f"B[{key}]") print(B[key]) # np.save(os.path.join(datadir, f'B_{key}.npy'), B[key]) B_ref = np.load(os.path.join(datadir, f"B_{key}.npy")) np.testing.assert_allclose(B[key], B_ref, rtol=0, atol=1.0e-10) # Compute U^x now: U_ai^x = G^(-1)_aibj * B_bj^x U[key] = np.einsum("iajb,bj->ai", Ginv, B[key]) psi4.core.print_out("\n") UMat = psi4.core.Matrix.from_array(U[key]) UMat.name = key UMat.print_out() # np.save(os.path.join(datadir, f'U_{key}.npy'), U[key]) U_ref = np.load(os.path.join(datadir, f"U_{key}.npy")) np.testing.assert_allclose(U[key], U_ref, rtol=0, atol=1.0e-10) # Build the response Hessian for atom1 in range(natoms): for atom2 in range(atom1 + 1): for p in range(3): for q in range(3): key1 = str(atom1) + cart[p] key2 = str(atom2) + cart[q] key1S = "S" + key1 key2S = "S" + key2 r = 3 * atom1 + p c = 3 * atom2 + q Hes["R"][r][c] = -2.0 * np.einsum( "ij,ij->", deriv1[key1S][:occ, :occ], F_grad[key2][:occ, :occ]) Hes["R"][r][c] -= 2.0 * np.einsum( "ij,ij->", deriv1[key2S][:occ, :occ], F_grad[key1][:occ, :occ]) Hes["R"][r][c] += 4.0 * np.einsum( "ii,mi,mi->", F[:occ, :occ], deriv1[key2S][:occ, :occ], deriv1[key1S][:occ, :occ], ) Hes["R"][r][c] += 4.0 * np.einsum( "ij,mn,imjn->", deriv1[key1S][:occ, :occ], deriv1[key2S][:occ, :occ], MO[:occ, :occ, :occ, :occ], ) Hes["R"][r][c] -= 2.0 * np.einsum( "ij,mn,imnj->", deriv1[key1S][:occ, :occ], deriv1[key2S][:occ, :occ], MO[:occ, :occ, :occ, :occ], ) Hes["R"][r][c] -= 4.0 * np.einsum("ai,ai->", U[key2], B[key1]) Hes["R"][c][r] = Hes["R"][r][c] # np.save(os.path.join(datadir, 'Hes_R.npy'), Hes["R"]) Hes_ref = np.load(os.path.join(datadir, "Hes_R.npy")) np.testing.assert_allclose(Hes["R"], Hes_ref, rtol=0, atol=1.0e-10) Mat = psi4.core.Matrix.from_array(Hes["R"]) Mat.name = " RESPONSE HESSIAN" Mat.print_out() for key in Hes: Hessian += Hes[key] # print('deriv1_mat') # print(deriv1_mat.keys()) # print('deriv1') # print(deriv1.keys()) # print('deriv2_mat') # print(deriv2_mat.keys()) # print('deriv2') # print(deriv2.keys()) # print('B') # print(B.keys()) # print('F_grad') # print(F_grad.keys()) # print('U') # print(U.keys()) # print('Hes') # print(Hes.keys()) Mat = psi4.core.Matrix.from_array(Hessian) Mat.name = " TOTAL HESSIAN" Mat.print_out() # pylint: disable=bad-whitespace H_psi4 = psi4.core.Matrix.from_list([ [ 7.613952269164418751e-02, 0.000000000000000000e00, 0.000000000000000000e00, -3.806976134297335168e-02, 0.000000000000000000e00, 0.000000000000000000e00, -3.806976134297410108e-02, 0.000000000000000000e00, 0.000000000000000000e00, ], [ 0.000000000000000000e00, 4.829053723748517601e-01, 0.000000000000000000e00, 0.000000000000000000e00, -2.414526861845633920e-01, 1.589001558536450587e-01, 0.000000000000000000e00, -2.414526861845646133e-01, -1.589001558536444758e-01, ], [ 0.000000000000000000e00, 0.000000000000000000e00, 4.373449597848400039e-01, 0.000000000000000000e00, 7.344233774055003439e-02, -2.186724798895446076e-01, 0.000000000000000000e00, -7.344233774054893804e-02, -2.186724798895475219e-01, ], [ -3.806976134297335168e-02, 0.000000000000000000e00, 0.000000000000000000e00, 4.537741758645107149e-02, 0.000000000000000000e00, 0.000000000000000000e00, -7.307656243475501093e-03, 0.000000000000000000e00, 0.000000000000000000e00, ], [ 0.000000000000000000e00, -2.414526861845633920e-01, 7.344233774055003439e-02, 0.000000000000000000e00, 2.578650065921952450e-01, -1.161712467970963669e-01, 0.000000000000000000e00, -1.641232040762596878e-02, 4.272890905654690846e-02, ], [ 0.000000000000000000e00, 1.589001558536450587e-01, -2.186724798895446076e-01, 0.000000000000000000e00, -1.161712467970963669e-01, 1.977519807685419462e-01, 0.000000000000000000e00, -4.272890905654720684e-02, 2.092049912100946499e-02, ], [ -3.806976134297410108e-02, 0.000000000000000000e00, 0.000000000000000000e00, -7.307656243475501093e-03, 0.000000000000000000e00, 0.000000000000000000e00, 4.537741758645268131e-02, 0.000000000000000000e00, 0.000000000000000000e00, ], [ 0.000000000000000000e00, -2.414526861845646133e-01, -7.344233774054893804e-02, 0.000000000000000000e00, -1.641232040762596878e-02, -4.272890905654720684e-02, 0.000000000000000000e00, 2.578650065921969103e-01, 1.161712467970957008e-01, ], [ 0.000000000000000000e00, -1.589001558536444758e-01, -2.186724798895475219e-01, 0.000000000000000000e00, 4.272890905654690846e-02, 2.092049912100946499e-02, 0.000000000000000000e00, 1.161712467970957008e-01, 1.977519807685442221e-01, ], ]) H_python_mat = psi4.core.Matrix.from_array(Hessian) psi4.compare_matrices(H_psi4, H_python_mat, 10, "RHF-HESSIAN-TEST") # TEST return
optimize=True) # Print TEI Component of the Gradient psi4.core.print_out("\n\n TEI Gradients:\n\n") TEI_grad = psi4.core.Matrix.from_array(Gradient["TEI"]) TEI_grad.name = " TEI GRADIENT" TEI_grad.print_out() # Build Total Gradient Gradient["Total"] = Gradient["OEI"] + Gradient["TEI"] + Gradient["N"] # Print Total Gradient psi4.core.print_out("\n\n Total Gradient:\n\n") Tot_grad = psi4.core.Matrix.from_array(Gradient["Total"]) Tot_grad.name = " TOTAL GRADIENT" Tot_grad.print_out() # PSI4's Total Gradient Total_G_psi4 = psi4.core.Matrix.from_list( [[-0.00000000000000, -0.00000000000000, -0.05413558328761], [0.00000000000000, -0.06662229046965, 0.02706779164384], [-0.00000000000000, 0.06662229046965, 0.02706779164384]]) # Psi4Numpy Total Gradient total_grad = psi4.core.Matrix.from_array(Gradient["Total"]) # Compare Total Gradients G_python_total_mat = psi4.core.Matrix.from_array(Gradient["Total"]) psi4.compare_matrices(Total_G_psi4, G_python_total_mat, 10, "MP2_TOTAL_GRADIENT_TEST")
# Build Total Hessian for key in Hes: Hessian += Hes[key] # Symmetrize Hessian Hessian = (Hessian + Hessian.T) / 2 print("\nMolecular Hessian (a.u.):\n", Hessian) Mat = psi4.core.Matrix.from_array(Hessian) Mat.name = " TOTAL HESSIAN" Mat.print_out() # Test Hessian psi_H = wfn.hessian() python_H = psi4.core.Matrix.from_array(Hessian) psi4.compare_matrices(psi_H, python_H, 8, "RHF-HESSIAN-TEST") # Mass Weight the Hessian Matrix: masses = np.array([mol.mass(i) * (1 / psi_au2amu) for i in range(mol.natom())]) M = np.diag(1 / np.sqrt(np.repeat(masses, 3))) mH = M.T.dot(Hessian).dot(M) #print("\nMass-weighted Hessian (a.u.):\n", mH) #print("\nMass-weighted Hessian (hartree/(bohr^2 amu)):\n", mH * (1 / psi_au2amu)) # Mass-weighted Normal modes from Hessian k2, Lxm = np.linalg.eigh(mH) #print(k2) #eigenvalues in a.u. (hartree/bohr^2 m_e) #print(Lxm) #eigenvectors are unitless #print(k2 * (1 / psi_au2amu)) #eigenvalues in (hartree/(bohr^2 amu)) #print(k2 * (1 / psi_au2amu) * hartree2joule / (sqbohr2sqmeter * amu2kg)) #eigenvalues in (J/kg*m^2), effectively s^-2
Hes["R"][r][c] -= 2.0 * np.einsum("ij,mn,imnj->", deriv1[key1S][:occ,:occ], deriv1[key2S][:occ,:occ], MO[:occ,:occ,:occ,:occ]) Hes["R"][r][c] -= 4.0 * np.einsum("ai,ai->", U[key2], B[key1]) Hes["R"][c][r] = Hes["R"][r][c] Mat = psi4.core.Matrix.from_array(Hes["R"]) Mat.name = " RESPONSE HESSIAN" Mat.print_out() for key in Hes: Hessian += Hes[key] Mat = psi4.core.Matrix.from_array(Hessian) Mat.name = " TOTAL HESSIAN" Mat.print_out() H_psi4 = psi4.core.Matrix.from_list([ [ 0.07613952484989, 0.00000000000000, 0.00000000000000,-0.03806976242497, 0.00000000000000,-0.00000000000000,-0.03806976242497,-0.00000000000000, 0.00000000000000], [ 0.00000000000000, 0.48290536165172,-0.00000000000000,-0.00000000000000,-0.24145268082589, 0.15890015082364, 0.00000000000000,-0.24145268082590,-0.15890015082364], [ 0.00000000000000,-0.00000000000000, 0.43734495429393,-0.00000000000000, 0.07344233387869,-0.21867247714697,-0.00000000000000,-0.07344233387869,-0.21867247714697], [-0.03806976242497,-0.00000000000000,-0.00000000000000, 0.04537741867538,-0.00000000000000, 0.00000000000000,-0.00730765625041, 0.00000000000000,-0.00000000000000], [ 0.00000000000000,-0.24145268082589, 0.07344233387869,-0.00000000000000, 0.25786500091002,-0.11617124235117, 0.00000000000000,-0.01641232008412, 0.04272890847247], [-0.00000000000000, 0.15890015082364,-0.21867247714697, 0.00000000000000,-0.11617124235117, 0.19775197798054, 0.00000000000000,-0.04272890847247, 0.02092049916645], [-0.03806976242497, 0.00000000000000,-0.00000000000000,-0.00730765625041, 0.00000000000000, 0.00000000000000, 0.04537741867538,-0.00000000000000, 0.00000000000000], [-0.00000000000000,-0.24145268082590,-0.07344233387869, 0.00000000000000,-0.01641232008412,-0.04272890847247,-0.00000000000000, 0.25786500091002, 0.11617124235117], [ 0.00000000000000,-0.15890015082364,-0.21867247714697,-0.00000000000000, 0.04272890847247, 0.02092049916645, 0.00000000000000, 0.11617124235117, 0.19775197798054] ]) H_python_mat = psi4.core.Matrix.from_array(Hessian) psi4.compare_matrices(H_psi4, H_python_mat, 10, "RHF-HESSIAN-TEST") #TEST
psi4.core.print_out("\n\n TEI Gradients:\n\n") J_grad = psi4.core.Matrix.from_array(Gradient["J"]) K_grad = psi4.core.Matrix.from_array(Gradient["K"]) J_grad.name = " COULOMB GRADIENT" K_grad.name = " EXCHANGE GRADIENT" J_grad.print_out() K_grad.print_out() Gradient["OEI"] = Gradient["T"] + Gradient["V"] + Gradient["S"] Gradient["TEI"] = 0.25 * (Gradient["J"] + Gradient["K"]) Gradient["Total"] = Gradient["OEI"] + Gradient["TEI"] + Gradient["N"] psi4.core.print_out("\n\n Total Gradient:\n\n") Tot_grad = psi4.core.Matrix.from_array(Gradient["Total"]) Tot_grad.name = " TOTAL GRADIENT" Tot_grad.print_out() # Psi4's Total Gradient psi4_total_grad = psi4.core.Matrix.from_list( [[-0.00000000000000, -0.00000000000000, -0.05413558328761], [0.00000000000000, -0.06662229046965, 0.02706779164384], [-0.00000000000000, 0.06662229046965, 0.02706779164384]]) # Psi4Numpy Total Gradient total_grad = psi4.core.Matrix.from_array(Gradient["Total"]) # Compare Total Gradients psi4.compare_matrices(psi4_total_grad, total_grad, 10, "RHF_TOTAL_GRADIENT_TEST")
], [ 0.00000000000009, -0.24145268618572, 0.07344233774235, -0.00000000000007, 0.25786500659260, -0.11617124679841, -0.00000000000002, -0.01641232040687, 0.04272890905606 ], [ 0.00000000000006, 0.15890015585447, -0.21867247989206, -0.00000000000004, -0.11617124679841, 0.19775198076992, -0.00000000000001, -0.04272890905606, 0.02092049912214 ], [ -0.03806976134685, -0.00000000000005, 0.00000000000006, -0.00730765624159, -0.00000000000002, -0.00000000000001, 0.04537741758844, 0.00000000000007, -0.00000000000004 ], [ -0.00000000000009, -0.24145268618572, -0.07344233774235, 0.00000000000002, -0.01641232040687, -0.04272890905606, 0.00000000000007, 0.25786500659260, 0.11617124679841 ], [ 0.00000000000006, -0.15890015585447, -0.21867247989205, -0.00000000000001, 0.04272890905606, 0.02092049912214, -0.00000000000004, 0.11617124679841, 0.19775198076991 ], ]) H_python_mat = psi4.core.Matrix.from_array(Hessian) psi4.compare_matrices(H_psi4, H_python_mat, 10, "RHF-HESSIAN-TEST") #TEST
psi4.core.print_out("\n\n TEI Gradients:\n\n") J_grad = psi4.core.Matrix.from_array(Gradient["J"]) K_grad = psi4.core.Matrix.from_array(Gradient["K"]) J_grad.name = " COULOMB GRADIENT" K_grad.name = " EXCHANGE GRADIENT" J_grad.print_out() K_grad.print_out() Gradient["OEI"] = Gradient["T"] + Gradient["V"] + Gradient["S"] Gradient["TEI"] = 0.25 * (Gradient["J"] + Gradient["K"]) Gradient["Total"] = Gradient["OEI"] + Gradient["TEI"] + Gradient["N"] psi4.core.print_out("\n\n Total Gradient:\n\n") Tot_grad = psi4.core.Matrix.from_array(Gradient["Total"]) Tot_grad.name = " TOTAL GRADIENT" Tot_grad.print_out() # Psi4's Total Gradient psi4_total_grad = psi4.core.Matrix.from_list( [[-0.00000000000000, -0.00000000000000, -0.05413558328761], [0.00000000000000, -0.06662229046965, 0.02706779164384], [-0.00000000000000, 0.06662229046965, 0.02706779164384]]) # Psi4Numpy Total Gradient total_grad = psi4.core.Matrix.from_array(Gradient["Total"]) # Compare Total Gradients psi4.compare_matrices(psi4_total_grad, total_grad, 10, "RHF_TOTAL_GRADIENT_TEST")