Ejemplo n.º 1
0
def test_inversion() -> None:
    """Test that each kind of inversion function gives identical results."""

    mol = molecules_pyscf.molecule_glycine_sto3g()
    mol.charge = 1
    mol.spin = 1
    mol.build()

    mf = pyscf.scf.uhf.UHF(mol)
    mf.scf()

    assert isinstance(mf.mo_coeff, np.ndarray)
    assert len(mf.mo_coeff) == 2
    C = utils.fix_mocoeffs_shape(mf.mo_coeff)
    E = utils.fix_moenergies_shape(mf.mo_energy)
    occupations = occupations_from_pyscf_mol(mol, C)

    calculator_ref = magnetic.Magnetizability(
        Program.PySCF,
        mol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
    )
    calculator_ref.form_operators()
    calculator_ref.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    calculator_ref.form_results()

    inv_funcs = (sp.linalg.inv, sp.linalg.pinv, sp.linalg.pinv2)

    thresh = 6.0e-14

    for inv_func in inv_funcs:
        calculator_res = magnetic.Magnetizability(
            Program.PySCF,
            mol,
            cphf.CPHF(solvers.ExactInv(C, E, occupations)),
            C,
            E,
            occupations,
        )
        calculator_res.form_operators()
        calculator_res.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
        calculator_res.form_results()

        np.testing.assert_equal(
            np.sign(calculator_ref.magnetizability),
            np.sign(calculator_res.magnetizability),
        )
        diff = calculator_ref.magnetizability - calculator_res.magnetizability
        abs_diff = np.abs(diff)
        print(abs_diff)
        assert np.all(abs_diff < thresh)
Ejemplo n.º 2
0
def test_final_result_rhf_h2o_sto3g_rpa_singlet_iter() -> None:
    mol = molecules_psi4.molecule_physicists_water_sto3g()
    psi4.core.set_active_molecule(mol)
    psi4.set_options({
        "scf_type": "direct",
        "df_scf_guess": False,
        "e_convergence": 1e-11,
        "d_convergence": 1e-11,
    })
    _, wfn = psi4.energy("hf", return_wfn=True)
    C = mocoeffs_from_psi4wfn(wfn)
    E = moenergies_from_psi4wfn(wfn)
    occupations = occupations_from_psi4wfn(wfn)

    frequencies = [0.0, 0.0773178]

    ref_polarizability = electric.Polarizability(
        Program.Psi4,
        mol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
        frequencies=frequencies,
    )
    ref_polarizability.form_operators()
    ref_polarizability.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    ref_polarizability.form_results()
    ref_operator = ref_polarizability.driver.solver.operators[0]
    res_polarizability = electric.Polarizability(
        Program.Psi4,
        mol,
        cphf.CPHF(
            solvers.IterativeLinEqSolver(C,
                                         E,
                                         occupations,
                                         integrals.JKPsi4(wfn),
                                         conv=1.0e-12)),
        C,
        E,
        occupations,
        frequencies=frequencies,
    )
    res_polarizability.form_operators()
    res_polarizability.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    res_polarizability.form_results()
    res_operator = res_polarizability.driver.solver.operators[0]

    np.testing.assert_allclose(
        ref_polarizability.polarizabilities,
        res_polarizability.polarizabilities,
        rtol=0.0,
        atol=1.0e-6,
    )
Ejemplo n.º 3
0
def test_magnetizability_uhf() -> None:
    mol = molecules.molecule_glycine_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 = occupations_from_pyscf_mol(mol, C)

    calculator_common = magnetic.Magnetizability(
        Program.PySCF,
        mol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
    )
    calculator_common.form_operators()
    calculator_common.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    calculator_common.form_results()
    ref_eigvals, ref_iso, _ = utils.tensor_printer(ref_magnetizability_rohf)
    res_eigvals, res_iso, _ = utils.tensor_printer(
        calculator_common.magnetizability)
    thresh_eigval = 1.0e-1
    for i in range(3):
        assert abs(ref_eigvals[i] - res_eigvals[i]) < thresh_eigval
Ejemplo n.º 4
0
def test_electronicgtensor_small() -> None:

    mol = molecules.molecule_bc2h4_neutral_radical_sto3g()
    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 = occupations_from_pyscf_mol(mol, C)

    gtensor_calculator = magnetic.ElectronicGTensor(
        Program.PySCF,
        mol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
    )
    gtensor_calculator.form_operators()
    gtensor_calculator.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    gtensor_calculator.form_results()

    print(ref_electronicgtensor_small)
    print(gtensor_calculator.g_oz_soc_1)

    assert np.all(
        np.equal(np.sign(ref_electronicgtensor_small),
                 np.sign(gtensor_calculator.g_oz_soc_1)))
Ejemplo n.º 5
0
def test_ORD_RPA_singlet_BC2H4_HF_STO3G():

    ref = BC2H4_HF_STO3G_RPA_singlet_nwchem

    pyscfmol = molecules.molecule_bc2h4_neutral_radical_sto3g()
    pyscfmol.build()

    mf = pyscf.scf.UHF(pyscfmol)
    mf.scf()

    C = utils.fix_mocoeffs_shape(mf.mo_coeff)
    E = utils.fix_moenergies_shape(mf.mo_energy)
    occupations = occupations_from_pyscf_mol(pyscfmol, C)

    frequencies = [0.0, 0.001, 0.0773178, 0.128347]
    ord_solver = optrot.ORD(
        Program.PySCF,
        pyscfmol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
        frequencies=frequencies,
        do_dipvel=False,
    )
    ord_solver.form_operators()
    ord_solver.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    ord_solver.form_results()

    print("Polarizabilities")
    assert len(frequencies) == len(ord_solver.polarizabilities)
    thresh = 6.5e-4
    for idxf, frequency in enumerate(frequencies):
        ref_polar = ref[frequency]["polar"]
        res_polar = ord_solver.polarizabilities[idxf]
        abs_diff = abs(res_polar - ref_polar)
        print(idxf, frequency)
        print(res_polar)
        print(abs_diff)
        if frequency == 0.128347:
            thresh = 6.0e-3
        assert (abs_diff < thresh).all()

    print(r"\beta(\omega)")
    thresh = 0.09
    for idxf, frequency in enumerate(frequencies):
        if "orbeta" in ref[frequency]:
            ref_beta = ref[frequency]["orbeta"]
            # TODO why no speed of light?
            # TODO why the (1/2)?
            res_beta = -(0.5 /
                         frequency) * ord_solver.driver.results[idxf][3:6, 0:3]
            abs_diff = abs(res_beta - ref_beta)
            print(idxf, frequency)
            print(res_beta)
            print(ref_beta)
            print(abs_diff)
            assert (abs_diff < thresh).all()
