Beispiel #1
0
def test_save_load_Sigma_iw():
    from dcore.tools import make_block_gf, save_Sigma_iw_sh_txt, load_Sigma_iw_sh_txt
    from dcore.tools import make_block_gf, save_giw, load_giw
    from triqs.gf import GfImFreq

    nsh = 2
    norb = 2
    beta = 10.0
    n_points = 10

    for spin_names in [['ud'], ['up', 'down']]:
        gf_struct = {sp: numpy.arange(norb) for sp in spin_names}

        Sigma_iw_sh = [
            make_block_gf(GfImFreq, gf_struct, beta, n_points)
            for ish in range(nsh)
        ]

        for ish in range(nsh):
            for sp in spin_names:
                Sigma_iw_sh[ish][sp].data[:, :, :] = numpy.random.randn(
                    2 * n_points, norb,
                    norb) + 1J * numpy.random.randn(2 * n_points, norb, norb)

        save_Sigma_iw_sh_txt('Sigma_iw_sh.txt', Sigma_iw_sh, spin_names)

        Sigma_iw_sh_loaded = [s.copy() for s in Sigma_iw_sh]
        for ish in range(nsh):
            Sigma_iw_sh_loaded[ish].zero()

        load_Sigma_iw_sh_txt('Sigma_iw_sh.txt', Sigma_iw_sh_loaded, spin_names)

        mesh_points = lambda mesh: numpy.array([complex(x) for x in mesh])

        for ish in range(nsh):
            for sp in spin_names:
                numpy.allclose(mesh_points(Sigma_iw_sh[ish][sp].mesh),
                               mesh_points(Sigma_iw_sh_loaded[ish][sp].mesh))
                numpy.allclose(Sigma_iw_sh[ish][sp].data,
                               Sigma_iw_sh_loaded[ish][sp].data)

        # HDF5
        Sigma_iw_sh0 = Sigma_iw_sh[0][spin_names[0]]
        with h5py.File('sigma.h5', 'w') as ar:
            save_giw(ar, '/sigma_iw', Sigma_iw_sh0)

        Sigma_iw_sh0_loaded = Sigma_iw_sh0.copy()
        with h5py.File('sigma.h5', 'r') as ar:
            load_giw(ar, '/sigma_iw', Sigma_iw_sh0_loaded)

        assert numpy.allclose(Sigma_iw_sh0.data, Sigma_iw_sh0_loaded.data)

        # Interpolation
        Sigma_iw_sh_twice_beta = [
            make_block_gf(GfImFreq, gf_struct, 2 * beta, 2 * n_points)
            for ish in range(nsh)
        ]
        load_Sigma_iw_sh_txt('Sigma_iw_sh.txt', Sigma_iw_sh_twice_beta,
                             spin_names)
Beispiel #2
0
def convert_to_triqs_bname(G, gf_struct, beta, n_iw):
    gf_struct_dict = {b[0]: b[1] for b in gf_struct}
    G_copy = make_block_gf(GfImFreq, gf_struct_dict, beta, n_iw)
    bname_trans = {'up': 'up', 'down': 'dn', 'ud': 'bl'}
    for bname, g in G:
        G_copy[bname_trans[bname]] = g
    return G_copy
Beispiel #3
0
def test_sumkdft_opt(request):
    org_dir = os.getcwd()
    os.chdir(request.fspath.dirname)

    kwargs = {
        'hdf_file': 'nis.h5',  # copied from test/pre_wannier
        'use_dft_blocks': False,
        'h_field': 0.0,
    }

    print("Initialize original SumkDFT")
    sk_org = SumkDFT(**kwargs)
    print("Initialize optimized SumkDFT")
    sk_opt = SumkDFT_opt(**kwargs)
    print("Initialization done")

    n_corr_shells = sk_org.n_corr_shells
    print("n_corr_shells = {}".format(n_corr_shells))
    gf_struct = sk_org.gf_struct_solver
    print("gf_struct = {}".format(gf_struct))

    # set up Sigma_imp
    beta = 10
    n_iw = 100
    Sigma_imp = [
        make_block_gf(GfImFreq, gf_struct[icrsh], beta, n_iw)
        for icrsh in range(n_corr_shells)
    ]
    numpy.random.seed(0)  # fix the seed of random number
    for icrsh in range(n_corr_shells):
        for sp, sigma in Sigma_imp[icrsh]:
            # Sigma is not hermitian but it doesn't matter
            sigma.data[:, :, :] = numpy.random.randn(*(sigma.data.shape)) \
                                + numpy.random.randn(*(sigma.data.shape)) * 1.0j

    # compute G_loc
    print("Compute G_loc")
    g_loc = []
    for sk, label in zip([sk_org, sk_opt], ['original ', 'optimized']):
        start = time.time()
        sk.set_Sigma(Sigma_imp)
        # sk.set_dc(dc_imp, dc_energ)
        # sk.set_mu(mu)
        g_loc.append(sk.extract_G_loc(with_Sigma=True, with_dc=False))
        print("{} : {} sec".format(label, time.time() - start))
    g_org = g_loc[0]
    g_opt = g_loc[1]

    # compare
    for icrsh in range(n_corr_shells):
        for sp in list(gf_struct[icrsh].keys()):
            assert numpy.allclose(g_org[icrsh][sp].data, g_opt[icrsh][sp].data)

    os.chdir(org_dir)
