def test_uncoupled_rhf() -> None: mol = molecules.molecule_trithiolane_sto3g() mol.build() mf = pyscf.scf.RHF(mol) mf.scf() C = utils.fix_mocoeffs_shape(mf.mo_coeff) E = utils.fix_moenergies_shape(mf.mo_energy) occupations = occupations_from_pyscf_mol(mol, C) solver = solvers.ExactInv(C, E, occupations) ao2mo = AO2MOpyscf(C, mol.verbose, mol) ao2mo.perform_rhf_partial() solver.tei_mo = ao2mo.tei_mo solver.tei_mo_type = AO2MOTransformationType.partial driver = cphf.CPHF(solver) operator_diplen = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False, triplet=False) integrals_diplen_ao = mol.intor("cint1e_r_sph", comp=3) operator_diplen.ao_integrals = integrals_diplen_ao driver.add_operator(operator_diplen) frequencies = [0.0, 0.0773178, 0.128347] driver.set_frequencies(frequencies) driver.run( hamiltonian=Hamiltonian.RPA, spin=Spin.singlet, program=Program.PySCF, program_obj=mol, ) for idxf, frequency in enumerate(frequencies): print(idxf, frequency) print("uncoupled") diag_res = np.diag(driver.uncoupled_results[idxf]) diag_ref = np.diag(rhf_uncoupled[frequency]["result"]) diff = diag_res - diag_ref print(diag_res) print(diag_ref) print(diff) assert np.max( np.abs(diff)) < rhf_uncoupled[frequency]["error_max_diag"] print("coupled") diag_res = np.diag(driver.results[idxf]) diag_ref = np.diag(rhf_coupled[frequency]["result"]) diff = diag_res - diag_ref print(diag_res) print(diag_ref) print(diff) assert np.max(np.abs(diff)) < rhf_coupled[frequency]["error_max_diag"]
def test_uncoupled_uhf(): mol = molecules.molecule_trithiolane_sto3g() mol.charge = 1 mol.spin = 1 mol.build() mf = pyscf.scf.uhf.UHF(mol) mf.scf() C = utils.fix_mocoeffs_shape(mf.mo_coeff) E = utils.fix_moenergies_shape(mf.mo_energy) occupations = utils.occupations_from_pyscf_mol(mol, C) solver = iterators.ExactInv(C, E, occupations) ao2mo = AO2MOpyscf(C, mol.verbose, mol) ao2mo.perform_uhf_partial() solver.tei_mo = ao2mo.tei_mo solver.tei_mo_type = "partial" driver = cphf.CPHF(solver) operator_diplen = operators.Operator( label="dipole", is_imaginary=False, is_spin_dependent=False, triplet=False ) integrals_diplen_ao = mol.intor("cint1e_r_sph", comp=3) operator_diplen.ao_integrals = integrals_diplen_ao driver.add_operator(operator_diplen) frequencies = [0.0, 0.0773178, 0.128347, 0.4556355] driver.set_frequencies(frequencies) driver.run(solver_type="exact", hamiltonian="rpa", spin="singlet") for idxf, frequency in enumerate(frequencies): print(idxf, frequency) print("uncoupled") diag_res = np.diag(driver.uncoupled_results[idxf]) diag_ref = np.diag(uhf_uncoupled[frequency]["result"]) diff = diag_res - diag_ref print(diag_res) print(diag_ref) print(diff) assert np.max(np.abs(diff)) < uhf_uncoupled[frequency]["error_max_diag"] print("coupled") diag_res = np.diag(driver.results[idxf]) diag_ref = np.diag(uhf_coupled[frequency]["result"]) diff = diag_res - diag_ref print(diag_res) print(diag_ref) print(diff) assert np.max(np.abs(diff)) < uhf_coupled[frequency]["error_max_diag"] return
def test_explicit_uhf(): mol = molecules.molecule_water_sto3g() mol.charge = 1 mol.spin = 1 mol.build() mf = pyscf.scf.UHF(mol) mf.kernel() C = np.stack(mf.mo_coeff, axis=0) E_a = np.diag(mf.mo_energy[0]) E_b = np.diag(mf.mo_energy[1]) assert E_a.shape == E_b.shape E = np.stack((E_a, E_b), axis=0) integrals_dipole_ao = mol.intor("cint1e_r_sph", comp=3) occupations = utils.occupations_from_pyscf_mol(mol, C) solver = iterators.ExactInv(C, E, occupations) ao2mo = AO2MOpyscf(C, mol.verbose, mol) ao2mo.perform_uhf_full() solver.tei_mo = ao2mo.tei_mo solver.tei_mo_type = "full" driver = cphf.CPHF(solver) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = integrals_dipole_ao driver.add_operator(operator_dipole) driver.set_frequencies() driver.run(solver_type="exact", hamiltonian="rpa", spin="singlet") assert len(driver.frequencies) == len(driver.results) == 1 res = driver.results[0] print(res) atol = 1.0e-5 rtol = 0.0 np.testing.assert_allclose(res, ref_water_cation_UHF_HF_STO3G, rtol=rtol, atol=atol)
def test_explicit_uhf() -> None: mol = molecules.molecule_water_sto3g() mol.charge = 1 mol.spin = 1 mol.build() mf = pyscf.scf.UHF(mol) mf.kernel() C = np.stack(mf.mo_coeff, axis=0) C_a = C[0, ...] C_b = C[1, ...] E_a = np.diag(mf.mo_energy[0]) E_b = np.diag(mf.mo_energy[1]) assert C_a.shape == C_b.shape assert E_a.shape == E_b.shape E = np.stack((E_a, E_b), axis=0) norb = C_a.shape[1] nocc_a, nocc_b = mol.nelec nvirt_a, nvirt_b = norb - nocc_a, norb - nocc_b occupations = [nocc_a, nvirt_a, nocc_b, nvirt_b] C_occ_alph = C_a[:, :nocc_a] C_virt_alph = C_a[:, nocc_a:] C_occ_beta = C_b[:, :nocc_b] C_virt_beta = C_b[:, nocc_b:] C_ovov_aaaa = (C_occ_alph, C_virt_alph, C_occ_alph, C_virt_alph) C_ovov_aabb = (C_occ_alph, C_virt_alph, C_occ_beta, C_virt_beta) C_ovov_bbaa = (C_occ_beta, C_virt_beta, C_occ_alph, C_virt_alph) C_ovov_bbbb = (C_occ_beta, C_virt_beta, C_occ_beta, C_virt_beta) C_oovv_aaaa = (C_occ_alph, C_occ_alph, C_virt_alph, C_virt_alph) C_oovv_bbbb = (C_occ_beta, C_occ_beta, C_virt_beta, C_virt_beta) tei_mo_ovov_aaaa = pyscf.ao2mo.general(mol, C_ovov_aaaa, aosym="s4", compact=False, verbose=5).reshape( nocc_a, nvirt_a, nocc_a, nvirt_a) tei_mo_ovov_aabb = pyscf.ao2mo.general(mol, C_ovov_aabb, aosym="s4", compact=False, verbose=5).reshape( nocc_a, nvirt_a, nocc_b, nvirt_b) tei_mo_ovov_bbaa = pyscf.ao2mo.general(mol, C_ovov_bbaa, aosym="s4", compact=False, verbose=5).reshape( nocc_b, nvirt_b, nocc_a, nvirt_a) tei_mo_ovov_bbbb = pyscf.ao2mo.general(mol, C_ovov_bbbb, aosym="s4", compact=False, verbose=5).reshape( nocc_b, nvirt_b, nocc_b, nvirt_b) tei_mo_oovv_aaaa = pyscf.ao2mo.general(mol, C_oovv_aaaa, aosym="s4", compact=False, verbose=5).reshape( nocc_a, nocc_a, nvirt_a, nvirt_a) tei_mo_oovv_bbbb = pyscf.ao2mo.general(mol, C_oovv_bbbb, aosym="s4", compact=False, verbose=5).reshape( nocc_b, nocc_b, nvirt_b, nvirt_b) integrals_dipole_ao = mol.intor("cint1e_r_sph", comp=3) solver = solvers.ExactInv(C, E, occupations) solver.tei_mo = ( tei_mo_ovov_aaaa, tei_mo_ovov_aabb, tei_mo_ovov_bbaa, tei_mo_ovov_bbbb, tei_mo_oovv_aaaa, tei_mo_oovv_bbbb, ) solver.tei_mo_type = AO2MOTransformationType.partial driver = cphf.CPHF(solver) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = integrals_dipole_ao driver.add_operator(operator_dipole) driver.set_frequencies() driver.run( hamiltonian=Hamiltonian.RPA, spin=Spin.singlet, program=Program.PySCF, program_obj=mol, ) assert len(driver.frequencies) == len(driver.results) == 1 res = driver.results[0] print(res) atol = 1.0e-5 rtol = 0.0 np.testing.assert_allclose(res, ref_water_cation_UHF_HF_STO3G, rtol=rtol, atol=atol)
def test_final_result_rhf_h2o_sto3g_tda_triplet() -> None: """Test correctness of the final result for water/STO-3G with the TDA approximation/CIS for triplet response induced by the dipole length operator computed with quantities from disk. """ hamiltonian = Hamiltonian.TDA spin = Spin.triplet C = utils.fix_mocoeffs_shape(utils.np_load(REFDIR / "C.npz")) E = utils.fix_moenergies_shape(utils.np_load(REFDIR / "F_MO.npz")) TEI_MO = utils.np_load(REFDIR / "TEI_MO.npz") # nocc_alph, nvirt_alph, nocc_beta, nvirt_beta occupations = np.asarray([5, 2, 5, 2], dtype=int) stub = "h2o_sto3g_" dim = occupations[0] + occupations[1] mat_dipole_x = utils.parse_int_file_2(REFDIR / f"{stub}mux.dat", dim) mat_dipole_y = utils.parse_int_file_2(REFDIR / f"{stub}muy.dat", dim) mat_dipole_z = utils.parse_int_file_2(REFDIR / f"{stub}muz.dat", dim) solver = solvers.ExactInv(C, E, occupations) solver.tei_mo = (TEI_MO, ) solver.tei_mo_type = AO2MOTransformationType.full driver = cphf.CPHF(solver) ao_integrals_dipole = np.stack((mat_dipole_x, mat_dipole_y, mat_dipole_z), axis=0) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = ao_integrals_dipole driver.add_operator(operator_dipole) frequencies = (0.0, 0.02, 0.06, 0.1) driver.set_frequencies(frequencies) driver.run(hamiltonian=hamiltonian, spin=spin, program=None, program_obj=None) assert len(driver.results) == len(frequencies) result__0_00 = np.array([[14.64430714, 0.0, 0.0], [0.0, 8.80921432, 0.0], [0.0, 0.0, 0.06859496]]) result__0_02 = np.array([[14.68168443, 0.0, 0.0], [0.0, 8.83562647, 0.0], [0.0, 0.0, 0.0689291]]) result__0_06 = np.array([[14.98774296, 0.0, 0.0], [0.0, 9.0532224, 0.0], [0.0, 0.0, 0.07172414]]) result__0_10 = np.array([[15.63997724, 0.0, 0.0], [0.0, 9.52504267, 0.0], [0.0, 0.0, 0.07805428]]) atol = 1.0e-8 rtol = 0.0 np.testing.assert_allclose(driver.results[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[3], result__0_10, rtol=rtol, atol=atol) mol = molecules.molecule_water_sto3g() mol.build() polarizability = electric.Polarizability( Program.PySCF, mol, cphf.CPHF(solvers.ExactInv(C, E, occupations)), C, E, occupations, frequencies=frequencies, ) polarizability.form_operators() polarizability.run(hamiltonian=hamiltonian, spin=spin) polarizability.form_results() np.testing.assert_allclose(polarizability.polarizabilities[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[3], result__0_10, rtol=rtol, atol=atol)
def test_final_result_rhf_h2o_sto3g_rpa_singlet() -> None: """Test correctness of the final result for water/STO-3G with full RPA for singlet response induced by the dipole length operator (the electric polarizability) computed with quantities from disk. """ hamiltonian = Hamiltonian.RPA spin = Spin.singlet C = utils.fix_mocoeffs_shape(utils.np_load(REFDIR / "C.npz")) E = utils.fix_moenergies_shape(utils.np_load(REFDIR / "F_MO.npz")) TEI_MO = utils.np_load(REFDIR / "TEI_MO.npz") # nocc_alph, nvirt_alph, nocc_beta, nvirt_beta occupations = np.asarray([5, 2, 5, 2], dtype=int) stub = "h2o_sto3g_" dim = occupations[0] + occupations[1] mat_dipole_x = utils.parse_int_file_2(REFDIR / f"{stub}mux.dat", dim) mat_dipole_y = utils.parse_int_file_2(REFDIR / f"{stub}muy.dat", dim) mat_dipole_z = utils.parse_int_file_2(REFDIR / f"{stub}muz.dat", dim) solver = solvers.ExactInv(C, E, occupations) solver.tei_mo = (TEI_MO, ) solver.tei_mo_type = AO2MOTransformationType.full driver = cphf.CPHF(solver) ao_integrals_dipole = np.stack((mat_dipole_x, mat_dipole_y, mat_dipole_z), axis=0) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = ao_integrals_dipole driver.add_operator(operator_dipole) frequencies = (0.0, 0.02, 0.06, 0.1) driver.set_frequencies(frequencies) driver.run(hamiltonian=hamiltonian, spin=spin, program=None, program_obj=None) assert len(driver.results) == len(frequencies) result__0_00 = np.array([[7.93556221, 0.0, 0.0], [0.0, 3.06821077, 0.0], [0.0, 0.0, 0.05038621]]) result__0_02 = np.array([[7.94312371, 0.0, 0.0], [0.0, 3.07051688, 0.0], [0.0, 0.0, 0.05054685]]) result__0_06 = np.array([[8.00414009, 0.0, 0.0], [0.0, 3.08913608, 0.0], [0.0, 0.0, 0.05186977]]) result__0_10 = np.array([[8.1290378, 0.0, 0.0], [0.0, 3.12731363, 0.0], [0.0, 0.0, 0.05473482]]) atol = 1.0e-8 rtol = 0.0 np.testing.assert_allclose(driver.results[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[3], result__0_10, rtol=rtol, atol=atol) # Reminder: there's no call to do SCF here because we already have # the MO coefficients. mol = molecules.molecule_water_sto3g() mol.build() polarizability = electric.Polarizability( Program.PySCF, mol, cphf.CPHF(solvers.ExactInv(C, E, occupations)), C, E, occupations, frequencies=frequencies, ) polarizability.form_operators() polarizability.run(hamiltonian=hamiltonian, spin=spin) polarizability.form_results() np.testing.assert_allclose(polarizability.polarizabilities[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[3], result__0_10, rtol=rtol, atol=atol)
def test_final_result_rhf_h2o_sto3g_rpa_triplet() -> None: """Test correctness of the final result for water/STO-3G with full RPA for triplet response induced by the dipole length operator computed with quantities from disk. """ hamiltonian = Hamiltonian.RPA spin = Spin.triplet C = utils.fix_mocoeffs_shape(utils.np_load(REFDIR / "C.npz")) E = utils.fix_moenergies_shape(utils.np_load(REFDIR / "F_MO.npz")) TEI_MO = utils.np_load(REFDIR / "TEI_MO.npz") # nocc_alph, nvirt_alph, nocc_beta, nvirt_beta occupations = np.asarray([5, 2, 5, 2], dtype=int) stub = "h2o_sto3g_" dim = occupations[0] + occupations[1] mat_dipole_x = utils.parse_int_file_2(REFDIR / f"{stub}mux.dat", dim) mat_dipole_y = utils.parse_int_file_2(REFDIR / f"{stub}muy.dat", dim) mat_dipole_z = utils.parse_int_file_2(REFDIR / f"{stub}muz.dat", dim) solver = solvers.ExactInv(C, E, occupations) solver.tei_mo = (TEI_MO, ) solver.tei_mo_type = AO2MOTransformationType.full driver = cphf.CPHF(solver) ao_integrals_dipole = np.stack((mat_dipole_x, mat_dipole_y, mat_dipole_z), axis=0) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = ao_integrals_dipole driver.add_operator(operator_dipole) frequencies = (0.0, 0.02, 0.06, 0.1) driver.set_frequencies(frequencies) driver.run(hamiltonian=hamiltonian, spin=spin, program=None, program_obj=None) assert len(driver.results) == len(frequencies) result__0_00 = np.array([[26.59744305, 0.0, 0.0], [0.0, 18.11879557, 0.0], [0.0, 0.0, 0.07798969]]) result__0_02 = np.array([[26.68282287, 0.0, 0.0], [0.0, 18.19390051, 0.0], [0.0, 0.0, 0.07837521]]) result__0_06 = np.array([[27.38617401, 0.0, 0.0], [0.0, 18.81922578, 0.0], [0.0, 0.0, 0.08160226]]) result__0_10 = np.array([[28.91067234, 0.0, 0.0], [0.0, 20.21670386, 0.0], [0.0, 0.0, 0.08892512]]) atol = 1.0e-8 rtol = 0.0 np.testing.assert_allclose(driver.results[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[3], result__0_10, rtol=rtol, atol=atol) mol = molecules.molecule_water_sto3g() mol.build() polarizability = electric.Polarizability( Program.PySCF, mol, cphf.CPHF(solvers.ExactInv(C, E, occupations)), C, E, occupations, frequencies=frequencies, ) polarizability.form_operators() polarizability.run(hamiltonian=hamiltonian, spin=spin) polarizability.form_results() np.testing.assert_allclose(polarizability.polarizabilities[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[3], result__0_10, rtol=rtol, atol=atol)
def test_final_result_rhf_h2o_sto3g_tda_triplet(): hamiltonian = "tda" spin = "triplet" C = utils.fix_mocoeffs_shape(utils.np_load(REFDIR / "C.npz")) E = utils.fix_moenergies_shape(utils.np_load(REFDIR / "F_MO.npz")) TEI_MO = utils.np_load(REFDIR / "TEI_MO.npz") # nocc_alph, nvirt_alph, nocc_beta, nvirt_beta occupations = [5, 2, 5, 2] stub = "h2o_sto3g_" dim = occupations[0] + occupations[1] mat_dipole_x = utils.parse_int_file_2(REFDIR / f"{stub}mux.dat", dim) mat_dipole_y = utils.parse_int_file_2(REFDIR / f"{stub}muy.dat", dim) mat_dipole_z = utils.parse_int_file_2(REFDIR / f"{stub}muz.dat", dim) solver = iterators.ExactInv(C, E, occupations) solver.tei_mo = (TEI_MO, ) solver.tei_mo_type = "full" driver = cphf.CPHF(solver) ao_integrals_dipole = np.stack((mat_dipole_x, mat_dipole_y, mat_dipole_z), axis=0) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = ao_integrals_dipole driver.add_operator(operator_dipole) frequencies = (0.0, 0.02, 0.06, 0.1) driver.set_frequencies(frequencies) driver.run(solver_type="exact", hamiltonian=hamiltonian, spin=spin) assert len(driver.results) == len(frequencies) result__0_00 = np.array([[14.64430714, 0.0, 0.0], [0.0, 8.80921432, 0.0], [0.0, 0.0, 0.06859496]]) result__0_02 = np.array([[14.68168443, 0.0, 0.0], [0.0, 8.83562647, 0.0], [0.0, 0.0, 0.0689291]]) result__0_06 = np.array([[14.98774296, 0.0, 0.0], [0.0, 9.0532224, 0.0], [0.0, 0.0, 0.07172414]]) result__0_10 = np.array([[15.63997724, 0.0, 0.0], [0.0, 9.52504267, 0.0], [0.0, 0.0, 0.07805428]]) atol = 1.0e-8 rtol = 0.0 np.testing.assert_allclose(driver.results[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[3], result__0_10, rtol=rtol, atol=atol) mol = molecules.molecule_water_sto3g() mol.build() polarizability = electric.Polarizability(Program.PySCF, mol, C, E, occupations, frequencies) polarizability.form_operators() polarizability.run(hamiltonian=hamiltonian, spin=spin) polarizability.form_results() np.testing.assert_allclose(polarizability.polarizabilities[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[3], result__0_10, rtol=rtol, atol=atol) return
def test_final_result_rhf_h2o_sto3g_tda_singlet(): hamiltonian = "tda" spin = "singlet" C = utils.fix_mocoeffs_shape(utils.np_load(REFDIR / "C.npz")) E = utils.fix_moenergies_shape(utils.np_load(REFDIR / "F_MO.npz")) TEI_MO = utils.np_load(REFDIR / "TEI_MO.npz") # nocc_alph, nvirt_alph, nocc_beta, nvirt_beta occupations = [5, 2, 5, 2] stub = "h2o_sto3g_" dim = occupations[0] + occupations[1] mat_dipole_x = utils.parse_int_file_2(REFDIR / f"{stub}mux.dat", dim) mat_dipole_y = utils.parse_int_file_2(REFDIR / f"{stub}muy.dat", dim) mat_dipole_z = utils.parse_int_file_2(REFDIR / f"{stub}muz.dat", dim) solver = iterators.ExactInv(C, E, occupations) solver.tei_mo = (TEI_MO, ) solver.tei_mo_type = "full" driver = cphf.CPHF(solver) ao_integrals_dipole = np.stack((mat_dipole_x, mat_dipole_y, mat_dipole_z), axis=0) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = ao_integrals_dipole driver.add_operator(operator_dipole) frequencies = (0.0, 0.02, 0.06, 0.1) driver.set_frequencies(frequencies) driver.run(solver_type="exact", hamiltonian=hamiltonian, spin=spin) assert len(driver.results) == len(frequencies) result__0_00 = np.array([[8.89855952, 0.0, 0.0], [0.0, 4.00026556, 0.0], [0.0, 0.0, 0.0552774]]) result__0_02 = np.array([[8.90690928, 0.0, 0.0], [0.0, 4.00298342, 0.0], [0.0, 0.0, 0.05545196]]) result__0_06 = np.array([[8.97427725, 0.0, 0.0], [0.0, 4.02491517, 0.0], [0.0, 0.0, 0.05688918]]) result__0_10 = np.array([[9.11212633, 0.0, 0.0], [0.0, 4.06981937, 0.0], [0.0, 0.0, 0.05999934]]) atol = 1.0e-8 rtol = 0.0 np.testing.assert_allclose(driver.results[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[3], result__0_10, rtol=rtol, atol=atol) mol = molecules.molecule_water_sto3g() mol.build() polarizability = electric.Polarizability(Program.PySCF, mol, C, E, occupations, frequencies) polarizability.form_operators() polarizability.run(hamiltonian=hamiltonian, spin=spin) polarizability.form_results() np.testing.assert_allclose(polarizability.polarizabilities[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[3], result__0_10, rtol=rtol, atol=atol) return
def test_final_result_rhf_h2o_sto3g_rpa_singlet(): hamiltonian = "rpa" spin = "singlet" C = utils.fix_mocoeffs_shape(utils.np_load(REFDIR / "C.npz")) E = utils.fix_moenergies_shape(utils.np_load(REFDIR / "F_MO.npz")) TEI_MO = utils.np_load(REFDIR / "TEI_MO.npz") # nocc_alph, nvirt_alph, nocc_beta, nvirt_beta occupations = [5, 2, 5, 2] stub = "h2o_sto3g_" dim = occupations[0] + occupations[1] mat_dipole_x = utils.parse_int_file_2(REFDIR / f"{stub}mux.dat", dim) mat_dipole_y = utils.parse_int_file_2(REFDIR / f"{stub}muy.dat", dim) mat_dipole_z = utils.parse_int_file_2(REFDIR / f"{stub}muz.dat", dim) solver = iterators.ExactInv(C, E, occupations) solver.tei_mo = (TEI_MO, ) solver.tei_mo_type = "full" driver = cphf.CPHF(solver) ao_integrals_dipole = np.stack((mat_dipole_x, mat_dipole_y, mat_dipole_z), axis=0) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = ao_integrals_dipole driver.add_operator(operator_dipole) frequencies = (0.0, 0.02, 0.06, 0.1) driver.set_frequencies(frequencies) driver.run(solver_type="exact", hamiltonian=hamiltonian, spin=spin) assert len(driver.results) == len(frequencies) result__0_00 = np.array([[7.93556221, 0.0, 0.0], [0.0, 3.06821077, 0.0], [0.0, 0.0, 0.05038621]]) result__0_02 = np.array([[7.94312371, 0.0, 0.0], [0.0, 3.07051688, 0.0], [0.0, 0.0, 0.05054685]]) result__0_06 = np.array([[8.00414009, 0.0, 0.0], [0.0, 3.08913608, 0.0], [0.0, 0.0, 0.05186977]]) result__0_10 = np.array([[8.1290378, 0.0, 0.0], [0.0, 3.12731363, 0.0], [0.0, 0.0, 0.05473482]]) atol = 1.0e-8 rtol = 0.0 np.testing.assert_allclose(driver.results[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[3], result__0_10, rtol=rtol, atol=atol) # Reminder: there's no call to do SCF here because we already have # the MO coefficients. mol = molecules.molecule_water_sto3g() mol.build() polarizability = electric.Polarizability(Program.PySCF, mol, C, E, occupations, frequencies) polarizability.form_operators() polarizability.run(hamiltonian=hamiltonian, spin=spin) polarizability.form_results() np.testing.assert_allclose(polarizability.polarizabilities[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[3], result__0_10, rtol=rtol, atol=atol) return
def test_final_result_rhf_h2o_sto3g_rpa_triplet(): hamiltonian = "rpa" spin = "triplet" C = utils.fix_mocoeffs_shape(utils.np_load(REFDIR / "C.npz")) E = utils.fix_moenergies_shape(utils.np_load(REFDIR / "F_MO.npz")) TEI_MO = utils.np_load(REFDIR / "TEI_MO.npz") # nocc_alph, nvirt_alph, nocc_beta, nvirt_beta occupations = [5, 2, 5, 2] stub = "h2o_sto3g_" dim = occupations[0] + occupations[1] mat_dipole_x = utils.parse_int_file_2(REFDIR / f"{stub}mux.dat", dim) mat_dipole_y = utils.parse_int_file_2(REFDIR / f"{stub}muy.dat", dim) mat_dipole_z = utils.parse_int_file_2(REFDIR / f"{stub}muz.dat", dim) solver = iterators.ExactInv(C, E, occupations) solver.tei_mo = (TEI_MO, ) solver.tei_mo_type = "full" driver = cphf.CPHF(solver) ao_integrals_dipole = np.stack((mat_dipole_x, mat_dipole_y, mat_dipole_z), axis=0) operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False) operator_dipole.ao_integrals = ao_integrals_dipole driver.add_operator(operator_dipole) frequencies = (0.0, 0.02, 0.06, 0.1) driver.set_frequencies(frequencies) driver.run(solver_type="exact", hamiltonian=hamiltonian, spin=spin) assert len(driver.results) == len(frequencies) result__0_00 = np.array([[26.59744305, 0.0, 0.0], [0.0, 18.11879557, 0.0], [0.0, 0.0, 0.07798969]]) result__0_02 = np.array([[26.68282287, 0.0, 0.0], [0.0, 18.19390051, 0.0], [0.0, 0.0, 0.07837521]]) result__0_06 = np.array([[27.38617401, 0.0, 0.0], [0.0, 18.81922578, 0.0], [0.0, 0.0, 0.08160226]]) result__0_10 = np.array([[28.91067234, 0.0, 0.0], [0.0, 20.21670386, 0.0], [0.0, 0.0, 0.08892512]]) atol = 1.0e-8 rtol = 0.0 np.testing.assert_allclose(driver.results[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(driver.results[3], result__0_10, rtol=rtol, atol=atol) mol = molecules.molecule_water_sto3g() mol.build() polarizability = electric.Polarizability(Program.PySCF, mol, C, E, occupations, frequencies) polarizability.form_operators() polarizability.run(hamiltonian=hamiltonian, spin=spin) polarizability.form_results() np.testing.assert_allclose(polarizability.polarizabilities[0], result__0_00, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[1], result__0_02, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[2], result__0_06, rtol=rtol, atol=atol) np.testing.assert_allclose(polarizability.polarizabilities[3], result__0_10, rtol=rtol, atol=atol) return
def calculate_uhf( dalton_tmpdir, hamiltonian=None, spin=None, operator_label=None, operator=None, source_moenergies=None, source_mocoeffs=None, source_operator=None, ): if operator_label: # TODO add dipvel assert operator_label in ("dipole", "angmom", "spinorb") assert source_moenergies in ("pyscf", "dalton") assert source_mocoeffs in ("pyscf", "dalton") dalton_molecule = dalmol.readin(dalton_tmpdir / "DALTON.BAS") lines = [] for atom in dalton_molecule: label = atom["label"][0] center = atom["center"][0] center_str = " ".join(["{:f}".format(pos) for pos in center]) line = "{:3} {}".format(label, center_str) lines.append(line) lines = "\n".join(lines) # PySCF molecule setup, needed for generating the TEIs in the MO # basis. mol = pyscf.gto.Mole() verbose = 1 mol.verbose = verbose mol.atom = lines mol.unit = "Bohr" # TODO read basis from DALTON molecule mol.basis = "sto-3g" mol.symmetry = False # TODO read charge from DALTON molecule? mol.charge = 1 # TODO read spin from DALTON molecule? mol.spin = 1 mol.build() ifc = sirifc.sirifc(dalton_tmpdir / "SIRIFC") occupations = utils.occupations_from_sirifc(ifc) if source_moenergies == "pyscf" or source_mocoeffs == "pyscf": mf = pyscf.scf.UHF(mol) mf.kernel() if source_moenergies == "pyscf": E_alph = np.diag(mf.mo_energy[0]) E_beta = np.diag(mf.mo_energy[1]) E = np.stack((E_alph, E_beta), axis=0) elif source_moenergies == "dalton": job = ccopen(dalton_tmpdir / "DALTON.OUT") data = job.parse() # pylint: disable=no-member E = np.diag([ convertor(x, "eV", "hartree") for x in data.moenergies[0] ])[np.newaxis, ...] E = np.concatenate((E, E), axis=0) else: pass if source_mocoeffs == "pyscf": C = mf.mo_coeff elif source_mocoeffs == "dalton": C = ifc.cmo[0][np.newaxis, ...] C = np.concatenate((C, C), axis=0) else: pass solver = iterators.ExactInv(C, E, occupations) solver.tei_mo = ao2mo.perform_tei_ao2mo_uhf_partial(mol, C) solver.tei_mo_type = "partial" driver = cphf.CPHF(solver) if operator: driver.add_operator(operator) elif operator_label: if operator_label == "dipole": operator_dipole = operators.Operator(label="dipole", is_imaginary=False, is_spin_dependent=False, triplet=False) integrals_dipole_ao = mol.intor("cint1e_r_sph", comp=3) operator_dipole.ao_integrals = integrals_dipole_ao driver.add_operator(operator_dipole) elif operator_label == "angmom": operator_angmom = operators.Operator(label="angmom", is_imaginary=True, is_spin_dependent=False, triplet=False) integrals_angmom_ao = mol.intor("cint1e_cg_irxp_sph", comp=3) operator_angmom.ao_integrals = integrals_angmom_ao driver.add_operator(operator_angmom) elif operator_label == "spinorb": operator_spinorb = operators.Operator(label="spinorb", is_imaginary=True, is_spin_dependent=False, triplet=False) integrals_spinorb_ao = 0 for atm_id in range(mol.natm): mol.set_rinv_orig(mol.atom_coord(atm_id)) chg = mol.atom_charge(atm_id) integrals_spinorb_ao += chg * mol.intor("cint1e_prinvxp_sph", comp=3) operator_spinorb.ao_integrals = integrals_spinorb_ao driver.add_operator(operator_spinorb) else: pass else: pass driver.set_frequencies() driver.run(solver_type="exact", hamiltonian=hamiltonian, spin=spin) return driver.results[0]