Ejemplo n.º 6
0
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"]
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
def test_ORD_RPA_singlet_trithiolane_HF_STO3G():

    ref = trithiolane_HF_STO3G_RPA_singlet

    pyscfmol = molecules.molecule_trithiolane_sto3g()
    pyscfmol.build()

    mf = pyscf.scf.RHF(pyscfmol)
    mf.scf()

    C = utils.fix_mocoeffs_shape(mf.mo_coeff)
    E = utils.fix_moenergies_shape(mf.mo_energy)
    occupations = occupations_from_pyscf_mol(pyscfmol, C)

    frequencies = [0.0, 0.0773178, 0.128347]
    ord_solver = optrot.ORD(
        Program.PySCF,
        pyscfmol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
        frequencies=frequencies,
        do_dipvel=False,
    )
    ord_solver.form_operators()
    ord_solver.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    ord_solver.form_results()

    print("Polarizabilities")
    assert len(frequencies) == len(ord_solver.polarizabilities)
    thresh = 5.0e-4
    for idxf, frequency in enumerate(frequencies):
        ref_polar = ref[frequency]["polar"]
        res_polar = ord_solver.polarizabilities[idxf]
        abs_diff = abs(res_polar - ref_polar)
        print(idxf, frequency)
        print(res_polar)
        print(abs_diff)
        assert (abs_diff < thresh).all()