Beispiel #4
0
def convert_to_dcore_format(gf_struct, h_int, G0_iw, beta, n_iw):
    """
     Convert input data to DCore format:
         * gf_struct is a dict.
         * Allowed block names are "ud", "up", "down".
     """
    # --------- Convert gf_struct ----------
    num_blocks = len(gf_struct)
    for b in gf_struct:
        assert b[0] in ['up', 'dn', 'bl']
    bname_tr = {'up': 'up', 'dn': 'down', 'bl': 'ud'}
    gf_struct_dcore = {bname_tr[b[0]]: b[1] for b in gf_struct}
    gf_struct_dcore_list = [(bname_tr[b[0]], b[1]) for b in gf_struct]
    norb = len(gf_struct[0][1]) // 2 if num_blocks == 1 else len(
        gf_struct[0][1])

    if num_blocks == 1:
        idx_tr = {('bl', i): i for i in range(2 * norb)}
        G0_iw_dcore = make_block_gf(GfImFreq, gf_struct_dcore, beta, n_iw)
        G0_iw_dcore['ud'] = G0_iw['bl']
    else:
        idx_tr = {(b, i): i + ispin * norb
                  for ispin, b in enumerate(['up', 'dn']) for i in range(norb)}
        G0_iw_dcore = make_block_gf(GfImFreq, gf_struct_dcore, beta, n_iw)
        G0_iw_dcore['up'] = G0_iw['up']
        G0_iw_dcore['down'] = G0_iw['dn']

    # --------- Construct Coulomb tensor from h_int ----------
    U_dict = extract_U_dict4(h_int)
    u_mat = numpy.zeros((2 * norb, ) * 4, dtype=complex)
    for idx4, v in list(U_dict.items()):
        #print("U_dict: ", idx4, v)
        idx4_ = [idx_tr[idx] for idx in idx4]
        u_mat[idx4_[0], idx4_[1], idx4_[2], idx4_[3]] += v
        #print(idx4, idx4_, u_mat[idx4_[0], idx4_[1], idx4_[2], idx4_[3]])

    return norb, gf_struct_dcore, u_mat, G0_iw_dcore
Beispiel #5
0
def test_symmetrization_Sigma_iw():
    """
    Consider two-orbital system and symmetrize Sigma over orbitals
    """
    from dcore.tools import make_block_gf, symmetrize, _to_numpy_array
    from triqs.gf import GfImFreq

    norb = 2
    beta = 10.0
    n_points = 10

    numpy.random.seed(1)

    for spin_names in [['ud'], ['up', 'down']]:
        # Create generator for symmetrizing two orbitals
        generator_mat = numpy.zeros((2, norb, 2, norb), dtype=complex)
        for isp in range(2):
            generator_mat[isp, :, isp, :] = numpy.array([[0, 1], [1, 0]])
        #generator_mat = generator_mat.reshape((2*norb, 2*norb))

        # Create Sigma with random data
        if len(spin_names) == 1:
            nblock_size = 2 * norb
            gen = {'ud': generator_mat.reshape(2 * norb, 2 * norb)}
        else:
            nblock_size = norb
            gen = {
                'up': generator_mat[0, :, 0, :],
                'down': generator_mat[1, :, 1, :]
            }

        gf_struct = {sp: numpy.arange(nblock_size) for sp in spin_names}
        Sigma_iw = make_block_gf(GfImFreq, gf_struct, beta, n_points)
        for sp in spin_names:
            Sigma_iw[sp].data[:, :, :] = numpy.random.randn(
                2 * n_points, nblock_size, nblock_size
            ) + 1J * numpy.random.randn(2 * n_points, nblock_size, nblock_size)

        # Symmetrize
        Sigma_iw_symm = symmetrize(Sigma_iw, [gen])

        data_symmetrized = _to_numpy_array(Sigma_iw_symm).reshape(
            (2 * n_points, 2, norb, 2, norb))

        for isp in range(2):
            numpy.testing.assert_allclose(data_symmetrized[:, isp, 0, isp, 0],
                                          data_symmetrized[:, isp, 1, isp, 1])