Example #1
0
def _write_integral_file(mc_chkfile, nevpt_scratch, comm):
    mpi_size = comm.Get_size()
    rank = comm.Get_rank()

    if rank == 0:
        fh5 = h5py.File(mc_chkfile, 'r')

        def load(key):
            if key in fh5:
                return comm.bcast(fh5[key][()])
            else:
                return comm.bcast([])
    else:

        def load(key):
            return comm.bcast(None)

    mol = gto.loads(load('mol'))
    ncore = load('mc/ncore')
    ncas = load('mc/ncas')
    nvirt = load('mc/nvirt')
    orbe = load('mc/orbe')
    orbsym = list(load('mc/orbsym'))
    nelecas = load('mc/nelecas')
    h1e_Si = load('h1e_Si')
    h1e_Sr = load('h1e_Sr')
    h1e = load('h1e')
    e_core = load('e_core')
    h2e = load('h2e')
    h2e_Si = load('h2e_Si')
    h2e_Sr = load('h2e_Sr')

    if rank == 0:
        fh5.close()

    if mol.symmetry and len(orbsym) > 0:
        orbsym = orbsym[ncore:ncore + ncas] + orbsym[:ncore] + orbsym[ncore +
                                                                      ncas:]
        orbsym = dmrg_sym.convert_orbsym(mol.groupname, orbsym)
    else:
        orbsym = [1] * (ncore + ncas + nvirt)

    partial_size = int(math.floor((ncore + nvirt) / float(mpi_size)))
    num_of_orb_begin = min(rank * partial_size, ncore + nvirt)
    num_of_orb_end = min((rank + 1) * partial_size, ncore + nvirt)
    #Adjust the distrubution the non-active orbitals to make sure one processor has at most one more orbital than average.
    if rank < (ncore + nvirt - partial_size * mpi_size):
        num_of_orb_begin += rank
        num_of_orb_end += rank + 1
    else:
        num_of_orb_begin += ncore + nvirt - partial_size * mpi_size
        num_of_orb_end += ncore + nvirt - partial_size * mpi_size

    if num_of_orb_begin < ncore:
        if num_of_orb_end < ncore:
            h1e_Si = h1e_Si[:, num_of_orb_begin:num_of_orb_end]
            h2e_Si = h2e_Si[:, num_of_orb_begin:num_of_orb_end, :, :]
            h1e_Sr = []
            h2e_Sr = []
    # elif num_of_orb_end > ncore + nvirt :
    #     h1e_Si = h1e_Si[:,num_of_orb_begin:]
    #     h2e_Si = h2e_Si[:,num_of_orb_begin:,:,:]
    #     #h2e_Sr = []
    #     orbsym = orbsym[:ncas] + orbsym[num_of_orb_begin:]
    #     norb = ncas + ncore + nvirt - num_of_orb_begin
        else:
            h1e_Si = h1e_Si[:, num_of_orb_begin:]
            h2e_Si = h2e_Si[:, num_of_orb_begin:, :, :]
            h1e_Sr = h1e_Sr[:num_of_orb_end - ncore, :]
            h2e_Sr = h2e_Sr[:num_of_orb_end - ncore, :, :, :]
    elif num_of_orb_begin < ncore + nvirt:
        if num_of_orb_end <= ncore + nvirt:
            h1e_Si = []
            h2e_Si = []
            h1e_Sr = h1e_Sr[num_of_orb_begin - ncore:num_of_orb_end - ncore, :]
            h2e_Sr = h2e_Sr[num_of_orb_begin - ncore:num_of_orb_end -
                            ncore, :, :, :]
    #    else :
    #        h1e_Si = []
    #        h2e_Si = []
    #        h1e_Sr = h1e_Sr[num_of_orb_begin - ncore:,:]
    #        h2e_Sr = h2e_Sr[num_of_orb_begin - ncore:,:,:,:]
    #        orbsym = orbsym[:ncas] + orbsym[ncas+num_of_orb_begin: ]
    #        norb = ncas + ncore + nvirt - num_of_orb_begin
    else:
        raise RuntimeError(
            'No job for this processor.  It may block MPI.COMM_WORLD.barrier')

    norb = ncas + num_of_orb_end - num_of_orb_begin
    orbsym = orbsym[:ncas] + orbsym[ncas + num_of_orb_begin:ncas +
                                    num_of_orb_end]

    if num_of_orb_begin >= ncore:
        partial_core = 0
        partial_virt = num_of_orb_end - num_of_orb_begin
    else:
        if num_of_orb_end >= ncore:
            partial_core = ncore - num_of_orb_begin
            partial_virt = num_of_orb_end - ncore
        else:
            partial_core = num_of_orb_end - num_of_orb_begin
            partial_virt = 0

    tol = float(1e-15)

    f = open(os.path.join(nevpt_scratch, 'FCIDUMP'), 'w')
    nelec = nelecas[0] + nelecas[1]
    fcidump.write_head(f,
                       norb,
                       nelec,
                       ms=abs(nelecas[0] - nelecas[1]),
                       orbsym=orbsym)
    #h2e in active space
    writeh2e_sym(h2e, f, tol)
    #h1e in active space
    writeh1e_sym(h1e, f, tol)

    orbe = list(orbe[:ncore]) + list(orbe[ncore + ncas:])
    orbe = orbe[num_of_orb_begin:num_of_orb_end]
    for i in range(len(orbe)):
        f.write('% .16f  %4d  %4d  %4d  %4d\n' %
                (orbe[i], i + 1 + ncas, i + 1 + ncas, 0, 0))
    f.write('%.16f  %4d  %4d  %4d  %4d\n' % (e_core, 0, 0, 0, 0))
    if (len(h2e_Sr)):
        writeh2e(h2e_Sr, f, tol, shift0=ncas + partial_core + 1)
    f.write('% 4d  %4d  %4d  %4d  %4d\n' % (0, 0, 0, 0, 0))
    if (len(h2e_Si)):
        writeh2e(h2e_Si, f, tol, shift1=ncas + 1)
    f.write('% 4d  %4d  %4d  %4d  %4d\n' % (0, 0, 0, 0, 0))
    if (len(h1e_Sr)):
        writeh1e(h1e_Sr, f, tol, shift0=ncas + partial_core + 1)
    f.write('% 4d  %4d  %4d  %4d  %4d\n' % (0, 0, 0, 0, 0))
    if (len(h1e_Si)):
        writeh1e(h1e_Si, f, tol, shift1=ncas + 1)
    f.write('% 4d  %4d  %4d  %4d  %4d\n' % (0, 0, 0, 0, 0))
    f.write('% 4d  %4d  %4d  %4d  %4d\n' % (0, 0, 0, 0, 0))
    f.close()

    return ncas, partial_core, partial_virt