Ejemplo n.º 10
0
def test_first_hyperpolarizability_shg_rhf_wigner_explicit_psi4numpy_pyscf_large():
    mol = molecule_physicists_water_augccpvdz()
    mol.build()

    mf = pyscf.scf.RHF(mol)
    mf.kernel()
    C = utils.fix_mocoeffs_shape(mf.mo_coeff)
    E = utils.fix_moenergies_shape(mf.mo_energy)
    occupations = occupations_from_pyscf_mol(mol, C)
    nocc_alph, nvirt_alph, _, _ = occupations
    nov_alph = nocc_alph * nvirt_alph
    norb = nocc_alph + nvirt_alph

    # calculate linear response vectors for electric dipole operator
    f1 = 0.0773178
    f2 = 2 * f1
    frequencies = [f1, f2]
    calculator = electric.Polarizability(
        Program.PySCF,
        mol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
        frequencies=frequencies,
    )
    calculator.form_operators()
    calculator.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    calculator.form_results()

    polarizability_1 = calculator.polarizabilities[0]
    polarizability_2 = calculator.polarizabilities[1]
    print("polarizability: {} a.u.".format(f1))
    print(polarizability_1)
    print("polarizability: {} a.u. (frequency doubled)".format(f2))
    print(polarizability_2)

    # each operator contains multiple sets of response vectors, one
    # set of components for each frequency
    assert isinstance(calculator.driver.solver.operators, list)
    assert len(calculator.driver.solver.operators) == 1
    operator = calculator.driver.solver.operators[0]
    rhsvecs = operator.mo_integrals_ai_supervector_alph
    assert isinstance(operator.rspvecs_alph, list)
    assert len(operator.rspvecs_alph) == 2
    rspvecs_1 = operator.rspvecs_alph[0]
    rspvecs_2 = operator.rspvecs_alph[1]

    ## Form the full [norb, norb] representation of everything.
    # Response vectors: transform X_{ia} and Y_{ia} -> U_{p,q}
    # 0. 'a' is fast index, 'i' slow
    # 1. rspvec == [X Y]
    # 2. U_{p, q} -> zero
    # 3. place X_{ia} into U_{i, a}
    # 4. place Y_{ia} into U_{a, i}

    ncomp = rhsvecs.shape[0]

    rspmats_1 = np.zeros(shape=(ncomp, norb, norb))
    rspmats_2 = np.zeros(shape=(ncomp, norb, norb))
    for i in range(ncomp):
        rspvec_1 = rspvecs_1[i, :, 0]
        rspvec_2 = rspvecs_2[i, :, 0]
        x_1 = rspvec_1[:nov_alph]
        y_1 = rspvec_1[nov_alph:]
        x_2 = rspvec_2[:nov_alph]
        y_2 = rspvec_2[nov_alph:]
        x_full_1 = utils.repack_vector_to_matrix(x_1, (nvirt_alph, nocc_alph))
        y_full_1 = utils.repack_vector_to_matrix(y_1, (nvirt_alph, nocc_alph))
        x_full_2 = utils.repack_vector_to_matrix(x_2, (nvirt_alph, nocc_alph))
        y_full_2 = utils.repack_vector_to_matrix(y_2, (nvirt_alph, nocc_alph))
        rspmats_1[i, :nocc_alph, nocc_alph:] = y_full_1.T
        rspmats_1[i, nocc_alph:, :nocc_alph] = x_full_1
        rspmats_2[i, :nocc_alph, nocc_alph:] = y_full_2.T
        rspmats_2[i, nocc_alph:, :nocc_alph] = x_full_2

    rhsmats = np.zeros(shape=(ncomp, norb, norb))
    for i in range(ncomp):
        rhsvec = rhsvecs[i, :, 0]
        rhsvec_top = rhsvec[:nov_alph]
        rhsvec_bot = rhsvec[nov_alph:]
        rhsvec_top_mat = utils.repack_vector_to_matrix(rhsvec_top, (nvirt_alph, nocc_alph))
        rhsvec_bot_mat = utils.repack_vector_to_matrix(rhsvec_bot, (nvirt_alph, nocc_alph))
        rhsmats[i, :nocc_alph, nocc_alph:] = rhsvec_top_mat.T
        rhsmats[i, nocc_alph:, :nocc_alph] = rhsvec_bot_mat

    polarizability_full_1 = np.empty_like(polarizability_1)
    polarizability_full_2 = np.empty_like(polarizability_2)
    for a in (0, 1, 2):
        for b in (0, 1, 2):
            polarizability_full_1[a, b] = 2 * np.trace(np.dot(rhsmats[a].T, rspmats_1[b]))
            polarizability_full_2[a, b] = 2 * np.trace(np.dot(rhsmats[a].T, rspmats_2[b]))

    np.testing.assert_almost_equal(polarizability_1, -polarizability_full_1)
    np.testing.assert_almost_equal(polarizability_2, -polarizability_full_2)

    # V_{p,q} <- full MO transformation of right hand side
    integrals_ao = operator.ao_integrals
    integrals_mo = np.empty_like(integrals_ao)
    for i in range(ncomp):
        integrals_mo[i] = (C[0].T).dot(integrals_ao[i]).dot(C[0])

    G_1 = np.empty_like(rspmats_1)
    G_2 = np.empty_like(rspmats_2)
    C = mf.mo_coeff
    # TODO I feel as though if I have all the MO-basis two-electron
    # integrals, I shouldn't need another JK build.
    for i in range(ncomp):
        V = integrals_mo[i]
        Dl_1 = (C[:, :nocc_alph]).dot(rspmats_1[i, :nocc_alph, :]).dot(C.T)
        Dr_1 = (-C).dot(rspmats_1[i, :, :nocc_alph]).dot(C[:, :nocc_alph].T)
        D_1 = Dl_1 + Dr_1
        Dl_2 = (C[:, :nocc_alph]).dot(rspmats_2[i, :nocc_alph, :]).dot(C.T)
        Dr_2 = (-C).dot(rspmats_2[i, :, :nocc_alph]).dot(C[:, :nocc_alph].T)
        D_2 = Dl_2 + Dr_2
        J_1, K_1 = mf.get_jk(mol, D_1, hermi=0)
        J_2, K_2 = mf.get_jk(mol, D_2, hermi=0)
        F_AO_1 = 2 * J_1 - K_1
        F_AO_2 = 2 * J_2 - K_2
        F_MO_1 = (C.T).dot(F_AO_1).dot(C)
        F_MO_2 = (C.T).dot(F_AO_2).dot(C)
        G_1[i] = V + F_MO_1
        G_2[i] = V + F_MO_2

    E_diag = np.diag(E[0, ...])
    epsilon_1 = G_1.copy()
    epsilon_2 = G_2.copy()
    for i in range(ncomp):
        eoU_1 = (E_diag[..., np.newaxis] + f1) * rspmats_1[i]
        Ue_1 = rspmats_1[i] * E_diag[np.newaxis, ...]
        epsilon_1[i] += eoU_1 - Ue_1
        eoU_2 = (E_diag[..., np.newaxis] + f2) * rspmats_2[i]
        Ue_2 = rspmats_2[i] * E_diag[np.newaxis, ...]
        epsilon_2[i] += eoU_2 - Ue_2

    # Assume some symmetry and calculate only part of the tensor.

    hyperpolarizability = np.zeros(shape=(6, 3))
    off1 = [0, 1, 2, 0, 0, 1]
    off2 = [0, 1, 2, 1, 2, 2]
    for r in range(6):
        b = off1[r]
        c = off2[r]
        for a in range(3):
            tl1 = np.trace(rspmats_2[a].T.dot(G_1[b]).dot(rspmats_1[c])[:nocc_alph, :nocc_alph])
            tl2 = np.trace(rspmats_1[c].dot(G_1[b]).dot(rspmats_2[a].T)[:nocc_alph, :nocc_alph])
            tl3 = np.trace(rspmats_2[a].T.dot(G_1[c]).dot(rspmats_1[b])[:nocc_alph, :nocc_alph])
            tl4 = np.trace(rspmats_1[b].dot(G_1[c]).dot(rspmats_2[a].T)[:nocc_alph, :nocc_alph])
            tl5 = np.trace(rspmats_1[c].dot(-G_2[a].T).dot(rspmats_1[b])[:nocc_alph, :nocc_alph])
            tl6 = np.trace(rspmats_1[b].dot(-G_2[a].T).dot(rspmats_1[c])[:nocc_alph, :nocc_alph])
            tr1 = np.trace(
                rspmats_1[c].dot(rspmats_1[b]).dot(-epsilon_2[a].T)[:nocc_alph, :nocc_alph]
            )
            tr2 = np.trace(
                rspmats_1[b].dot(rspmats_1[c]).dot(-epsilon_2[a].T)[:nocc_alph, :nocc_alph]
            )
            tr3 = np.trace(
                rspmats_1[c].dot(rspmats_2[a].T).dot(epsilon_1[b])[:nocc_alph, :nocc_alph]
            )
            tr4 = np.trace(
                rspmats_2[a].T.dot(rspmats_1[c]).dot(epsilon_1[b])[:nocc_alph, :nocc_alph]
            )
            tr5 = np.trace(
                rspmats_1[b].dot(rspmats_2[a].T).dot(epsilon_1[c])[:nocc_alph, :nocc_alph]
            )
            tr6 = np.trace(
                rspmats_2[a].T.dot(rspmats_1[b]).dot(epsilon_1[c])[:nocc_alph, :nocc_alph]
            )
            tl = tl1 + tl2 + tl3 + tl4 + tl5 + tl6
            tr = tr1 + tr2 + tr3 + tr4 + tr5 + tr6
            hyperpolarizability[r, a] = -2 * (tl - tr)

    # pylint: disable=C0326
    ref = np.array(
        [
            [0.00000000, 0.00000000, 1.92505358],
            [0.00000000, 0.00000000, -31.33652886],
            [0.00000000, 0.00000000, -13.92830863],
            [0.00000000, 0.00000000, 0.00000000],
            [-1.80626084, 0.00000000, 0.00000000],
            [0.00000000, -31.13504192, 0.00000000],
        ]
    )
    ref_avgs = np.array([0.00000000, 0.00000000, 45.69300223])
    ref_avg = 45.69300223
    diff = np.abs(ref - hyperpolarizability)
    print("abs diff")
    print(diff)
    thresh = 2.5e-04
    assert np.all(diff < thresh)

    print("hyperpolarizability: SHG, (-{}; {}, {}), symmetry-unique components".format(f2, f1, f1))
    print(hyperpolarizability)
    print("ref")
    print(ref)

    # Transpose all frequency-doubled quantities (+2w) to get -2w.

    for i in range(ncomp):
        rspmats_2[i] = rspmats_2[i].T
        G_2[i] = -G_2[i].T
        epsilon_2[i] = -epsilon_2[i].T

    # Assume some symmetry and calculate only part of the tensor. This
    # time, work with the in-place manipulated quantities (this tests
    # their correctness).

    mU = (rspmats_2, rspmats_1)
    mG = (G_2, G_1)
    me = (epsilon_2, epsilon_1)

    hyperpolarizability = np.zeros(shape=(6, 3))
    off1 = [0, 1, 2, 0, 0, 1]
    off2 = [0, 1, 2, 1, 2, 2]
    for r in range(6):
        b = off1[r]
        c = off2[r]
        for a in range(3):
            tl1 = np.trace(mU[0][a].dot(mG[1][b]).dot(mU[1][c])[:nocc_alph, :nocc_alph])
            tl2 = np.trace(mU[1][c].dot(mG[1][b]).dot(mU[0][a])[:nocc_alph, :nocc_alph])
            tl3 = np.trace(mU[0][a].dot(mG[1][c]).dot(mU[1][b])[:nocc_alph, :nocc_alph])
            tl4 = np.trace(mU[1][b].dot(mG[1][c]).dot(mU[0][a])[:nocc_alph, :nocc_alph])
            tl5 = np.trace(mU[1][c].dot(mG[0][a]).dot(mU[1][b])[:nocc_alph, :nocc_alph])
            tl6 = np.trace(mU[1][b].dot(mG[0][a]).dot(mU[1][c])[:nocc_alph, :nocc_alph])
            tr1 = np.trace(mU[1][c].dot(mU[1][b]).dot(me[0][a])[:nocc_alph, :nocc_alph])
            tr2 = np.trace(mU[1][b].dot(mU[1][c]).dot(me[0][a])[:nocc_alph, :nocc_alph])
            tr3 = np.trace(mU[1][c].dot(mU[0][a]).dot(me[1][b])[:nocc_alph, :nocc_alph])
            tr4 = np.trace(mU[0][a].dot(mU[1][c]).dot(me[1][b])[:nocc_alph, :nocc_alph])
            tr5 = np.trace(mU[1][b].dot(mU[0][a]).dot(me[1][c])[:nocc_alph, :nocc_alph])
            tr6 = np.trace(mU[0][a].dot(mU[1][b]).dot(me[1][c])[:nocc_alph, :nocc_alph])
            tl = [tl1, tl2, tl3, tl4, tl5, tl6]
            tr = [tr1, tr2, tr3, tr4, tr5, tr6]
            hyperpolarizability[r, a] = -2 * (sum(tl) - sum(tr))

    assert np.all(np.abs(ref - hyperpolarizability) < thresh)

    # Assume no symmetry and calculate the full tensor.

    hyperpolarizability_full = np.zeros(shape=(3, 3, 3))

    # components x, y, z
    for ip, p in enumerate(list(product(range(3), range(3), range(3)))):
        a, b, c = p
        tl, tr = [], []
        # 1st tuple -> index a, b, c (*not* x, y, z!)
        # 2nd tuple -> index frequency (0 -> -2w, 1 -> +w)
        for iq, q in enumerate(list(permutations(zip(p, (0, 1, 1)), 3))):
            d, e, f = q
            tlp = (mU[d[1]][d[0]]).dot(mG[e[1]][e[0]]).dot(mU[f[1]][f[0]])
            tle = np.trace(tlp[:nocc_alph, :nocc_alph])
            tl.append(tle)
            trp = (mU[d[1]][d[0]]).dot(mU[e[1]][e[0]]).dot(me[f[1]][f[0]])
            tre = np.trace(trp[:nocc_alph, :nocc_alph])
            tr.append(tre)
        hyperpolarizability_full[a, b, c] = -2 * (sum(tl) - sum(tr))
    print("hyperpolarizability: SHG, (-{}; {}, {}), full tensor".format(f2, f1, f1))
    print(hyperpolarizability_full)

    # Check that the elements of the reduced and full tensors are
    # equivalent.

    for r in range(6):
        b = off1[r]
        c = off2[r]
        for a in range(3):
            diff = hyperpolarizability[r, a] - hyperpolarizability_full[a, b, c]
            # TODO why not 14?
            assert abs(diff) < 1.0e-13

    # Compute averages and compare to reference.

    avgs, avg = utils.form_first_hyperpolarizability_averages(hyperpolarizability_full)
    assert np.allclose(ref_avgs, avgs, rtol=0, atol=1.0e-3)
    assert np.allclose([ref_avg], [avg], rtol=0, atol=1.0e-3)
    print(avgs)
    print(avg)

    return
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
def test_first_hyperpolarizability_static_rhf_wigner_explicit():
    mol = molecule_water_sto3g_angstrom()
    mol.build()

    mf = pyscf.scf.RHF(mol)
    mf.kernel()
    C = utils.fix_mocoeffs_shape(mf.mo_coeff)
    E = utils.fix_moenergies_shape(mf.mo_energy)
    occupations = occupations_from_pyscf_mol(mol, C)
    nocc_alph, nvirt_alph, _, _ = occupations
    nov_alph = nocc_alph * nvirt_alph
    norb = nocc_alph + nvirt_alph

    # calculate linear response vectors for electric dipole operator
    calculator = electric.Polarizability(
        Program.PySCF,
        mol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
        frequencies=[0.0],
    )
    calculator.form_operators()
    calculator.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    calculator.form_results()

    polarizability = calculator.polarizabilities[0]
    print("polarizability (static)")
    print(polarizability)

    operator = calculator.driver.solver.operators[0]
    rhsvecs = operator.mo_integrals_ai_supervector_alph
    rspvecs = operator.rspvecs_alph[0]

    ## Form the full [norb, norb] representation of everything.
    # Response vectors: transform X_{ia} and Y_{ia} -> U_{p,q}
    # 0. 'a' is fast index, 'i' slow
    # 1. rspvec == [X Y]
    # 2. U_{p, q} -> zero
    # 3. place X_{ia} into U_{i, a}
    # 4. place Y_{ia} into U_{a, i}

    ncomp = rhsvecs.shape[0]

    rspmats = np.zeros(shape=(ncomp, norb, norb))
    for i in range(ncomp):
        rspvec = rspvecs[i, :, 0]
        x = rspvec[:nov_alph]
        y = rspvec[nov_alph:]
        x_full = utils.repack_vector_to_matrix(x, (nvirt_alph, nocc_alph))
        y_full = utils.repack_vector_to_matrix(y, (nvirt_alph, nocc_alph))
        rspmats[i, :nocc_alph, nocc_alph:] = x_full.T
        rspmats[i, nocc_alph:, :nocc_alph] = y_full

    rhsmats = np.zeros(shape=(ncomp, norb, norb))
    for i in range(ncomp):
        rhsvec = rhsvecs[i, :, 0]
        rhsvec_top = rhsvec[:nov_alph]
        rhsvec_bot = rhsvec[nov_alph:]
        rhsvec_top_mat = utils.repack_vector_to_matrix(rhsvec_top,
                                                       (nvirt_alph, nocc_alph))
        rhsvec_bot_mat = utils.repack_vector_to_matrix(rhsvec_bot,
                                                       (nvirt_alph, nocc_alph))
        rhsmats[i, :nocc_alph, nocc_alph:] = rhsvec_top_mat.T
        rhsmats[i, nocc_alph:, :nocc_alph] = rhsvec_bot_mat

    polarizability_full = np.empty_like(polarizability)
    for a in (0, 1, 2):
        for b in (0, 1, 2):
            polarizability_full[a, b] = 2 * np.trace(rhsmats[a, ...].T.dot(
                rspmats[b, ...]))

    np.testing.assert_almost_equal(polarizability, polarizability_full)

    # V_{p,q} <- full MO transformation of right hand side
    integrals_ao = operator.ao_integrals
    integrals_mo = np.empty_like(integrals_ao)
    for i in range(ncomp):
        integrals_mo[i, ...] = (C[0, ...].T).dot(integrals_ao[i,
                                                              ...]).dot(C[0,
                                                                          ...])

    G = np.empty_like(rspmats)
    C = mf.mo_coeff
    # TODO I feel as though if I have all the MO-basis two-electron
    # integrals, I shouldn't need another JK build.
    for i in range(ncomp):
        V = integrals_mo[i, ...]
        Dl = (C[:, nocc_alph:].dot(
            utils.repack_vector_to_matrix(rspvecs[i, :nov_alph, 0],
                                          (nvirt_alph, nocc_alph))).dot(
                                              C[:, :nocc_alph].T))
        J, K = mf.get_jk(mol, Dl, hermi=0)
        F_AO = -(4 * J - K - K.T)
        F_MO = (C.T).dot(F_AO).dot(C)
        G[i, ...] = V + F_MO

    E_diag = np.diag(E[0, ...])
    epsilon = G.copy()
    omega = 0
    for i in range(ncomp):
        eoU = (E_diag[..., np.newaxis] + omega) * rspmats[i, ...]
        Ue = rspmats[i, ...] * E_diag[np.newaxis, ...]
        epsilon[i, ...] += eoU - Ue

    # Assume some symmetry and calculate only part of the tensor.

    hyperpolarizability = np.zeros(shape=(6, 3))
    off1 = [0, 1, 2, 0, 0, 1]
    off2 = [0, 1, 2, 1, 2, 2]
    for r in range(6):
        b = off1[r]
        c = off2[r]
        for a in range(3):
            tl1 = 2 * np.trace(rspmats[a, ...].dot(G[b, ...]).dot(
                rspmats[c, ...])[:nocc_alph, :nocc_alph])
            tl2 = 2 * np.trace(rspmats[a, ...].dot(G[c, ...]).dot(
                rspmats[b, ...])[:nocc_alph, :nocc_alph])
            tl3 = 2 * np.trace(rspmats[c, ...].dot(G[a, ...]).dot(
                rspmats[b, ...])[:nocc_alph, :nocc_alph])
            tr1 = np.trace(rspmats[c, ...].dot(rspmats[b, ...]).dot(
                epsilon[a, ...])[:nocc_alph, :nocc_alph])
            tr2 = np.trace(rspmats[b, ...].dot(rspmats[c, ...]).dot(
                epsilon[a, ...])[:nocc_alph, :nocc_alph])
            tr3 = np.trace(rspmats[c, ...].dot(rspmats[a, ...]).dot(
                epsilon[b, ...])[:nocc_alph, :nocc_alph])
            tr4 = np.trace(rspmats[a, ...].dot(rspmats[c, ...]).dot(
                epsilon[b, ...])[:nocc_alph, :nocc_alph])
            tr5 = np.trace(rspmats[b, ...].dot(rspmats[a, ...]).dot(
                epsilon[c, ...])[:nocc_alph, :nocc_alph])
            tr6 = np.trace(rspmats[a, ...].dot(rspmats[b, ...]).dot(
                epsilon[c, ...])[:nocc_alph, :nocc_alph])
            tl = tl1 + tl2 + tl3
            tr = tr1 + tr2 + tr3 + tr4 + tr5 + tr6
            hyperpolarizability[r, a] = 2 * (tl - tr)

    # pylint: disable=C0326
    ref = np.array([
        [-8.86822254, 0.90192130, -0.50796586],
        [1.98744058, 5.13635628, -2.95319400],
        [0.66008119, 1.62699646, -0.85632412],
        [0.90192130, 1.98744058, -1.09505123],
        [-0.50796586, -1.09505123, 0.66008119],
        [-1.09505123, -2.95319400, 1.62699646],
    ])
    ref_avgs = np.array([6.22070078, -7.66527404, 4.31748398])
    ref_avg = 10.77470242

    thresh = 1.5e-4
    assert np.all(np.abs(ref - hyperpolarizability) < thresh)

    print("hyperpolarizability (static), symmetry-unique components")
    print(hyperpolarizability)

    # Assume no symmetry and calculate the full tensor.

    hyperpolarizability_full = np.zeros(shape=(3, 3, 3))
    for p in product(range(3), range(3), range(3)):
        a, b, c = p
        tl, tr = 0, 0
        for q in permutations(p, 3):
            d, e, f = q
            tl += np.trace(rspmats[d, ...].dot(G[e, ...]).dot(
                rspmats[f, ...])[:nocc_alph, :nocc_alph])
            tr += np.trace(rspmats[d, ...].dot(rspmats[e, ...]).dot(
                epsilon[f, ...])[:nocc_alph, :nocc_alph])
        hyperpolarizability_full[a, b, c] = 2 * (tl - tr)
    print("hyperpolarizability (static), full tensor")
    print(hyperpolarizability_full)

    # Check that the elements of the reduced and full tensors are
    # equivalent.

    thresh = 1.0e-14
    for r in range(6):
        b = off1[r]
        c = off2[r]
        for a in range(3):
            diff = hyperpolarizability[r, a] - hyperpolarizability_full[a, b,
                                                                        c]
            assert abs(diff) < thresh

    # Compute averages and compare to reference.
    # This is the slow way.
    # avgs = []
    # for i in range(3):
    #     avg_c = 0
    #     for j in range(3):
    #         avg_c += hyperpolarizability_full[i, j, j] + hyperpolarizability_full[j, i, j] + hyperpolarizability_full[j, j, i]
    #     avgs.append((-1/3) * avg_c)
    # print(np.asarray(avgs))
    x = hyperpolarizability_full
    # This is the simplest non-einsum way.
    # avgs = (-1 / 3) * np.asarray([np.trace(x[i, :, :] + x[:, i, :] + x[:, :, i]) for i in range(3)])
    # This is the best way.
    avgs = (-1 / 3) * (np.einsum("ijj->i", x) + np.einsum("jij->i", x) +
                       np.einsum("jji->i", x))
    # print(list(set([''.join(p) for p in list(permutations('ijj', 3))])))
    assert np.allclose(ref_avgs, avgs, rtol=0, atol=1.0e-3)
    avg = np.sum(avgs**2)**(1 / 2)
    assert np.allclose([ref_avg], [avg], rtol=0, atol=1.0e-3)
    print(avgs)
    print(avg)

    utils_avgs, utils_avg = utils.form_first_hyperpolarizability_averages(x)
    assert np.allclose(avgs, utils_avgs, rtol=0, atol=1.0e-13)
    assert np.allclose([avg], [utils_avg], rtol=0, atol=1.0e-13)

    return
