def create_kpoint_descriptor(self, nspins): par = self.parameters bzkpts_kc = kpts2ndarray(par.kpts, self.atoms) kpt_refine = par.experimental.get('kpt_refine') if kpt_refine is None: kd = KPointDescriptor(bzkpts_kc, nspins) self.timer.start('Set symmetry') kd.set_symmetry(self.atoms, self.symmetry, comm=self.world) self.timer.stop('Set symmetry') else: self.timer.start('Set k-point refinement') kd = create_kpoint_descriptor_with_refinement(kpt_refine, bzkpts_kc, nspins, self.atoms, self.symmetry, comm=self.world, timer=self.timer) self.timer.stop('Set k-point refinement') # Update quantities which might have changed, if symmetry # was changed self.symmetry = kd.symmetry self.setups.set_symmetry(kd.symmetry) self.log(kd) return kd
def get_ibz_q_points(self, bz_k_points): # Get all q-points all_qs = [] for k1 in bz_k_points: for k2 in bz_k_points: all_qs.append(k1-k2) all_qs = np.array(all_qs) # Fold q-points into Brillouin zone all_qs[np.where(all_qs > 0.501)] -= 1. all_qs[np.where(all_qs < -0.499)] += 1. # Make list of non-identical q-points in full BZ bz_qs = [all_qs[0]] for q_a in all_qs: q_in_list = False for q_b in bz_qs: if (abs(q_a[0]-q_b[0]) < 0.01 and abs(q_a[1]-q_b[1]) < 0.01 and abs(q_a[2]-q_b[2]) < 0.01): q_in_list = True break if q_in_list == False: bz_qs.append(q_a) self.bz_q_points = bz_qs # Obtain q-points and weights in the irreducible part of the BZ kpt_descriptor = KPointDescriptor(bz_qs, self.nspins) kpt_descriptor.set_symmetry(self.atoms, self.setups, usesymm=True) ibz_q_points = kpt_descriptor.ibzk_kc q_weights = kpt_descriptor.weight_k return ibz_q_points, q_weights
def get_ibz_q_points(self, bz_k_points): # Get all q-points all_qs = [] for k1 in bz_k_points: for k2 in bz_k_points: all_qs.append(k1 - k2) all_qs = np.array(all_qs) # Fold q-points into Brillouin zone all_qs[np.where(all_qs > 0.501)] -= 1. all_qs[np.where(all_qs < -0.499)] += 1. # Make list of non-identical q-points in full BZ bz_qs = [all_qs[0]] for q_a in all_qs: q_in_list = False for q_b in bz_qs: if (abs(q_a[0] - q_b[0]) < 0.01 and abs(q_a[1] - q_b[1]) < 0.01 and abs(q_a[2] - q_b[2]) < 0.01): q_in_list = True break if q_in_list == False: bz_qs.append(q_a) self.bz_q_points = bz_qs # Obtain q-points and weights in the irreducible part of the BZ kpt_descriptor = KPointDescriptor(bz_qs, self.nspins) kpt_descriptor.set_symmetry(self.atoms, self.setups, usesymm=True) ibz_q_points = kpt_descriptor.ibzk_kc q_weights = kpt_descriptor.weight_k return ibz_q_points, q_weights
def setUp(self): for virtvar in ['boundaries']: assert getattr(self, virtvar) is not None, 'Virtual "%s"!' % virtvar # Basic unit cell information: res, N_c = shapeopt(100, self.G**3, 3, 0.2) #N_c = 4*np.round(np.array(N_c)/4) # makes domain decomposition easier cell_cv = self.h * np.diag(N_c) pbc_c = {'zero' : (False,False,False), \ 'periodic': (True,True,True), \ 'mixed' : (True, False, True)}[self.boundaries] # Create randomized gas-like atomic configuration on interim grid tmpgd = GridDescriptor(N_c, cell_cv, pbc_c) self.atoms = create_random_atoms(tmpgd) # Create setups Z_a = self.atoms.get_atomic_numbers() assert 1 == self.nspins self.setups = Setups(Z_a, p.setups, p.basis, p.lmax, xc) self.natoms = len(self.setups) # Decide how many kpoints to sample from the 1st Brillouin Zone kpts_c = np.ceil( (10 / Bohr) / np.sum(cell_cv**2, axis=1)**0.5).astype(int) kpts_c = tuple(kpts_c * pbc_c + 1 - pbc_c) self.bzk_kc = kpts2ndarray(kpts_c) # Set up k-point descriptor self.kd = KPointDescriptor(self.bzk_kc, self.nspins) self.kd.set_symmetry(self.atoms, self.setups, p.usesymm) # Set the dtype if self.kd.gamma: self.dtype = float else: self.dtype = complex # Create communicators parsize, parsize_bands = self.get_parsizes() assert self.nbands % np.prod(parsize_bands) == 0 domain_comm, kpt_comm, band_comm = distribute_cpus( parsize, parsize_bands, self.nspins, self.kd.nibzkpts) self.kd.set_communicator(kpt_comm) # Set up band descriptor: self.bd = BandDescriptor(self.nbands, band_comm) # Set up grid descriptor: self.gd = GridDescriptor(N_c, cell_cv, pbc_c, domain_comm, parsize) # Set up kpoint/spin descriptor (to be removed): self.kd_old = KPointDescriptorOld(self.nspins, self.kd.nibzkpts, kpt_comm, self.kd.gamma, self.dtype)
def ibz2bz(self, atoms): """Transform wave functions in IBZ to the full BZ.""" assert self.kd.comm.size == 1 # New k-point descriptor for full BZ: kd = KPointDescriptor(self.kd.bzk_kc, nspins=self.nspins) #kd.set_symmetry(atoms, self.setups, enabled=False) kd.set_communicator(serial_comm) self.pt = LFC(self.gd, [setup.pt_j for setup in self.setups], kd, dtype=self.dtype) self.pt.set_positions(atoms.get_scaled_positions()) self.initialize_wave_functions_from_restart_file() weight = 2.0 / kd.nspins / kd.nbzkpts # Build new list of k-points: kpt_u = [] for s in range(self.nspins): for k in range(kd.nbzkpts): # Index of symmetry related point in the IBZ ik = self.kd.bz2ibz_k[k] r, u = self.kd.get_rank_and_index(s, ik) assert r == 0 kpt = self.kpt_u[u] phase_cd = np.exp(2j * np.pi * self.gd.sdisp_cd * kd.bzk_kc[k, :, np.newaxis]) # New k-point: kpt2 = KPoint(weight, s, k, k, phase_cd) kpt2.f_n = kpt.f_n / kpt.weight / kd.nbzkpts * 2 / self.nspins kpt2.eps_n = kpt.eps_n.copy() # Transform wave functions using symmetry operation: Psit_nG = self.gd.collect(kpt.psit_nG) if Psit_nG is not None: Psit_nG = Psit_nG.copy() for Psit_G in Psit_nG: Psit_G[:] = self.kd.transform_wave_function(Psit_G, k) kpt2.psit_nG = self.gd.empty(self.bd.nbands, dtype=self.dtype) self.gd.distribute(Psit_nG, kpt2.psit_nG) # Calculate PAW projections: kpt2.P_ani = self.pt.dict(len(kpt.psit_nG)) self.pt.integrate(kpt2.psit_nG, kpt2.P_ani, k) kpt_u.append(kpt2) self.kd = kd self.kpt_u = kpt_u
def ibz2bz(self, atoms): """Transform wave functions in IBZ to the full BZ.""" assert self.kd.comm.size == 1 # New k-point descriptor for full BZ: kd = KPointDescriptor(self.kd.bzk_kc, nspins=self.nspins) kd.set_symmetry(atoms, self.setups, usesymm=None) kd.set_communicator(serial_comm) self.pt = LFC(self.gd, [setup.pt_j for setup in self.setups], kd, dtype=self.dtype) self.pt.set_positions(atoms.get_scaled_positions()) self.initialize_wave_functions_from_restart_file() weight = 2.0 / kd.nspins / kd.nbzkpts # Build new list of k-points: kpt_u = [] for s in range(self.nspins): for k in range(kd.nbzkpts): # Index of symmetry related point in the IBZ ik = self.kd.bz2ibz_k[k] r, u = self.kd.get_rank_and_index(s, ik) assert r == 0 kpt = self.kpt_u[u] phase_cd = np.exp(2j * np.pi * self.gd.sdisp_cd * kd.bzk_kc[k, :, np.newaxis]) # New k-point: kpt2 = KPoint(weight, s, k, k, phase_cd) kpt2.f_n = kpt.f_n / kpt.weight / kd.nbzkpts * 2 / self.nspins kpt2.eps_n = kpt.eps_n.copy() # Transform wave functions using symmetry operation: Psit_nG = self.gd.collect(kpt.psit_nG) if Psit_nG is not None: Psit_nG = Psit_nG.copy() for Psit_G in Psit_nG: Psit_G[:] = self.kd.transform_wave_function(Psit_G, k) kpt2.psit_nG = self.gd.empty(self.bd.nbands, dtype=self.dtype) self.gd.distribute(Psit_nG, kpt2.psit_nG) # Calculate PAW projections: kpt2.P_ani = self.pt.dict(len(kpt.psit_nG)) self.pt.integrate(kpt2.psit_nG, kpt2.P_ani, k) kpt_u.append(kpt2) self.kd = kd self.kpt_u = kpt_u
def setUp(self): UTDomainParallelSetup.setUp(self) for virtvar in ['dtype']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Create randomized atoms self.atoms = create_random_atoms(self.gd, 5) # also tested: 10xNH3/BDA # XXX DEBUG START if False: from ase import view view(self.atoms*(1+2*self.gd.pbc_c)) # XXX DEBUG END # Do we agree on the atomic positions? pos_ac = self.atoms.get_positions() pos_rac = np.empty((world.size,)+pos_ac.shape, pos_ac.dtype) world.all_gather(pos_ac, pos_rac) if (pos_rac-pos_rac[world.rank,...][np.newaxis,...]).any(): raise RuntimeError('Discrepancy in atomic positions detected.') # Create setups for atoms self.Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(self.Z_a, p.setups, p.basis, p.lmax, xc) # K-point descriptor bzk_kc = np.array([[0, 0, 0]], dtype=float) self.kd = KPointDescriptor(bzk_kc, 1) self.kd.set_symmetry(self.atoms, self.setups) self.kd.set_communicator(self.kpt_comm) # Create gamma-point dummy wavefunctions self.wfs = FDWFS(self.gd, self.bd, self.kd, self.setups, self.block_comm, self.dtype) spos_ac = self.atoms.get_scaled_positions() % 1.0 self.wfs.set_positions(spos_ac) self.pt = self.wfs.pt # XXX shortcut ## Also create pseudo partial waveves #from gpaw.lfc import LFC #self.phit = LFC(self.gd, [setup.phit_j for setup in self.setups], \ # self.kpt_comm, dtype=self.dtype) #self.phit.set_positions(spos_ac) self.r_cG = None self.buf_G = None self.psit_nG = None self.allocate()
def __init__(self, calc, xc, ibzq_qc, fd, unit_cells, density_cut, ecut, tag, timer): self.calc = calc self.gd = calc.density.gd self.xc = xc self.ibzq_qc = ibzq_qc self.fd = fd self.unit_cells = unit_cells self.density_cut = density_cut self.ecut = ecut self.tag = tag self.timer = timer self.A_x = -(3 / 4.) * (3 / np.pi)**(1 / 3.) self.n_g = calc.get_all_electron_density(gridrefinement=1) self.n_g *= Bohr**3 if xc[-3:] == 'PBE': nf_g = calc.get_all_electron_density(gridrefinement=2) nf_g *= Bohr**3 gdf = self.gd.refine() grad_v = [Gradient(gdf, v, n=1).apply for v in range(3)] gradnf_vg = gdf.empty(3) for v in range(3): grad_v[v](nf_g, gradnf_vg[v]) self.gradn_vg = gradnf_vg[:, ::2, ::2, ::2] qd = KPointDescriptor(self.ibzq_qc) self.pd = PWDescriptor(ecut / Hartree, self.gd, complex, qd)
def get_pw_descriptor(q_c, calc, ecut, gammacentered=False): """Get the planewave descriptor of q_c.""" qd = KPointDescriptor([q_c]) pd = PWDescriptor(ecut, calc.wfs.gd, complex, qd, gammacentered=gammacentered) return pd
def initialize(self, density, hamiltonian, wfs, occupations): self.xc.initialize(density, hamiltonian, wfs, occupations) self.nspins = wfs.nspins self.setups = wfs.setups self.density = density self.kpt_u = wfs.kpt_u self.gd = density.gd self.kd = wfs.kd self.bd = wfs.bd if self.bd.comm.size > 1: raise ValueError('Band parallelization not supported by hybridk') self.wfs = wfs self.world = wfs.world self.fd = logfile(self.fd, self.world.rank) N = self.gd.N_c.prod() vol = self.gd.dv * N if self.alpha is None: # XXX ? self.alpha = 6 * vol**(2 / 3.0) / pi**2 if self.ecut is None: self.ecut = 0.5 * pi**2 / (self.gd.h_cv**2).sum(1).max() * 0.9999 self.bzq_qc = self.kd.get_bz_q_points() qd = KPointDescriptor(self.bzq_qc) q0 = self.kd.where_is_q(np.zeros(3), self.bzq_qc) self.pwd = PWDescriptor(self.ecut, self.gd, complex, kd=qd) G2_qG = self.pwd.G2_qG G2_qG[q0][0] = 117.0 self.iG2_qG = [1.0 / G2_G for G2_G in G2_qG] G2_qG[q0][0] = 0.0 self.iG2_qG[q0][0] = 0.0 self.gamma = (vol / (2 * pi)**2 * sqrt(pi / self.alpha) * self.kd.nbzkpts) for q in range(self.kd.nbzkpts): self.gamma -= np.dot(np.exp(-self.alpha * G2_qG[q]), self.iG2_qG[q]) self.iG2_qG[q0][0] = self.gamma self.ghat = LFC(self.gd, [setup.ghat_l for setup in density.setups], qd, dtype=complex) self.log('Value of alpha parameter:', self.alpha) self.log('Value of gamma parameter:', self.gamma) self.log('Cutoff energy:', self.ecut, 'Hartree') self.log('%d x %d x %d k-points' % tuple(self.kd.N_c))
def get_PWDescriptor(self, q_c, gammacentered=False): """Get the planewave descriptor of q_c.""" qd = KPointDescriptor([q_c]) pd = PWDescriptor(self.ecut, self.calc.wfs.gd, complex, qd, gammacentered=gammacentered) return pd
def setUp(self): for virtvar in ['boundaries']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Basic unit cell information: res, N_c = shapeopt(100, self.G**3, 3, 0.2) #N_c = 4*np.round(np.array(N_c)/4) # makes domain decomposition easier cell_cv = self.h * np.diag(N_c) pbc_c = {'zero' : (False,False,False), \ 'periodic': (True,True,True), \ 'mixed' : (True, False, True)}[self.boundaries] # Create randomized gas-like atomic configuration on interim grid tmpgd = GridDescriptor(N_c, cell_cv, pbc_c) self.atoms = create_random_atoms(tmpgd) # Create setups Z_a = self.atoms.get_atomic_numbers() assert 1 == self.nspins self.setups = Setups(Z_a, p.setups, p.basis, p.lmax, xc) self.natoms = len(self.setups) # Decide how many kpoints to sample from the 1st Brillouin Zone kpts_c = np.ceil((10/Bohr)/np.sum(cell_cv**2,axis=1)**0.5).astype(int) kpts_c = tuple(kpts_c * pbc_c + 1 - pbc_c) self.bzk_kc = kpts2ndarray(kpts_c) # Set up k-point descriptor self.kd = KPointDescriptor(self.bzk_kc, self.nspins) self.kd.set_symmetry(self.atoms, self.setups, p.usesymm) # Set the dtype if self.kd.gamma: self.dtype = float else: self.dtype = complex # Create communicators parsize, parsize_bands = self.get_parsizes() assert self.nbands % np.prod(parsize_bands) == 0 domain_comm, kpt_comm, band_comm = distribute_cpus(parsize, parsize_bands, self.nspins, self.kd.nibzkpts) self.kd.set_communicator(kpt_comm) # Set up band descriptor: self.bd = BandDescriptor(self.nbands, band_comm) # Set up grid descriptor: self.gd = GridDescriptor(N_c, cell_cv, pbc_c, domain_comm, parsize) # Set up kpoint/spin descriptor (to be removed): self.kd_old = KPointDescriptorOld(self.nspins, self.kd.nibzkpts, kpt_comm, self.kd.gamma, self.dtype)
def calculate(self, q_c, spin='all', A_x=None): wfs = self.calc.wfs if spin == 'all': spins = range(wfs.nspins) else: assert spin in range(wfs.nspins) spins = [spin] q_c = np.asarray(q_c, dtype=float) qd = KPointDescriptor([q_c]) pd = PWDescriptor(self.ecut, wfs.gd, complex, qd) self.print_chi(pd) if extra_parameters.get('df_dry_run'): print(' Dry run exit', file=self.fd) raise SystemExit nG = pd.ngmax nw = len(self.omega_w) mynG = (nG + self.blockcomm.size - 1) // self.blockcomm.size self.Ga = self.blockcomm.rank * mynG self.Gb = min(self.Ga + mynG, nG) assert mynG * (self.blockcomm.size - 1) < nG if A_x is not None: nx = nw * (self.Gb - self.Ga) * nG chi0_wGG = A_x[:nx].reshape((nw, self.Gb - self.Ga, nG)) chi0_wGG[:] = 0.0 else: chi0_wGG = np.zeros((nw, self.Gb - self.Ga, nG), complex) if np.allclose(q_c, 0.0): chi0_wxvG = np.zeros((len(self.omega_w), 2, 3, nG), complex) chi0_wvv = np.zeros((len(self.omega_w), 3, 3), complex) self.chi0_vv = np.zeros((3, 3), complex) else: chi0_wxvG = None chi0_wvv = None print('Initializing PAW Corrections', file=self.fd) self.Q_aGii = self.initialize_paw_corrections(pd) # Do all empty bands: m1 = self.nocc1 m2 = self.nbands self._calculate(pd, chi0_wGG, chi0_wxvG, chi0_wvv, self.Q_aGii, m1, m2, spins) return pd, chi0_wGG, chi0_wxvG, chi0_wvv
def setUp(self): UTDomainParallelSetup.setUp(self) for virtvar in ['dtype']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Create randomized atoms self.atoms = create_random_atoms(self.gd, 5) # also tested: 10xNH3/BDA # XXX DEBUG START if False: from ase import view view(self.atoms*(1+2*self.gd.pbc_c)) # XXX DEBUG END # Do we agree on the atomic positions? pos_ac = self.atoms.get_positions() pos_rac = np.empty((world.size,)+pos_ac.shape, pos_ac.dtype) world.all_gather(pos_ac, pos_rac) if (pos_rac-pos_rac[world.rank,...][np.newaxis,...]).any(): raise RuntimeError('Discrepancy in atomic positions detected.') # Create setups for atoms self.Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(self.Z_a, p.setups, p.basis, p.lmax, xc) # K-point descriptor bzk_kc = np.array([[0, 0, 0]], dtype=float) self.kd = KPointDescriptor(bzk_kc, 1) self.kd.set_symmetry(self.atoms, self.setups, usesymm=True) self.kd.set_communicator(self.kpt_comm) # Create gamma-point dummy wavefunctions self.wfs = FDWFS(self.gd, self.bd, self.kd, self.setups, self.dtype) spos_ac = self.atoms.get_scaled_positions() % 1.0 self.wfs.set_positions(spos_ac) self.pt = self.wfs.pt # XXX shortcut ## Also create pseudo partial waveves #from gpaw.lfc import LFC #self.phit = LFC(self.gd, [setup.phit_j for setup in self.setups], \ # self.kpt_comm, dtype=self.dtype) #self.phit.set_positions(spos_ac) self.r_cG = None self.buf_G = None self.psit_nG = None self.allocate()
def setUp(self): for virtvar in ['equipartition']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar kpts = {'even' : (12,1,2), \ 'prime': (23,1,1)}[self.equipartition] #primes = [i for i in xrange(50,1,-1) if ~np.any(i%np.arange(2,i)==0)] bzk_kc = kpts2ndarray(kpts) assert p.usesymm == None self.nibzkpts = len(bzk_kc) #parsize, parsize_bands = create_parsize_minbands(self.nbands, world.size) parsize, parsize_bands = 1, 1 #XXX assert self.nbands % np.prod(parsize_bands) == 0 domain_comm, kpt_comm, band_comm = distribute_cpus(parsize, parsize_bands, self.nspins, self.nibzkpts) # Set up band descriptor: self.bd = BandDescriptor(self.nbands, band_comm, p.parallel['stridebands']) # Set up grid descriptor: res, ngpts = shapeopt(300, self.G**3, 3, 0.2) cell_c = self.h * np.array(ngpts) pbc_c = (True, False, True) self.gd = GridDescriptor(ngpts, cell_c, pbc_c, domain_comm, parsize) # Create randomized gas-like atomic configuration self.atoms = create_random_atoms(self.gd) # Create setups Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(Z_a, p.setups, p.basis, p.lmax, xc) self.natoms = len(self.setups) # Set up kpoint descriptor: self.kd = KPointDescriptor(bzk_kc, self.nspins) self.kd.set_symmetry(self.atoms, self.setups, p.usesymm) self.kd.set_communicator(kpt_comm)
def dscf_load_band(filename, paw, molecule=None): """Load and distribute all information for a band from a tar file.""" if not paw.wfs: paw.initialize() world, bd, gd, kd = paw.wfs.world, paw.wfs.bd, paw.wfs.gd, \ KPointDescriptor(paw.wfs.nspins, paw.wfs.nibzkpts, paw.wfs.kpt_comm, \ paw.wfs.gamma, paw.wfs.dtype) if bd.comm.size != 1: raise NotImplementedError('Undefined action for band parallelization.') r = Reader(filename) assert (r.dimension('nspins') == kd.nspins and \ r.dimension('nibzkpts') == kd.nibzkpts), 'Incompatible spin/kpoints.' # Read wave function for every spin/kpoint owned by this rank psit_uG = gd.empty(kd.mynks, kd.dtype) for myu, psit_G in enumerate(psit_uG): u = kd.global_index(myu) s, k = kd.what_is(u) if gd.comm.rank == 0: big_psit_G = np.array(r.get('PseudoWaveFunction', s, k), kd.dtype) else: big_psit_G = None gd.distribute(big_psit_G, psit_G) # Find domain ranks for each atom atoms = paw.get_atoms() spos_ac = atoms.get_scaled_positions() % 1.0 rank_a = gd.get_ranks_from_positions(spos_ac) #my_atom_indices = np.argwhere(rank_a == gd.comm.rank).ravel() #assert np.all(my_atom_indices == paw.wfs.pt.my_atom_indices) assert r.dimension('nproj') == sum([setup.ni for setup in paw.wfs.setups]) if molecule is None: molecule = range(len(atoms)) # Read projections for every spin/kpoint and atom owned by this rank P_uai = [{}] * kd.mynks #[paw.wfs.pt.dict() for myu in range(kd.mynks)] for myu, P_ai in enumerate(P_uai): u = kd.global_index(myu) s, k = kd.what_is(u) P_i = r.get('Projection', s, k) i1 = 0 for a in molecule: setup = paw.wfs.setups[a] i2 = i1 + setup.ni if gd.comm.rank == rank_a[a]: P_ai[a] = np.array(P_i[i1:i2], kd.dtype) i1 = i2 return psit_uG, P_uai
def __init__(self, atoms, kpts, symmetry): """Initialize base class and attributes. Parameters ---------- atoms: Atoms ASE atoms. kpts: tuple or list of tuples Shape of Monkhorst-Pack grid or list of k-points used in the dfpt calculation. symmetry: bool or None Symmetry parameter used in dfpt calculation. """ # Init base class with ``Atoms`` object phonons.Phonons.__init__(atoms) # Create k-point descriptor self.kd = KPointDescriptor(kpts, 1) self.kd.set_symmetry(self.atoms, self.calc.wfs.setups, symmetry) # Overwrite ``N_c`` attribute self.N_c = tuple(self.kd.N_c) # Index of the gamma point -- for the acoustic sum-rule self.gamma_index = None if self.kd.gamma: self.gamma_index = 0 self.dtype == float else: self.dtype == comples for k, k_c in enumerate(self.kd.ibzk_kc): if np.all(k_c == 0.): self.gamma_index = k assert self.gamma_index is not None
def check(self, i_cG, shift0_c, N_c, q_c, Q_aGii): I0_G = np.ravel_multi_index(i_cG - shift0_c[:, None], N_c, 'wrap') qd1 = KPointDescriptor([q_c]) pd1 = PWDescriptor(self.ecut, self.calc.wfs.gd, complex, qd1) G_I = np.empty(N_c.prod(), int) G_I[:] = -1 I1_G = pd1.Q_qG[0] G_I[I1_G] = np.arange(len(I0_G)) G_G = G_I[I0_G] assert len(I0_G) == len(I1_G) assert (G_G >= 0).all() for a, Q_Gii in enumerate(self.initialize_paw_corrections(pd1)): e = abs(Q_aGii[a] - Q_Gii[G_G]).max() assert e < 1e-12
def calculate_gamma(self, vol, alpha): if self.molecule: return 0.0 N_c = self.kd.N_c offset_c = (N_c + 1) % 2 * 0.5 / N_c bzq_qc = monkhorst_pack(N_c) + offset_c qd = KPointDescriptor(bzq_qc) pd = PWDescriptor(self.wfs.pd.ecut, self.wfs.gd, kd=qd) gamma = (vol / (2 * pi)**2 * sqrt(pi / alpha) * self.kd.nbzkpts) for G2_G in pd.G2_qG: if G2_G[0] < 1e-7: G2_G = G2_G[1:] gamma -= np.dot(np.exp(-alpha * G2_G), G2_G**-1) return gamma / self.qstride_c.prod()
def dscf_save_band(filename, paw, n): """Extract and save all information for band `n` to a tar file.""" world, bd, gd, kd = paw.wfs.world, paw.wfs.bd, paw.wfs.gd, \ KPointDescriptor(paw.wfs.nspins, paw.wfs.nibzkpts, paw.wfs.kpt_comm, \ paw.wfs.gamma, paw.wfs.dtype) if world.rank == 0: # Minimal amount of information needed: w = Writer(filename) w.dimension('nspins', kd.nspins) w.dimension('nibzkpts', kd.nibzkpts) w.dimension('nproj', sum([setup.ni for setup in paw.wfs.setups])) ng = gd.get_size_of_global_array() w.dimension('ngptsx', ng[0]) w.dimension('ngptsy', ng[1]) w.dimension('ngptsz', ng[2]) # Write projections: if world.rank == 0: w.add('Projection', ('nspins', 'nibzkpts', 'nproj'), dtype=kd.dtype) for s in range(kd.nspins): for k in range(kd.nibzkpts): all_P_ni = paw.wfs.collect_projections(k, s) # gets all bands if world.rank == 0: w.fill(all_P_ni[n]) # Write wave functions: if world.rank == 0: w.add('PseudoWaveFunction', ('nspins', 'nibzkpts', 'ngptsx', 'ngptsy', 'ngptsz'), dtype=kd.dtype) for s in range(kd.nspins): for k in range(kd.nibzkpts): psit_G = paw.wfs.get_wave_function_array(n, k, s) if world.rank == 0: w.fill(psit_G) if world.rank == 0: # Close the file here to ensure that the last wave function is # written to disk: w.close() # We don't want the slaves to start reading before the master has # finished writing: world.barrier()
def setUp(self): UTDomainParallelSetup.setUp(self) for virtvar in ['dtype']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Set up kpoint descriptor: self.kd = KPointDescriptor(self.nspins, self.nibzkpts, self.kpt_comm, \ self.gamma, self.dtype) # Choose a sufficiently small width of gaussian test functions cell_c = np.sum(self.gd.cell_cv**2, axis=1)**0.5 self.sigma = np.min((0.1+0.4*self.gd.pbc_c)*cell_c) if debug and world.rank == 0: print 'sigma=%8.5f Ang' % (self.sigma*Bohr), 'cell_c:', cell_c*Bohr, 'Ang', 'N_c:', self.gd.N_c self.atoms = create_random_atoms(self.gd, 4, 'H', 4*self.sigma) self.r_vG = None self.wf_uG = None self.laplace0_uG = None self.allocate()
def calculate_q(self, i, kpt1, kpt2): wfs = self.calc.wfs q_c = wfs.kd.bzk_kc[kpt2.K] - wfs.kd.bzk_kc[kpt1.K] qd = KPointDescriptor([q_c]) pd = PWDescriptor(self.ecut, wfs.gd, wfs.dtype, kd=qd) Q_G = self.get_fft_indices(kpt1.K, kpt2.K, q_c, pd, kpt1.shift_c - kpt2.shift_c) Q_aGii = self.initialize_paw_corrections(pd, soft=True) for n in range(kpt1.n2 - kpt1.n1): ut1cc_R = kpt1.ut_nR[n].conj() C1_aGi = [ np.dot(Q_Gii, P1_ni[n].conj()) for Q_Gii, P1_ni in zip(Q_aGii, kpt1.P_ani) ] n_mG = self.calculate_pair_densities(ut1cc_R, C1_aGi, kpt2, pd, Q_G) e = self.calculate_n(pd, n, n_mG, kpt2) self.exxvv_sin[kpt1.s, i, n] += e
def setUp(self): for virtvar in ['equipartition']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar kpts = {'even' : (12,1,2), \ 'prime': (23,1,1)}[self.equipartition] #primes = [i for i in xrange(50,1,-1) if ~np.any(i%np.arange(2,i)==0)] bzk_kc = kpts2ndarray(kpts) assert p.usesymm == None self.nibzkpts = len(bzk_kc) #parsize_domain, parsize_bands = create_parsize_minbands(self.nbands, world.size) parsize_domain, parsize_bands = 1, 1 #XXX assert self.nbands % np.prod(parsize_bands) == 0 domain_comm, kpt_comm, band_comm = distribute_cpus(parsize_domain, parsize_bands, self.nspins, self.nibzkpts) # Set up band descriptor: self.bd = BandDescriptor(self.nbands, band_comm, p.parallel['stridebands']) # Set up grid descriptor: res, ngpts = shapeopt(300, self.G**3, 3, 0.2) cell_c = self.h * np.array(ngpts) pbc_c = (True, False, True) self.gd = GridDescriptor(ngpts, cell_c, pbc_c, domain_comm, parsize_domain) # Create randomized gas-like atomic configuration self.atoms = create_random_atoms(self.gd) # Create setups Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(Z_a, p.setups, p.basis, p.lmax, xc) self.natoms = len(self.setups) # Set up kpoint descriptor: self.kd = KPointDescriptor(bzk_kc, self.nspins) self.kd.set_symmetry(self.atoms, self.setups, usesymm=p.usesymm) self.kd.set_communicator(kpt_comm)
def __init__(self, atoms, kpts, symmetry): """Initialize base class and attributes. Parameters ---------- atoms: Atoms ASE atoms. kpts: tuple or list of tuples Shape of Monkhorst-Pack grid or list of k-points used in the dfpt calculation. symmetry: bool or None Symmetry parameter used in dfpt calculation. """ # Init base class with ``Atoms`` object phonons.Phonons.__init__(atoms) # Create k-point descriptor self.kd = KPointDescriptor(kpts, 1) self.kd.set_symmetry(self.atoms, self.calc.wfs.setups, usesymm=symmetry) # Overwrite ``N_c`` attribute self.N_c = tuple(self.kd.N_c) # Index of the gamma point -- for the acoustic sum-rule self.gamma_index = None if self.kd.gamma: self.gamma_index = 0 self.dtype == float else: self.dtype == comples for k, k_c in enumerate(self.kd.ibzk_kc): if np.all(k_c == 0.): self.gamma_index = k assert self.gamma_index is not None
class G0W0(PairDensity): def __init__(self, calc, filename='gw', kpts=None, bands=None, nbands=None, ppa=False, wstc=False, ecut=150.0, eta=0.1, E0=1.0 * Hartree, domega0=0.025, omega2=10.0, world=mpi.world): PairDensity.__init__(self, calc, ecut, world=world, txt=filename + '.txt') self.filename = filename ecut /= Hartree self.ppa = ppa self.wstc = wstc self.eta = eta / Hartree self.E0 = E0 / Hartree self.domega0 = domega0 / Hartree self.omega2 = omega2 / Hartree print(' ___ _ _ _ ', file=self.fd) print(' | || | | |', file=self.fd) print(' | | || | | |', file=self.fd) print(' |__ ||_____|', file=self.fd) print(' |___| ', file=self.fd) print(file=self.fd) self.kpts = select_kpts(kpts, self.calc) if bands is None: bands = [0, self.nocc2] self.bands = bands b1, b2 = bands self.shape = shape = (self.calc.wfs.nspins, len(self.kpts), b2 - b1) self.eps_sin = np.empty(shape) # KS-eigenvalues self.f_sin = np.empty(shape) # occupation numbers self.sigma_sin = np.zeros(shape) # self-energies self.dsigma_sin = np.zeros(shape) # derivatives of self-energies self.vxc_sin = None # KS XC-contributions self.exx_sin = None # exact exchange contributions self.Z_sin = None # renormalization factors if nbands is None: nbands = int(self.vol * ecut**1.5 * 2**0.5 / 3 / pi**2) self.nbands = nbands kd = self.calc.wfs.kd self.mysKn1n2 = None # my (s, K, n1, n2) indices self.distribute_k_points_and_bands(b1, b2, kd.ibz2bz_k[self.kpts]) # Find q-vectors and weights in the IBZ: assert -1 not in kd.bz2bz_ks offset_c = 0.5 * ((kd.N_c + 1) % 2) / kd.N_c bzq_qc = monkhorst_pack(kd.N_c) + offset_c self.qd = KPointDescriptor(bzq_qc) self.qd.set_symmetry(self.calc.atoms, self.calc.wfs.setups, usesymm=self.calc.input_parameters.usesymm, N_c=self.calc.wfs.gd.N_c) assert self.calc.wfs.nspins == 1 @timer('G0W0') def calculate(self): kd = self.calc.wfs.kd self.calculate_ks_xc_contribution() self.calculate_exact_exchange() # Get KS eigenvalues and occupation numbers: b1, b2 = self.bands for i, k in enumerate(self.kpts): kpt = self.calc.wfs.kpt_u[k] self.eps_sin[0, i] = kpt.eps_n[b1:b2] self.f_sin[0, i] = kpt.f_n[b1:b2] / kpt.weight # My part of the states we want to calculate QP-energies for: mykpts = [self.get_k_point(s, K, n1, n2) for s, K, n1, n2 in self.mysKn1n2] # Loop over q in the IBZ: for pd0, W0, q_c in self.calculate_screened_potential(): for kpt1 in mykpts: K2 = kd.find_k_plus_q(q_c, [kpt1.K])[0] kpt2 = self.get_k_point(0, K2, 0, self.nbands) k1 = kd.bz2ibz_k[kpt1.K] i = self.kpts.index(k1) self.calculate_q(i, kpt1, kpt2, pd0, W0) self.world.sum(self.sigma_sin) self.world.sum(self.dsigma_sin) self.Z_sin = 1 / (1 - self.dsigma_sin) self.qp_sin = self.eps_sin + self.Z_sin * (self.sigma_sin + self.exx_sin - self.vxc_sin) results = {'f': self.f_sin, 'eps': self.eps_sin * Hartree, 'vxc': self.vxc_sin * Hartree, 'exx': self.exx_sin * Hartree, 'sigma': self.sigma_sin * Hartree, 'Z': self.Z_sin, 'qp': self.qp_sin * Hartree} self.print_results(results) return results def calculate_q(self, i, kpt1, kpt2, pd0, W0): wfs = self.calc.wfs N_c = pd0.gd.N_c i_cG = self.sign * np.dot(self.U_cc, np.unravel_index(pd0.Q_qG[0], N_c)) q_c = wfs.kd.bzk_kc[kpt2.K] - wfs.kd.bzk_kc[kpt1.K] q0 = np.allclose(q_c, 0) shift0_c = q_c - self.sign * np.dot(self.U_cc, pd0.kd.bzk_kc[0]) assert np.allclose(shift0_c.round(), shift0_c) shift0_c = shift0_c.round().astype(int) shift_c = kpt1.shift_c - kpt2.shift_c - shift0_c I_G = np.ravel_multi_index(i_cG + shift_c[:, None], N_c, 'wrap') G_Gv = pd0.G_Qv[pd0.Q_qG[0]] + pd0.K_qv[0] pos_av = np.dot(self.spos_ac, pd0.gd.cell_cv) M_vv = np.dot(pd0.gd.cell_cv.T, np.dot(self.U_cc.T, np.linalg.inv(pd0.gd.cell_cv).T)) Q_aGii = [] for a, Q_Gii in enumerate(self.Q_aGii): x_G = np.exp(1j * np.dot(G_Gv, (pos_av[a] - self.sign * np.dot(M_vv, pos_av[a])))) U_ii = self.calc.wfs.setups[a].R_sii[self.s] Q_Gii = np.dot(np.dot(U_ii, Q_Gii * x_G[:, None, None]), U_ii.T).transpose(1, 0, 2) Q_aGii.append(Q_Gii) if debug: self.check(i_cG, shift0_c, N_c, q_c, Q_aGii) if self.ppa: calculate_sigma = self.calculate_sigma_ppa else: calculate_sigma = self.calculate_sigma for n in range(kpt1.n2 - kpt1.n1): ut1cc_R = kpt1.ut_nR[n].conj() eps1 = kpt1.eps_n[n] C1_aGi = [np.dot(Q_Gii, P1_ni[n].conj()) for Q_Gii, P1_ni in zip(Q_aGii, kpt1.P_ani)] n_mG = self.calculate_pair_densities(ut1cc_R, C1_aGi, kpt2, pd0, I_G) if self.sign == 1: n_mG = n_mG.conj() if q0: n_mG[:, 0] = 0 m = n + kpt1.n1 - kpt2.n1 if 0 <= m < len(n_mG): n_mG[m, 0] = 1.0 f_m = kpt2.f_n deps_m = eps1 - kpt2.eps_n sigma, dsigma = calculate_sigma(n_mG, deps_m, f_m, W0) nn = kpt1.n1 + n - self.bands[0] self.sigma_sin[kpt1.s, i, nn] += sigma self.dsigma_sin[kpt1.s, i, nn] += dsigma def check(self, i_cG, shift0_c, N_c, q_c, Q_aGii): I0_G = np.ravel_multi_index(i_cG - shift0_c[:, None], N_c, 'wrap') qd1 = KPointDescriptor([q_c]) pd1 = PWDescriptor(self.ecut, self.calc.wfs.gd, complex, qd1) G_I = np.empty(N_c.prod(), int) G_I[:] = -1 I1_G = pd1.Q_qG[0] G_I[I1_G] = np.arange(len(I0_G)) G_G = G_I[I0_G] assert len(I0_G) == len(I1_G) assert (G_G >= 0).all() for a, Q_Gii in enumerate(self.initialize_paw_corrections(pd1)): e = abs(Q_aGii[a] - Q_Gii[G_G]).max() assert e < 1e-12 @timer('Sigma') def calculate_sigma(self, n_mG, deps_m, f_m, C_swGG): o_m = abs(deps_m) # Add small number to avoid zeros for degenerate states: sgn_m = np.sign(deps_m + 1e-15) # Pick +i*eta or -i*eta: s_m = (1 + sgn_m * np.sign(0.5 - f_m)).astype(int) // 2 beta = (2**0.5 - 1) * self.domega0 / self.omega2 w_m = (o_m / (self.domega0 + beta * o_m)).astype(int) o1_m = self.omega_w[w_m] o2_m = self.omega_w[w_m + 1] x = 1.0 / (self.qd.nbzkpts * 2 * pi * self.vol) sigma = 0.0 dsigma = 0.0 for o, o1, o2, sgn, s, w, n_G in zip(o_m, o1_m, o2_m, sgn_m, s_m, w_m, n_mG): C1_GG = C_swGG[s][w] C2_GG = C_swGG[s][w + 1] p = x * sgn sigma1 = p * np.dot(np.dot(n_G, C1_GG), n_G.conj()).imag sigma2 = p * np.dot(np.dot(n_G, C2_GG), n_G.conj()).imag sigma += ((o - o1) * sigma2 + (o2 - o) * sigma1) / (o2 - o1) dsigma += sgn * (sigma2 - sigma1) / (o2 - o1) return sigma, dsigma @timer('W') def calculate_screened_potential(self): print('Calulating screened Coulomb potential', file=self.fd) if self.ppa: print('Using Godby-Needs plasmon-pole approximation:', file=self.fd) print(' Fitting energy: i*E0, E0 = %.3f Hartee' % self.E0, file=self.fd) # use small imaginary frequency to avoid dividing by zero: frequencies = [1e-10j, 1j * self.E0 * Hartree] parameters = {'eta': 0, 'hilbert': False, 'timeordered': False, 'frequencies': frequencies} else: parameters = {'eta': self.eta * Hartree, 'hilbert': True, 'timeordered': True, 'domega0': self.domega0 * Hartree, 'omega2': self.omega2 * Hartree} chi0 = Chi0(self.calc, nbands=self.nbands, ecut=self.ecut * Hartree, intraband=False, real_space_derivatives=False, txt=self.filename + '.w.txt', timer=self.timer, **parameters) self.omega_w = chi0.omega_w self.omegamax = chi0.omegamax htp = HilbertTransform(self.omega_w, self.eta, gw=True) htm = HilbertTransform(self.omega_w, -self.eta, gw=True) for iq, q_c in enumerate(self.qd.ibzk_kc): if self.wstc: wstc = WignerSeitzTruncatedCoulomb( self.calc.wfs.gd.cell_cv, self.calc.wfs.kd.N_c, chi0.fd) else: wstc = None pd, chi0_wGG = chi0.calculate(q_c)[:2] self.Q_aGii = chi0.Q_aGii W = self.calculate_w(pd, chi0_wGG, q_c, htp, htm, wstc) Q1 = self.qd.ibz2bz_k[iq] done = set() for s, Q2 in enumerate(self.qd.bz2bz_ks[Q1]): if Q2 >= 0 and Q2 not in done: s = self.qd.sym_k[Q2] self.s = s self.U_cc = self.qd.symmetry.op_scc[s] time_reversal = self.qd.time_reversal_k[Q2] self.sign = 1 - 2 * time_reversal Q_c = self.qd.bzk_kc[Q2] d_c = self.sign * np.dot(self.U_cc, q_c) - Q_c assert np.allclose(d_c.round(), d_c) yield pd, W, Q_c done.add(Q2) @timer('WW') def calculate_w(self, pd, chi0_wGG, q_c, htp, htm, wstc): if self.wstc: iG_G = (wstc.get_potential(pd) / (4 * pi))**0.5 if np.allclose(q_c, 0): chi0_wGG[:, 0] = 0.0 chi0_wGG[:, :, 0] = 0.0 G0inv = 0.0 G20inv = 0.0 else: G0inv = None G20inv = None else: if np.allclose(q_c, 0): dq3 = (2 * pi)**3 / (self.qd.nbzkpts * self.vol) qc = (dq3 / 4 / pi * 3)**(1 / 3) G0inv = 2 * pi * qc**2 / dq3 G20inv = 4 * pi * qc / dq3 G_G = pd.G2_qG[0]**0.5 G_G[0] = 1 iG_G = 1 / G_G else: iG_G = pd.G2_qG[0]**-0.5 G0inv = None G20inv = None delta_GG = np.eye(len(iG_G)) if self.ppa: return self.ppa_w(chi0_wGG, iG_G, delta_GG, G0inv, G20inv, q_c) self.timer.start('Dyson eq.') # Calculate W and store it in chi0_wGG ndarray: for chi0_GG in chi0_wGG: e_GG = (delta_GG - 4 * pi * chi0_GG * iG_G * iG_G[:, np.newaxis]) W_GG = chi0_GG W_GG[:] = 4 * pi * (np.linalg.inv(e_GG) - delta_GG) * iG_G * iG_G[:, np.newaxis] if np.allclose(q_c, 0): W_GG[0, 0] *= G20inv W_GG[1:, 0] *= G0inv W_GG[0, 1:] *= G0inv Wp_wGG = chi0_wGG.copy() Wm_wGG = chi0_wGG with self.timer('Hilbert transform'): htp(Wp_wGG) htm(Wm_wGG) self.timer.stop('Dyson eq.') return [Wp_wGG, Wm_wGG] @timer('Kohn-Sham XC-contribution') def calculate_ks_xc_contribution(self): name = self.filename + '.vxc.npy' fd = opencew(name) if fd is None: print('Reading Kohn-Sham XC contribution from file:', name, file=self.fd) with open(name) as fd: self.vxc_sin = np.load(fd) assert self.vxc_sin.shape == self.shape, self.vxc_sin.shape return print('Calculating Kohn-Sham XC contribution', file=self.fd) vxc_skn = vxc(self.calc, self.calc.hamiltonian.xc) / Hartree n1, n2 = self.bands self.vxc_sin = vxc_skn[:, self.kpts, n1:n2] np.save(fd, self.vxc_sin) @timer('EXX') def calculate_exact_exchange(self): name = self.filename + '.exx.npy' fd = opencew(name) if fd is None: print('Reading EXX contribution from file:', name, file=self.fd) with open(name) as fd: self.exx_sin = np.load(fd) assert self.exx_sin.shape == self.shape, self.exx_sin.shape return print('Calculating EXX contribution', file=self.fd) exx = EXX(self.calc, kpts=self.kpts, bands=self.bands, txt=self.filename + '.exx.txt', timer=self.timer) exx.calculate() self.exx_sin = exx.get_eigenvalue_contributions() / Hartree np.save(fd, self.exx_sin) def print_results(self, results): description = ['f: Occupation numbers', 'eps: KS-eigenvalues [eV]', 'vxc: KS vxc [eV]', 'exx: Exact exchange [eV]', 'sigma: Self-energies [eV]', 'Z: Renormalization factors', 'qp: QP-energies [eV]'] print('\nResults:', file=self.fd) for line in description: print(line, file=self.fd) b1, b2 = self.bands names = [line.split(':', 1)[0] for line in description] ibzk_kc = self.calc.wfs.kd.ibzk_kc for s in range(self.calc.wfs.nspins): for i, ik in enumerate(self.kpts): print('\nk-point ' + '{0} ({1}): ({2:.3f}, {3:.3f}, {4:.3f})'.format( i, ik, *ibzk_kc[ik]), file=self.fd) print('band' + ''.join('{0:>8}'.format(name) for name in names), file=self.fd) for n in range(b2 - b1): print('{0:4}'.format(n + b1) + ''.join('{0:8.3f}'.format(results[name][s, i, n]) for name in names), file=self.fd) self.timer.write(self.fd) @timer('PPA') def ppa_w(self, chi0_wGG, iG_G, delta_GG, G0inv, G20inv, q_c): einv_wGG = [] for chi0_GG in chi0_wGG: e_GG = (delta_GG - 4 * pi * chi0_GG * iG_G * iG_G[:, np.newaxis]) einv_wGG.append(np.linalg.inv(e_GG) - delta_GG) if self.wstc and np.allclose(q_c, 0): einv_wGG[0][0] = 42 einv_wGG[0][:, 0] = 42 omegat_GG = self.E0 * np.sqrt(einv_wGG[1] / (einv_wGG[0] - einv_wGG[1])) R_GG = -0.5 * omegat_GG * einv_wGG[0] W_GG = 4 * pi**2 * R_GG * iG_G * iG_G[:, np.newaxis] if np.allclose(q_c, 0): W_GG[0, 0] *= G20inv W_GG[1:, 0] *= G0inv W_GG[0, 1:] *= G0inv return [W_GG, omegat_GG] @timer('PPA-Sigma') def calculate_sigma_ppa(self, n_mG, deps_m, f_m, W): W_GG, omegat_GG = W deps_mGG = deps_m[:, np.newaxis, np.newaxis] sign_mGG = 2 * f_m[:, np.newaxis, np.newaxis] - 1 x1_mGG = 1 / (deps_mGG + omegat_GG - 1j * self.eta) x2_mGG = 1 / (deps_mGG - omegat_GG + 1j * self.eta) x3_mGG = 1 / (deps_mGG + omegat_GG - 1j * self.eta * sign_mGG) x4_mGG = 1 / (deps_mGG - omegat_GG - 1j * self.eta * sign_mGG) x_mGG = W_GG * (sign_mGG * (x1_mGG - x2_mGG) + x3_mGG + x4_mGG) dx_mGG = W_GG * (sign_mGG * (x1_mGG**2 - x2_mGG**2) + x3_mGG**2 + x4_mGG**2) sigma = 0.0 dsigma = 0.0 for m in range(np.shape(n_mG)[0]): nW_mG = np.dot(n_mG[m], x_mGG[m]) sigma += np.vdot(n_mG[m], nW_mG).real nW_mG = np.dot(n_mG[m], dx_mGG[m]) dsigma -= np.vdot(n_mG[m], nW_mG).real x = 1 / (self.qd.nbzkpts * 2 * pi * self.vol) return x * sigma, x * dsigma
class UTKPointParallelSetup(TestCase): """ Setup a simple kpoint parallel calculation.""" # Number of bands nbands = 1 # Spin-polarized nspins = 1 # Mean spacing and number of grid points per axis (G x G x G) h = 0.25 / Bohr G = 48 ## Symmetry-reduction of k-points TODO #symmetry = p.usesymm #XXX 'None' is an allowed value!!! # Whether spin/k-points are equally distributed (determines nibzkpts) equipartition = None nibzkpts = None gamma = False # can't be gamma point when nibzkpts > 1 ... dtype = complex #XXX virtual so far.. # ================================= def setUp(self): for virtvar in ['equipartition']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar kpts = {'even' : (12,1,2), \ 'prime': (23,1,1)}[self.equipartition] #primes = [i for i in xrange(50,1,-1) if ~np.any(i%np.arange(2,i)==0)] bzk_kc = kpts2ndarray(kpts) assert p.usesymm == None self.nibzkpts = len(bzk_kc) #parsize_domain, parsize_bands = create_parsize_minbands(self.nbands, world.size) parsize_domain, parsize_bands = 1, 1 #XXX assert self.nbands % np.prod(parsize_bands) == 0 domain_comm, kpt_comm, band_comm = distribute_cpus(parsize_domain, parsize_bands, self.nspins, self.nibzkpts) # Set up band descriptor: self.bd = BandDescriptor(self.nbands, band_comm, p.parallel['stridebands']) # Set up grid descriptor: res, ngpts = shapeopt(300, self.G**3, 3, 0.2) cell_c = self.h * np.array(ngpts) pbc_c = (True, False, True) self.gd = GridDescriptor(ngpts, cell_c, pbc_c, domain_comm, parsize_domain) # Create randomized gas-like atomic configuration self.atoms = create_random_atoms(self.gd) # Create setups Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(Z_a, p.setups, p.basis, p.lmax, xc) self.natoms = len(self.setups) # Set up kpoint descriptor: self.kd = KPointDescriptor(bzk_kc, self.nspins) self.kd.set_symmetry(self.atoms, self.setups, usesymm=p.usesymm) self.kd.set_communicator(kpt_comm) def tearDown(self): del self.bd, self.gd, self.kd del self.setups, self.atoms def get_parsizes(self): #XXX NO LONGER IN UT_HSOPS?!? # Careful, overwriting imported GPAW params may cause amnesia in Python. from gpaw import parsize_domain, parsize_bands # Choose the largest possible parallelization over kpoint/spins test_parsize_ks_pairs = gcd(self.nspins*self.nibzkpts, world.size) remsize = world.size//test_parsize_ks_pairs # If parsize_bands is not set, choose the largest possible test_parsize_bands = parsize_bands or gcd(self.nbands, remsize) # If parsize_bands is not set, choose as few domains as possible test_parsize_domain = parsize_domain or (remsize//test_parsize_bands) return test_parsize_domain, test_parsize_bands # ================================= def verify_comm_sizes(self): #TODO needs work if world.size == 1: return comm_sizes = tuple([comm.size for comm in [world, self.bd.comm, \ self.gd.comm, self.kd.comm]]) self._parinfo = '%d world, %d band, %d domain, %d kpt' % comm_sizes #self.assertEqual((self.nspins*self.nibzkpts) % self.kd.comm.size, 0) #XXX def verify_slice_consistency(self): for kpt_rank in range(self.kd.comm.size): uslice = self.kd.get_slice(kpt_rank) myus = np.arange(*uslice.indices(self.kd.nks)) for myu,u in enumerate(myus): self.assertEqual(self.kd.who_has(u), (kpt_rank, myu)) def verify_combination_consistency(self): for u in range(self.kd.nks): s, k = self.kd.what_is(u) self.assertEqual(self.kd.where_is(s, k), u) for s in range(self.kd.nspins): for k in range(self.kd.nibzkpts): u = self.kd.where_is(s, k) self.assertEqual(self.kd.what_is(u), (s,k,)) def verify_indexing_consistency(self): for u in range(self.kd.nks): kpt_rank, myu = self.kd.who_has(u) self.assertEqual(self.kd.global_index(myu, kpt_rank), u) for kpt_rank in range(self.kd.comm.size): for myu in range(self.kd.get_count(kpt_rank)): u = self.kd.global_index(myu, kpt_rank) self.assertEqual(self.kd.who_has(u), (kpt_rank, myu)) def verify_ranking_consistency(self): ranks = self.kd.get_ranks() for kpt_rank in range(self.kd.comm.size): my_indices = self.kd.get_indices(kpt_rank) matches = np.argwhere(ranks == kpt_rank).ravel() self.assertTrue((matches == my_indices).all()) for myu in range(self.kd.get_count(kpt_rank)): u = self.kd.global_index(myu, kpt_rank) self.assertEqual(my_indices[myu], u)
nb = 6 a = Atoms('H', cell=(3 * np.eye(3)), pbc=True) calc = GPAW(mode=PW(600), kpts=[[0, 0, 0], [0.25, 0, 0]]) a.calc = calc a.get_potential_energy() calc.diagonalize_full_hamiltonian(nbands=nb, expert=True) calc.write('a.gpw', 'all') pair = PairDensity('a.gpw', ecut=100) # Check continuity eq. for q_c in [[0, 0, 0], [1. / 4, 0, 0]]: ol = np.allclose(q_c, 0.0) qd = KPointDescriptor([q_c]) pd = PWDescriptor(pair.ecut, calc.wfs.gd, complex, qd) kptpair = pair.get_kpoint_pair(pd, 0, [0, 0, 0], 0, nb, 0, nb) deps_nm = kptpair.get_transition_energies(np.arange(0, nb), np.arange(0, nb)) n_nmG = pair.get_pair_density(pd, kptpair, np.arange(0, nb), np.arange(0, nb), optical_limit=ol) n_nmvG = pair.get_pair_momentum(pd, kptpair, np.arange(0, nb), np.arange(0, nb)) if ol:
class PhononCalculator: """This class defines the interface for phonon calculations.""" def __init__(self, calc, gamma=True, symmetry=False, e_ph=False, communicator=serial_comm): """Inititialize class with a list of atoms. The atoms object must contain a converged ground-state calculation. The set of q-vectors in which the dynamical matrix will be calculated is determined from the ``symmetry`` kwarg. For now, only time-reversal symmetry is used to generate the irrecducible BZ. Add a little note on parallelization strategy here. Parameters ---------- calc: str or Calculator Calculator containing a ground-state calculation. gamma: bool Gamma-point calculation with respect to the q-vector of the dynamical matrix. When ``False``, the Monkhorst-Pack grid from the ground-state calculation is used. symmetry: bool Use symmetries to reduce the q-vectors of the dynamcial matrix (None, False or True). The different options are equivalent to the options in a ground-state calculation. e_ph: bool Save the derivative of the effective potential. communicator: Communicator Communicator for parallelization over k-points and real-space domain. """ # XXX assert symmetry in [None, False], "Spatial symmetries not allowed yet" self.symmetry = symmetry if isinstance(calc, str): self.calc = GPAW(calc, communicator=serial_comm, txt=None) else: self.calc = calc # Make sure localized functions are initialized self.calc.set_positions() # Note that this under some circumstances (e.g. when called twice) # allocates a new array for the P_ani coefficients !! # Store useful objects self.atoms = self.calc.get_atoms() # Get rid of ``calc`` attribute self.atoms.calc = None # Boundary conditions pbc_c = self.calc.atoms.get_pbc() if np.all(pbc_c == False): self.gamma = True self.dtype = float kpts = None # Multigrid Poisson solver poisson_solver = PoissonSolver() else: if gamma: self.gamma = True self.dtype = float kpts = None else: self.gamma = False self.dtype = complex # Get k-points from ground-state calculation kpts = self.calc.input_parameters.kpts # FFT Poisson solver poisson_solver = FFTPoissonSolver(dtype=self.dtype) # K-point descriptor for the q-vectors of the dynamical matrix # Note, no explicit parallelization here. self.kd = KPointDescriptor(kpts, 1) self.kd.set_symmetry(self.atoms, self.calc.wfs.setups, usesymm=symmetry) self.kd.set_communicator(serial_comm) # Number of occupied bands nvalence = self.calc.wfs.nvalence nbands = nvalence / 2 + nvalence % 2 assert nbands <= self.calc.wfs.bd.nbands # Extract other useful objects # Ground-state k-point descriptor - used for the k-points in the # ResponseCalculator # XXX replace communicators when ready to parallelize kd_gs = self.calc.wfs.kd gd = self.calc.density.gd kpt_u = self.calc.wfs.kpt_u setups = self.calc.wfs.setups dtype_gs = self.calc.wfs.dtype # WaveFunctions wfs = WaveFunctions(nbands, kpt_u, setups, kd_gs, gd, dtype=dtype_gs) # Linear response calculator self.response_calc = ResponseCalculator(self.calc, wfs, dtype=self.dtype) # Phonon perturbation self.perturbation = PhononPerturbation(self.calc, self.kd, poisson_solver, dtype=self.dtype) # Dynamical matrix self.dyn = DynamicalMatrix(self.atoms, self.kd, dtype=self.dtype) # Electron-phonon couplings if e_ph: self.e_ph = ElectronPhononCoupling(self.atoms, gd, self.kd, dtype=self.dtype) else: self.e_ph = None # Initialization flag self.initialized = False # Parallel communicator for parallelization over kpts and domain self.comm = communicator def initialize(self): """Initialize response calculator and perturbation.""" # Get scaled atomic positions spos_ac = self.atoms.get_scaled_positions() self.perturbation.initialize(spos_ac) self.response_calc.initialize(spos_ac) self.initialized = True def __getstate__(self): """Method used when pickling. Bound method attributes cannot be pickled and must therefore be deleted before an instance is dumped to file. """ # Get state of object and take care of troublesome attributes state = dict(self.__dict__) state['kd'].__dict__['comm'] = serial_comm state.pop('calc') state.pop('perturbation') state.pop('response_calc') return state def run(self, qpts_q=None, clean=False, name=None, path=None): """Run calculation for atomic displacements and update matrix. Parameters ---------- qpts: List List of q-points indices for which the dynamical matrix will be calculated (only temporary). """ if not self.initialized: self.initialize() if self.gamma: qpts_q = [0] elif qpts_q is None: qpts_q = range(self.kd.nibzkpts) else: assert isinstance(qpts_q, list) # Update name and path attributes self.set_name_and_path(name=name, path=path) # Get string template for filenames filename_str = self.get_filename_string() # Delay the ranks belonging to the same k-point/domain decomposition # equally time.sleep(rank // self.comm.size) # XXX Make a single ground_state_contributions member function # Ground-state contributions to the force constants self.dyn.density_ground_state(self.calc) # self.dyn.wfs_ground_state(self.calc, self.response_calc) # Calculate linear response wrt q-vectors and displacements of atoms for q in qpts_q: if not self.gamma: self.perturbation.set_q(q) # First-order contributions to the force constants for a in self.dyn.indices: for v in [0, 1, 2]: # Check if the calculation has already been done filename = filename_str % (q, a, v) # Wait for all sub-ranks to enter self.comm.barrier() if os.path.isfile(os.path.join(self.path, filename)): continue if self.comm.rank == 0: fd = open(os.path.join(self.path, filename), 'w') # Wait for all sub-ranks here self.comm.barrier() components = ['x', 'y', 'z'] symbols = self.atoms.get_chemical_symbols() print "q-vector index: %i" % q print "Atom index: %i" % a print "Atomic symbol: %s" % symbols[a] print "Component: %s" % components[v] # Set atom and cartesian component of perturbation self.perturbation.set_av(a, v) # Calculate linear response self.response_calc(self.perturbation) # Calculate row of the matrix of force constants self.dyn.calculate_row(self.perturbation, self.response_calc) # Write force constants to file if self.comm.rank == 0: self.dyn.write(fd, q, a, v) fd.close() # Store effective potential derivative if self.e_ph is not None: v1_eff_G = self.perturbation.v1_G + \ self.response_calc.vHXC1_G self.e_ph.v1_eff_qavG.append(v1_eff_G) # Wait for the file-writing rank here self.comm.barrier() # XXX # Check that all files are valid and collect in a single file # Remove the files if clean: self.clean() def get_atoms(self): """Return atoms.""" return self.atoms def get_dynamical_matrix(self): """Return reference to ``dyn`` attribute.""" return self.dyn def get_filename_string(self): """Return string template for force constant filenames.""" name_str = (self.name + '.' + 'q_%%0%ii_' % len(str(self.kd.nibzkpts)) + 'a_%%0%ii_' % len(str(len(self.atoms))) + 'v_%i' + '.pckl') return name_str def set_atoms(self, atoms): """Set atoms to be included in the calculation. Parameters ---------- atoms: list Can be either a list of strings, ints or ... """ assert isinstance(atoms, list) if isinstance(atoms[0], str): assert np.all([isinstance(atom, str) for atom in atoms]) sym_a = self.atoms.get_chemical_symbols() # List for atomic indices indices = [] for type in atoms: indices.extend([a for a, atom in enumerate(sym_a) if atom == type]) else: assert np.all([isinstance(atom, int) for atom in atoms]) indices = atoms self.dyn.set_indices(indices) def set_name_and_path(self, name=None, path=None): """Set name and path of the force constant files. name: str Base name for the files which the elements of the matrix of force constants will be written to. path: str Path specifying the directory where the files will be dumped. """ if name is None: self.name = 'phonon.' + self.atoms.get_chemical_formula() else: self.name = name # self.name += '.nibzkpts_%i' % self.kd.nibzkpts if path is None: self.path = '.' else: self.path = path # Set corresponding attributes in the ``dyn`` attribute filename_str = self.get_filename_string() self.dyn.set_name_and_path(filename_str, self.path) def clean(self): """Delete generated files.""" filename_str = self.get_filename_string() for q in range(self.kd.nibzkpts): for a in range(len(self.atoms)): for v in [0, 1, 2]: filename = filename_str % (q, a, v) if os.path.isfile(os.path.join(self.path, filename)): os.remove(filename) def band_structure(self, path_kc, modes=False, acoustic=True): """Calculate phonon dispersion along a path in the Brillouin zone. The dynamical matrix at arbitrary q-vectors is obtained by Fourier transforming the real-space matrix. In case of negative eigenvalues (squared frequency), the corresponding negative frequency is returned. Parameters ---------- path_kc: ndarray List of k-point coordinates (in units of the reciprocal lattice vectors) specifying the path in the Brillouin zone for which the dynamical matrix will be calculated. modes: bool Returns both frequencies and modes (mass scaled) when True. acoustic: bool Restore the acoustic sum-rule in the calculated force constants. """ for k_c in path_kc: assert np.all(np.asarray(k_c) <= 1.0), \ "Scaled coordinates must be given" # Assemble the dynanical matrix from calculated force constants self.dyn.assemble(acoustic=acoustic) # Get the dynamical matrix in real-space DR_lmn, R_clmn = self.dyn.real_space() # Reshape for the evaluation of the fourier sums shape = DR_lmn.shape DR_m = DR_lmn.reshape((-1,) + shape[-2:]) R_cm = R_clmn.reshape((3, -1)) # Lists for frequencies and modes along path omega_kn = [] u_kn = [] # Number of atoms included N = len(self.dyn.get_indices()) # Mass prefactor for the normal modes m_inv_av = self.dyn.get_mass_array() for q_c in path_kc: # Evaluate fourier transform phase_m = np.exp(-2.j * pi * np.dot(q_c, R_cm)) # Dynamical matrix in unit of Ha / Bohr**2 / amu D_q = np.sum(phase_m[:, np.newaxis, np.newaxis] * DR_m, axis=0) if modes: omega2_n, u_avn = la.eigh(D_q, UPLO='L') # Sort eigenmodes according to eigenvalues (see below) and # multiply with mass prefactor u_nav = u_avn[:, omega2_n.argsort()].T.copy() * m_inv_av # Multiply with mass prefactor u_kn.append(u_nav.reshape((3*N, -1, 3))) else: omega2_n = la.eigvalsh(D_q, UPLO='L') # Sort eigenvalues in increasing order omega2_n.sort() # Use dtype=complex to handle negative eigenvalues omega_n = np.sqrt(omega2_n.astype(complex)) # Take care of imaginary frequencies if not np.all(omega2_n >= 0.): indices = np.where(omega2_n < 0)[0] print ("WARNING, %i imaginary frequencies at " "q = (% 5.2f, % 5.2f, % 5.2f) ; (omega_q =% 5.3e*i)" % (len(indices), q_c[0], q_c[1], q_c[2], omega_n[indices][0].imag)) omega_n[indices] = -1 * np.sqrt(np.abs(omega2_n[indices].real)) omega_kn.append(omega_n.real) # Conversion factor from sqrt(Ha / Bohr**2 / amu) -> eV s = units.Hartree**0.5 * units._hbar * 1.e10 / \ (units._e * units._amu)**(0.5) / units.Bohr # Convert to eV and Ang omega_kn = s * np.asarray(omega_kn) if modes: u_kn = np.asarray(u_kn) * units.Bohr return omega_kn, u_kn return omega_kn def write_modes(self, q_c, branches=0, kT=units.kB*300, repeat=(1, 1, 1), nimages=30, acoustic=True): """Write mode to trajectory file. The classical equipartioning theorem states that each normal mode has an average energy:: <E> = 1/2 * k_B * T = 1/2 * omega^2 * Q^2 => Q = sqrt(k_B*T) / omega at temperature T. Here, Q denotes the normal coordinate of the mode. Parameters ---------- q_c: ndarray q-vector of the modes. branches: int or list Branch index of calculated modes. kT: float Temperature in units of eV. Determines the amplitude of the atomic displacements in the modes. repeat: tuple Repeat atoms (l, m, n) times in the directions of the lattice vectors. Displacements of atoms in repeated cells carry a Bloch phase factor given by the q-vector and the cell lattice vector R_m. nimages: int Number of images in an oscillation. """ if isinstance(branches, int): branch_n = [branches] else: branch_n = list(branches) # Calculate modes omega_n, u_n = self.band_structure([q_c], modes=True, acoustic=acoustic) # Repeat atoms atoms = self.atoms * repeat pos_mav = atoms.positions.copy() # Total number of unit cells M = np.prod(repeat) # Corresponding lattice vectors R_m R_cm = np.indices(repeat[::-1]).reshape(3, -1)[::-1] # Bloch phase phase_m = np.exp(2.j * pi * np.dot(q_c, R_cm)) phase_ma = phase_m.repeat(len(self.atoms)) for n in branch_n: omega = omega_n[0, n] u_av = u_n[0, n] # .reshape((-1, 3)) # Mean displacement at high T ? u_av *= sqrt(kT / abs(omega)) mode_av = np.zeros((len(self.atoms), 3), dtype=self.dtype) indices = self.dyn.get_indices() mode_av[indices] = u_av mode_mav = (np.vstack([mode_av]*M) * phase_ma[:, np.newaxis]).real traj = PickleTrajectory('%s.mode.%d.traj' % (self.name, n), 'w') for x in np.linspace(0, 2*pi, nimages, endpoint=False): # XXX Is it correct to take out the sine component here ? atoms.set_positions(pos_mav + sin(x) * mode_mav) traj.write(atoms) traj.close()
def initialize(self, density, hamiltonian, wfs, occupations): self.xc.initialize(density, hamiltonian, wfs, occupations) self.nspins = wfs.nspins self.setups = wfs.setups self.density = density self.kpt_u = wfs.kpt_u self.gd = density.gd self.kd = wfs.kd self.bd = wfs.bd N_c = self.gd.N_c N = self.gd.N_c.prod() vol = self.gd.dv * N if self.alpha is None: self.alpha = 6 * vol**(2 / 3.0) / pi**2 self.gamma = (vol / (2 * pi)**2 * sqrt(pi / self.alpha) * self.kd.nbzkpts) ecut = 0.5 * pi**2 / (self.gd.h_cv**2).sum(1).max() if self.kd.N_c is None: self.bzk_kc = np.zeros((1, 3)) dfghdfgh else: n = self.kd.N_c * 2 - 1 bzk_kc = np.indices(n).transpose((1, 2, 3, 0)) bzk_kc.shape = (-1, 3) bzk_kc -= self.kd.N_c - 1 self.bzk_kc = bzk_kc.astype(float) / self.kd.N_c self.pwd = PWDescriptor(ecut, self.gd, self.bzk_kc) n = 0 for k_c, Gpk2_G in zip(self.bzk_kc[:], self.pwd.G2_qG): if (k_c > -0.5).all() and (k_c <= 0.5).all(): #XXX??? if k_c.any(): self.gamma -= np.dot(np.exp(-self.alpha * Gpk2_G), Gpk2_G**-1) else: self.gamma -= np.dot(np.exp(-self.alpha * Gpk2_G[1:]), Gpk2_G[1:]**-1) n += 1 assert n == self.kd.N_c.prod() self.ghat = LFC(self.gd, [setup.ghat_l for setup in density.setups], dtype=complex ) self.ghat.set_k_points(self.bzk_kc) self.fullkd = KPointDescriptor(self.kd.bzk_kc, nspins=1) class S: id_a = [] def set_symmetry(self, s): pass self.fullkd.set_symmetry(Atoms(pbc=True), S(), False) self.fullkd.set_communicator(world) self.pt = LFC(self.gd, [setup.pt_j for setup in density.setups], dtype=complex) self.pt.set_k_points(self.fullkd.ibzk_kc) self.interpolator = density.interpolator
class UTGaussianWavefunctionSetup(UTDomainParallelSetup): __doc__ = UTDomainParallelSetup.__doc__ + """ The pseudo wavefunctions are moving gaussians centered around each atom.""" allocated = False dtype = None # Default arguments for scaled Gaussian wave _sigma0 = 2.0 #0.75 _k0_c = 2*np.pi*np.array([1/5., 1/3., 0.]) def setUp(self): UTDomainParallelSetup.setUp(self) for virtvar in ['dtype']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Create randomized atoms self.atoms = create_random_atoms(self.gd, 5) # also tested: 10xNH3/BDA # XXX DEBUG START if False: from ase import view view(self.atoms*(1+2*self.gd.pbc_c)) # XXX DEBUG END # Do we agree on the atomic positions? pos_ac = self.atoms.get_positions() pos_rac = np.empty((world.size,)+pos_ac.shape, pos_ac.dtype) world.all_gather(pos_ac, pos_rac) if (pos_rac-pos_rac[world.rank,...][np.newaxis,...]).any(): raise RuntimeError('Discrepancy in atomic positions detected.') # Create setups for atoms self.Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(self.Z_a, p.setups, p.basis, p.lmax, xc) # K-point descriptor bzk_kc = np.array([[0, 0, 0]], dtype=float) self.kd = KPointDescriptor(bzk_kc, 1) self.kd.set_symmetry(self.atoms, self.setups) self.kd.set_communicator(self.kpt_comm) # Create gamma-point dummy wavefunctions self.wfs = FDWFS(self.gd, self.bd, self.kd, self.setups, self.block_comm, self.dtype) spos_ac = self.atoms.get_scaled_positions() % 1.0 self.wfs.set_positions(spos_ac) self.pt = self.wfs.pt # XXX shortcut ## Also create pseudo partial waveves #from gpaw.lfc import LFC #self.phit = LFC(self.gd, [setup.phit_j for setup in self.setups], \ # self.kpt_comm, dtype=self.dtype) #self.phit.set_positions(spos_ac) self.r_cG = None self.buf_G = None self.psit_nG = None self.allocate() def tearDown(self): UTDomainParallelSetup.tearDown(self) del self.r_cG, self.buf_G, self.psit_nG del self.pt, self.setups, self.atoms self.allocated = False def allocate(self): self.r_cG = self.gd.get_grid_point_coordinates() cell_cv = self.atoms.get_cell() / Bohr assert np.abs(cell_cv-self.gd.cell_cv).max() < 1e-9 center_c = 0.5*cell_cv.diagonal() self.buf_G = self.gd.empty(dtype=self.dtype) self.psit_nG = self.gd.empty(self.bd.mynbands, dtype=self.dtype) for myn,psit_G in enumerate(self.psit_nG): n = self.bd.global_index(myn) psit_G[:] = self.get_scaled_gaussian_wave(center_c, scale=10+2j*n) k_c = 2*np.pi*np.array([1/2., -1/7., 0.]) for pos_c in self.atoms.get_positions() / Bohr: sigma = self._sigma0/(1+np.sum(pos_c**2))**0.5 psit_G += self.get_scaled_gaussian_wave(pos_c, sigma, k_c, n+5j) self.allocated = True def get_scaled_gaussian_wave(self, pos_c, sigma=None, k_c=None, scale=None): if sigma is None: sigma = self._sigma0 if k_c is None: k_c = self._k0_c if scale is None: A = None else: # 4*pi*int(exp(-r^2/(2*w^2))^2*r^2, r=0...infinity)= w^3*pi^(3/2) # = scale/A^2 -> A = scale*(sqrt(Pi)*w)^(-3/2) hence int -> scale^2 A = scale/(sigma*(np.pi)**0.5)**1.5 return gaussian_wave(self.r_cG, pos_c, sigma, k_c, A, self.dtype, self.buf_G) def check_and_plot(self, P_ani, P0_ani, digits, keywords=''): # Collapse into viewable matrices P_In = self.wfs.collect_projections(P_ani) P0_In = self.wfs.collect_projections(P0_ani) # Construct fingerprint of input matrices for comparison fingerprint = np.array([md5_array(P_In, numeric=True), md5_array(P0_In, numeric=True)]) # Compare fingerprints across all processors fingerprints = np.empty((world.size, 2), np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') # If assertion fails, catch temporarily while plotting, then re-raise try: self.assertAlmostEqual(np.abs(P_In-P0_In).max(), 0, digits) except AssertionError: if world.rank == 0 and mpl is not None: from matplotlib.figure import Figure fig = Figure() ax = fig.add_axes([0.0, 0.1, 1.0, 0.83]) ax.set_title(self.__class__.__name__) im = ax.imshow(np.abs(P_In-P0_In), interpolation='nearest') fig.colorbar(im) fig.text(0.5, 0.05, 'Keywords: ' + keywords, \ horizontalalignment='center', verticalalignment='top') from matplotlib.backends.backend_agg import FigureCanvasAgg img = 'ut_invops_%s_%s.png' % (self.__class__.__name__, \ '_'.join(keywords.split(','))) FigureCanvasAgg(fig).print_figure(img.lower(), dpi=90) raise # ================================= def test_projection_linearity(self): kpt = self.wfs.kpt_u[0] Q_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(self.psit_nG, Q_ani, q=kpt.q) for Q_ni in Q_ani.values(): self.assertTrue(Q_ni.dtype == self.dtype) P0_ani = dict([(a,Q_ni.copy()) for a,Q_ni in Q_ani.items()]) self.pt.add(self.psit_nG, Q_ani, q=kpt.q) self.pt.integrate(self.psit_nG, P0_ani, q=kpt.q) #rank_a = self.gd.get_ranks_from_positions(spos_ac) #my_atom_indices = np.argwhere(self.gd.comm.rank == rank_a).ravel() # ~ a ~ a' #TODO XXX should fix PairOverlap-ish stuff for < p | phi > overlaps # i i' #spos_ac = self.pt.spos_ac # NewLFC doesn't have this spos_ac = self.atoms.get_scaled_positions() % 1.0 gpo = GridPairOverlap(self.gd, self.setups) B_aa = gpo.calculate_overlaps(spos_ac, self.pt) # Compare fingerprints across all processors fingerprint = np.array([md5_array(B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') P_ani = dict([(a,Q_ni.copy()) for a,Q_ni in Q_ani.items()]) for a1 in range(len(self.atoms)): if a1 in P_ani.keys(): P_ni = P_ani[a1] else: # Atom a1 is not in domain so allocate a temporary buffer P_ni = np.zeros((self.bd.mynbands,self.setups[a1].ni,), dtype=self.dtype) for a2, Q_ni in Q_ani.items(): # B_aa are the projector overlaps across atomic pairs B_ii = gpo.extract_atomic_pair_matrix(B_aa, a1, a2) P_ni += np.dot(Q_ni, B_ii.T) #sum over a2 and last i in B_ii self.gd.comm.sum(P_ni) self.check_and_plot(P_ani, P0_ani, 8, 'projection,linearity') def test_extrapolate_overlap(self): kpt = self.wfs.kpt_u[0] ppo = ProjectorPairOverlap(self.wfs, self.atoms) # Compare fingerprints across all processors fingerprint = np.array([md5_array(ppo.B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') work_nG = np.empty_like(self.psit_nG) P_ani = ppo.apply(self.psit_nG, work_nG, self.wfs, kpt, \ calculate_P_ani=True, extrapolate_P_ani=True) P0_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(work_nG, P0_ani, kpt.q) del work_nG self.check_and_plot(P_ani, P0_ani, 11, 'extrapolate,overlap') def test_extrapolate_inverse(self): kpt = self.wfs.kpt_u[0] ppo = ProjectorPairOverlap(self.wfs, self.atoms) # Compare fingerprints across all processors fingerprint = np.array([md5_array(ppo.B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') work_nG = np.empty_like(self.psit_nG) P_ani = ppo.apply_inverse(self.psit_nG, work_nG, self.wfs, kpt, \ calculate_P_ani=True, extrapolate_P_ani=True) P0_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(work_nG, P0_ani, kpt.q) del work_nG self.check_and_plot(P_ani, P0_ani, 11, 'extrapolate,inverse') def test_overlap_inverse_after(self): kpt = self.wfs.kpt_u[0] kpt.P_ani = self.pt.dict(self.bd.mynbands) ppo = ProjectorPairOverlap(self.wfs, self.atoms) # Compare fingerprints across all processors fingerprint = np.array([md5_array(ppo.B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') work_nG = np.empty_like(self.psit_nG) self.pt.integrate(self.psit_nG, kpt.P_ani, kpt.q) P0_ani = dict([(a,P_ni.copy()) for a,P_ni in kpt.P_ani.items()]) ppo.apply(self.psit_nG, work_nG, self.wfs, kpt, calculate_P_ani=False) res_nG = np.empty_like(self.psit_nG) ppo.apply_inverse(work_nG, res_nG, self.wfs, kpt, calculate_P_ani=True) del work_nG P_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(res_nG, P_ani, kpt.q) abserr = np.empty(1, dtype=float) for n in range(self.nbands): band_rank, myn = self.bd.who_has(n) if band_rank == self.bd.comm.rank: abserr[:] = np.abs(self.psit_nG[myn] - res_nG[myn]).max() self.gd.comm.max(abserr) self.bd.comm.broadcast(abserr, band_rank) self.assertAlmostEqual(abserr.item(), 0, 10) self.check_and_plot(P_ani, P0_ani, 10, 'overlap,inverse,after') def test_overlap_inverse_before(self): kpt = self.wfs.kpt_u[0] kpt.P_ani = self.pt.dict(self.bd.mynbands) ppo = ProjectorPairOverlap(self.wfs, self.atoms) # Compare fingerprints across all processors fingerprint = np.array([md5_array(ppo.B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') work_nG = np.empty_like(self.psit_nG) self.pt.integrate(self.psit_nG, kpt.P_ani, kpt.q) P0_ani = dict([(a,P_ni.copy()) for a,P_ni in kpt.P_ani.items()]) ppo.apply_inverse(self.psit_nG, work_nG, self.wfs, kpt, calculate_P_ani=False) res_nG = np.empty_like(self.psit_nG) ppo.apply(work_nG, res_nG, self.wfs, kpt, calculate_P_ani=True) del work_nG P_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(res_nG, P_ani, kpt.q) abserr = np.empty(1, dtype=float) for n in range(self.nbands): band_rank, myn = self.bd.who_has(n) if band_rank == self.bd.comm.rank: abserr[:] = np.abs(self.psit_nG[myn] - res_nG[myn]).max() self.gd.comm.max(abserr) self.bd.comm.broadcast(abserr, band_rank) self.assertAlmostEqual(abserr.item(), 0, 10) self.check_and_plot(P_ani, P0_ani, 10, 'overlap,inverse,before')
def bloch_matrix(self, kpts, qpts, c_kn, u_ql, omega_ql=None, kpts_from=None): """Calculate el-ph coupling in the Bloch basis for the electrons. This function calculates the electron-phonon coupling between the specified Bloch states, i.e.:: ______ mnl / hbar ^ g = /------- < m k + q | e . grad V | n k > kq \/ 2 M w ql q ql In case the ``omega_ql`` keyword argument is not given, the bare matrix element (in units of eV / Ang) without the sqrt prefactor is returned. Parameters ---------- kpts: ndarray or tuple. k-vectors of the Bloch states. When a tuple of integers is given, a Monkhorst-Pack grid with the specified number of k-points along the directions of the reciprocal lattice vectors is generated. qpts: ndarray or tuple. q-vectors of the phonons. c_kn: ndarray Expansion coefficients for the Bloch states. The ordering must be the same as in the ``kpts`` argument. u_ql: ndarray Mass-scaled polarization vectors (in units of 1 / sqrt(amu)) of the phonons. Again, the ordering must be the same as in the corresponding ``qpts`` argument. omega_ql: ndarray Vibrational frequencies in eV. kpts_from: list of ints or int Calculate only the matrix element for the k-vectors specified by their index in the ``kpts`` argument (default: all). In short, phonon frequencies and mode vectors must be given in ase units. """ assert self.g_xNNMM is not None, "Load supercell matrix." assert len(c_kn.shape) == 3 assert len(u_ql.shape) == 4 if omega_ql is not None: assert np.all(u_ql.shape[:2] == omega_ql.shape[:2]) # Translate k-points into 1. BZ (required by ``find_k_plus_q``` member # function of the ```KPointDescriptor``). if isinstance(kpts, np.ndarray): assert kpts.shape[1] == 3, "kpts_kc array must be given" # XXX This does not seem to cause problems! kpts -= kpts.round() # Use the KPointDescriptor to keep track of the k and q-vectors kd_kpts = KPointDescriptor(kpts) kd_qpts = KPointDescriptor(qpts) # Check that number of k- and q-points agree with the number of Bloch # functions and polarization vectors assert kd_kpts.nbzkpts == len(c_kn) assert kd_qpts.nbzkpts == len(u_ql) # Include all k-point per default if kpts_from is None: kpts_kc = kd_kpts.bzk_kc kpts_k = range(kd_kpts.nbzkpts) else: kpts_kc = kd_kpts.bzk_kc[kpts_from] if isinstance(kpts_from, int): kpts_k = list([kpts_from]) else: kpts_k = list(kpts_from) # Supercell matrix (real matrix in Hartree / Bohr) g_xNNMM = self.g_xNNMM # Number of phonon modes and electronic bands nmodes = u_ql.shape[1] nbands = c_kn.shape[1] # Number of atoms displacements and basis functions ndisp = np.prod(u_ql.shape[2:]) assert ndisp == (3 * len(self.indices)) nao = c_kn.shape[2] assert ndisp == g_xNNMM.shape[0] assert nao == g_xNNMM.shape[-1] # Lattice vectors R_cN = self.lattice_vectors() # Number of unit cell in supercell N = np.prod(self.N_c) # Allocate array for couplings g_qklnn = np.zeros( (kd_qpts.nbzkpts, len(kpts_kc), nmodes, nbands, nbands), dtype=complex) self.timer.write_now("Calculating coupling matrix elements") for q, q_c in enumerate(kd_qpts.bzk_kc): # Find indices of k+q for the k-points kplusq_k = kd_kpts.find_k_plus_q(q_c, kpts_k=kpts_k) # Here, ``i`` is counting from 0 and ``k`` is the global index of # the k-point for i, (k, k_c) in enumerate(zip(kpts_k, kpts_kc)): # Check the wave vectors (adapted to the ``KPointDescriptor`` class) kplusq_c = k_c + q_c kplusq_c -= kplusq_c.round() assert np.allclose(kplusq_c, kd_kpts.bzk_kc[kplusq_k[i]] ), \ (i, k, k_c, q_c, kd_kpts.bzk_kc[kplusq_k[i]]) # Allocate array g_xMM = np.zeros((ndisp, nao, nao), dtype=complex) # Multiply phase factors for m in range(N): for n in range(N): Rm_c = R_cN[:, m] Rn_c = R_cN[:, n] phase = np.exp( 2.j * pi * (np.dot(k_c, Rm_c - Rn_c) + np.dot(q_c, Rm_c))) # Sum contributions from different cells g_xMM += g_xNNMM[:, m, n, :, :] * phase # LCAO coefficient for Bloch states ck_nM = c_kn[k] ckplusq_nM = c_kn[kplusq_k[i]] # Mass scaled polarization vectors u_lx = u_ql[q].reshape(nmodes, 3 * len(self.atoms)) g_nxn = np.dot(ckplusq_nM.conj(), np.dot(g_xMM, ck_nM.T)) g_lnn = np.dot(u_lx, g_nxn) # Insert value g_qklnn[q, i] = g_lnn # XXX Temp if np.all(q_c == 0.0): # These should be real print(g_qklnn[q].imag.min(), g_qklnn[q].imag.max()) self.timer.write_now( "Finished calculation of coupling matrix elements") # Return the bare matrix element if frequencies are not given if omega_ql is None: # Convert to eV / Ang g_qklnn *= units.Hartree / units.Bohr else: # Multiply prefactor sqrt(hbar / 2 * M * omega) in units of Bohr amu = units._amu # atomic mass unit me = units._me # electron mass g_qklnn /= np.sqrt(2 * amu / me / units.Hartree * \ omega_ql[:, np.newaxis, :, np.newaxis, np.newaxis]) # Convert to eV g_qklnn *= units.Hartree # Return couplings in eV (or eV / Ang) return g_qklnn
class Phonons(phonons.Phonons): """DFPT version of the ``Phonons`` class from ase. This class recycle the functionality from the ASE class. """ def __init__(self, atoms, kpts, symmetry): """Initialize base class and attributes. Parameters ---------- atoms: Atoms ASE atoms. kpts: tuple or list of tuples Shape of Monkhorst-Pack grid or list of k-points used in the dfpt calculation. symmetry: bool or None Symmetry parameter used in dfpt calculation. """ # Init base class with ``Atoms`` object phonons.Phonons.__init__(atoms) # Create k-point descriptor self.kd = KPointDescriptor(kpts, 1) self.kd.set_symmetry(self.atoms, self.calc.wfs.setups, symmetry) # Overwrite ``N_c`` attribute self.N_c = tuple(self.kd.N_c) # Index of the gamma point -- for the acoustic sum-rule self.gamma_index = None if self.kd.gamma: self.gamma_index = 0 self.dtype == float else: self.dtype == comples for k, k_c in enumerate(self.kd.ibzk_kc): if np.all(k_c == 0.): self.gamma_index = k assert self.gamma_index is not None def run(self): """Overwrite base class member function.""" raise RuntimeError, "Use only this class for post-processing" def read(self): """Read force constants from files.""" # Data structure for force constants self.C_qaavv = [dict([(a, dict([(a_, np.zeros((3, 3), dtype=dtype)) for a_ in self.indices])) for a in self.indices]) for q in range(self.kd.nibzkpts)] assert self.name is not None assert self.path is not None for q in range(self.kd.nibzkpts): for a in self.indices: for v in [0, 1, 2]: filename = self.name % (q, a, v) try: fd = open(os.path.join(self.path, filename)) except EOFError: print ("Redo file %s " % os.path.join(self.path, filename)) C_qav_a = pickle.load(fd) fd.close() for a_ in self.indices: self.C_qaavv[q][a][a_][v] = C_qav_a[a_] def assemble(self, acoustic=True): """Assemble dynamical matrix from the force constants attribute. The elements of the dynamical matrix are given by:: D_ij(q) = 1/(M_i + M_j) * C_ij(q) , where i and j are collective atomic and cartesian indices. During the assembly, various symmetries of the dynamical matrix are enforced:: 1) Hermiticity 2) Acoustic sum-rule 3) D(q) = D*(-q) Parameters ---------- acoustic: bool When True, the diagonal of the matrix of force constants is corrected to ensure that the acoustic sum-rule is fulfilled. """ # Read force constants from files self.read() # Number of atoms included N = len(self.indices) # Assemble matrix of force constants self.C_q = [] for q, C_aavv in enumerate(self.C_qaavv): C_avav = np.zeros((3*N, 3*N), dtype=self.dtype) for i, a in enumerate(self.indices): for j, a_ in enumerate(self.indices): C_avav[3*i : 3*i + 3, 3*j : 3*j + 3] += C_aavv[a][a_] self.C_q.append(C_avav) # XXX Figure out in which order the corrections should be done # Make C(q) Hermitian for C in self.C_q: C *= 0.5 C += C.T.conj() # Get matrix of force constants in the Gamma-point (is real!) C_gamma = self.C_q[self.gamma_index].real # Make Gamma-component real self.C_q[self.gamma_index] = C_gamma.copy() # Apply acoustic sum-rule if requested if acoustic: # Correct atomic diagonal for each q-vector for C in self.C_q: for a in range(N): for a_ in range(N): C[3*a : 3*a + 3, 3*a : 3*a + 3] -= \ C_gamma[3*a: 3*a+3, 3*a_: 3*a_+3] # Check sum-rule for Gamma-component in debug mode if debug: C = self.C_q[self.gamma_index] assert np.all(np.sum(C.reshape((3*N, N, 3)), axis=1) < 1e-15) # Move this bit to an ``unfold`` member function # XXX Time-reversal symmetry C_q = np.asarray(self.C_q) if self.kd.nibzkpts != self.kd.nbzkpts: self.D_k = np.concatenate((C_q[:0:-1].conjugate(), C_q)) else: self.D_k = 0.5 * C_q self.D_k += self.D_k[::-1].conjugate() # Mass prefactor for the dynamical matrix m = self.atoms.get_masses() self.m_inv_av = np.repeat(m[self.indices]**-0.5, 3) M_avav = self.m_inv_av[:, np.newaxis] * self.m_inv_av for C in self.D_k: C *= M_avav self.assembled = True def real_space(self): """Fourier transform the dynamical matrix to real-space.""" if not self.assembled: self.assemble() # Shape of q-point grid N_c = self.N_c # Reshape before Fourier transforming shape = self.D_k.shape Dq_lmn = self.D_k.reshape(N_c + shape[1:]) DR_lmn = fft.ifftn(fft.ifftshift(Dq_lmn, axes=(0, 1, 2)), axes=(0, 1, 2)) if debug: # Check that D_R is real enough assert np.all(DR_lmn.imag < 1e-8) DR_lmn = DR_lmn.real # Corresponding R_m vectors in units of the basis vectors R_cm = np.indices(N_c).reshape(3, -1) N1_c = np.array(N_c)[:, np.newaxis] R_cm += N1_c // 2 R_cm %= N1_c R_cm -= N1_c // 2 R_clmn = R_cm.reshape((3,) + N_c) return DR_lmn, R_clmn def fourier_interpolate(self, N_c): """Fourier interpolate dynamical matrix onto a finer q-vector grid.""" # Move this member function to the ASE class raise NotImplementedError
class UTDomainParallelSetup(TestCase): """ Setup a simple domain parallel calculation.""" # Number of bands nbands = 12 # Spin-polarized nspins = 1 # Mean spacing and number of grid points per axis (G x G x G) h = 0.25 / Bohr G = 48 # Type of boundary conditions employed (determines nibzkpts and dtype) boundaries = None nibzkpts = None dtype = None timer = nulltimer def setUp(self): for virtvar in ['boundaries']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Basic unit cell information: res, N_c = shapeopt(100, self.G**3, 3, 0.2) #N_c = 4*np.round(np.array(N_c)/4) # makes domain decomposition easier cell_cv = self.h * np.diag(N_c) pbc_c = {'zero' : (False,False,False), \ 'periodic': (True,True,True), \ 'mixed' : (True, False, True)}[self.boundaries] # Create randomized gas-like atomic configuration on interim grid tmpgd = GridDescriptor(N_c, cell_cv, pbc_c) self.atoms = create_random_atoms(tmpgd) # Create setups Z_a = self.atoms.get_atomic_numbers() assert 1 == self.nspins self.setups = Setups(Z_a, p.setups, p.basis, p.lmax, xc) self.natoms = len(self.setups) # Decide how many kpoints to sample from the 1st Brillouin Zone kpts_c = np.ceil((10/Bohr)/np.sum(cell_cv**2,axis=1)**0.5).astype(int) kpts_c = tuple(kpts_c * pbc_c + 1 - pbc_c) self.bzk_kc = kpts2ndarray(kpts_c) # Set up k-point descriptor self.kd = KPointDescriptor(self.bzk_kc, self.nspins) self.kd.set_symmetry(self.atoms, self.setups, p.usesymm) # Set the dtype if self.kd.gamma: self.dtype = float else: self.dtype = complex # Create communicators parsize, parsize_bands = self.get_parsizes() assert self.nbands % np.prod(parsize_bands) == 0 domain_comm, kpt_comm, band_comm = distribute_cpus(parsize, parsize_bands, self.nspins, self.kd.nibzkpts) self.kd.set_communicator(kpt_comm) # Set up band descriptor: self.bd = BandDescriptor(self.nbands, band_comm) # Set up grid descriptor: self.gd = GridDescriptor(N_c, cell_cv, pbc_c, domain_comm, parsize) # Set up kpoint/spin descriptor (to be removed): self.kd_old = KPointDescriptorOld(self.nspins, self.kd.nibzkpts, kpt_comm, self.kd.gamma, self.dtype) def tearDown(self): del self.atoms, self.bd, self.gd, self.kd, self.kd_old def get_parsizes(self): # Careful, overwriting imported GPAW params may cause amnesia in Python. from gpaw import parsize, parsize_bands # D: number of domains # B: number of band groups if parsize is None: D = min(world.size, 2) else: D = parsize assert world.size % D == 0 if parsize_bands is None: B = world.size // D else: B = parsize_bands return D, B # ================================= def verify_comm_sizes(self): if world.size == 1: return comm_sizes = tuple([comm.size for comm in [world, self.bd.comm, \ self.gd.comm, self.kd_old.comm]]) self._parinfo = '%d world, %d band, %d domain, %d kpt' % comm_sizes self.assertEqual(self.nbands % self.bd.comm.size, 0) self.assertEqual((self.nspins * self.kd.nibzkpts) % self.kd_old.comm.size, 0)
def create_kpoint_descriptor(bzkpts_kc, nspins, atoms, symmetry, comm): kd = KPointDescriptor(bzkpts_kc, nspins) # self.timer.start('Set symmetry') kd.set_symmetry(atoms, symmetry, comm=comm) # self.timer.stop('Set symmetry') return kd
def __init__(self, calc, filename='gw', kpts=None, bands=None, nbands=None, ppa=False, wstc=False, ecut=150.0, eta=0.1, E0=1.0 * Hartree, domega0=0.025, omega2=10.0, nblocks=1, savew=False, world=mpi.world): if world.rank != 0: txt = devnull else: txt = open(filename + '.txt', 'w') p = functools.partial(print, file=txt) p(' ___ _ _ _ ') p(' | || | | |') p(' | | || | | |') p(' |__ ||_____|') p(' |___|') p() PairDensity.__init__(self, calc, ecut, world=world, nblocks=nblocks, txt=txt) self.filename = filename self.savew = savew ecut /= Hartree self.ppa = ppa self.wstc = wstc self.eta = eta / Hartree self.E0 = E0 / Hartree self.domega0 = domega0 / Hartree self.omega2 = omega2 / Hartree self.kpts = select_kpts(kpts, self.calc) if bands is None: bands = [0, self.nocc2] self.bands = bands b1, b2 = bands self.shape = shape = (self.calc.wfs.nspins, len(self.kpts), b2 - b1) self.eps_sin = np.empty(shape) # KS-eigenvalues self.f_sin = np.empty(shape) # occupation numbers self.sigma_sin = np.zeros(shape) # self-energies self.dsigma_sin = np.zeros(shape) # derivatives of self-energies self.vxc_sin = None # KS XC-contributions self.exx_sin = None # exact exchange contributions self.Z_sin = None # renormalization factors if nbands is None: nbands = int(self.vol * ecut**1.5 * 2**0.5 / 3 / pi**2) self.nbands = nbands p() p('Quasi particle states:') if kpts is None: p('All k-points in IBZ') else: kptstxt = ', '.join(['{0:d}'.format(k) for k in self.kpts]) p('k-points (IBZ indices): [' + kptstxt + ']') p('Band range: ({0:d}, {1:d})'.format(b1, b2)) p() p('Computational parameters:') p('Plane wave cut-off: {0:g} eV'.format(self.ecut * Hartree)) p('Number of bands: {0:d}'.format(self.nbands)) p('Broadening: {0:g} eV'.format(self.eta * Hartree)) kd = self.calc.wfs.kd self.mysKn1n2 = None # my (s, K, n1, n2) indices self.distribute_k_points_and_bands(b1, b2, kd.ibz2bz_k[self.kpts]) # Find q-vectors and weights in the IBZ: assert -1 not in kd.bz2bz_ks offset_c = 0.5 * ((kd.N_c + 1) % 2) / kd.N_c bzq_qc = monkhorst_pack(kd.N_c) + offset_c self.qd = KPointDescriptor(bzq_qc) self.qd.set_symmetry(self.calc.atoms, kd.symmetry) assert self.calc.wfs.nspins == 1
class G0W0(PairDensity): """This class defines the G0W0 calculator. The G0W0 calculator is used is used to calculate the quasi particle energies through the G0W0 approximation for a number of states. Note: So far the G0W0 calculation only works for spin-paired systems. Parameters: calc: str or PAW object GPAW calculator object or filename of saved calculator object. filename: str Base filename of output files. kpts: list List of indices of the IBZ k-points to calculate the quasi particle energies for. bands: tuple Range of band indices, like (n1, n2+1), to calculate the quasi particle energies for. Note that the second band index is not included. ecut: float Plane wave cut-off energy in eV. nbands: int Number of bands to use in the calculation. If :None: the number will be determined from :ecut: to yield a number close to the number of plane waves used. ppa: bool Sets whether the Godby-Needs plasmon-pole approximation for the dielectric function should be used. E0: float Energy (in eV) used for fitting in the plasmon-pole approximation. domega0: float Minimum frequency step (in eV) used in the generation of the non- linear frequency grid. omega2: float Control parameter for the non-linear frequency grid, equal to the frequency where the grid spacing has doubled in size. wstc: bool Sets whether a Wigner-Seitz truncation should be used for the Coloumb potential. """ def __init__(self, calc, filename='gw', kpts=None, bands=None, nbands=None, ppa=False, wstc=False, ecut=150.0, eta=0.1, E0=1.0 * Hartree, domega0=0.025, omega2=10.0, nblocks=1, savew=False, world=mpi.world): if world.rank != 0: txt = devnull else: txt = open(filename + '.txt', 'w') p = functools.partial(print, file=txt) p(' ___ _ _ _ ') p(' | || | | |') p(' | | || | | |') p(' |__ ||_____|') p(' |___|') p() PairDensity.__init__(self, calc, ecut, world=world, nblocks=nblocks, txt=txt) self.filename = filename self.savew = savew ecut /= Hartree self.ppa = ppa self.wstc = wstc self.eta = eta / Hartree self.E0 = E0 / Hartree self.domega0 = domega0 / Hartree self.omega2 = omega2 / Hartree self.kpts = select_kpts(kpts, self.calc) if bands is None: bands = [0, self.nocc2] self.bands = bands b1, b2 = bands self.shape = shape = (self.calc.wfs.nspins, len(self.kpts), b2 - b1) self.eps_sin = np.empty(shape) # KS-eigenvalues self.f_sin = np.empty(shape) # occupation numbers self.sigma_sin = np.zeros(shape) # self-energies self.dsigma_sin = np.zeros(shape) # derivatives of self-energies self.vxc_sin = None # KS XC-contributions self.exx_sin = None # exact exchange contributions self.Z_sin = None # renormalization factors if nbands is None: nbands = int(self.vol * ecut**1.5 * 2**0.5 / 3 / pi**2) self.nbands = nbands p() p('Quasi particle states:') if kpts is None: p('All k-points in IBZ') else: kptstxt = ', '.join(['{0:d}'.format(k) for k in self.kpts]) p('k-points (IBZ indices): [' + kptstxt + ']') p('Band range: ({0:d}, {1:d})'.format(b1, b2)) p() p('Computational parameters:') p('Plane wave cut-off: {0:g} eV'.format(self.ecut * Hartree)) p('Number of bands: {0:d}'.format(self.nbands)) p('Broadening: {0:g} eV'.format(self.eta * Hartree)) kd = self.calc.wfs.kd self.mysKn1n2 = None # my (s, K, n1, n2) indices self.distribute_k_points_and_bands(b1, b2, kd.ibz2bz_k[self.kpts]) # Find q-vectors and weights in the IBZ: assert -1 not in kd.bz2bz_ks offset_c = 0.5 * ((kd.N_c + 1) % 2) / kd.N_c bzq_qc = monkhorst_pack(kd.N_c) + offset_c self.qd = KPointDescriptor(bzq_qc) self.qd.set_symmetry(self.calc.atoms, kd.symmetry) assert self.calc.wfs.nspins == 1 @timer('G0W0') def calculate(self, ecuts=None): """Starts the G0W0 calculation. Returns a dict with the results with the following key/value pairs: f: (s, k, n) ndarray Occupation numbers eps: (s, k, n) ndarray Kohn-Sham eigenvalues in eV vxc: (s, k, n) ndarray Exchange-correlation contributions in eV exx: (s, k, n) ndarray Exact exchange contributions in eV sigma: (s, k, n) ndarray Self-energy contributions in eV Z: (s, k, n) ndarray Renormalization factors qp: (s, k, n) ndarray Quasi particle energies in eV """ kd = self.calc.wfs.kd self.calculate_ks_xc_contribution() self.calculate_exact_exchange() # Reset calculation self.sigma_sin = np.zeros(self.shape) # self-energies self.dsigma_sin = np.zeros(self.shape) # derivatives of self-energies # Get KS eigenvalues and occupation numbers: b1, b2 = self.bands for i, k in enumerate(self.kpts): kpt = self.calc.wfs.kpt_u[k] self.eps_sin[0, i] = kpt.eps_n[b1:b2] self.f_sin[0, i] = kpt.f_n[b1:b2] / kpt.weight # My part of the states we want to calculate QP-energies for: mykpts = [self.get_k_point(s, K, n1, n2) for s, K, n1, n2 in self.mysKn1n2] # Loop over q in the IBZ: for pd0, W0, q_c in self.calculate_screened_potential(): for kpt1 in mykpts: K2 = kd.find_k_plus_q(q_c, [kpt1.K])[0] kpt2 = self.get_k_point(0, K2, 0, self.nbands, block=True) k1 = kd.bz2ibz_k[kpt1.K] i = self.kpts.index(k1) self.calculate_q(i, kpt1, kpt2, pd0, W0) self.world.sum(self.sigma_sin) self.world.sum(self.dsigma_sin) self.Z_sin = 1 / (1 - self.dsigma_sin) self.qp_sin = self.eps_sin + self.Z_sin * (self.sigma_sin + self.exx_sin - self.vxc_sin) results = {'f': self.f_sin, 'eps': self.eps_sin * Hartree, 'vxc': self.vxc_sin * Hartree, 'exx': self.exx_sin * Hartree, 'sigma': self.sigma_sin * Hartree, 'Z': self.Z_sin, 'qp': self.qp_sin * Hartree} self.print_results(results) return results def calculate_q(self, i, kpt1, kpt2, pd0, W0): """Calculates the contribution to the self-energy and its derivative for a given set of k-points, kpt1 and kpt2.""" wfs = self.calc.wfs N_c = pd0.gd.N_c i_cG = self.sign * np.dot(self.U_cc, np.unravel_index(pd0.Q_qG[0], N_c)) q_c = wfs.kd.bzk_kc[kpt2.K] - wfs.kd.bzk_kc[kpt1.K] q0 = np.allclose(q_c, 0) and not self.wstc shift0_c = q_c - self.sign * np.dot(self.U_cc, pd0.kd.bzk_kc[0]) assert np.allclose(shift0_c.round(), shift0_c) shift0_c = shift0_c.round().astype(int) shift_c = kpt1.shift_c - kpt2.shift_c - shift0_c I_G = np.ravel_multi_index(i_cG + shift_c[:, None], N_c, 'wrap') G_Gv = pd0.get_reciprocal_vectors() pos_av = np.dot(self.spos_ac, pd0.gd.cell_cv) M_vv = np.dot(pd0.gd.cell_cv.T, np.dot(self.U_cc.T, np.linalg.inv(pd0.gd.cell_cv).T)) Q_aGii = [] for a, Q_Gii in enumerate(self.Q_aGii): x_G = np.exp(1j * np.dot(G_Gv, (pos_av[a] - self.sign * np.dot(M_vv, pos_av[a])))) U_ii = self.calc.wfs.setups[a].R_sii[self.s] Q_Gii = np.dot(np.dot(U_ii, Q_Gii * x_G[:, None, None]), U_ii.T).transpose(1, 0, 2) Q_aGii.append(Q_Gii) if debug: self.check(i_cG, shift0_c, N_c, q_c, Q_aGii) if self.ppa: calculate_sigma = self.calculate_sigma_ppa else: calculate_sigma = self.calculate_sigma for n in range(kpt1.n2 - kpt1.n1): ut1cc_R = kpt1.ut_nR[n].conj() eps1 = kpt1.eps_n[n] C1_aGi = [np.dot(Qa_Gii, P1_ni[n].conj()) for Qa_Gii, P1_ni in zip(Q_aGii, kpt1.P_ani)] n_mG = self.calculate_pair_densities(ut1cc_R, C1_aGi, kpt2, pd0, I_G) if self.sign == 1: n_mG = n_mG.conj() if q0: n_mG[:, 0] = 0 m = n + kpt1.n1 - kpt2.n1 if 0 <= m < len(n_mG): n_mG[m, 0] = 1.0 f_m = kpt2.f_n deps_m = eps1 - kpt2.eps_n sigma, dsigma = calculate_sigma(n_mG, deps_m, f_m, W0) nn = kpt1.n1 + n - self.bands[0] self.sigma_sin[kpt1.s, i, nn] += sigma self.dsigma_sin[kpt1.s, i, nn] += dsigma def check(self, i_cG, shift0_c, N_c, q_c, Q_aGii): I0_G = np.ravel_multi_index(i_cG - shift0_c[:, None], N_c, 'wrap') qd1 = KPointDescriptor([q_c]) pd1 = PWDescriptor(self.ecut, self.calc.wfs.gd, complex, qd1) G_I = np.empty(N_c.prod(), int) G_I[:] = -1 I1_G = pd1.Q_qG[0] G_I[I1_G] = np.arange(len(I0_G)) G_G = G_I[I0_G] assert len(I0_G) == len(I1_G) assert (G_G >= 0).all() for a, Q_Gii in enumerate(self.initialize_paw_corrections(pd1)): e = abs(Q_aGii[a] - Q_Gii[G_G]).max() assert e < 1e-12 @timer('Sigma') def calculate_sigma(self, n_mG, deps_m, f_m, C_swGG): """Calculates a contribution to the self-energy and its derivative for a given (k, k-q)-pair from its corresponding pair-density and energy.""" o_m = abs(deps_m) # Add small number to avoid zeros for degenerate states: sgn_m = np.sign(deps_m + 1e-15) # Pick +i*eta or -i*eta: s_m = (1 + sgn_m * np.sign(0.5 - f_m)).astype(int) // 2 beta = (2**0.5 - 1) * self.domega0 / self.omega2 w_m = (o_m / (self.domega0 + beta * o_m)).astype(int) o1_m = self.omega_w[w_m] o2_m = self.omega_w[w_m + 1] x = 1.0 / (self.qd.nbzkpts * 2 * pi * self.vol) sigma = 0.0 dsigma = 0.0 # Performing frequency integration for o, o1, o2, sgn, s, w, n_G in zip(o_m, o1_m, o2_m, sgn_m, s_m, w_m, n_mG): C1_GG = C_swGG[s][w] C2_GG = C_swGG[s][w + 1] p = x * sgn myn_G = n_G[self.Ga:self.Gb] sigma1 = p * np.dot(np.dot(myn_G, C1_GG), n_G.conj()).imag sigma2 = p * np.dot(np.dot(myn_G, C2_GG), n_G.conj()).imag sigma += ((o - o1) * sigma2 + (o2 - o) * sigma1) / (o2 - o1) dsigma += sgn * (sigma2 - sigma1) / (o2 - o1) return sigma, dsigma def calculate_screened_potential(self): """Calculates the screened potential for each q-point in the 1st BZ. Since many q-points are related by symmetry, the actual calculation is only done for q-points in the IBZ and the rest are obtained by symmetry transformations. Results are returned as a generator to that it is not necessary to store a huge matrix for each q-point in the memory.""" # The decorator $timer('W') doesn't work for generators, do we will # have to manually start and stop the timer here: self.timer.start('W') print('Calculating screened Coulomb potential', file=self.fd) if self.wstc: print('Using Wigner-Seitz truncated Coloumb potential', file=self.fd) if self.ppa: print('Using Godby-Needs plasmon-pole approximation:', file=self.fd) print(' Fitting energy: i*E0, E0 = %.3f Hartee' % self.E0, file=self.fd) # use small imaginary frequency to avoid dividing by zero: frequencies = [1e-10j, 1j * self.E0 * Hartree] parameters = {'eta': 0, 'hilbert': False, 'timeordered': False, 'frequencies': frequencies} else: print('Using full frequency integration:', file=self.fd) print(' domega0: {0:g}'.format(self.domega0 * Hartree), file=self.fd) print(' omega2: {0:g}'.format(self.omega2 * Hartree), file=self.fd) parameters = {'eta': self.eta * Hartree, 'hilbert': True, 'timeordered': True, 'domega0': self.domega0 * Hartree, 'omega2': self.omega2 * Hartree} chi0 = Chi0(self.calc, nbands=self.nbands, ecut=self.ecut * Hartree, intraband=False, real_space_derivatives=False, txt=self.filename + '.w.txt', timer=self.timer, keep_occupied_states=True, nblocks=self.blockcomm.size, no_optical_limit=self.wstc, **parameters) if self.wstc: wstc = WignerSeitzTruncatedCoulomb( self.calc.wfs.gd.cell_cv, self.calc.wfs.kd.N_c, chi0.fd) else: wstc = None self.omega_w = chi0.omega_w self.omegamax = chi0.omegamax htp = HilbertTransform(self.omega_w, self.eta, gw=True) htm = HilbertTransform(self.omega_w, -self.eta, gw=True) # Find maximum size of chi-0 matrices: gd = self.calc.wfs.gd nGmax = max(count_reciprocal_vectors(self.ecut, gd, q_c) for q_c in self.qd.ibzk_kc) nw = len(self.omega_w) size = self.blockcomm.size mynGmax = (nGmax + size - 1) // size mynw = (nw + size - 1) // size # Allocate memory in the beginning and use for all q: A1_x = np.empty(nw * mynGmax * nGmax, complex) A2_x = np.empty(max(mynw * nGmax, nw * mynGmax) * nGmax, complex) # Need to pause the timer in between iterations self.timer.stop('W') for iq, q_c in enumerate(self.qd.ibzk_kc): self.timer.start('W') if self.savew: wfilename = self.filename + '.w.q%d.pckl' % iq fd = opencew(wfilename) if self.savew and fd is None: # Read screened potential from file with open(wfilename) as fd: pd, W = pickle.load(fd) else: # First time calculation pd, W = self.calculate_w(chi0, q_c, htp, htm, wstc, A1_x, A2_x) if self.savew: pickle.dump((pd, W), fd, pickle.HIGHEST_PROTOCOL) self.timer.stop('W') # Loop over all k-points in the BZ and find those that are related # to the current IBZ k-point by symmetry Q1 = self.qd.ibz2bz_k[iq] done = set() for s, Q2 in enumerate(self.qd.bz2bz_ks[Q1]): if Q2 >= 0 and Q2 not in done: s = self.qd.sym_k[Q2] self.s = s self.U_cc = self.qd.symmetry.op_scc[s] time_reversal = self.qd.time_reversal_k[Q2] self.sign = 1 - 2 * time_reversal Q_c = self.qd.bzk_kc[Q2] d_c = self.sign * np.dot(self.U_cc, q_c) - Q_c assert np.allclose(d_c.round(), d_c) yield pd, W, Q_c done.add(Q2) @timer('WW') def calculate_w(self, chi0, q_c, htp, htm, wstc, A1_x, A2_x): """Calculates the screened potential for a specified q-point.""" pd, chi0_wGG = chi0.calculate(q_c, A_x=A1_x)[:2] self.Q_aGii = chi0.Q_aGii self.Ga = chi0.Ga self.Gb = chi0.Gb if self.blockcomm.size > 1: A1_x = chi0_wGG.ravel() chi0_wGG = chi0.redistribute(chi0_wGG, A2_x) if self.wstc: iG_G = (wstc.get_potential(pd) / (4 * pi))**0.5 if np.allclose(q_c, 0): chi0_wGG[:, 0] = 0.0 chi0_wGG[:, :, 0] = 0.0 G0inv = 0.0 G20inv = 0.0 else: G0inv = None G20inv = None else: if np.allclose(q_c, 0): dq3 = (2 * pi)**3 / (self.qd.nbzkpts * self.vol) qc = (dq3 / 4 / pi * 3)**(1 / 3) G0inv = 2 * pi * qc**2 / dq3 G20inv = 4 * pi * qc / dq3 G_G = pd.G2_qG[0]**0.5 G_G[0] = 1 iG_G = 1 / G_G else: iG_G = pd.G2_qG[0]**-0.5 G0inv = None G20inv = None delta_GG = np.eye(len(iG_G)) if self.ppa: return pd, self.ppa_w(chi0_wGG, iG_G, delta_GG, G0inv, G20inv, q_c) self.timer.start('Dyson eq.') # Calculate W and store it in chi0_wGG ndarray: for chi0_GG in chi0_wGG: e_GG = (delta_GG - 4 * pi * chi0_GG * iG_G * iG_G[:, np.newaxis]) W_GG = chi0_GG W_GG[:] = 4 * pi * (np.linalg.inv(e_GG) - delta_GG) * iG_G * iG_G[:, np.newaxis] if np.allclose(q_c, 0): W_GG[0, 0] *= G20inv W_GG[1:, 0] *= G0inv W_GG[0, 1:] *= G0inv if self.blockcomm.size > 1: Wm_wGG = chi0.redistribute(chi0_wGG, A1_x) else: Wm_wGG = chi0_wGG Wp_wGG = A2_x[:Wm_wGG.size].reshape(Wm_wGG.shape) Wp_wGG[:] = Wm_wGG with self.timer('Hilbert transform'): htp(Wp_wGG) htm(Wm_wGG) self.timer.stop('Dyson eq.') return pd, [Wp_wGG, Wm_wGG] @timer('Kohn-Sham XC-contribution') def calculate_ks_xc_contribution(self): name = self.filename + '.vxc.npy' fd = opencew(name) if fd is None: print('Reading Kohn-Sham XC contribution from file:', name, file=self.fd) with open(name) as fd: self.vxc_sin = np.load(fd) assert self.vxc_sin.shape == self.shape, self.vxc_sin.shape return print('Calculating Kohn-Sham XC contribution', file=self.fd) if self.reader is not None: self.calc.wfs.read_projections(self.reader) vxc_skn = vxc(self.calc, self.calc.hamiltonian.xc) / Hartree n1, n2 = self.bands self.vxc_sin = vxc_skn[:, self.kpts, n1:n2] np.save(fd, self.vxc_sin) @timer('EXX') def calculate_exact_exchange(self): name = self.filename + '.exx.npy' fd = opencew(name) if fd is None: print('Reading EXX contribution from file:', name, file=self.fd) with open(name) as fd: self.exx_sin = np.load(fd) assert self.exx_sin.shape == self.shape, self.exx_sin.shape return print('Calculating EXX contribution', file=self.fd) exx = EXX(self.calc, kpts=self.kpts, bands=self.bands, txt=self.filename + '.exx.txt', timer=self.timer) exx.calculate() self.exx_sin = exx.get_eigenvalue_contributions() / Hartree np.save(fd, self.exx_sin) def print_results(self, results): description = ['f: Occupation numbers', 'eps: KS-eigenvalues [eV]', 'vxc: KS vxc [eV]', 'exx: Exact exchange [eV]', 'sigma: Self-energies [eV]', 'Z: Renormalization factors', 'qp: QP-energies [eV]'] print('\nResults:', file=self.fd) for line in description: print(line, file=self.fd) b1, b2 = self.bands names = [line.split(':', 1)[0] for line in description] ibzk_kc = self.calc.wfs.kd.ibzk_kc for s in range(self.calc.wfs.nspins): for i, ik in enumerate(self.kpts): print('\nk-point ' + '{0} ({1}): ({2:.3f}, {3:.3f}, {4:.3f})'.format( i, ik, *ibzk_kc[ik]), file=self.fd) print('band' + ''.join('{0:>8}'.format(name) for name in names), file=self.fd) for n in range(b2 - b1): print('{0:4}'.format(n + b1) + ''.join('{0:8.3f}'.format(results[name][s, i, n]) for name in names), file=self.fd) self.timer.write(self.fd) @timer('PPA') def ppa_w(self, chi0_wGG, iG_G, delta_GG, G0inv, G20inv, q_c): einv_wGG = [] for chi0_GG in chi0_wGG: e_GG = (delta_GG - 4 * pi * chi0_GG * iG_G * iG_G[:, np.newaxis]) einv_wGG.append(np.linalg.inv(e_GG) - delta_GG) if self.wstc and np.allclose(q_c, 0): einv_wGG[0][0] = 42 einv_wGG[0][:, 0] = 42 omegat_GG = self.E0 * np.sqrt(einv_wGG[1] / (einv_wGG[0] - einv_wGG[1])) R_GG = -0.5 * omegat_GG * einv_wGG[0] W_GG = 4 * pi**2 * R_GG * iG_G * iG_G[:, np.newaxis] if np.allclose(q_c, 0): W_GG[0, 0] *= G20inv W_GG[1:, 0] *= G0inv W_GG[0, 1:] *= G0inv return [W_GG, omegat_GG] @timer('PPA-Sigma') def calculate_sigma_ppa(self, n_mG, deps_m, f_m, W): W_GG, omegat_GG = W sigma = 0.0 dsigma = 0.0 # init variables (is this necessary?) nG = n_mG.shape[1] deps_GG = np.empty((nG, nG)) sign_GG = np.empty((nG, nG)) x1_GG = np.empty((nG, nG)) x2_GG = np.empty((nG, nG)) x3_GG = np.empty((nG, nG)) x4_GG = np.empty((nG, nG)) x_GG = np.empty((nG, nG)) dx_GG = np.empty((nG, nG)) nW_G = np.empty(nG) for m in range(np.shape(n_mG)[0]): deps_GG = deps_m[m] sign_GG = 2 * f_m[m] - 1 x1_GG = 1 / (deps_GG + omegat_GG - 1j * self.eta) x2_GG = 1 / (deps_GG - omegat_GG + 1j * self.eta) x3_GG = 1 / (deps_GG + omegat_GG - 1j * self.eta * sign_GG) x4_GG = 1 / (deps_GG - omegat_GG - 1j * self.eta * sign_GG) x_GG = W_GG * (sign_GG * (x1_GG - x2_GG) + x3_GG + x4_GG) dx_GG = W_GG * (sign_GG * (x1_GG**2 - x2_GG**2) + x3_GG**2 + x4_GG**2) nW_G = np.dot(n_mG[m], x_GG) sigma += np.vdot(n_mG[m], nW_G).real nW_G = np.dot(n_mG[m], dx_GG) dsigma -= np.vdot(n_mG[m], nW_G).real x = 1 / (self.qd.nbzkpts * 2 * pi * self.vol) return x * sigma, x * dsigma
class UTGaussianWavefunctionSetup(UTDomainParallelSetup): __doc__ = UTDomainParallelSetup.__doc__ + """ The pseudo wavefunctions are moving gaussians centered around each atom.""" allocated = False dtype = None # Default arguments for scaled Gaussian wave _sigma0 = 2.0 #0.75 _k0_c = 2*np.pi*np.array([1/5., 1/3., 0.]) def setUp(self): UTDomainParallelSetup.setUp(self) for virtvar in ['dtype']: assert getattr(self,virtvar) is not None, 'Virtual "%s"!' % virtvar # Create randomized atoms self.atoms = create_random_atoms(self.gd, 5) # also tested: 10xNH3/BDA # XXX DEBUG START if False: from ase import view view(self.atoms*(1+2*self.gd.pbc_c)) # XXX DEBUG END # Do we agree on the atomic positions? pos_ac = self.atoms.get_positions() pos_rac = np.empty((world.size,)+pos_ac.shape, pos_ac.dtype) world.all_gather(pos_ac, pos_rac) if (pos_rac-pos_rac[world.rank,...][np.newaxis,...]).any(): raise RuntimeError('Discrepancy in atomic positions detected.') # Create setups for atoms self.Z_a = self.atoms.get_atomic_numbers() self.setups = Setups(self.Z_a, p.setups, p.basis, p.lmax, xc) # K-point descriptor bzk_kc = np.array([[0, 0, 0]], dtype=float) self.kd = KPointDescriptor(bzk_kc, 1) self.kd.set_symmetry(self.atoms, self.setups, usesymm=True) self.kd.set_communicator(self.kpt_comm) # Create gamma-point dummy wavefunctions self.wfs = FDWFS(self.gd, self.bd, self.kd, self.setups, self.dtype) spos_ac = self.atoms.get_scaled_positions() % 1.0 self.wfs.set_positions(spos_ac) self.pt = self.wfs.pt # XXX shortcut ## Also create pseudo partial waveves #from gpaw.lfc import LFC #self.phit = LFC(self.gd, [setup.phit_j for setup in self.setups], \ # self.kpt_comm, dtype=self.dtype) #self.phit.set_positions(spos_ac) self.r_cG = None self.buf_G = None self.psit_nG = None self.allocate() def tearDown(self): UTDomainParallelSetup.tearDown(self) del self.r_cG, self.buf_G, self.psit_nG del self.pt, self.setups, self.atoms self.allocated = False def allocate(self): self.r_cG = self.gd.get_grid_point_coordinates() cell_cv = self.atoms.get_cell() / Bohr assert np.abs(cell_cv-self.gd.cell_cv).max() < 1e-9 center_c = 0.5*cell_cv.diagonal() self.buf_G = self.gd.empty(dtype=self.dtype) self.psit_nG = self.gd.empty(self.bd.mynbands, dtype=self.dtype) for myn,psit_G in enumerate(self.psit_nG): n = self.bd.global_index(myn) psit_G[:] = self.get_scaled_gaussian_wave(center_c, scale=10+2j*n) k_c = 2*np.pi*np.array([1/2., -1/7., 0.]) for pos_c in self.atoms.get_positions() / Bohr: sigma = self._sigma0/(1+np.sum(pos_c**2))**0.5 psit_G += self.get_scaled_gaussian_wave(pos_c, sigma, k_c, n+5j) self.allocated = True def get_scaled_gaussian_wave(self, pos_c, sigma=None, k_c=None, scale=None): if sigma is None: sigma = self._sigma0 if k_c is None: k_c = self._k0_c if scale is None: A = None else: # 4*pi*int(exp(-r^2/(2*w^2))^2*r^2, r=0...infinity)= w^3*pi^(3/2) # = scale/A^2 -> A = scale*(sqrt(Pi)*w)^(-3/2) hence int -> scale^2 A = scale/(sigma*(np.pi)**0.5)**1.5 return gaussian_wave(self.r_cG, pos_c, sigma, k_c, A, self.dtype, self.buf_G) def check_and_plot(self, P_ani, P0_ani, digits, keywords=''): # Collapse into viewable matrices P_In = self.wfs.collect_projections(P_ani) P0_In = self.wfs.collect_projections(P0_ani) # Construct fingerprint of input matrices for comparison fingerprint = np.array([md5_array(P_In, numeric=True), md5_array(P0_In, numeric=True)]) # Compare fingerprints across all processors fingerprints = np.empty((world.size, 2), np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') # If assertion fails, catch temporarily while plotting, then re-raise try: self.assertAlmostEqual(np.abs(P_In-P0_In).max(), 0, digits) except AssertionError: if world.rank == 0 and mpl is not None: from matplotlib.figure import Figure fig = Figure() ax = fig.add_axes([0.0, 0.1, 1.0, 0.83]) ax.set_title(self.__class__.__name__) im = ax.imshow(np.abs(P_In-P0_In), interpolation='nearest') fig.colorbar(im) fig.text(0.5, 0.05, 'Keywords: ' + keywords, \ horizontalalignment='center', verticalalignment='top') from matplotlib.backends.backend_agg import FigureCanvasAgg img = 'ut_invops_%s_%s.png' % (self.__class__.__name__, \ '_'.join(keywords.split(','))) FigureCanvasAgg(fig).print_figure(img.lower(), dpi=90) raise # ================================= def test_projection_linearity(self): kpt = self.wfs.kpt_u[0] Q_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(self.psit_nG, Q_ani, q=kpt.q) for Q_ni in Q_ani.values(): self.assertTrue(Q_ni.dtype == self.dtype) P0_ani = dict([(a,Q_ni.copy()) for a,Q_ni in Q_ani.items()]) self.pt.add(self.psit_nG, Q_ani, q=kpt.q) self.pt.integrate(self.psit_nG, P0_ani, q=kpt.q) #rank_a = self.gd.get_ranks_from_positions(spos_ac) #my_atom_indices = np.argwhere(self.gd.comm.rank == rank_a).ravel() # ~ a ~ a' #TODO XXX should fix PairOverlap-ish stuff for < p | phi > overlaps # i i' #spos_ac = self.pt.spos_ac # NewLFC doesn't have this spos_ac = self.atoms.get_scaled_positions() % 1.0 gpo = GridPairOverlap(self.gd, self.setups) B_aa = gpo.calculate_overlaps(spos_ac, self.pt) # Compare fingerprints across all processors fingerprint = np.array([md5_array(B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') P_ani = dict([(a,Q_ni.copy()) for a,Q_ni in Q_ani.items()]) for a1 in range(len(self.atoms)): if a1 in P_ani.keys(): P_ni = P_ani[a1] else: # Atom a1 is not in domain so allocate a temporary buffer P_ni = np.zeros((self.bd.mynbands,self.setups[a1].ni,), dtype=self.dtype) for a2, Q_ni in Q_ani.items(): # B_aa are the projector overlaps across atomic pairs B_ii = gpo.extract_atomic_pair_matrix(B_aa, a1, a2) P_ni += np.dot(Q_ni, B_ii.T) #sum over a2 and last i in B_ii self.gd.comm.sum(P_ni) self.check_and_plot(P_ani, P0_ani, 8, 'projection,linearity') def test_extrapolate_overlap(self): kpt = self.wfs.kpt_u[0] ppo = ProjectorPairOverlap(self.wfs, self.atoms) # Compare fingerprints across all processors fingerprint = np.array([md5_array(ppo.B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') work_nG = np.empty_like(self.psit_nG) P_ani = ppo.apply(self.psit_nG, work_nG, self.wfs, kpt, \ calculate_P_ani=True, extrapolate_P_ani=True) P0_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(work_nG, P0_ani, kpt.q) del work_nG self.check_and_plot(P_ani, P0_ani, 11, 'extrapolate,overlap') def test_extrapolate_inverse(self): kpt = self.wfs.kpt_u[0] ppo = ProjectorPairOverlap(self.wfs, self.atoms) # Compare fingerprints across all processors fingerprint = np.array([md5_array(ppo.B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') work_nG = np.empty_like(self.psit_nG) P_ani = ppo.apply_inverse(self.psit_nG, work_nG, self.wfs, kpt, \ calculate_P_ani=True, extrapolate_P_ani=True) P0_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(work_nG, P0_ani, kpt.q) del work_nG self.check_and_plot(P_ani, P0_ani, 11, 'extrapolate,inverse') def test_overlap_inverse_after(self): kpt = self.wfs.kpt_u[0] kpt.P_ani = self.pt.dict(self.bd.mynbands) ppo = ProjectorPairOverlap(self.wfs, self.atoms) # Compare fingerprints across all processors fingerprint = np.array([md5_array(ppo.B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') work_nG = np.empty_like(self.psit_nG) self.pt.integrate(self.psit_nG, kpt.P_ani, kpt.q) P0_ani = dict([(a,P_ni.copy()) for a,P_ni in kpt.P_ani.items()]) ppo.apply(self.psit_nG, work_nG, self.wfs, kpt, calculate_P_ani=False) res_nG = np.empty_like(self.psit_nG) ppo.apply_inverse(work_nG, res_nG, self.wfs, kpt, calculate_P_ani=True) del work_nG P_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(res_nG, P_ani, kpt.q) abserr = np.empty(1, dtype=float) for n in range(self.nbands): band_rank, myn = self.bd.who_has(n) if band_rank == self.bd.comm.rank: abserr[:] = np.abs(self.psit_nG[myn] - res_nG[myn]).max() self.gd.comm.max(abserr) self.bd.comm.broadcast(abserr, band_rank) self.assertAlmostEqual(abserr.item(), 0, 10) self.check_and_plot(P_ani, P0_ani, 10, 'overlap,inverse,after') def test_overlap_inverse_before(self): kpt = self.wfs.kpt_u[0] kpt.P_ani = self.pt.dict(self.bd.mynbands) ppo = ProjectorPairOverlap(self.wfs, self.atoms) # Compare fingerprints across all processors fingerprint = np.array([md5_array(ppo.B_aa, numeric=True)]) fingerprints = np.empty(world.size, np.int64) world.all_gather(fingerprint, fingerprints) if fingerprints.ptp(0).any(): raise RuntimeError('Distributed matrices are not identical!') work_nG = np.empty_like(self.psit_nG) self.pt.integrate(self.psit_nG, kpt.P_ani, kpt.q) P0_ani = dict([(a,P_ni.copy()) for a,P_ni in kpt.P_ani.items()]) ppo.apply_inverse(self.psit_nG, work_nG, self.wfs, kpt, calculate_P_ani=False) res_nG = np.empty_like(self.psit_nG) ppo.apply(work_nG, res_nG, self.wfs, kpt, calculate_P_ani=True) del work_nG P_ani = self.pt.dict(self.bd.mynbands) self.pt.integrate(res_nG, P_ani, kpt.q) abserr = np.empty(1, dtype=float) for n in range(self.nbands): band_rank, myn = self.bd.who_has(n) if band_rank == self.bd.comm.rank: abserr[:] = np.abs(self.psit_nG[myn] - res_nG[myn]).max() self.gd.comm.max(abserr) self.bd.comm.broadcast(abserr, band_rank) self.assertAlmostEqual(abserr.item(), 0, 10) self.check_and_plot(P_ani, P0_ani, 10, 'overlap,inverse,before')
def create_wave_functions(self, mode, realspace, nspins, nbands, nao, nvalence, setups, magmom_a, cell_cv, pbc_c): par = self.parameters bzkpts_kc = kpts2ndarray(par.kpts, self.atoms) kd = KPointDescriptor(bzkpts_kc, nspins) self.timer.start('Set symmetry') kd.set_symmetry(self.atoms, self.symmetry, comm=self.world) self.timer.stop('Set symmetry') self.log(kd) parallelization = mpi.Parallelization(self.world, nspins * kd.nibzkpts) parsize_kpt = self.parallel['kpt'] parsize_domain = self.parallel['domain'] parsize_bands = self.parallel['band'] ndomains = None if parsize_domain is not None: ndomains = np.prod(parsize_domain) if mode.name == 'pw': if ndomains is not None and ndomains > 1: raise ValueError('Planewave mode does not support ' 'domain decomposition.') ndomains = 1 parallelization.set(kpt=parsize_kpt, domain=ndomains, band=parsize_bands) comms = parallelization.build_communicators() domain_comm = comms['d'] kpt_comm = comms['k'] band_comm = comms['b'] kptband_comm = comms['D'] domainband_comm = comms['K'] self.comms = comms if par.gpts is not None: if par.h is not None: raise ValueError("""You can't use both "gpts" and "h"!""") N_c = np.array(par.gpts) else: h = par.h if h is not None: h /= Bohr N_c = get_number_of_grid_points(cell_cv, h, mode, realspace, kd.symmetry) self.symmetry.check_grid(N_c) kd.set_communicator(kpt_comm) parstride_bands = self.parallel['stridebands'] # Unfortunately we need to remember that we adjusted the # number of bands so we can print a warning if it differs # from the number specified by the user. (The number can # be inferred from the input parameters, but it's tricky # because we allow negative numbers) self.nbands_parallelization_adjustment = -nbands % band_comm.size nbands += self.nbands_parallelization_adjustment bd = BandDescriptor(nbands, band_comm, parstride_bands) # Construct grid descriptor for coarse grids for wave functions: gd = self.create_grid_descriptor(N_c, cell_cv, pbc_c, domain_comm, parsize_domain) if hasattr(self, 'time') or mode.force_complex_dtype: dtype = complex else: if kd.gamma: dtype = float else: dtype = complex wfs_kwargs = dict(gd=gd, nvalence=nvalence, setups=setups, bd=bd, dtype=dtype, world=self.world, kd=kd, kptband_comm=kptband_comm, timer=self.timer) if self.parallel['sl_auto']: # Choose scalapack parallelization automatically for key, val in self.parallel.items(): if (key.startswith('sl_') and key != 'sl_auto' and val is not None): raise ValueError("Cannot use 'sl_auto' together " "with '%s'" % key) max_scalapack_cpus = bd.comm.size * gd.comm.size nprow = max_scalapack_cpus npcol = 1 # Get a sort of reasonable number of columns/rows while npcol < nprow and nprow % 2 == 0: npcol *= 2 nprow //= 2 assert npcol * nprow == max_scalapack_cpus # ScaLAPACK creates trouble if there aren't at least a few # whole blocks; choose block size so there will always be # several blocks. This will crash for small test systems, # but so will ScaLAPACK in any case blocksize = min(-(-nbands // 4), 64) sl_default = (nprow, npcol, blocksize) else: sl_default = self.parallel['sl_default'] if mode.name == 'lcao': # Layouts used for general diagonalizer sl_lcao = self.parallel['sl_lcao'] if sl_lcao is None: sl_lcao = sl_default lcaoksl = get_KohnSham_layouts(sl_lcao, 'lcao', gd, bd, domainband_comm, dtype, nao=nao, timer=self.timer) self.wfs = mode(lcaoksl, **wfs_kwargs) elif mode.name == 'fd' or mode.name == 'pw': # buffer_size keyword only relevant for fdpw buffer_size = self.parallel['buffer_size'] # Layouts used for diagonalizer sl_diagonalize = self.parallel['sl_diagonalize'] if sl_diagonalize is None: sl_diagonalize = sl_default diagksl = get_KohnSham_layouts( sl_diagonalize, 'fd', # XXX # choice of key 'fd' not so nice gd, bd, domainband_comm, dtype, buffer_size=buffer_size, timer=self.timer) # Layouts used for orthonormalizer sl_inverse_cholesky = self.parallel['sl_inverse_cholesky'] if sl_inverse_cholesky is None: sl_inverse_cholesky = sl_default if sl_inverse_cholesky != sl_diagonalize: message = 'sl_inverse_cholesky != sl_diagonalize ' \ 'is not implemented.' raise NotImplementedError(message) orthoksl = get_KohnSham_layouts(sl_inverse_cholesky, 'fd', gd, bd, domainband_comm, dtype, buffer_size=buffer_size, timer=self.timer) # Use (at most) all available LCAO for initialization lcaonbands = min(nbands, nao // band_comm.size * band_comm.size) try: lcaobd = BandDescriptor(lcaonbands, band_comm, parstride_bands) except RuntimeError: initksl = None else: # Layouts used for general diagonalizer # (LCAO initialization) sl_lcao = self.parallel['sl_lcao'] if sl_lcao is None: sl_lcao = sl_default initksl = get_KohnSham_layouts(sl_lcao, 'lcao', gd, lcaobd, domainband_comm, dtype, nao=nao, timer=self.timer) self.wfs = mode(diagksl, orthoksl, initksl, **wfs_kwargs) else: self.wfs = mode(self, **wfs_kwargs) self.log(self.wfs, '\n')
def initialize(self, atoms=None): """Inexpensive initialization.""" if atoms is None: atoms = self.atoms else: # Save the state of the atoms: self.atoms = atoms.copy() par = self.input_parameters world = par.communicator if world is None: world = mpi.world elif hasattr(world, 'new_communicator'): # Check for whether object has correct type already # # Using isinstance() is complicated because of all the # combinations, serial/parallel/debug... pass else: # world should be a list of ranks: world = mpi.world.new_communicator(np.asarray(world)) self.wfs.world = world self.set_text(par.txt, par.verbose) natoms = len(atoms) pos_av = atoms.get_positions() / Bohr cell_cv = atoms.get_cell() pbc_c = atoms.get_pbc() Z_a = atoms.get_atomic_numbers() magmom_a = atoms.get_initial_magnetic_moments() magnetic = magmom_a.any() spinpol = par.spinpol if par.hund: if natoms != 1: raise ValueError('hund=True arg only valid for single atoms!') spinpol = True if spinpol is None: spinpol = magnetic elif magnetic and not spinpol: raise ValueError('Non-zero initial magnetic moment for a ' 'spin-paired calculation!') nspins = 1 + int(spinpol) if isinstance(par.xc, str): xc = XC(par.xc) else: xc = par.xc setups = Setups(Z_a, par.setups, par.basis, par.lmax, xc, world) # K-point descriptor kd = KPointDescriptor(par.kpts, nspins) width = par.width if width is None: if kd.gamma: width = 0.0 else: width = 0.1 # eV else: assert par.occupations is None if par.gpts is not None and par.h is None: N_c = np.array(par.gpts) else: if par.h is None: self.text('Using default value for grid spacing.') h = 0.2 else: h = par.h N_c = h2gpts(h, cell_cv) cell_cv /= Bohr if hasattr(self, 'time') or par.dtype==complex: dtype = complex else: if kd.gamma: dtype = float else: dtype = complex kd.set_symmetry(atoms, setups, par.usesymm, N_c) nao = setups.nao nvalence = setups.nvalence - par.charge nbands = par.nbands if nbands is None: nbands = nao elif nbands > nao and par.mode == 'lcao': raise ValueError('Too many bands for LCAO calculation: ' + '%d bands and only %d atomic orbitals!' % (nbands, nao)) if nvalence < 0: raise ValueError( 'Charge %f is not possible - not enough valence electrons' % par.charge) M = magmom_a.sum() if par.hund: f_si = setups[0].calculate_initial_occupation_numbers( magmom=0, hund=True, charge=par.charge, nspins=nspins) Mh = f_si[0].sum() - f_si[1].sum() if magnetic and M != Mh: raise RuntimeError('You specified a magmom that does not' 'agree with hunds rule!') else: M = Mh if nbands <= 0: nbands = int(nvalence + M + 0.5) // 2 + (-nbands) if nvalence > 2 * nbands: raise ValueError('Too few bands! Electrons: %d, bands: %d' % (nvalence, nbands)) if par.width is not None: self.text('**NOTE**: please start using ' 'occupations=FermiDirac(width).') if par.fixmom: self.text('**NOTE**: please start using ' 'occupations=FermiDirac(width, fixmagmom=True).') if self.occupations is None: if par.occupations is None: # Create object for occupation numbers: self.occupations = occupations.FermiDirac(width, par.fixmom) else: self.occupations = par.occupations self.occupations.magmom = M cc = par.convergence if par.mode == 'lcao': niter_fixdensity = 0 else: niter_fixdensity = None if self.scf is None: self.scf = SCFLoop( cc['eigenstates'] * nvalence, cc['energy'] / Hartree * max(nvalence, 1), cc['density'] * nvalence, par.maxiter, par.fixdensity, niter_fixdensity) parsize, parsize_bands = par.parallel['domain'], par.parallel['band'] if parsize_bands is None: parsize_bands = 1 # TODO delete/restructure so all checks are in BandDescriptor if nbands % parsize_bands != 0: raise RuntimeError('Cannot distribute %d bands to %d processors' % (nbands, parsize_bands)) if not self.wfs: if parsize == 'domain only': #XXX this was silly! parsize = world.size domain_comm, kpt_comm, band_comm = mpi.distribute_cpus(parsize, parsize_bands, nspins, kd.nibzkpts, world, par.idiotproof) kd.set_communicator(kpt_comm) parstride_bands = par.parallel['stridebands'] bd = BandDescriptor(nbands, band_comm, parstride_bands) if (self.density is not None and self.density.gd.comm.size != domain_comm.size): # Domain decomposition has changed, so we need to # reinitialize density and hamiltonian: if par.fixdensity: raise RuntimeError("I'm confused - please specify parsize." ) self.density = None self.hamiltonian = None # Construct grid descriptor for coarse grids for wave functions: gd = self.grid_descriptor_class(N_c, cell_cv, pbc_c, domain_comm, parsize) # do k-point analysis here? XXX args = (gd, nvalence, setups, bd, dtype, world, kd, self.timer) if par.mode == 'lcao': # Layouts used for general diagonalizer sl_lcao = par.parallel['sl_lcao'] if sl_lcao is None: sl_lcao = par.parallel['sl_default'] lcaoksl = get_KohnSham_layouts(sl_lcao, 'lcao', gd, bd, dtype, nao=nao, timer=self.timer) self.wfs = LCAOWaveFunctions(lcaoksl, *args) elif par.mode == 'fd' or isinstance(par.mode, PW): # buffer_size keyword only relevant for fdpw buffer_size = par.parallel['buffer_size'] # Layouts used for diagonalizer sl_diagonalize = par.parallel['sl_diagonalize'] if sl_diagonalize is None: sl_diagonalize = par.parallel['sl_default'] diagksl = get_KohnSham_layouts(sl_diagonalize, 'fd', gd, bd, dtype, buffer_size=buffer_size, timer=self.timer) # Layouts used for orthonormalizer sl_inverse_cholesky = par.parallel['sl_inverse_cholesky'] if sl_inverse_cholesky is None: sl_inverse_cholesky = par.parallel['sl_default'] if sl_inverse_cholesky != sl_diagonalize: message = 'sl_inverse_cholesky != sl_diagonalize ' \ 'is not implemented.' raise NotImplementedError(message) orthoksl = get_KohnSham_layouts(sl_inverse_cholesky, 'fd', gd, bd, dtype, buffer_size=buffer_size, timer=self.timer) # Use (at most) all available LCAO for initialization lcaonbands = min(nbands, nao) lcaobd = BandDescriptor(lcaonbands, band_comm, parstride_bands) assert nbands <= nao or bd.comm.size == 1 assert lcaobd.mynbands == min(bd.mynbands, nao) #XXX # Layouts used for general diagonalizer (LCAO initialization) sl_lcao = par.parallel['sl_lcao'] if sl_lcao is None: sl_lcao = par.parallel['sl_default'] initksl = get_KohnSham_layouts(sl_lcao, 'lcao', gd, lcaobd, dtype, nao=nao, timer=self.timer) if par.mode == 'fd': self.wfs = FDWaveFunctions(par.stencils[0], diagksl, orthoksl, initksl, *args) else: # Planewave basis: self.wfs = par.mode(diagksl, orthoksl, initksl, gd, nvalence, setups, bd, world, kd, self.timer) else: self.wfs = par.mode(self, *args) else: self.wfs.set_setups(setups) if not self.wfs.eigensolver: # Number of bands to converge: nbands_converge = cc['bands'] if nbands_converge == 'all': nbands_converge = nbands elif nbands_converge != 'occupied': assert isinstance(nbands_converge, int) if nbands_converge < 0: nbands_converge += nbands eigensolver = get_eigensolver(par.eigensolver, par.mode, par.convergence) eigensolver.nbands_converge = nbands_converge # XXX Eigensolver class doesn't define an nbands_converge property self.wfs.set_eigensolver(eigensolver) if self.density is None: gd = self.wfs.gd if par.stencils[1] != 9: # Construct grid descriptor for fine grids for densities # and potentials: finegd = gd.refine() else: # Special case (use only coarse grid): finegd = gd self.density = Density(gd, finegd, nspins, par.charge + setups.core_charge) self.density.initialize(setups, par.stencils[1], self.timer, magmom_a, par.hund) self.density.set_mixer(par.mixer) if self.hamiltonian is None: gd, finegd = self.density.gd, self.density.finegd self.hamiltonian = Hamiltonian(gd, finegd, nspins, setups, par.stencils[1], self.timer, xc, par.poissonsolver, par.external) xc.initialize(self.density, self.hamiltonian, self.wfs, self.occupations) self.text() self.print_memory_estimate(self.txt, maxdepth=memory_estimate_depth) self.txt.flush() if dry_run: self.dry_run() self.initialized = True
class PhononCalculator: """This class defines the interface for phonon calculations.""" def __init__(self, calc, gamma=True, symmetry=False, e_ph=False, communicator=serial_comm): """Inititialize class with a list of atoms. The atoms object must contain a converged ground-state calculation. The set of q-vectors in which the dynamical matrix will be calculated is determined from the ``symmetry`` kwarg. For now, only time-reversal symmetry is used to generate the irrecducible BZ. Add a little note on parallelization strategy here. Parameters ---------- calc: str or Calculator Calculator containing a ground-state calculation. gamma: bool Gamma-point calculation with respect to the q-vector of the dynamical matrix. When ``False``, the Monkhorst-Pack grid from the ground-state calculation is used. symmetry: bool Use symmetries to reduce the q-vectors of the dynamcial matrix (None, False or True). The different options are equivalent to the old style options in a ground-state calculation (see usesymm). e_ph: bool Save the derivative of the effective potential. communicator: Communicator Communicator for parallelization over k-points and real-space domain. """ # XXX assert symmetry in [None, False], "Spatial symmetries not allowed yet" if isinstance(calc, str): self.calc = GPAW(calc, communicator=serial_comm, txt=None) else: self.calc = calc cell_cv = self.calc.atoms.get_cell() setups = self.calc.wfs.setups # XXX - no clue how to get magmom - ignore it for the moment # m_av = magmom_av.round(decimals=3) # round off # id_a = zip(setups.id_a, *m_av.T) id_a = setups.id_a if symmetry is None: self.symmetry = Symmetry(id_a, cell_cv, point_group=False, time_reversal=False) else: self.symmetry = Symmetry(id_a, cell_cv, point_group=False, time_reversal=True) # Make sure localized functions are initialized self.calc.set_positions() # Note that this under some circumstances (e.g. when called twice) # allocates a new array for the P_ani coefficients !! # Store useful objects self.atoms = self.calc.get_atoms() # Get rid of ``calc`` attribute self.atoms.calc = None # Boundary conditions pbc_c = self.calc.atoms.get_pbc() if np.all(pbc_c == False): self.gamma = True self.dtype = float kpts = None # Multigrid Poisson solver poisson_solver = PoissonSolver() else: if gamma: self.gamma = True self.dtype = float kpts = None else: self.gamma = False self.dtype = complex # Get k-points from ground-state calculation kpts = self.calc.input_parameters.kpts # FFT Poisson solver poisson_solver = FFTPoissonSolver(dtype=self.dtype) # K-point descriptor for the q-vectors of the dynamical matrix # Note, no explicit parallelization here. self.kd = KPointDescriptor(kpts, 1) self.kd.set_symmetry(self.atoms, self.symmetry) self.kd.set_communicator(serial_comm) # Number of occupied bands nvalence = self.calc.wfs.nvalence nbands = nvalence // 2 + nvalence % 2 assert nbands <= self.calc.wfs.bd.nbands # Extract other useful objects # Ground-state k-point descriptor - used for the k-points in the # ResponseCalculator # XXX replace communicators when ready to parallelize kd_gs = self.calc.wfs.kd gd = self.calc.density.gd kpt_u = self.calc.wfs.kpt_u setups = self.calc.wfs.setups dtype_gs = self.calc.wfs.dtype # WaveFunctions wfs = WaveFunctions(nbands, kpt_u, setups, kd_gs, gd, dtype=dtype_gs) # Linear response calculator self.response_calc = ResponseCalculator(self.calc, wfs, dtype=self.dtype) # Phonon perturbation self.perturbation = PhononPerturbation(self.calc, self.kd, poisson_solver, dtype=self.dtype) # Dynamical matrix self.dyn = DynamicalMatrix(self.atoms, self.kd, dtype=self.dtype) # Electron-phonon couplings if e_ph: self.e_ph = ElectronPhononCoupling(self.atoms, gd, self.kd, dtype=self.dtype) else: self.e_ph = None # Initialization flag self.initialized = False # Parallel communicator for parallelization over kpts and domain self.comm = communicator def initialize(self): """Initialize response calculator and perturbation.""" # Get scaled atomic positions spos_ac = self.atoms.get_scaled_positions() self.perturbation.initialize(spos_ac) self.response_calc.initialize(spos_ac) self.initialized = True def __getstate__(self): """Method used when pickling. Bound method attributes cannot be pickled and must therefore be deleted before an instance is dumped to file. """ # Get state of object and take care of troublesome attributes state = dict(self.__dict__) state['kd'].__dict__['comm'] = serial_comm state.pop('calc') state.pop('perturbation') state.pop('response_calc') return state def run(self, qpts_q=None, clean=False, name=None, path=None): """Run calculation for atomic displacements and update matrix. Parameters ---------- qpts: List List of q-points indices for which the dynamical matrix will be calculated (only temporary). """ if not self.initialized: self.initialize() if self.gamma: qpts_q = [0] elif qpts_q is None: qpts_q = range(self.kd.nibzkpts) else: assert isinstance(qpts_q, list) # Update name and path attributes self.set_name_and_path(name=name, path=path) # Get string template for filenames filename_str = self.get_filename_string() # Delay the ranks belonging to the same k-point/domain decomposition # equally time.sleep(rank // self.comm.size) # XXX Make a single ground_state_contributions member function # Ground-state contributions to the force constants self.dyn.density_ground_state(self.calc) # self.dyn.wfs_ground_state(self.calc, self.response_calc) # Calculate linear response wrt q-vectors and displacements of atoms for q in qpts_q: if not self.gamma: self.perturbation.set_q(q) # First-order contributions to the force constants for a in self.dyn.indices: for v in [0, 1, 2]: # Check if the calculation has already been done filename = filename_str % (q, a, v) # Wait for all sub-ranks to enter self.comm.barrier() if os.path.isfile(os.path.join(self.path, filename)): continue if self.comm.rank == 0: fd = open(os.path.join(self.path, filename), 'w') # Wait for all sub-ranks here self.comm.barrier() components = ['x', 'y', 'z'] symbols = self.atoms.get_chemical_symbols() print("q-vector index: %i" % q) print("Atom index: %i" % a) print("Atomic symbol: %s" % symbols[a]) print("Component: %s" % components[v]) # Set atom and cartesian component of perturbation self.perturbation.set_av(a, v) # Calculate linear response self.response_calc(self.perturbation) # Calculate row of the matrix of force constants self.dyn.calculate_row(self.perturbation, self.response_calc) # Write force constants to file if self.comm.rank == 0: self.dyn.write(fd, q, a, v) fd.close() # Store effective potential derivative if self.e_ph is not None: v1_eff_G = self.perturbation.v1_G + \ self.response_calc.vHXC1_G self.e_ph.v1_eff_qavG.append(v1_eff_G) # Wait for the file-writing rank here self.comm.barrier() # XXX # Check that all files are valid and collect in a single file # Remove the files if clean: self.clean() def get_atoms(self): """Return atoms.""" return self.atoms def get_dynamical_matrix(self): """Return reference to ``dyn`` attribute.""" return self.dyn def get_filename_string(self): """Return string template for force constant filenames.""" name_str = (self.name + '.' + 'q_%%0%ii_' % len(str(self.kd.nibzkpts)) + 'a_%%0%ii_' % len(str(len(self.atoms))) + 'v_%i' + '.pckl') return name_str def set_atoms(self, atoms): """Set atoms to be included in the calculation. Parameters ---------- atoms: list Can be either a list of strings, ints or ... """ assert isinstance(atoms, list) if isinstance(atoms[0], str): assert np.all([isinstance(atom, str) for atom in atoms]) sym_a = self.atoms.get_chemical_symbols() # List for atomic indices indices = [] for type in atoms: indices.extend( [a for a, atom in enumerate(sym_a) if atom == type]) else: assert np.all([isinstance(atom, int) for atom in atoms]) indices = atoms self.dyn.set_indices(indices) def set_name_and_path(self, name=None, path=None): """Set name and path of the force constant files. name: str Base name for the files which the elements of the matrix of force constants will be written to. path: str Path specifying the directory where the files will be dumped. """ if name is None: self.name = 'phonon.' + self.atoms.get_chemical_formula() else: self.name = name # self.name += '.nibzkpts_%i' % self.kd.nibzkpts if path is None: self.path = '.' else: self.path = path # Set corresponding attributes in the ``dyn`` attribute filename_str = self.get_filename_string() self.dyn.set_name_and_path(filename_str, self.path) def clean(self): """Delete generated files.""" filename_str = self.get_filename_string() for q in range(self.kd.nibzkpts): for a in range(len(self.atoms)): for v in [0, 1, 2]: filename = filename_str % (q, a, v) if os.path.isfile(os.path.join(self.path, filename)): os.remove(filename) def band_structure(self, path_kc, modes=False, acoustic=True): """Calculate phonon dispersion along a path in the Brillouin zone. The dynamical matrix at arbitrary q-vectors is obtained by Fourier transforming the real-space matrix. In case of negative eigenvalues (squared frequency), the corresponding negative frequency is returned. Parameters ---------- path_kc: ndarray List of k-point coordinates (in units of the reciprocal lattice vectors) specifying the path in the Brillouin zone for which the dynamical matrix will be calculated. modes: bool Returns both frequencies and modes (mass scaled) when True. acoustic: bool Restore the acoustic sum-rule in the calculated force constants. """ for k_c in path_kc: assert np.all(np.asarray(k_c) <= 1.0), \ "Scaled coordinates must be given" # Assemble the dynanical matrix from calculated force constants self.dyn.assemble(acoustic=acoustic) # Get the dynamical matrix in real-space DR_lmn, R_clmn = self.dyn.real_space() # Reshape for the evaluation of the fourier sums shape = DR_lmn.shape DR_m = DR_lmn.reshape((-1, ) + shape[-2:]) R_cm = R_clmn.reshape((3, -1)) # Lists for frequencies and modes along path omega_kn = [] u_kn = [] # Number of atoms included N = len(self.dyn.get_indices()) # Mass prefactor for the normal modes m_inv_av = self.dyn.get_mass_array() for q_c in path_kc: # Evaluate fourier transform phase_m = np.exp(-2.j * pi * np.dot(q_c, R_cm)) # Dynamical matrix in unit of Ha / Bohr**2 / amu D_q = np.sum(phase_m[:, np.newaxis, np.newaxis] * DR_m, axis=0) if modes: omega2_n, u_avn = la.eigh(D_q, UPLO='L') # Sort eigenmodes according to eigenvalues (see below) and # multiply with mass prefactor u_nav = u_avn[:, omega2_n.argsort()].T.copy() * m_inv_av # Multiply with mass prefactor u_kn.append(u_nav.reshape((3 * N, -1, 3))) else: omega2_n = la.eigvalsh(D_q, UPLO='L') # Sort eigenvalues in increasing order omega2_n.sort() # Use dtype=complex to handle negative eigenvalues omega_n = np.sqrt(omega2_n.astype(complex)) # Take care of imaginary frequencies if not np.all(omega2_n >= 0.): indices = np.where(omega2_n < 0)[0] print(("WARNING, %i imaginary frequencies at " "q = (% 5.2f, % 5.2f, % 5.2f) ; (omega_q =% 5.3e*i)" % (len(indices), q_c[0], q_c[1], q_c[2], omega_n[indices][0].imag))) omega_n[indices] = -1 * np.sqrt(np.abs(omega2_n[indices].real)) omega_kn.append(omega_n.real) # Conversion factor from sqrt(Ha / Bohr**2 / amu) -> eV s = units.Hartree**0.5 * units._hbar * 1.e10 / \ (units._e * units._amu)**(0.5) / units.Bohr # Convert to eV and Ang omega_kn = s * np.asarray(omega_kn) if modes: u_kn = np.asarray(u_kn) * units.Bohr return omega_kn, u_kn return omega_kn def write_modes(self, q_c, branches=0, kT=units.kB * 300, repeat=(1, 1, 1), nimages=30, acoustic=True): """Write mode to trajectory file. The classical equipartioning theorem states that each normal mode has an average energy:: <E> = 1/2 * k_B * T = 1/2 * omega^2 * Q^2 => Q = sqrt(k_B*T) / omega at temperature T. Here, Q denotes the normal coordinate of the mode. Parameters ---------- q_c: ndarray q-vector of the modes. branches: int or list Branch index of calculated modes. kT: float Temperature in units of eV. Determines the amplitude of the atomic displacements in the modes. repeat: tuple Repeat atoms (l, m, n) times in the directions of the lattice vectors. Displacements of atoms in repeated cells carry a Bloch phase factor given by the q-vector and the cell lattice vector R_m. nimages: int Number of images in an oscillation. """ if isinstance(branches, int): branch_n = [branches] else: branch_n = list(branches) # Calculate modes omega_n, u_n = self.band_structure([q_c], modes=True, acoustic=acoustic) # Repeat atoms atoms = self.atoms * repeat pos_mav = atoms.positions.copy() # Total number of unit cells M = np.prod(repeat) # Corresponding lattice vectors R_m R_cm = np.indices(repeat[::-1]).reshape(3, -1)[::-1] # Bloch phase phase_m = np.exp(2.j * pi * np.dot(q_c, R_cm)) phase_ma = phase_m.repeat(len(self.atoms)) for n in branch_n: omega = omega_n[0, n] u_av = u_n[0, n] # .reshape((-1, 3)) # Mean displacement at high T ? u_av *= sqrt(kT / abs(omega)) mode_av = np.zeros((len(self.atoms), 3), dtype=self.dtype) indices = self.dyn.get_indices() mode_av[indices] = u_av mode_mav = (np.vstack([mode_av] * M) * phase_ma[:, np.newaxis]).real traj = Trajectory('%s.mode.%d.traj' % (self.name, n), 'w') for x in np.linspace(0, 2 * pi, nimages, endpoint=False): # XXX Is it correct to take out the sine component here ? atoms.set_positions(pos_mav + sin(x) * mode_mav) traj.write(atoms) traj.close()
def initialize(self, density, hamiltonian, wfs, occupations): self.xc.initialize(density, hamiltonian, wfs, occupations) self.nspins = wfs.nspins self.setups = wfs.setups self.density = density self.kpt_u = wfs.kpt_u self.wfs = wfs self.gd = density.gd self.kd = wfs.kd self.bd = wfs.bd N_c = self.gd.N_c N = self.gd.N_c.prod() vol = self.gd.dv * N if self.alpha is None: # XXX ? self.alpha = 6 * vol**(2 / 3.0) / pi**2 self.gamma = (vol / (2 * pi)**2 * sqrt(pi / self.alpha) * self.kd.nbzkpts) if self.ecut is None: self.ecut = 0.5 * pi**2 / (self.gd.h_cv**2).sum(1).max() * 0.9999 assert self.kd.N_c is not None n = self.kd.N_c * 2 - 1 bzk_kc = np.indices(n).transpose((1, 2, 3, 0)) bzk_kc.shape = (-1, 3) bzk_kc -= self.kd.N_c - 1 self.bzk_kc = bzk_kc.astype(float) / self.kd.N_c self.bzq_qc = self.kd.get_bz_q_points() if self.qsym: op_scc = self.kd.symmetry.op_scc self.ibzq_qc = self.kd.get_ibz_q_points(self.bzq_qc, op_scc)[0] self.q_weights = self.kd.q_weights * len(self.bzq_qc) else: self.ibzq_qc = self.bzq_qc self.q_weights = np.ones(len(self.bzq_qc)) self.pwd = PWDescriptor(self.ecut, self.gd, complex) self.G2_qG = self.pwd.g2(self.bzk_kc) n = 0 for k_c, Gpk2_G in zip(self.bzk_kc[:], self.G2_qG): if (k_c > -0.5).all() and (k_c <= 0.5).all(): #XXX??? if k_c.any(): self.gamma -= np.dot(np.exp(-self.alpha * Gpk2_G), Gpk2_G**-1) else: self.gamma -= np.dot(np.exp(-self.alpha * Gpk2_G[1:]), Gpk2_G[1:]**-1) n += 1 assert n == self.kd.N_c.prod() self.pwd = PWDescriptor(self.ecut, self.gd, complex) self.G2_qG = self.pwd.g2(self.ibzq_qc) self.ghat = LFC(self.gd, [setup.ghat_l for setup in density.setups], KPointDescriptor(self.bzq_qc), dtype=complex) #self.interpolator = density.interpolator self.print_initialization(hamiltonian.xc.name)
def __init__(self, calc, gamma=True, symmetry=False, e_ph=False, communicator=serial_comm): """Inititialize class with a list of atoms. The atoms object must contain a converged ground-state calculation. The set of q-vectors in which the dynamical matrix will be calculated is determined from the ``symmetry`` kwarg. For now, only time-reversal symmetry is used to generate the irrecducible BZ. Add a little note on parallelization strategy here. Parameters ---------- calc: str or Calculator Calculator containing a ground-state calculation. gamma: bool Gamma-point calculation with respect to the q-vector of the dynamical matrix. When ``False``, the Monkhorst-Pack grid from the ground-state calculation is used. symmetry: bool Use symmetries to reduce the q-vectors of the dynamcial matrix (None, False or True). The different options are equivalent to the old style options in a ground-state calculation (see usesymm). e_ph: bool Save the derivative of the effective potential. communicator: Communicator Communicator for parallelization over k-points and real-space domain. """ # XXX assert symmetry in [None, False], "Spatial symmetries not allowed yet" if isinstance(calc, str): self.calc = GPAW(calc, communicator=serial_comm, txt=None) else: self.calc = calc cell_cv = self.calc.atoms.get_cell() setups = self.calc.wfs.setups # XXX - no clue how to get magmom - ignore it for the moment # m_av = magmom_av.round(decimals=3) # round off # id_a = zip(setups.id_a, *m_av.T) id_a = setups.id_a if symmetry is None: self.symmetry = Symmetry(id_a, cell_cv, point_group=False, time_reversal=False) else: self.symmetry = Symmetry(id_a, cell_cv, point_group=False, time_reversal=True) # Make sure localized functions are initialized self.calc.set_positions() # Note that this under some circumstances (e.g. when called twice) # allocates a new array for the P_ani coefficients !! # Store useful objects self.atoms = self.calc.get_atoms() # Get rid of ``calc`` attribute self.atoms.calc = None # Boundary conditions pbc_c = self.calc.atoms.get_pbc() if np.all(pbc_c == False): self.gamma = True self.dtype = float kpts = None # Multigrid Poisson solver poisson_solver = PoissonSolver() else: if gamma: self.gamma = True self.dtype = float kpts = None else: self.gamma = False self.dtype = complex # Get k-points from ground-state calculation kpts = self.calc.input_parameters.kpts # FFT Poisson solver poisson_solver = FFTPoissonSolver(dtype=self.dtype) # K-point descriptor for the q-vectors of the dynamical matrix # Note, no explicit parallelization here. self.kd = KPointDescriptor(kpts, 1) self.kd.set_symmetry(self.atoms, self.symmetry) self.kd.set_communicator(serial_comm) # Number of occupied bands nvalence = self.calc.wfs.nvalence nbands = nvalence // 2 + nvalence % 2 assert nbands <= self.calc.wfs.bd.nbands # Extract other useful objects # Ground-state k-point descriptor - used for the k-points in the # ResponseCalculator # XXX replace communicators when ready to parallelize kd_gs = self.calc.wfs.kd gd = self.calc.density.gd kpt_u = self.calc.wfs.kpt_u setups = self.calc.wfs.setups dtype_gs = self.calc.wfs.dtype # WaveFunctions wfs = WaveFunctions(nbands, kpt_u, setups, kd_gs, gd, dtype=dtype_gs) # Linear response calculator self.response_calc = ResponseCalculator(self.calc, wfs, dtype=self.dtype) # Phonon perturbation self.perturbation = PhononPerturbation(self.calc, self.kd, poisson_solver, dtype=self.dtype) # Dynamical matrix self.dyn = DynamicalMatrix(self.atoms, self.kd, dtype=self.dtype) # Electron-phonon couplings if e_ph: self.e_ph = ElectronPhononCoupling(self.atoms, gd, self.kd, dtype=self.dtype) else: self.e_ph = None # Initialization flag self.initialized = False # Parallel communicator for parallelization over kpts and domain self.comm = communicator
class HybridXC(XCFunctional): orbital_dependent = True def __init__(self, name, hybrid=None, xc=None, finegrid=False, alpha=None): """Mix standard functionals with exact exchange. name: str Name of hybrid functional. hybrid: float Fraction of exact exchange. xc: str or XCFunctional object Standard DFT functional with scaled down exchange. finegrid: boolean Use fine grid for energy functional evaluations? """ if name == 'EXX': assert hybrid is None and xc is None hybrid = 1.0 xc = XC(XCNull()) elif name == 'PBE0': assert hybrid is None and xc is None hybrid = 0.25 xc = XC('HYB_GGA_XC_PBEH') elif name == 'B3LYP': assert hybrid is None and xc is None hybrid = 0.2 xc = XC('HYB_GGA_XC_B3LYP') if isinstance(xc, str): xc = XC(xc) self.hybrid = hybrid self.xc = xc self.type = xc.type self.alpha = alpha self.exx = 0.0 XCFunctional.__init__(self, name) def get_setup_name(self): return 'PBE' def calculate_radial(self, rgd, n_sLg, Y_L, v_sg, dndr_sLg=None, rnablaY_Lv=None, tau_sg=None, dedtau_sg=None): return self.xc.calculate_radial(rgd, n_sLg, Y_L, v_sg, dndr_sLg, rnablaY_Lv) def initialize(self, density, hamiltonian, wfs, occupations): self.xc.initialize(density, hamiltonian, wfs, occupations) self.nspins = wfs.nspins self.setups = wfs.setups self.density = density self.kpt_u = wfs.kpt_u self.gd = density.gd self.kd = wfs.kd self.bd = wfs.bd N_c = self.gd.N_c N = self.gd.N_c.prod() vol = self.gd.dv * N if self.alpha is None: self.alpha = 6 * vol**(2 / 3.0) / pi**2 self.gamma = (vol / (2 * pi)**2 * sqrt(pi / self.alpha) * self.kd.nbzkpts) ecut = 0.5 * pi**2 / (self.gd.h_cv**2).sum(1).max() if self.kd.N_c is None: self.bzk_kc = np.zeros((1, 3)) dfghdfgh else: n = self.kd.N_c * 2 - 1 bzk_kc = np.indices(n).transpose((1, 2, 3, 0)) bzk_kc.shape = (-1, 3) bzk_kc -= self.kd.N_c - 1 self.bzk_kc = bzk_kc.astype(float) / self.kd.N_c self.pwd = PWDescriptor(ecut, self.gd, self.bzk_kc) n = 0 for k_c, Gpk2_G in zip(self.bzk_kc[:], self.pwd.G2_qG): if (k_c > -0.5).all() and (k_c <= 0.5).all(): #XXX??? if k_c.any(): self.gamma -= np.dot(np.exp(-self.alpha * Gpk2_G), Gpk2_G**-1) else: self.gamma -= np.dot(np.exp(-self.alpha * Gpk2_G[1:]), Gpk2_G[1:]**-1) n += 1 assert n == self.kd.N_c.prod() self.ghat = LFC(self.gd, [setup.ghat_l for setup in density.setups], dtype=complex ) self.ghat.set_k_points(self.bzk_kc) self.fullkd = KPointDescriptor(self.kd.bzk_kc, nspins=1) class S: id_a = [] def set_symmetry(self, s): pass self.fullkd.set_symmetry(Atoms(pbc=True), S(), False) self.fullkd.set_communicator(world) self.pt = LFC(self.gd, [setup.pt_j for setup in density.setups], dtype=complex) self.pt.set_k_points(self.fullkd.ibzk_kc) self.interpolator = density.interpolator def set_positions(self, spos_ac): self.ghat.set_positions(spos_ac) self.pt.set_positions(spos_ac) def calculate(self, gd, n_sg, v_sg=None, e_g=None): # Normal XC contribution: exc = self.xc.calculate(gd, n_sg, v_sg, e_g) # Add EXX contribution: return exc + self.exx def calculate_exx(self): """Non-selfconsistent calculation.""" kd = self.kd K = self.fullkd.nibzkpts assert self.nspins == 1 Q = K // world.size assert Q * world.size == K parallel = (world.size > self.nspins) self.exx = 0.0 self.exx_skn = np.zeros((self.nspins, K, self.bd.nbands)) kpt_u = [] for k in range(world.rank * Q, (world.rank + 1) * Q): k_c = self.fullkd.ibzk_kc[k] for k1, k1_c in enumerate(kd.bzk_kc): if abs(k1_c - k_c).max() < 1e-10: break # Index of symmetry related point in the irreducible BZ ik = kd.kibz_k[k1] kpt = self.kpt_u[ik] # KPoint from ground-state calculation phase_cd = np.exp(2j * pi * self.gd.sdisp_cd * k_c[:, np.newaxis]) kpt2 = KPoint0(kpt.weight, kpt.s, k, None, phase_cd) kpt2.psit_nG = np.empty_like(kpt.psit_nG) kpt2.f_n = kpt.f_n / kpt.weight / K * 2 for n, psit_G in enumerate(kpt2.psit_nG): psit_G[:] = kd.transform_wave_function(kpt.psit_nG[n], k1) kpt2.P_ani = self.pt.dict(len(kpt.psit_nG)) self.pt.integrate(kpt2.psit_nG, kpt2.P_ani, k) kpt_u.append(kpt2) for s in range(self.nspins): kpt1_q = [KPoint(self.fullkd, kpt) for kpt in kpt_u if kpt.s == s] kpt2_q = kpt1_q[:] if len(kpt1_q) == 0: # No s-spins on this CPU: continue # Send rank: srank = self.fullkd.get_rank_and_index(s, (kpt1_q[0].k - 1) % K)[0] # Receive rank: rrank = self.fullkd.get_rank_and_index(s, (kpt1_q[-1].k + 1) % K)[0] # Shift k-points K // 2 times: for i in range(K // 2 + 1): if i < K // 2: if parallel: kpt = kpt2_q[-1].next() kpt.start_receiving(rrank) kpt2_q[0].start_sending(srank) else: kpt = kpt2_q[0] for kpt1, kpt2 in zip(kpt1_q, kpt2_q): if 2 * i == K: self.apply(kpt1, kpt2, invert=(kpt1.k > kpt2.k)) else: self.apply(kpt1, kpt2) self.apply(kpt1, kpt2, invert=True) if i < K // 2: if parallel: kpt.wait() kpt2_q[0].wait() kpt2_q.pop(0) kpt2_q.append(kpt) self.exx = world.sum(self.exx) world.sum(self.exx_skn) self.exx += self.calculate_paw_correction() def apply(self, kpt1, kpt2, invert=False): #print world.rank,kpt1.k,kpt2.k,invert k1_c = self.fullkd.ibzk_kc[kpt1.k] k2_c = self.fullkd.ibzk_kc[kpt2.k] if invert: k2_c = -k2_c k12_c = k1_c - k2_c N_c = self.gd.N_c eikr_R = np.exp(2j * pi * np.dot(np.indices(N_c).T, k12_c / N_c).T) for q, k_c in enumerate(self.bzk_kc): if abs(k_c + k12_c).max() < 1e-9: q0 = q break for q, k_c in enumerate(self.bzk_kc): if abs(k_c - k12_c).max() < 1e-9: q00 = q break Gpk2_G = self.pwd.G2_qG[q0] if Gpk2_G[0] == 0: Gpk2_G = Gpk2_G.copy() Gpk2_G[0] = 1.0 / self.gamma N = N_c.prod() vol = self.gd.dv * N nspins = self.nspins same = (kpt1.k == kpt2.k) for n1, psit1_R in enumerate(kpt1.psit_nG): f1 = kpt1.f_n[n1] for n2, psit2_R in enumerate(kpt2.psit_nG): if same and n2 > n1: continue f2 = kpt2.f_n[n2] nt_R = self.calculate_pair_density(n1, n2, kpt1, kpt2, q0, invert) nt_G = self.pwd.fft(nt_R * eikr_R) / N vt_G = nt_G.copy() vt_G *= -pi * vol / Gpk2_G e = np.vdot(nt_G, vt_G).real * nspins * self.hybrid if same and n1 == n2: e /= 2 self.exx += e * f1 * f2 self.ekin -= 2 * e * f1 * f2 self.exx_skn[kpt1.s, kpt1.k, n1] += f2 * e self.exx_skn[kpt2.s, kpt2.k, n2] += f1 * e calculate_potential = not True if calculate_potential: vt_R = self.pwd.ifft(vt_G).conj() * eikr_R * N / vol if kpt1 is kpt2 and not invert and n1 == n2: kpt1.vt_nG[n1] = 0.5 * f1 * vt_R if invert: kpt1.Htpsit_nG[n1] += \ f2 * nspins * psit2_R.conj() * vt_R else: kpt1.Htpsit_nG[n1] += f2 * nspins * psit2_R * vt_R if kpt1 is not kpt2: if invert: kpt2.Htpsit_nG[n2] += (f1 * nspins * psit1_R.conj() * vt_R) else: kpt2.Htpsit_nG[n2] += (f1 * nspins * psit1_R * vt_R.conj()) def calculate_paw_correction(self): exx = 0 deg = 2 // self.nspins # spin degeneracy for a, D_sp in self.density.D_asp.items(): setup = self.setups[a] for D_p in D_sp: D_ii = unpack2(D_p) ni = len(D_ii) for i1 in range(ni): for i2 in range(ni): A = 0.0 for i3 in range(ni): p13 = packed_index(i1, i3, ni) for i4 in range(ni): p24 = packed_index(i2, i4, ni) A += setup.M_pp[p13, p24] * D_ii[i3, i4] p12 = packed_index(i1, i2, ni) exx -= self.hybrid / deg * D_ii[i1, i2] * A if setup.X_p is not None: exx -= self.hybrid * np.dot(D_p, setup.X_p) exx += self.hybrid * setup.ExxC return exx def calculate_pair_density(self, n1, n2, kpt1, kpt2, q, invert): if invert: nt_G = kpt1.psit_nG[n1].conj() * kpt2.psit_nG[n2].conj() else: nt_G = kpt1.psit_nG[n1].conj() * kpt2.psit_nG[n2] Q_aL = {} for a, P1_ni in kpt1.P_ani.items(): P1_i = P1_ni[n1] P2_i = kpt2.P_ani[a][n2] if invert: D_ii = np.outer(P1_i.conj(), P2_i.conj()) else: D_ii = np.outer(P1_i.conj(), P2_i) D_p = pack(D_ii) Q_aL[a] = np.dot(D_p, self.setups[a].Delta_pL) self.ghat.add(nt_G, Q_aL, q) return nt_G
def __init__(self, calc, nbands=None, Fock=False): self.calc = calc self.Fock = Fock self.K = calc.get_ibz_k_points() # reduced Brillioun zone self.NK = self.K.shape[0] self.wk = calc.get_k_point_weights( ) # weight of reduced Brillioun zone if nbands is None: self.nbands = calc.get_number_of_bands() else: self.nbands = nbands self.nvalence = int(calc.get_number_of_electrons() / 2) self.EK = [ calc.get_eigenvalues(k)[:self.nbands] for k in range(self.NK) ] # bands energy self.EK = np.array(self.EK) / Hartree self.shape = tuple( calc.get_number_of_grid_points()) # shape of real space grid self.density = calc.get_pseudo_density( ) * Bohr**3 # density at zero time # array of u_nk (periodic part of Kohn-Sham orbitals,only reduced Brillion zone) self.ukn = np.zeros(( self.NK, self.nbands, ) + self.shape, dtype=np.complex) for k in range(self.NK): kpt = calc.wfs.kpt_u[k] for n in range(self.nbands): psit_G = kpt.psit_nG[n] psit_R = calc.wfs.pd.ifft(psit_G, kpt.q) self.ukn[k, n] = psit_R self.icell = 2.0 * np.pi * calc.wfs.gd.icell_cv # inverse cell self.cell = calc.wfs.gd.cell_cv # cell self.r = calc.wfs.gd.get_grid_point_coordinates() for i in range(3): self.r[i] -= self.cell[i, i] / 2. self.volume = np.abs(np.linalg.det( calc.wfs.gd.cell_cv)) # volume of cell self.norm = calc.wfs.gd.dv # self.Fermi = calc.get_fermi_level() / Hartree #Fermi level #desriptors at q=gamma for Hartree self.kdH = KPointDescriptor([[0, 0, 0]]) self.pdH = PWDescriptor(ecut=calc.wfs.pd.ecut, gd=calc.wfs.gd, kd=self.kdH, dtype=complex) #desriptors at q=gamma for Fock self.kdF = KPointDescriptor([[0, 0, 0]]) self.pdF = PWDescriptor(ecut=calc.wfs.pd.ecut / 4., gd=calc.wfs.gd, kd=self.kdF, dtype=complex) #Fermi-Dirac temperature self.temperature = calc.occupations.width #calculate pair-density matrices if Fock: self.M = np.zeros((self.nbands, self.nbands, self.NK, self.NK, self.pdF.get_reciprocal_vectors().shape[0]), dtype=np.complex) indexes = [(n, k) for n, k in product(range(self.nbands), range(self.NK))] for i1 in range(len(indexes)): n1, k1 = indexes[i1] for i2 in range(i1, len(indexes)): n2, k2 = indexes[i1] self.M[n1, n2, k1, k2] = self.pdF.fft( self.ukn[k1, n1].conj() * self.ukn[k2, n2]) self.M[n2, n1, k2, k1] = self.M[n1, n2, k1, k2].conj() self.M *= calc.wfs.gd.dv #Fermi-Dirac distribution self.f = 1 / (1 + np.exp((self.EK - self.Fermi) / self.temperature)) self.Hartree_elements = np.zeros( (self.NK, self.nbands, self.NK, self.nbands, self.nbands), dtype=np.complex) self.LDAx_elements = np.zeros( (self.NK, self.nbands, self.NK, self.nbands, self.nbands), dtype=np.complex) self.LDAc_elements = np.zeros( (self.NK, self.nbands, self.NK, self.nbands, self.nbands), dtype=np.complex) G = self.pdH.get_reciprocal_vectors() G2 = np.linalg.norm(G, axis=1)**2 G2[G2 == 0] = np.inf matrix = np.zeros((self.NK, self.nbands, self.nbands), dtype=np.complex) for k in tqdm(range(self.NK)): for n in range(self.nbands): density = 2 * np.abs(self.ukn[k, n])**2 operator = xc.VLDAx(density) self.LDAx_elements[k, n] = operator_matrix_periodic( matrix, operator, self.ukn.conj(), self.ukn) * self.norm operator = xc.VLDAc(density) self.LDAc_elements[k, n] = operator_matrix_periodic( matrix, operator, self.ukn.conj(), self.ukn) * self.norm density = self.pdH.fft(density) operator = 4 * np.pi * self.pdH.ifft(density / G2) self.Hartree_elements[k, n] = operator_matrix_periodic( matrix, operator, self.ukn.conj(), self.ukn) * self.norm self.wavefunction = np.zeros((self.NK, self.nbands, self.nbands), dtype=np.complex) self.Kinetic = np.zeros((self.NK, self.nbands, self.nbands), dtype=np.complex) self.dipole = self.get_dipole_matrix() for k in range(self.NK): self.wavefunction[k] = np.eye(self.nbands) self.Kinetic[k] = np.diag(self.EK[k]) self.VH0 = self.get_Hartree_matrix(self.wavefunction) self.VLDAc0 = self.get_LDA_correlation_matrix(self.wavefunction) self.VLDAx0 = self.get_LDA_exchange_matrix(self.wavefunction) self.Full_BZ = calc.get_bz_k_points() self.IBZ_map = calc.get_bz_to_ibz_map()
def initialize(self, atoms=None): """Inexpensive initialization.""" if atoms is None: atoms = self.atoms else: # Save the state of the atoms: self.atoms = atoms.copy() par = self.input_parameters world = par.communicator if world is None: world = mpi.world elif hasattr(world, 'new_communicator'): # Check for whether object has correct type already # # Using isinstance() is complicated because of all the # combinations, serial/parallel/debug... pass else: # world should be a list of ranks: world = mpi.world.new_communicator(np.asarray(world)) self.wfs.world = world self.set_text(par.txt, par.verbose) natoms = len(atoms) cell_cv = atoms.get_cell() / Bohr pbc_c = atoms.get_pbc() Z_a = atoms.get_atomic_numbers() magmom_av = atoms.get_initial_magnetic_moments() # Generate new xc functional only when it is reset by set if self.hamiltonian is None or self.hamiltonian.xc is None: if isinstance(par.xc, str): xc = XC(par.xc) else: xc = par.xc else: xc = self.hamiltonian.xc mode = par.mode if xc.orbital_dependent and mode == 'lcao': raise NotImplementedError('LCAO mode does not support ' 'orbital-dependent XC functionals.') if mode == 'pw': mode = PW() if mode == 'fd' and par.usefractrans: raise NotImplementedError('FD mode does not support ' 'fractional translations.') if mode == 'lcao' and par.usefractrans: raise Warning('Fractional translations have not been tested ' 'with LCAO mode. Use with care!') if par.realspace is None: realspace = not isinstance(mode, PW) else: realspace = par.realspace if isinstance(mode, PW): assert not realspace if par.gpts is not None: N_c = np.array(par.gpts) else: h = par.h if h is not None: h /= Bohr N_c = get_number_of_grid_points(cell_cv, h, mode, realspace) if par.filter is None and not isinstance(mode, PW): gamma = 1.6 hmax = ((np.linalg.inv(cell_cv)**2).sum(0)**-0.5 / N_c).max() def filter(rgd, rcut, f_r, l=0): gcut = np.pi / hmax - 2 / rcut / gamma f_r[:] = rgd.filter(f_r, rcut * gamma, gcut, l) else: filter = par.filter setups = Setups(Z_a, par.setups, par.basis, par.lmax, xc, filter, world) if magmom_av.ndim == 1: collinear = True magmom_av, magmom_a = np.zeros((natoms, 3)), magmom_av magmom_av[:, 2] = magmom_a else: collinear = False magnetic = magmom_av.any() spinpol = par.spinpol if par.hund: if natoms != 1: raise ValueError('hund=True arg only valid for single atoms!') spinpol = True magmom_av[0] = (0, 0, setups[0].get_hunds_rule_moment(par.charge)) if spinpol is None: spinpol = magnetic elif magnetic and not spinpol: raise ValueError('Non-zero initial magnetic moment for a ' + 'spin-paired calculation!') if collinear: nspins = 1 + int(spinpol) ncomp = 1 else: nspins = 1 ncomp = 2 # K-point descriptor bzkpts_kc = kpts2ndarray(par.kpts, self.atoms) kd = KPointDescriptor(bzkpts_kc, nspins, collinear, par.usefractrans) width = par.width if width is None: if pbc_c.any(): width = 0.1 # eV else: width = 0.0 else: assert par.occupations is None if hasattr(self, 'time') or par.dtype == complex: dtype = complex else: if kd.gamma: dtype = float else: dtype = complex ## rbw: If usefractrans=True, kd.set_symmetry might overwrite N_c. ## This is necessary, because N_c must be dividable by 1/(fractional translation), ## f.e. fractional translations of a grid point must land on a grid point. N_c = kd.set_symmetry(atoms, setups, magmom_av, par.usesymm, N_c, world) nao = setups.nao nvalence = setups.nvalence - par.charge M_v = magmom_av.sum(0) M = np.dot(M_v, M_v)**0.5 nbands = par.nbands if nbands is None: nbands = 0 for setup in setups: nbands_from_atom = setup.get_default_nbands() # Any obscure setup errors? if nbands_from_atom < -(-setup.Nv // 2): raise ValueError('Bad setup: This setup requests %d' ' bands but has %d electrons.' % (nbands_from_atom, setup.Nv)) nbands += nbands_from_atom nbands = min(nao, nbands) elif nbands > nao and mode == 'lcao': raise ValueError('Too many bands for LCAO calculation: ' '%d bands and only %d atomic orbitals!' % (nbands, nao)) if nvalence < 0: raise ValueError( 'Charge %f is not possible - not enough valence electrons' % par.charge) if nbands <= 0: nbands = int(nvalence + M + 0.5) // 2 + (-nbands) if nvalence > 2 * nbands: raise ValueError('Too few bands! Electrons: %f, bands: %d' % (nvalence, nbands)) nbands *= ncomp if par.width is not None: self.text('**NOTE**: please start using ' 'occupations=FermiDirac(width).') if par.fixmom: self.text('**NOTE**: please start using ' 'occupations=FermiDirac(width, fixmagmom=True).') if self.occupations is None: if par.occupations is None: # Create object for occupation numbers: self.occupations = occupations.FermiDirac(width, par.fixmom) else: self.occupations = par.occupations self.occupations.magmom = M_v[2] cc = par.convergence if mode == 'lcao': niter_fixdensity = 0 else: niter_fixdensity = None if self.scf is None: self.scf = SCFLoop( cc['eigenstates'] / Hartree**2 * nvalence, cc['energy'] / Hartree * max(nvalence, 1), cc['density'] * nvalence, par.maxiter, par.fixdensity, niter_fixdensity) parsize_kpt = par.parallel['kpt'] parsize_domain = par.parallel['domain'] parsize_bands = par.parallel['band'] if not realspace: pbc_c = np.ones(3, bool) if not self.wfs: if parsize_domain == 'domain only': # XXX this was silly! parsize_domain = world.size parallelization = mpi.Parallelization(world, nspins * kd.nibzkpts) ndomains = None if parsize_domain is not None: ndomains = np.prod(parsize_domain) if isinstance(mode, PW): if ndomains > 1: raise ValueError('Planewave mode does not support ' 'domain decomposition.') ndomains = 1 parallelization.set(kpt=parsize_kpt, domain=ndomains, band=parsize_bands) domain_comm, kpt_comm, band_comm = \ parallelization.build_communicators() #domain_comm, kpt_comm, band_comm = mpi.distribute_cpus( # parsize_domain, parsize_bands, # nspins, kd.nibzkpts, world, par.idiotproof, mode) kd.set_communicator(kpt_comm) parstride_bands = par.parallel['stridebands'] # Unfortunately we need to remember that we adjusted the # number of bands so we can print a warning if it differs # from the number specified by the user. (The number can # be inferred from the input parameters, but it's tricky # because we allow negative numbers) self.nbands_parallelization_adjustment = -nbands % band_comm.size nbands += self.nbands_parallelization_adjustment # I would like to give the following error message, but apparently # there are cases, e.g. gpaw/test/gw_ppa.py, which involve # nbands > nao and are supposed to work that way. #if nbands > nao: # raise ValueError('Number of bands %d adjusted for band ' # 'parallelization %d exceeds number of atomic ' # 'orbitals %d. This problem can be fixed ' # 'by reducing the number of bands a bit.' # % (nbands, band_comm.size, nao)) bd = BandDescriptor(nbands, band_comm, parstride_bands) if (self.density is not None and self.density.gd.comm.size != domain_comm.size): # Domain decomposition has changed, so we need to # reinitialize density and hamiltonian: if par.fixdensity: raise RuntimeError('Density reinitialization conflict ' + 'with "fixdensity" - specify domain decomposition.') self.density = None self.hamiltonian = None # Construct grid descriptor for coarse grids for wave functions: gd = self.grid_descriptor_class(N_c, cell_cv, pbc_c, domain_comm, parsize_domain) # do k-point analysis here? XXX args = (gd, nvalence, setups, bd, dtype, world, kd, self.timer) if par.parallel['sl_auto']: # Choose scalapack parallelization automatically for key, val in par.parallel.items(): if (key.startswith('sl_') and key != 'sl_auto' and val is not None): raise ValueError("Cannot use 'sl_auto' together " "with '%s'" % key) max_scalapack_cpus = bd.comm.size * gd.comm.size nprow = max_scalapack_cpus npcol = 1 # Get a sort of reasonable number of columns/rows while npcol < nprow and nprow % 2 == 0: npcol *= 2 nprow //= 2 assert npcol * nprow == max_scalapack_cpus # ScaLAPACK creates trouble if there aren't at least a few # whole blocks; choose block size so there will always be # several blocks. This will crash for small test systems, # but so will ScaLAPACK in any case blocksize = min(-(-nbands // 4), 64) sl_default = (nprow, npcol, blocksize) else: sl_default = par.parallel['sl_default'] if mode == 'lcao': # Layouts used for general diagonalizer sl_lcao = par.parallel['sl_lcao'] if sl_lcao is None: sl_lcao = sl_default lcaoksl = get_KohnSham_layouts(sl_lcao, 'lcao', gd, bd, dtype, nao=nao, timer=self.timer) if collinear: self.wfs = LCAOWaveFunctions(lcaoksl, *args) else: from gpaw.xc.noncollinear import \ NonCollinearLCAOWaveFunctions self.wfs = NonCollinearLCAOWaveFunctions(lcaoksl, *args) elif mode == 'fd' or isinstance(mode, PW): # buffer_size keyword only relevant for fdpw buffer_size = par.parallel['buffer_size'] # Layouts used for diagonalizer sl_diagonalize = par.parallel['sl_diagonalize'] if sl_diagonalize is None: sl_diagonalize = sl_default diagksl = get_KohnSham_layouts(sl_diagonalize, 'fd', gd, bd, dtype, buffer_size=buffer_size, timer=self.timer) # Layouts used for orthonormalizer sl_inverse_cholesky = par.parallel['sl_inverse_cholesky'] if sl_inverse_cholesky is None: sl_inverse_cholesky = sl_default if sl_inverse_cholesky != sl_diagonalize: message = 'sl_inverse_cholesky != sl_diagonalize ' \ 'is not implemented.' raise NotImplementedError(message) orthoksl = get_KohnSham_layouts(sl_inverse_cholesky, 'fd', gd, bd, dtype, buffer_size=buffer_size, timer=self.timer) # Use (at most) all available LCAO for initialization lcaonbands = min(nbands, nao) try: lcaobd = BandDescriptor(lcaonbands, band_comm, parstride_bands) except RuntimeError: initksl = None else: # Layouts used for general diagonalizer # (LCAO initialization) sl_lcao = par.parallel['sl_lcao'] if sl_lcao is None: sl_lcao = sl_default initksl = get_KohnSham_layouts(sl_lcao, 'lcao', gd, lcaobd, dtype, nao=nao, timer=self.timer) if hasattr(self, 'time'): assert mode == 'fd' from gpaw.tddft import TimeDependentWaveFunctions self.wfs = TimeDependentWaveFunctions(par.stencils[0], diagksl, orthoksl, initksl, gd, nvalence, setups, bd, world, kd, self.timer) elif mode == 'fd': self.wfs = FDWaveFunctions(par.stencils[0], diagksl, orthoksl, initksl, *args) else: # Planewave basis: self.wfs = mode(diagksl, orthoksl, initksl, *args) else: self.wfs = mode(self, *args) else: self.wfs.set_setups(setups) if not self.wfs.eigensolver: # Number of bands to converge: nbands_converge = cc['bands'] if nbands_converge == 'all': nbands_converge = nbands elif nbands_converge != 'occupied': assert isinstance(nbands_converge, int) if nbands_converge < 0: nbands_converge += nbands eigensolver = get_eigensolver(par.eigensolver, mode, par.convergence) eigensolver.nbands_converge = nbands_converge # XXX Eigensolver class doesn't define an nbands_converge property if isinstance(xc, SIC): eigensolver.blocksize = 1 self.wfs.set_eigensolver(eigensolver) if self.density is None: gd = self.wfs.gd if par.stencils[1] != 9: # Construct grid descriptor for fine grids for densities # and potentials: finegd = gd.refine() else: # Special case (use only coarse grid): finegd = gd if realspace: self.density = RealSpaceDensity( gd, finegd, nspins, par.charge + setups.core_charge, collinear, par.stencils[1]) else: self.density = ReciprocalSpaceDensity( gd, finegd, nspins, par.charge + setups.core_charge, collinear) self.density.initialize(setups, self.timer, magmom_av, par.hund) self.density.set_mixer(par.mixer) if self.hamiltonian is None: gd, finegd = self.density.gd, self.density.finegd if realspace: self.hamiltonian = RealSpaceHamiltonian( gd, finegd, nspins, setups, self.timer, xc, par.external, collinear, par.poissonsolver, par.stencils[1], world) else: self.hamiltonian = ReciprocalSpaceHamiltonian( gd, finegd, self.density.pd2, self.density.pd3, nspins, setups, self.timer, xc, par.external, collinear, world) xc.initialize(self.density, self.hamiltonian, self.wfs, self.occupations) self.text() self.print_memory_estimate(self.txt, maxdepth=memory_estimate_depth) self.txt.flush() self.timer.print_info(self) if dry_run: self.dry_run() self.initialized = True
def bloch_matrix(self, kpts, qpts, c_kn, u_ql, omega_ql=None, kpts_from=None): """Calculate el-ph coupling in the Bloch basis for the electrons. This function calculates the electron-phonon coupling between the specified Bloch states, i.e.:: ______ mnl / hbar ^ g = /------- < m k + q | e . grad V | n k > kq \/ 2 M w ql q ql In case the ``omega_ql`` keyword argument is not given, the bare matrix element (in units of eV / Ang) without the sqrt prefactor is returned. Parameters ---------- kpts: ndarray or tuple. k-vectors of the Bloch states. When a tuple of integers is given, a Monkhorst-Pack grid with the specified number of k-points along the directions of the reciprocal lattice vectors is generated. qpts: ndarray or tuple. q-vectors of the phonons. c_kn: ndarray Expansion coefficients for the Bloch states. The ordering must be the same as in the ``kpts`` argument. u_ql: ndarray Mass-scaled polarization vectors (in units of 1 / sqrt(amu)) of the phonons. Again, the ordering must be the same as in the corresponding ``qpts`` argument. omega_ql: ndarray Vibrational frequencies in eV. kpts_from: list of ints or int Calculate only the matrix element for the k-vectors specified by their index in the ``kpts`` argument (default: all). In short, phonon frequencies and mode vectors must be given in ase units. """ assert self.g_xNNMM is not None, "Load supercell matrix." assert len(c_kn.shape) == 3 assert len(u_ql.shape) == 4 if omega_ql is not None: assert np.all(u_ql.shape[:2] == omega_ql.shape[:2]) # Translate k-points into 1. BZ (required by ``find_k_plus_q``` member # function of the ```KPointDescriptor``). if isinstance(kpts, np.ndarray): assert kpts.shape[1] == 3, "kpts_kc array must be given" # XXX This does not seem to cause problems! kpts -= kpts.round() # Use the KPointDescriptor to keep track of the k and q-vectors kd_kpts = KPointDescriptor(kpts) kd_qpts = KPointDescriptor(qpts) # Check that number of k- and q-points agree with the number of Bloch # functions and polarization vectors assert kd_kpts.nbzkpts == len(c_kn) assert kd_qpts.nbzkpts == len(u_ql) # Include all k-point per default if kpts_from is None: kpts_kc = kd_kpts.bzk_kc kpts_k = range(kd_kpts.nbzkpts) else: kpts_kc = kd_kpts.bzk_kc[kpts_from] if isinstance(kpts_from, int): kpts_k = list([kpts_from]) else: kpts_k = list(kpts_from) # Supercell matrix (real matrix in Hartree / Bohr) g_xNNMM = self.g_xNNMM # Number of phonon modes and electronic bands nmodes = u_ql.shape[1] nbands = c_kn.shape[1] # Number of atoms displacements and basis functions ndisp = np.prod(u_ql.shape[2:]) assert ndisp == (3 * len(self.indices)) nao = c_kn.shape[2] assert ndisp == g_xNNMM.shape[0] assert nao == g_xNNMM.shape[-1] # Lattice vectors R_cN = self.lattice_vectors() # Number of unit cell in supercell N = np.prod(self.N_c) # Allocate array for couplings g_qklnn = np.zeros((kd_qpts.nbzkpts, len(kpts_kc), nmodes, nbands, nbands), dtype=complex) self.timer.write_now("Calculating coupling matrix elements") for q, q_c in enumerate(kd_qpts.bzk_kc): # Find indices of k+q for the k-points kplusq_k = kd_kpts.find_k_plus_q(q_c, kpts_k=kpts_k) # Here, ``i`` is counting from 0 and ``k`` is the global index of # the k-point for i, (k, k_c) in enumerate(zip(kpts_k, kpts_kc)): # Check the wave vectors (adapted to the ``KPointDescriptor`` class) kplusq_c = k_c + q_c kplusq_c -= kplusq_c.round() assert np.allclose(kplusq_c, kd_kpts.bzk_kc[kplusq_k[i]] ), \ (i, k, k_c, q_c, kd_kpts.bzk_kc[kplusq_k[i]]) # Allocate array g_xMM = np.zeros((ndisp, nao, nao), dtype=complex) # Multiply phase factors for m in range(N): for n in range(N): Rm_c = R_cN[:, m] Rn_c = R_cN[:, n] phase = np.exp(2.j * pi * (np.dot(k_c, Rm_c - Rn_c) + np.dot(q_c, Rm_c))) # Sum contributions from different cells g_xMM += g_xNNMM[:, m, n, :, :] * phase # LCAO coefficient for Bloch states ck_nM = c_kn[k] ckplusq_nM = c_kn[kplusq_k[i]] # Mass scaled polarization vectors u_lx = u_ql[q].reshape(nmodes, 3 * len(self.atoms)) g_nxn = np.dot(ckplusq_nM.conj(), np.dot(g_xMM, ck_nM.T)) g_lnn = np.dot(u_lx, g_nxn) # Insert value g_qklnn[q, i] = g_lnn # XXX Temp if np.all(q_c == 0.0): # These should be real print g_qklnn[q].imag.min(), g_qklnn[q].imag.max() self.timer.write_now("Finished calculation of coupling matrix elements") # Return the bare matrix element if frequencies are not given if omega_ql is None: # Convert to eV / Ang g_qklnn *= units.Hartree / units.Bohr else: # Multiply prefactor sqrt(hbar / 2 * M * omega) in units of Bohr amu = units._amu # atomic mass unit me = units._me # electron mass g_qklnn /= np.sqrt(2 * amu / me / units.Hartree * \ omega_ql[:, np.newaxis, :, np.newaxis, np.newaxis]) # Convert to eV g_qklnn *= units.Hartree # Return couplings in eV (or eV / Ang) return g_qklnn
def __init__(self, calc, gamma=True, symmetry=False, e_ph=False, communicator=serial_comm): """Inititialize class with a list of atoms. The atoms object must contain a converged ground-state calculation. The set of q-vectors in which the dynamical matrix will be calculated is determined from the ``symmetry`` kwarg. For now, only time-reversal symmetry is used to generate the irrecducible BZ. Add a little note on parallelization strategy here. Parameters ---------- calc: str or Calculator Calculator containing a ground-state calculation. gamma: bool Gamma-point calculation with respect to the q-vector of the dynamical matrix. When ``False``, the Monkhorst-Pack grid from the ground-state calculation is used. symmetry: bool Use symmetries to reduce the q-vectors of the dynamcial matrix (None, False or True). The different options are equivalent to the options in a ground-state calculation. e_ph: bool Save the derivative of the effective potential. communicator: Communicator Communicator for parallelization over k-points and real-space domain. """ # XXX assert symmetry in [None, False], "Spatial symmetries not allowed yet" self.symmetry = symmetry if isinstance(calc, str): self.calc = GPAW(calc, communicator=serial_comm, txt=None) else: self.calc = calc # Make sure localized functions are initialized self.calc.set_positions() # Note that this under some circumstances (e.g. when called twice) # allocates a new array for the P_ani coefficients !! # Store useful objects self.atoms = self.calc.get_atoms() # Get rid of ``calc`` attribute self.atoms.calc = None # Boundary conditions pbc_c = self.calc.atoms.get_pbc() if np.all(pbc_c == False): self.gamma = True self.dtype = float kpts = None # Multigrid Poisson solver poisson_solver = PoissonSolver() else: if gamma: self.gamma = True self.dtype = float kpts = None else: self.gamma = False self.dtype = complex # Get k-points from ground-state calculation kpts = self.calc.input_parameters.kpts # FFT Poisson solver poisson_solver = FFTPoissonSolver(dtype=self.dtype) # K-point descriptor for the q-vectors of the dynamical matrix # Note, no explicit parallelization here. self.kd = KPointDescriptor(kpts, 1) self.kd.set_symmetry(self.atoms, self.calc.wfs.setups, usesymm=symmetry) self.kd.set_communicator(serial_comm) # Number of occupied bands nvalence = self.calc.wfs.nvalence nbands = nvalence / 2 + nvalence % 2 assert nbands <= self.calc.wfs.bd.nbands # Extract other useful objects # Ground-state k-point descriptor - used for the k-points in the # ResponseCalculator # XXX replace communicators when ready to parallelize kd_gs = self.calc.wfs.kd gd = self.calc.density.gd kpt_u = self.calc.wfs.kpt_u setups = self.calc.wfs.setups dtype_gs = self.calc.wfs.dtype # WaveFunctions wfs = WaveFunctions(nbands, kpt_u, setups, kd_gs, gd, dtype=dtype_gs) # Linear response calculator self.response_calc = ResponseCalculator(self.calc, wfs, dtype=self.dtype) # Phonon perturbation self.perturbation = PhononPerturbation(self.calc, self.kd, poisson_solver, dtype=self.dtype) # Dynamical matrix self.dyn = DynamicalMatrix(self.atoms, self.kd, dtype=self.dtype) # Electron-phonon couplings if e_ph: self.e_ph = ElectronPhononCoupling(self.atoms, gd, self.kd, dtype=self.dtype) else: self.e_ph = None # Initialization flag self.initialized = False # Parallel communicator for parallelization over kpts and domain self.comm = communicator
def __init__(self, calc, filename='gw', kpts=None, bands=None, nbands=None, ppa=False, wstc=False, ecut=150.0, eta=0.1, E0=1.0 * Hartree, domega0=0.025, omega2=10.0, world=mpi.world): PairDensity.__init__(self, calc, ecut, world=world, txt=filename + '.txt') self.filename = filename ecut /= Hartree self.ppa = ppa self.wstc = wstc self.eta = eta / Hartree self.E0 = E0 / Hartree self.domega0 = domega0 / Hartree self.omega2 = omega2 / Hartree print(' ___ _ _ _ ', file=self.fd) print(' | || | | |', file=self.fd) print(' | | || | | |', file=self.fd) print(' |__ ||_____|', file=self.fd) print(' |___| ', file=self.fd) print(file=self.fd) self.kpts = select_kpts(kpts, self.calc) if bands is None: bands = [0, self.nocc2] self.bands = bands b1, b2 = bands self.shape = shape = (self.calc.wfs.nspins, len(self.kpts), b2 - b1) self.eps_sin = np.empty(shape) # KS-eigenvalues self.f_sin = np.empty(shape) # occupation numbers self.sigma_sin = np.zeros(shape) # self-energies self.dsigma_sin = np.zeros(shape) # derivatives of self-energies self.vxc_sin = None # KS XC-contributions self.exx_sin = None # exact exchange contributions self.Z_sin = None # renormalization factors if nbands is None: nbands = int(self.vol * ecut**1.5 * 2**0.5 / 3 / pi**2) self.nbands = nbands kd = self.calc.wfs.kd self.mysKn1n2 = None # my (s, K, n1, n2) indices self.distribute_k_points_and_bands(b1, b2, kd.ibz2bz_k[self.kpts]) # Find q-vectors and weights in the IBZ: assert -1 not in kd.bz2bz_ks offset_c = 0.5 * ((kd.N_c + 1) % 2) / kd.N_c bzq_qc = monkhorst_pack(kd.N_c) + offset_c self.qd = KPointDescriptor(bzq_qc) self.qd.set_symmetry(self.calc.atoms, self.calc.wfs.setups, usesymm=self.calc.input_parameters.usesymm, N_c=self.calc.wfs.gd.N_c) assert self.calc.wfs.nspins == 1
class UTDomainParallelSetup(TestCase): """ Setup a simple domain parallel calculation.""" # Number of bands nbands = 12 # Spin-polarized nspins = 1 # Mean spacing and number of grid points per axis (G x G x G) h = 0.25 / Bohr G = 48 # Type of boundary conditions employed (determines nibzkpts and dtype) boundaries = None nibzkpts = None dtype = None timer = nulltimer def setUp(self): for virtvar in ['boundaries']: assert getattr(self, virtvar) is not None, 'Virtual "%s"!' % virtvar # Basic unit cell information: res, N_c = shapeopt(100, self.G**3, 3, 0.2) #N_c = 4*np.round(np.array(N_c)/4) # makes domain decomposition easier cell_cv = self.h * np.diag(N_c) pbc_c = {'zero' : (False,False,False), \ 'periodic': (True,True,True), \ 'mixed' : (True, False, True)}[self.boundaries] # Create randomized gas-like atomic configuration on interim grid tmpgd = GridDescriptor(N_c, cell_cv, pbc_c) self.atoms = create_random_atoms(tmpgd) # Create setups Z_a = self.atoms.get_atomic_numbers() assert 1 == self.nspins self.setups = Setups(Z_a, p.setups, p.basis, p.lmax, xc) self.natoms = len(self.setups) # Decide how many kpoints to sample from the 1st Brillouin Zone kpts_c = np.ceil( (10 / Bohr) / np.sum(cell_cv**2, axis=1)**0.5).astype(int) kpts_c = tuple(kpts_c * pbc_c + 1 - pbc_c) self.bzk_kc = kpts2ndarray(kpts_c) # Set up k-point descriptor self.kd = KPointDescriptor(self.bzk_kc, self.nspins) self.kd.set_symmetry(self.atoms, self.setups, p.usesymm) # Set the dtype if self.kd.gamma: self.dtype = float else: self.dtype = complex # Create communicators parsize, parsize_bands = self.get_parsizes() assert self.nbands % np.prod(parsize_bands) == 0 domain_comm, kpt_comm, band_comm = distribute_cpus( parsize, parsize_bands, self.nspins, self.kd.nibzkpts) self.kd.set_communicator(kpt_comm) # Set up band descriptor: self.bd = BandDescriptor(self.nbands, band_comm) # Set up grid descriptor: self.gd = GridDescriptor(N_c, cell_cv, pbc_c, domain_comm, parsize) # Set up kpoint/spin descriptor (to be removed): self.kd_old = KPointDescriptorOld(self.nspins, self.kd.nibzkpts, kpt_comm, self.kd.gamma, self.dtype) def tearDown(self): del self.atoms, self.bd, self.gd, self.kd, self.kd_old def get_parsizes(self): # Careful, overwriting imported GPAW params may cause amnesia in Python. from gpaw import parsize, parsize_bands # D: number of domains # B: number of band groups if parsize is None: D = min(world.size, 2) else: D = parsize assert world.size % D == 0 if parsize_bands is None: B = world.size // D else: B = parsize_bands return D, B # ================================= def verify_comm_sizes(self): if world.size == 1: return comm_sizes = tuple([comm.size for comm in [world, self.bd.comm, \ self.gd.comm, self.kd_old.comm]]) self._parinfo = '%d world, %d band, %d domain, %d kpt' % comm_sizes self.assertEqual(self.nbands % self.bd.comm.size, 0) self.assertEqual( (self.nspins * self.kd.nibzkpts) % self.kd_old.comm.size, 0)
def calculate_exx(self): """Non-selfconsistent calculation.""" self.timer.start('EXX') self.timer.start('Initialization') kd = self.kd wfs = self.wfs if fftw.FFTPlan is fftw.NumpyFFTPlan: self.log('NOT USING FFTW !!') self.log('Spins:', self.wfs.nspins) W = max(1, self.wfs.kd.comm.size // self.wfs.nspins) # Are the k-points distributed? kparallel = (W > 1) # Find number of occupied bands: self.nocc_sk = np.zeros((self.wfs.nspins, kd.nibzkpts), int) for kpt in self.wfs.kpt_u: for n, f in enumerate(kpt.f_n): if abs(f) < self.fcut: self.nocc_sk[kpt.s, kpt.k] = n break else: self.nocc_sk[kpt.s, kpt.k] = self.wfs.bd.nbands self.wfs.kd.comm.sum(self.nocc_sk) noccmin = self.nocc_sk.min() noccmax = self.nocc_sk.max() self.log('Number of occupied bands (min, max): %d, %d' % (noccmin, noccmax)) self.log('Number of valence electrons:', self.wfs.setups.nvalence) if self.bandstructure: self.log('Calculating eigenvalue shifts.') # allocate array for eigenvalue shifts: self.exx_skn = np.zeros( (self.wfs.nspins, kd.nibzkpts, self.wfs.bd.nbands)) if self.bands is None: noccmax = self.wfs.bd.nbands else: noccmax = max(max(self.bands) + 1, noccmax) N_c = self.kd.N_c vol = wfs.gd.dv * wfs.gd.N_c.prod() if self.alpha is None: alpha = 6 * vol**(2 / 3.0) / pi**2 else: alpha = self.alpha if self.gamma_point == 1: if alpha == 0.0: qvol = (2 * np.pi)**3 / vol / N_c.prod() self.gamma = 4 * np.pi * (3 * qvol / (4 * np.pi))**(1 / 3.) / qvol else: self.gamma = self.calculate_gamma(vol, alpha) else: kcell_cv = wfs.gd.cell_cv.copy() kcell_cv[0] *= N_c[0] kcell_cv[1] *= N_c[1] kcell_cv[2] *= N_c[2] self.gamma = madelung(kcell_cv) * vol * N_c.prod() / (4 * np.pi) self.log('Value of alpha parameter: %.3f Bohr^2' % alpha) self.log('Value of gamma parameter: %.3f Bohr^2' % self.gamma) # Construct all possible q=k2-k1 vectors: Nq_c = (N_c - 1) // self.qstride_c i_qc = np.indices(Nq_c * 2 + 1, float).transpose((1, 2, 3, 0)).reshape( (-1, 3)) self.bzq_qc = (i_qc - Nq_c) / N_c * self.qstride_c self.q0 = ((Nq_c * 2 + 1).prod() - 1) // 2 # index of q=(0,0,0) assert not self.bzq_qc[self.q0].any() # Count number of pairs for each q-vector: self.npairs_q = np.zeros(len(self.bzq_qc), int) for s in range(kd.nspins): for k1 in range(kd.nibzkpts): for k2 in range(kd.nibzkpts): for K2, q, n1_n, n2 in self.indices(s, k1, k2): self.npairs_q[q] += len(n1_n) self.npairs0 = self.npairs_q.sum() # total number of pairs self.log('Number of pairs:', self.npairs0) # Distribute q-vectors to Q processors: Q = self.world.size // self.wfs.kd.comm.size myrank = self.world.rank // self.wfs.kd.comm.size rank = 0 N = 0 myq = [] nq = 0 for q, n in enumerate(self.npairs_q): if n > 0: nq += 1 if rank == myrank: myq.append(q) N += n if N >= (rank + 1.0) * self.npairs0 / Q: rank += 1 assert len(myq) > 0, 'Too few q-vectors for too many processes!' self.bzq_qc = self.bzq_qc[myq] try: self.q0 = myq.index(self.q0) except ValueError: self.q0 = None self.log('%d x %d x %d k-points' % tuple(self.kd.N_c)) self.log('Distributing %d IBZ k-points over %d process(es).' % (kd.nibzkpts, self.wfs.kd.comm.size)) self.log('Distributing %d q-vectors over %d process(es).' % (nq, Q)) # q-point descriptor for my q-vectors: qd = KPointDescriptor(self.bzq_qc) # Plane-wave descriptor for all wave-functions: self.pd = PWDescriptor(wfs.pd.ecut, wfs.gd, dtype=wfs.pd.dtype, kd=kd) # Plane-wave descriptor pair-densities: self.pd2 = PWDescriptor(self.dens.pd2.ecut, self.dens.gd, dtype=wfs.dtype, kd=qd) self.log('Cutoff energies:') self.log(' Wave functions: %10.3f eV' % (self.pd.ecut * Hartree)) self.log(' Density: %10.3f eV' % (self.pd2.ecut * Hartree)) # Calculate 1/|G+q|^2 with special treatment of |G+q|=0: G2_qG = self.pd2.G2_qG if self.q0 is None: if self.omega is None: self.iG2_qG = [1.0 / G2_G for G2_G in G2_qG] else: self.iG2_qG = [ (1.0 / G2_G * (1 - np.exp(-G2_G / (4 * self.omega**2)))) for G2_G in G2_qG ] else: G2_qG[self.q0][0] = 117.0 # avoid division by zero if self.omega is None: self.iG2_qG = [1.0 / G2_G for G2_G in G2_qG] self.iG2_qG[self.q0][0] = self.gamma else: self.iG2_qG = [ (1.0 / G2_G * (1 - np.exp(-G2_G / (4 * self.omega**2)))) for G2_G in G2_qG ] self.iG2_qG[self.q0][0] = 1 / (4 * self.omega**2) G2_qG[self.q0][0] = 0.0 # restore correct value # Compensation charges: self.ghat = PWLFC([setup.ghat_l for setup in wfs.setups], self.pd2) self.ghat.set_positions(self.spos_ac) if self.molecule: self.initialize_gaussian() self.log('Value of beta parameter: %.3f 1/Bohr^2' % self.beta) self.timer.stop('Initialization') # Ready ... set ... go: self.t0 = time() self.npairs = 0 self.evv = 0.0 self.evvacdf = 0.0 for s in range(self.wfs.nspins): kpt1_q = [ KPoint(self.wfs, noccmax).initialize(kpt) for kpt in self.wfs.kpt_u if kpt.s == s ] kpt2_q = kpt1_q[:] if len(kpt1_q) == 0: # No s-spins on this CPU: continue # Send and receive ranks: srank = self.wfs.kd.get_rank_and_index(s, (kpt1_q[0].k - 1) % kd.nibzkpts)[0] rrank = self.wfs.kd.get_rank_and_index(s, (kpt1_q[-1].k + 1) % kd.nibzkpts)[0] # Shift k-points kd.nibzkpts - 1 times: for i in range(kd.nibzkpts): if i < kd.nibzkpts - 1: if kparallel: kpt = kpt2_q[-1].next(self.wfs) kpt.start_receiving(rrank) kpt2_q[0].start_sending(srank) else: kpt = kpt2_q[0] self.timer.start('Calculate') for kpt1, kpt2 in zip(kpt1_q, kpt2_q): # Loop over all k-points that k2 can be mapped to: for K2, q, n1_n, n2 in self.indices(s, kpt1.k, kpt2.k): self.apply(K2, q, kpt1, kpt2, n1_n, n2) self.timer.stop('Calculate') if i < kd.nibzkpts - 1: self.timer.start('Wait') if kparallel: kpt.wait() kpt2_q[0].wait() self.timer.stop('Wait') kpt2_q.pop(0) kpt2_q.append(kpt) self.evv = self.world.sum(self.evv) self.evvacdf = self.world.sum(self.evvacdf) self.calculate_exx_paw_correction() if self.method == 'standard': self.exx = self.evv + self.devv + self.evc + self.ecc elif self.method == 'acdf': self.exx = self.evvacdf + self.devv + self.evc + self.ecc else: 1 / 0 self.log('Exact exchange energy:') for txt, e in [('core-core', self.ecc), ('valence-core', self.evc), ('valence-valence (pseudo, acdf)', self.evvacdf), ('valence-valence (pseudo, standard)', self.evv), ('valence-valence (correction)', self.devv), ('total (%s)' % self.method, self.exx)]: self.log(' %-36s %14.6f eV' % (txt + ':', e * Hartree)) self.log('Total time: %10.3f seconds' % (time() - self.t0)) self.npairs = self.world.sum(self.npairs) assert self.npairs == self.npairs0 self.timer.stop('EXX') self.timer.write(self.fd)