示例#1
0
def get_all_rhok(fwf, kbrags, norb=None, ispin=0, show_progress=True):
  from qharv.seed import wf_h5
  from qharv.inspect import axes_pos
  from rsgub.grids.forlib.find_gvecs import calc_rhok
  fp = wf_h5.read(fwf)
  axes = wf_h5.get(fp, 'axes')
  # calculate ukbrags
  raxes = axes_pos.raxes(axes)
  kcand = np.dot(kbrags, np.linalg.inv(raxes))
  ukbrags = np.round(kcand).astype(int)
  # check ukbrags
  kbrags1 = np.dot(ukbrags, raxes)
  assert np.allclose(kbrags, kbrags1)
  # calculate rhok
  gvecs = wf_h5.get(fp, 'gvectors')
  ntwist = wf_h5.get(fp, 'nkpt')[ispin]
  # store complex numbers in real view
  data = np.zeros([ntwist, 2*len(ukbrags)])
  if show_progress:
    from progressbar import ProgressBar
    bar = ProgressBar(maxval=ntwist)
  for itwist in range(ntwist):
    cmat = wf_h5.get_cmat(fp, itwist, ispin, norb=norb)
    val = 2*calc_rhok(ukbrags, gvecs, cmat)
    data[itwist, :] = val.view(float)
    if show_progress:
      bar.update(itwist)
  fp.close()
  return data
示例#2
0
def get_momenta_and_weights(fp, ispin, ikpt, kmax, ecore, efermi):
    """ Histogram psig^2 for a single determinant of Kohn-Sham orbitals at a
  single twist. Select states with momenta k<kmax and energy ecore<e<efermi.

  Args:
    fp (h5py.File): pwscf.pwscf.h5 file pointer
    ispin (int): determinant index (0: unpolarized, 0/1: polarized)
    ikpt (int): twist vector index
    kmax (float): maximum momentum to record
    ecore (float): Core state energy bound. States below ecore are excluded
    efermi (float): Fermi energy
  Return:
    (np.array, np.array): (momenta, weights)
  """
    # get reciprocal lattice vectors to do lattice transformation
    axes = wf_h5.get(fp, 'axes')
    raxes = axes_pos.raxes(axes)
    # Bloch function momenta (kvecs)
    gvecs = wf_h5.get(fp, 'gvectors')
    kvecs = np.dot(gvecs, raxes)
    # crystal momentum (tvec)
    kpath = wf_h5.kpoint_path(ikpt)
    utvec = fp[os.path.join(kpath, 'reduced_k')].value
    tvec = np.dot(utvec, raxes)
    # true momentum = crystal + Bloch momentum
    mykvecs = kvecs + tvec[np.newaxis, :]

    # keep kvectors below kmax
    mykmags = np.linalg.norm(mykvecs, axis=1)
    sel = mykmags < kmax
    momenta = mykvecs[sel]

    # accumulate occupation
    weights = np.zeros(len(momenta))
    # keep states below the Fermi level and outside the core
    nstate = fp[os.path.join(kpath, 'spin_%d' % ispin,
                             'number_of_states')].value[0]
    evals = fp[os.path.join(kpath, 'spin_%d' % ispin, 'eigenvalues')].value
    esel = (evals > ecore) & (evals < efermi)
    states = np.arange(nstate)
    for istate in states[esel]:
        psig = wf_h5.get_orb_in_pw(fp, ikpt, ispin, istate)
        pg2 = psig.conj() * psig
        if not np.allclose(pg2.imag, 0): raise RuntimeError('dot not zero')
        pg2 = pg2.real[sel]
        weights += pg2
    return momenta, weights
示例#3
0
def write_detsk(h5file, ikpt, fwf, ispin, nsh0, kc):
  """Calculate determinant S(k) at given twist

  Args:
    h5file (tables.file.File): hdf5 file handle
    ikpt (int): twist index
    fwf (str): wf h5 file e.g. pwscf.pwscf.h5
    ispin (int): spin index, use 0 for unpolarized
    nsh0 (int): number of shells to use
    kc (float): PW cutoff
  """
  from qharv.seed import wf_h5
  from qharv.inspect.axes_pos import raxes
  from qharv.reel.config_h5 import save_dict
  from solith.li_nofk.forlib.det import calc_detsk
  from chiesa_correction import mirror_xyz, cubic_pos
  # creat slab for twist
  gname = 'twist%s' % str(ikpt).zfill(3)
  slab = h5file.create_group('/', gname, '')

  # read wf file
  fp = wf_h5.read(fwf)
  gvecs = wf_h5.get(fp, 'gvectors')
  cmat = wf_h5.get_cmat(fp, ikpt, ispin)
  wf_h5.normalize_cmat(cmat)
  axes = wf_h5.get(fp, 'axes')
  fp.close()
  raxes = raxes(axes)

  # decide which qvectors to calculate S(q)
  qvecs = mirror_xyz(cubic_pos(nsh0))
  kvecs = np.dot(qvecs, raxes)
  kmags = np.linalg.norm(kvecs, axis=-1)
  qsel = (1e-8 < kmags) & (kmags < kc)

  # calculate S(k)
  sk0 = calc_detsk(qvecs[qsel],  gvecs, cmat)

  # save
  arr_dict = {
    'raxes': raxes,
    'gvecs': qvecs[qsel],
    'sk0': sk0
  }
  save_dict(arr_dict, h5file, slab)