Ejemplo n.º 15
0
def test_first_hyperpolarizability_or_rhf_wigner_explicit():
    mol = molecule_water_sto3g_angstrom()
    mol.build()

    mf = pyscf.scf.RHF(mol)
    mf.kernel()
    C = utils.fix_mocoeffs_shape(mf.mo_coeff)
    E = utils.fix_moenergies_shape(mf.mo_energy)
    occupations = occupations_from_pyscf_mol(mol, C)
    nocc_alph, nvirt_alph, _, _ = occupations
    nov_alph = nocc_alph * nvirt_alph
    norb = nocc_alph + nvirt_alph

    # calculate linear response vectors for electric dipole operator
    f1 = 0.0
    f2 = 0.0773178
    frequencies = [f1, f2]
    calculator = electric.Polarizability(
        Program.PySCF,
        mol,
        cphf.CPHF(solvers.ExactInv(C, E, occupations)),
        C,
        E,
        occupations,
        frequencies=frequencies,
    )
    calculator.form_operators()
    calculator.run(hamiltonian=Hamiltonian.RPA, spin=Spin.singlet)
    calculator.form_results()

    polarizability_1 = calculator.polarizabilities[0]
    polarizability_2 = calculator.polarizabilities[1]
    print("polarizability (static)")
    print(polarizability_1)
    print("polarizability: {} a.u.".format(f2))
    print(polarizability_2)

    # each operator contains multiple sets of response vectors, one
    # set of components for each frequency
    assert isinstance(calculator.driver.solver.operators, list)
    assert len(calculator.driver.solver.operators) == 1
    operator = calculator.driver.solver.operators[0]
    rhsvecs = operator.mo_integrals_ai_supervector_alph
    assert isinstance(operator.rspvecs_alph, list)
    assert len(operator.rspvecs_alph) == 2
    rspvecs_1 = operator.rspvecs_alph[0]
    rspvecs_2 = operator.rspvecs_alph[1]

    ## Form the full [norb, norb] representation of everything.
    # Response vectors: transform X_{ia} and Y_{ia} -> U_{p,q}
    # 0. 'a' is fast index, 'i' slow
    # 1. rspvec == [X Y]
    # 2. U_{p, q} -> zero
    # 3. place X_{ia} into U_{i, a}
    # 4. place Y_{ia} into U_{a, i}

    ncomp = rhsvecs.shape[0]

    rspmats_1 = np.zeros(shape=(ncomp, norb, norb))
    rspmats_2 = np.zeros(shape=(ncomp, norb, norb))
    for i in range(ncomp):
        rspvec_1 = rspvecs_1[i, :, 0]
        rspvec_2 = rspvecs_2[i, :, 0]
        x_1 = rspvec_1[:nov_alph]
        y_1 = rspvec_1[nov_alph:]
        x_2 = rspvec_2[:nov_alph]
        y_2 = rspvec_2[nov_alph:]
        x_full_1 = utils.repack_vector_to_matrix(x_1, (nvirt_alph, nocc_alph))
        y_full_1 = utils.repack_vector_to_matrix(y_1, (nvirt_alph, nocc_alph))
        x_full_2 = utils.repack_vector_to_matrix(x_2, (nvirt_alph, nocc_alph))
        y_full_2 = utils.repack_vector_to_matrix(y_2, (nvirt_alph, nocc_alph))
        rspmats_1[i, :nocc_alph, nocc_alph:] = y_full_1.T
        rspmats_1[i, nocc_alph:, :nocc_alph] = x_full_1
        rspmats_2[i, :nocc_alph, nocc_alph:] = y_full_2.T
        rspmats_2[i, nocc_alph:, :nocc_alph] = x_full_2

    rhsmats = np.zeros(shape=(ncomp, norb, norb))
    for i in range(ncomp):
        rhsvec = rhsvecs[i, :, 0]
        rhsvec_top = rhsvec[:nov_alph]
        rhsvec_bot = rhsvec[nov_alph:]
        rhsvec_top_mat = utils.repack_vector_to_matrix(rhsvec_top,
                                                       (nvirt_alph, nocc_alph))
        rhsvec_bot_mat = utils.repack_vector_to_matrix(rhsvec_bot,
                                                       (nvirt_alph, nocc_alph))
        rhsmats[i, :nocc_alph, nocc_alph:] = rhsvec_top_mat.T
        rhsmats[i, nocc_alph:, :nocc_alph] = rhsvec_bot_mat

    polarizability_full_1 = np.empty_like(polarizability_1)
    polarizability_full_2 = np.empty_like(polarizability_2)
    for a in (0, 1, 2):
        for b in (0, 1, 2):
            polarizability_full_1[a, b] = 2 * np.trace(rhsmats[a, ...].T.dot(
                rspmats_1[b, ...]))
            polarizability_full_2[a, b] = 2 * np.trace(rhsmats[a, ...].T.dot(
                rspmats_2[b, ...]))

    np.testing.assert_almost_equal(polarizability_1, -polarizability_full_1)
    np.testing.assert_almost_equal(polarizability_2, -polarizability_full_2)

    # V_{p,q} <- full MO transformation of right hand side
    integrals_ao = operator.ao_integrals
    integrals_mo = np.empty_like(integrals_ao)
    for i in range(ncomp):
        integrals_mo[i, ...] = (C[0, ...].T).dot(integrals_ao[i,
                                                              ...]).dot(C[0,
                                                                          ...])

    # from pyresponse.ao2mo import AO2MOpyscf
    # ao2mo = AO2MOpyscf(C, pyscfmol=mol)
    # ao2mo.perform_rhf_full()
    # tei_mo = ao2mo.tei_mo[0]

    G_1 = np.empty_like(rspmats_1)
    G_2 = np.empty_like(rspmats_2)
    C = mf.mo_coeff
    # TODO I feel as though if I have all the MO-basis two-electron
    # integrals, I shouldn't need another JK build.
    for i in range(ncomp):
        V = integrals_mo[i, ...]
        Dl_1 = (C[:, :nocc_alph]).dot(rspmats_1[i, :nocc_alph, :]).dot(C.T)
        Dr_1 = (-C).dot(rspmats_1[i, :, :nocc_alph]).dot(C[:, :nocc_alph].T)
        D_1 = Dl_1 + Dr_1
        Dl_2 = (C[:, :nocc_alph]).dot(rspmats_2[i, :nocc_alph, :]).dot(C.T)
        Dr_2 = (-C).dot(rspmats_2[i, :, :nocc_alph]).dot(C[:, :nocc_alph].T)
        D_2 = Dl_2 + Dr_2
        J_1, K_1 = mf.get_jk(mol, D_1, hermi=0)
        J_2, K_2 = mf.get_jk(mol, D_2, hermi=0)
        F_AO_1 = 2 * J_1 - K_1
        F_AO_2 = 2 * J_2 - K_2
        F_MO_1 = (C.T).dot(F_AO_1).dot(C)
        F_MO_2 = (C.T).dot(F_AO_2).dot(C)
        G_1[i, ...] = V + F_MO_1
        G_2[i, ...] = V + F_MO_2

    E_diag = np.diag(E[0, ...])
    epsilon_1 = G_1.copy()
    epsilon_2 = G_2.copy()
    for i in range(ncomp):
        eoU_1 = (E_diag[..., np.newaxis] + f1) * rspmats_1[i, ...]
        Ue_1 = rspmats_1[i, ...] * E_diag[np.newaxis, ...]
        epsilon_1[i, ...] += eoU_1 - Ue_1
        eoU_2 = (E_diag[..., np.newaxis] + f2) * rspmats_2[i, ...]
        Ue_2 = rspmats_2[i, ...] * E_diag[np.newaxis, ...]
        epsilon_2[i, ...] += eoU_2 - Ue_2

    # Assume some symmetry and calculate only part of the tensor.

    hyperpolarizability = np.zeros(shape=(6, 3))
    off1 = [0, 1, 2, 0, 0, 1]
    off2 = [0, 1, 2, 1, 2, 2]
    for r in range(6):
        b = off1[r]
        c = off2[r]
        for a in range(3):
            # _1 -> 0
            # _2 -> +w
            # a is _1, b is _2, c is _2 transposed/negated
            tl1 = np.trace(rspmats_1[a, ...].dot(G_2[b, ...]).dot(
                rspmats_2[c, ...].T)[:nocc_alph, :nocc_alph])
            tl2 = np.trace(rspmats_2[c, ...].T.dot(G_2[b, ...]).dot(
                rspmats_1[a, ...])[:nocc_alph, :nocc_alph])
            tl3 = np.trace(rspmats_1[a, ...].dot(-G_2[c, ...].T).dot(
                rspmats_2[b, ...])[:nocc_alph, :nocc_alph])
            tl4 = np.trace(rspmats_2[b, ...].dot(-G_2[c, ...].T).dot(
                rspmats_1[a, ...])[:nocc_alph, :nocc_alph])
            tl5 = np.trace(rspmats_2[c, ...].T.dot(G_1[a, ...]).dot(
                rspmats_2[b, ...])[:nocc_alph, :nocc_alph])
            tl6 = np.trace(rspmats_2[b, ...].dot(G_1[a, ...]).dot(
                rspmats_2[c, ...].T)[:nocc_alph, :nocc_alph])
            tr1 = np.trace(rspmats_2[c, ...].T.dot(rspmats_2[b, ...]).dot(
                epsilon_1[a, ...])[:nocc_alph, :nocc_alph])
            tr2 = np.trace(rspmats_2[b, ...].dot(rspmats_2[c, ...].T).dot(
                epsilon_1[a, ...])[:nocc_alph, :nocc_alph])
            tr3 = np.trace(rspmats_2[c, ...].T.dot(rspmats_1[a, ...]).dot(
                epsilon_2[b, ...])[:nocc_alph, :nocc_alph])
            tr4 = np.trace(rspmats_1[a, ...].dot(rspmats_2[c, ...].T).dot(
                epsilon_2[b, ...])[:nocc_alph, :nocc_alph])
            tr5 = np.trace(rspmats_2[b, ...].dot(
                rspmats_1[a,
                          ...]).dot(-epsilon_2[c,
                                               ...].T)[:nocc_alph, :nocc_alph])
            tr6 = np.trace(rspmats_1[a, ...].dot(
                rspmats_2[b,
                          ...]).dot(-epsilon_2[c,
                                               ...].T)[:nocc_alph, :nocc_alph])
            tl = tl1 + tl2 + tl3 + tl4 + tl5 + tl6
            tr = tr1 + tr2 + tr3 + tr4 + tr5 + tr6
            hyperpolarizability[r, a] = -2 * (tl - tr)

    # pylint: disable=C0326
    ref = np.array([
        [-9.02854579, 0.92998934, -0.52377445],
        [2.01080066, 5.23470702, -3.01208409],
        [0.66669794, 1.66112712, -0.87205853],
        [0.92021130, 2.01769267, -1.11067223],
        [-0.51824440, -1.11067586, 0.67140102],
        [-1.10887175, -3.00950655, 1.65659586],
    ])
    ref_avgs = np.array([6.34331713, -7.81628395, 4.40251201])
    ref_avg = 10.98699590

    thresh = 4.0e-5
    assert np.all(np.abs(ref - hyperpolarizability) < thresh)

    print("hyperpolarizability: OR, (0; {}, -{}), symmetry-unique components".
          format(f2, f2))
    print(hyperpolarizability)

    return
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
def calculate_disk_uhf(testcasedir, hamiltonian, spin, frequency, label_1,
                       label_2):

    occupations = utils.read_file_occupations(testcasedir / "occupations")
    nocc_alph, nvirt_alph, nocc_beta, nvirt_beta = occupations
    norb = nocc_alph + nvirt_alph
    C = utils.read_file_3(testcasedir / "C")
    assert C.shape[0] == 2
    assert C.shape[2] == norb
    nbasis = C.shape[1]
    moene = utils.read_file_2(testcasedir / "moene")
    assert moene.shape == (norb, 2)
    moints_iajb_aaaa = utils.read_file_4(testcasedir / "moints_iajb_aaaa")
    moints_iajb_aabb = utils.read_file_4(testcasedir / "moints_iajb_aabb")
    moints_iajb_bbaa = utils.read_file_4(testcasedir / "moints_iajb_bbaa")
    moints_iajb_bbbb = utils.read_file_4(testcasedir / "moints_iajb_bbbb")
    moints_ijab_aaaa = utils.read_file_4(testcasedir / "moints_ijab_aaaa")
    moints_ijab_bbbb = utils.read_file_4(testcasedir / "moints_ijab_bbbb")
    assert moints_iajb_aaaa.shape == (nocc_alph, nvirt_alph, nocc_alph,
                                      nvirt_alph)
    assert moints_iajb_aabb.shape == (nocc_alph, nvirt_alph, nocc_beta,
                                      nvirt_beta)
    assert moints_iajb_bbaa.shape == (nocc_beta, nvirt_beta, nocc_alph,
                                      nvirt_alph)
    assert moints_iajb_bbbb.shape == (nocc_beta, nvirt_beta, nocc_beta,
                                      nvirt_beta)
    assert moints_ijab_aaaa.shape == (nocc_alph, nocc_alph, nvirt_alph,
                                      nvirt_alph)
    assert moints_ijab_bbbb.shape == (nocc_beta, nocc_beta, nvirt_beta,
                                      nvirt_beta)

    operator_1 = utils.dalton_label_to_operator(label_1)
    operator_2 = utils.dalton_label_to_operator(label_2)

    operator_1_integrals_mn = utils.read_file_3(
        testcasedir / f"operator_mn_{operator_1.label}")
    operator_2_integrals_mn = utils.read_file_3(
        testcasedir / f"operator_mn_{operator_2.label}")
    # The first dimension can"t be checked since there may be multiple
    # components.
    assert operator_1_integrals_mn.shape[1:] == (nbasis, nbasis)
    assert operator_2_integrals_mn.shape[1:] == (nbasis, nbasis)

    # Only take the component/slice from the integral as determined
    # from the DALTON operator label.
    operator_1_integrals_mn = operator_1_integrals_mn[operator_1.slice_idx]
    operator_2_integrals_mn = operator_2_integrals_mn[operator_2.slice_idx]
    # However, this eliminates an axis, which needs to be added back.
    operator_1_integrals_mn = operator_1_integrals_mn[np.newaxis, ...]
    operator_2_integrals_mn = operator_2_integrals_mn[np.newaxis, ...]

    operator_1.ao_integrals = operator_1_integrals_mn
    operator_2.ao_integrals = operator_2_integrals_mn

    moene_alph = np.diag(moene[:, 0])
    moene_beta = np.diag(moene[:, 1])
    moene = np.stack((moene_alph, moene_beta), axis=0)
    assert moene.shape == (2, norb, norb)

    solver = iterators.ExactInv(C, moene, occupations)
    solver.tei_mo = (
        moints_iajb_aaaa,
        moints_iajb_aabb,
        moints_iajb_bbaa,
        moints_iajb_bbbb,
        moints_ijab_aaaa,
        moints_ijab_bbbb,
    )
    solver.tei_mo_type = "partial"

    driver = cphf.CPHF(solver)
    driver.add_operator(operator_1)
    driver.add_operator(operator_2)

    driver.set_frequencies([float(frequency)])

    driver.run(solver_type="exact", hamiltonian=hamiltonian, spin=spin)

    assert len(driver.frequencies) == len(driver.results) == 1
    res = driver.results[0]
    assert res.shape == (2, 2)
    bl = res[1, 0]
    tr = res[0, 1]
    diff = abs(abs(bl) - abs(tr))
    # Results should be symmetric w.r.t. interchange between operators
    # in the LR equations.
    thresh = 1.0e-14
    assert diff < thresh

    return bl