Example #2
0
def _write_integral_file(mc_chkfile, nevpt_scratch, comm):
    mpi_size = comm.Get_size()
    rank = comm.Get_rank()

    if rank == 0:
        fh5 = h5py.File(mc_chkfile, 'r')
        def load(key):
            if key in fh5:
                return comm.bcast(fh5[key].value)
            else:
                return comm.bcast([])
    else:
        def load(key):
            return comm.bcast(None)

    mol       = gto.loads(load('mol'))
    ncore     = load('mc/ncore')
    ncas      = load('mc/ncas')
    nvirt     = load('mc/nvirt')
    orbe      = load('mc/orbe')
    orbsym    = list(load('mc/orbsym'))
    nelecas   = load('mc/nelecas')
    h1e_Si    = load('h1e_Si')
    h1e_Sr    = load('h1e_Sr')
    h1e       = load('h1e')
    e_core    = load('e_core')
    h2e       = load('h2e')
    h2e_Si    = load('h2e_Si')
    h2e_Sr    = load('h2e_Sr')

    if rank == 0:
        fh5.close()

    if mol.symmetry and len(orbsym) > 0:
        orbsym = orbsym[ncore:ncore+ncas] + orbsym[:ncore] + orbsym[ncore+ncas:]
        orbsym = dmrg_sym.convert_orbsym(mol.groupname, orbsym)
    else:
        orbsym = [1] * (ncore+ncas+nvirt)

    partial_size = int(math.floor((ncore+nvirt)/float(mpi_size)))
    num_of_orb_begin = min(rank*partial_size, ncore+nvirt)
    num_of_orb_end = min((rank+1)*partial_size, ncore+nvirt)
    #Adjust the distrubution the non-active orbitals to make sure one processor has at most one more orbital than average.
    if rank < (ncore+nvirt - partial_size*mpi_size):
        num_of_orb_begin += rank
        num_of_orb_end += rank + 1
    else :
        num_of_orb_begin += ncore+nvirt - partial_size*mpi_size
        num_of_orb_end += ncore+nvirt - partial_size*mpi_size

    if num_of_orb_begin < ncore:
        if num_of_orb_end < ncore:
            h1e_Si = h1e_Si[:,num_of_orb_begin:num_of_orb_end]
            h2e_Si = h2e_Si[:,num_of_orb_begin:num_of_orb_end,:,:]
            h1e_Sr = []
            h2e_Sr = []
       # elif num_of_orb_end > ncore + nvirt :
       #     h1e_Si = h1e_Si[:,num_of_orb_begin:]
       #     h2e_Si = h2e_Si[:,num_of_orb_begin:,:,:]
       #     #h2e_Sr = []
       #     orbsym = orbsym[:ncas] + orbsym[num_of_orb_begin:]
       #     norb = ncas + ncore + nvirt - num_of_orb_begin
        else :
            h1e_Si = h1e_Si[:,num_of_orb_begin:]
            h2e_Si = h2e_Si[:,num_of_orb_begin:,:,:]
            h1e_Sr = h1e_Sr[:num_of_orb_end - ncore,:]
            h2e_Sr = h2e_Sr[:num_of_orb_end - ncore,:,:,:]
    elif num_of_orb_begin < ncore + nvirt :
        if num_of_orb_end <= ncore + nvirt:
            h1e_Si = []
            h2e_Si = []
            h1e_Sr = h1e_Sr[num_of_orb_begin - ncore:num_of_orb_end - ncore,:]
            h2e_Sr = h2e_Sr[num_of_orb_begin - ncore:num_of_orb_end - ncore,:,:,:]
    #    else :
    #        h1e_Si = []
    #        h2e_Si = []
    #        h1e_Sr = h1e_Sr[num_of_orb_begin - ncore:,:]
    #        h2e_Sr = h2e_Sr[num_of_orb_begin - ncore:,:,:,:]
    #        orbsym = orbsym[:ncas] + orbsym[ncas+num_of_orb_begin: ]
    #        norb = ncas + ncore + nvirt - num_of_orb_begin
    else :
        raise RuntimeError('No job for this processor.  It may block MPI.COMM_WORLD.barrier')


    norb = ncas + num_of_orb_end - num_of_orb_begin
    orbsym = orbsym[:ncas] + orbsym[ncas + num_of_orb_begin:ncas + num_of_orb_end]

    if num_of_orb_begin >= ncore:
        partial_core = 0
        partial_virt = num_of_orb_end - num_of_orb_begin
    else:
        if num_of_orb_end >= ncore:
            partial_core = ncore -num_of_orb_begin
            partial_virt = num_of_orb_end - ncore
        else:
            partial_core = num_of_orb_end -num_of_orb_begin
            partial_virt = 0

    tol = float(1e-15)

    f = open(os.path.join(nevpt_scratch, 'FCIDUMP'), 'w')
    nelec = nelecas[0] + nelecas[1]
    fcidump.write_head(f,norb, nelec, ms=abs(nelecas[0]-nelecas[1]), orbsym=orbsym)
    #h2e in active space
    writeh2e_sym(h2e,f,tol)
    #h1e in active space
    writeh1e_sym(h1e,f,tol)


    orbe =list(orbe[:ncore]) + list(orbe[ncore+ncas:])
    orbe = orbe[num_of_orb_begin:num_of_orb_end]
    for i in range(len(orbe)):
        f.write('% .16f  %4d  %4d  %4d  %4d\n'%(orbe[i],i+1+ncas,i+1+ncas,0,0))
    f.write('%.16f  %4d  %4d  %4d  %4d\n'%(e_core,0,0,0,0))
    if (len(h2e_Sr)):
        writeh2e(h2e_Sr,f,tol, shift0 = ncas + partial_core+1)
    f.write('% 4d  %4d  %4d  %4d  %4d\n'%(0,0,0,0,0))
    if (len(h2e_Si)):
        writeh2e(h2e_Si,f,tol, shift1 = ncas+1)
    f.write('% 4d  %4d  %4d  %4d  %4d\n'%(0,0,0,0,0))
    if (len(h1e_Sr)):
        writeh1e(h1e_Sr,f,tol, shift0 = ncas + partial_core+1)
    f.write('% 4d  %4d  %4d  %4d  %4d\n'%(0,0,0,0,0))
    if (len(h1e_Si)):
        writeh1e(h1e_Si,f,tol, shift1 = ncas+1)
    f.write('% 4d  %4d  %4d  %4d  %4d\n'%(0,0,0,0,0))
    f.write('% 4d  %4d  %4d  %4d  %4d\n'%(0,0,0,0,0))
    f.close()

    return ncas, partial_core, partial_virt