示例#4
0
def get_det_nk(fp, efermi, ecore=-np.inf, kmax=np.inf, ispin=0):
    """Calculate momentum distribution from Kohn-Sham determinant

  same as get_momentum_distribution, but faster

  Args:
    fp (h5py.File): pwscf.pwscf.h5 file pointer
    efermi (float): Fermi energy
    ecore (float, optional): core energy, default -np.inf
    kmax (float, optional): maximum k magnitude to record, default np.inf
    ispin (int, optiona): default 0
  Return:
    (np.array, np.array): (kvecs, nkm), kvectors and n(k) mean (no error)
  """
    from solith.li_nofk.forlib.det import nofk
    # need Kohn-Sham eigenvalues to decide which states to use
    bands = wf_h5.get_bands(fp, ispin=ispin)
    nt, nstate = bands.shape

    # need kgrid info: basis (raxes), unshifted (kvecs0), twists (tvecs)
    axes = wf_h5.get(fp, 'axes')
    raxes = axes_pos.raxes(axes)
    gvecs = wf_h5.get(fp, 'gvectors')
    utvecs = wf_h5.get_twists(fp)
    tvecs = np.dot(utvecs, raxes)
    kvecs0 = np.dot(gvecs, raxes)

    # calculate n(k) at each twist
    kvecsl = []
    nkml = []
    for it in range(nt):
        kvecs = kvecs0 + tvecs[it]  # current twist
        # histogram orb^2
        cmat = wf_h5.get_cmat(fp, it, ispin, nstate)
        nocc, npw = cmat.shape
        weights = nofk(kvecs, cmat, bands[it], kmax, ecore, efermi)
        # save within a cutoff
        kmags = np.linalg.norm(kvecs, axis=-1)
        sel = kmags < kmax
        kvecsl.append(kvecs[sel])
        nkml.append(weights[sel])

    kvecs = np.concatenate(kvecsl, axis=0)
    nkm = np.concatenate(nkml)
    return kvecs, nkm
示例#5
0
def get_rhok(fwf, kbrags, norb=None, itwist=0, ispin=0):
  from qharv.seed import wf_h5
  from qharv.inspect import axes_pos
  from rsgub.grids.forlib.find_gvecs import calc_rhok
  fp = wf_h5.read(fwf)
  axes = wf_h5.get(fp, 'axes')
  gvecs = wf_h5.get(fp, 'gvectors')
  cmat = wf_h5.get_cmat(fp, itwist, ispin, norb=norb)
  fp.close()
  # calculate ukbrags
  raxes = axes_pos.raxes(axes)
  kcand = np.dot(kbrags, np.linalg.inv(raxes))
  ukbrags = np.round(kcand).astype(int)
  # check ukbrags
  kbrags1 = np.dot(ukbrags, raxes)
  assert np.allclose(kbrags, kbrags1)
  # calculate rhok
  val = 2*calc_rhok(ukbrags, gvecs, cmat)
  return val
示例#6
0
def get_momentum_distribution(fp, kmax, efermi, ispin=0, ecore=-np.inf):
    """ obtain momentum distribution from Kohn-Sham determinant stored 
  in QMCPACK wf.h5 file format

  Args:
    fp (h5py.File): pwscf.pwscf.h5 file pointer
    kmax (float): maximum k magnitude to record
    efermi (float): Fermi energy
  Return:
    (np.array, np.array): (kvecs, nkm), kvectors and n(k) mean (no error)
  """
    nkpt = wf_h5.get(fp, 'nkpt')[0]
    mlist = []
    wlist = []
    for ikpt in range(nkpt):
        momenta, weights = get_momenta_and_weights(fp, ispin, ikpt, kmax,
                                                   ecore, efermi)
        mlist += momenta.tolist()
        wlist += weights.tolist()
    # end for ikpt
    kvecs = np.array(mlist)
    nkm = np.array(wlist)
    return kvecs, nkm