Ejemplo n.º 19
0
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]
Ejemplo n.º 20
0
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)
Ejemplo n.º 21
0
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
Ejemplo n.º 22
0
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
Ejemplo n.º 23
0
def calculate_disk_rhf(
    testcasedir: Path,
    hamiltonian: str,
    spin: str,
    frequency: str,
    label_1: str,
    label_2: str,
) -> float:

    occupations = utils.read_file_occupations(testcasedir / "occupations")
    nocc_alph, nvirt_alph, nocc_beta, nvirt_beta = occupations
    assert nocc_alph == nocc_beta
    assert nvirt_alph == nvirt_beta
    norb = nocc_alph + nvirt_alph
    C = utils.read_file_3(testcasedir / "C")
    assert C.shape[0] == 1
    assert C.shape[2] == norb
    nbasis = C.shape[1]
    moene = utils.read_file_2(testcasedir / "moene")
    assert moene.shape == (norb, 1)
    moints_iajb_aaaa = utils.read_file_4(testcasedir / "moints_iajb_aaaa")
    moints_ijab_aaaa = utils.read_file_4(testcasedir / "moints_ijab_aaaa")
    assert moints_iajb_aaaa.shape == (nocc_alph, nvirt_alph, nocc_alph,
                                      nvirt_alph)
    assert moints_ijab_aaaa.shape == (nocc_alph, nocc_alph, nvirt_alph,
                                      nvirt_alph)

    operator_1 = dalton_label_to_operator(label_1)
    operator_2 = dalton_label_to_operator(label_2)

    operator_1_integrals_mn = utils.read_file_3(
        testcasedir / f"operator_mn_{operator_1.label}")
    operator_2_integrals_mn = utils.read_file_3(
        testcasedir / f"operator_mn_{operator_2.label}")
    # The first dimension can"t be checked since there may be multiple
    # components.
    assert operator_1_integrals_mn.shape[1:] == (nbasis, nbasis)
    assert operator_2_integrals_mn.shape[1:] == (nbasis, nbasis)

    # Only take the component/slice from the integral as determined
    # from the DALTON operator label.
    operator_1_integrals_mn = operator_1_integrals_mn[operator_1.slice_idx]
    operator_2_integrals_mn = operator_2_integrals_mn[operator_2.slice_idx]
    # However, this eliminates an axis, which needs to be added back.
    operator_1_integrals_mn = operator_1_integrals_mn[np.newaxis, ...]
    operator_2_integrals_mn = operator_2_integrals_mn[np.newaxis, ...]

    operator_1.ao_integrals = operator_1_integrals_mn
    operator_2.ao_integrals = operator_2_integrals_mn

    moene = np.diag(moene[:, 0])[np.newaxis, ...]
    assert moene.shape == (1, norb, norb)

    solver = solvers.ExactInv(C, moene, occupations)
    solver.tei_mo = (moints_iajb_aaaa, moints_ijab_aaaa)
    solver.tei_mo_type = AO2MOTransformationType.partial

    driver = cphf.CPHF(solver)
    driver.add_operator(operator_1)
    driver.add_operator(operator_2)

    driver.set_frequencies([float(frequency)])

    driver.run(
        hamiltonian=Hamiltonian[hamiltonian.upper()],
        spin=Spin[spin],
        program=None,
        program_obj=None,
    )

    assert len(driver.frequencies) == len(driver.results) == 1
    res = driver.results[0]
    assert res.shape == (2, 2)
    bl = res[1, 0]
    tr = res[0, 1]
    diff = abs(abs(bl) - abs(tr))
    # Results should be symmetric w.r.t. interchange between operators
    # in the LR equations.
    thresh = 1.0e-13
    assert diff < thresh

    return bl