Exemple #1
0
def test_mol_cache():
    # test if cache is stored correctly
    cache_fname = "_temp_cache.h5"
    # remove the cache if exists
    if os.path.exists(cache_fname):
        os.remove(cache_fname)

    moldesc = "H 0 0 0"
    mol = Mol(moldesc, basis="3-21G").set_cache(cache_fname)
    h = mol.get_hamiltonian()
    h.build()

    # read the stored file
    with h5py.File(cache_fname, "r") as f:
        olp_cache = torch.as_tensor(f["hamilton/overlap"])

    olp = h.get_overlap().fullmatrix()
    assert torch.allclose(olp, olp_cache)

    # try again with different atom, if cache is set, then it should be same
    # as previous (although it is a wrong result)
    # TODO: raise a warning if mol properties do not match
    moldesc1 = "Li 0 0 0"
    mol1 = Mol(moldesc1, basis="3-21G").set_cache(cache_fname)
    h1 = mol1.get_hamiltonian()
    h1.build()

    # remove the cache
    if os.path.exists(cache_fname):
        os.remove(cache_fname)

    olp1 = h1.get_overlap().fullmatrix()
    assert torch.allclose(olp, olp1)
Exemple #2
0
def test_mol_cache():
    # test if cache is stored correctly
    cache_fname = "_temp_cache.h5"
    # remove the cache if exists
    if os.path.exists(cache_fname):
        os.remove(cache_fname)

    moldesc = "H 0 0 0; H 1 0 0"
    mol = Mol(moldesc, basis="3-21G").set_cache(cache_fname)
    h = mol.get_hamiltonian()
    h.build()
    olp1 = h.get_overlap().fullmatrix()

    # read the stored file
    with h5py.File(cache_fname, "r") as f:
        olp_cache = torch.as_tensor(f["hamilton/overlap"])

    # test with a new exact same system, for sanity check
    mol_copy = Mol(moldesc, basis="3-21G").set_cache(cache_fname)
    h_copy = mol_copy.get_hamiltonian()
    h_copy.build()
    olp1_copy = h_copy.get_overlap().fullmatrix()
    assert torch.allclose(olp1, olp1_copy)

    # store a different cache into the same file, to make sure the next
    # mol loads from it
    with h5py.File(cache_fname, "w") as f:
        olp_cache2 = 2 * olp_cache
        f["hamilton/overlap"] = olp_cache2

    # the same exact system, but the cache has been altered
    mol = Mol(moldesc, basis="3-21G").set_cache(cache_fname)
    h = mol.get_hamiltonian()
    h.build()
    olp2 = h.get_overlap().fullmatrix()
    assert not torch.allclose(olp1, olp2)

    # Try again with different positions, if cache is set, then it should be same
    # as previous (although it is a wrong result)
    # It must raise a warning for different system
    with pytest.warns(UserWarning, match=r"Mismatch [ \w]*cached signature[ \w]*"):
        moldesc1 = "H 1 0 0; H 0.5 0 0"
        mol1 = Mol(moldesc1, basis="3-21G").set_cache(cache_fname, ["hamilton.overlap"])
        h1 = mol1.get_hamiltonian()
        h1.build()

    # remove the cache
    if os.path.exists(cache_fname):
        os.remove(cache_fname)
Exemple #3
0
def system1(request):
    poss = torch.tensor([[0.0, 0.0, 0.8], [0.0, 0.0, -0.8]], dtype=dtype)
    moldesc = ([1, 1], poss)
    # untruncated grid is required to pass the hamiltonian tests
    m = Mol(moldesc, basis="3-21G", dtype=dtype, grid=3,
            orthogonalize_basis=request.param["basis_ortho"])
    m.setup_grid()
    hamilton = m.get_hamiltonian()
    hamilton.build()
    hamilton.setup_grid(m.get_grid())
    return m
Exemple #4
0
def dqcelrepxc(atom: str, spin: int = 0, xc: str = "lda_x", basis: str = BASIS):
    # returns the electron repulsion and xc operator using DQC
    mol = Mol(atom, spin=spin, basis=basis, dtype=DTYPE, grid=4)
    qc = KS(mol, xc=xc)
    hamilt = mol.get_hamiltonian()
    if spin == 0:
        # set dm to be an identity matrix
        dm = torch.eye(hamilt.nao, dtype=DTYPE)
        velrepxc = hamilt.get_vxc(dm) + hamilt.get_elrep(dm)
        return velrepxc.fullmatrix()
    else:
        dmu = torch.eye(hamilt.nao, dtype=DTYPE)
        dm = SpinParam(u=dmu, d=dmu)
        vxc = hamilt.get_vxc(dm)
        elrep = hamilt.get_elrep(dm.u + dm.d)
        return torch.cat(((vxc.u + elrep).fullmatrix().unsqueeze(0),
                          (vxc.d + elrep).fullmatrix().unsqueeze(0)), dim=0)