def __init__(self, generator, name=None, run=True, gtxt='-', non_relativistic_guess=False, xc='PBE'): if isinstance(generator, str): # treat 'generator' as symbol generator = Generator(generator, scalarrel=True, xcname=xc, txt=gtxt, nofiles=True) generator.N *= 4 self.generator = generator self.rgd = AERadialGridDescriptor(generator.beta, generator.N, default_spline_points=100) self.name = name if run: if non_relativistic_guess: ae0 = AllElectron(generator.symbol, scalarrel=False, nofiles=False, txt=gtxt, xcname=xc) ae0.N = generator.N ae0.beta = generator.beta ae0.run() # Now files will be stored such that they can # automagically be used by the next run() generator.run(write_xml=False, use_restart_file=False, **parameters[generator.symbol])
def get_core_eigenvalues(self, a, scalarrel=True): """Return the core eigenvalues by solving the radial schrodinger equation. Using AllElectron potential class, the spherically averaged Kohn--Sham potential is obtained around the spesified atom. The eigenstates for this potential are solved, the the resulting core states returned. Still experimental. """ r, v_g = self.get_spherical_ks_potential(a) # Get xccorr for atom a setup = self.paw.density.setups[a] xccorr = setup.xc_correction symbol = setup.symbol # Create AllElectron object for eigensolver atom = AllElectron(symbol, txt=None, scalarrel=scalarrel) # Calculate initial guess atom.run() # Set the potential atom.vr[:len(v_g)] = v_g # After the setups cutoff, arbitrary barrier is used atom.vr[len(v_g):] = 10.0 # Solve the eigenstates atom.solve() # The display format is just copy paste from AllElectron class # TODO: Make it a method in AllElectron class, thus it can be called directly def t(a): print(a) t('Calculated core eigenvalues of atom ' + str(a) + ':' + symbol) t('state eigenvalue ekin rmax') t('-----------------------------------------------') for m, l, f, e, u in zip(atom.n_j, atom.l_j, atom.f_j, atom.e_j, atom.u_j): # Find kinetic energy: k = e - np.sum(( np.where(abs(u) < 1e-160, 0, u)**2 * #XXXNumeric! atom.vr * atom.dr)[1:] / atom.r[1:]) # Find outermost maximum: g = atom.N - 4 while u[g - 1] >= u[g]: g -= 1 x = atom.r[g - 1:g + 2] y = u[g - 1:g + 2] A = np.transpose(np.array([x**i for i in range(3)])) c, b, a = np.linalg.solve(A, y) assert a < 0.0 rmax = -0.5 * b / a s = 'spdf'[l] t('%d%s^%-4.1f: %12.6f %12.6f %12.3f' % (m, s, f, e, k, rmax)) t('-----------------------------------------------') t('(units: Bohr and Hartree)') return atom.e_j
def get_core_eigenvalues(self, a, scalarrel=True): """Return the core eigenvalues by solving the radial schrodinger equation. Using AllElectron potential class, the spherically averaged Kohn--Sham potential is obtained around the spesified atom. The eigenstates for this potential are solved, the the resulting core states returned. Still experimental. """ r, v_g = self.get_spherical_ks_potential(a) # Get xccorr for atom a setup = self.paw.density.setups[a] xccorr = setup.xc_correction symbol = setup.symbol # Create AllElectron object for eigensolver atom = AllElectron(symbol, txt=None, scalarrel=scalarrel) # Calculate initial guess atom.run() # Set the potential atom.vr[:len(v_g)] = v_g # After the setups cutoff, arbitrary barrier is used atom.vr[len(v_g):] = 10.0 # Solve the eigenstates atom.solve() # The display format is just copy paste from AllElectron class # TODO: Make it a method in AllElectron class, thus it can be called directly def t(a): print a t('Calculated core eigenvalues of atom '+str(a)+':'+symbol) t('state eigenvalue ekin rmax') t('-----------------------------------------------') for m, l, f, e, u in zip(atom.n_j, atom.l_j, atom.f_j, atom.e_j, atom.u_j): # Find kinetic energy: k = e - np.sum((np.where(abs(u) < 1e-160, 0, u)**2 * #XXXNumeric! atom.vr * atom.dr)[1:] / atom.r[1:]) # Find outermost maximum: g = atom.N - 4 while u[g - 1] >= u[g]: g -= 1 x = atom.r[g - 1:g + 2] y = u[g - 1:g + 2] A = np.transpose(np.array([x**i for i in range(3)])) c, b, a = np.linalg.solve(A, y) assert a < 0.0 rmax = -0.5 * b / a s = 'spdf'[l] t('%d%s^%-4.1f: %12.6f %12.6f %12.3f' % (m, s, f, e, k, rmax)) t('-----------------------------------------------') t('(units: Bohr and Hartree)') return atom.e_j
def write_spherical_ks_potentials(self, txt): f = open(txt,'w') for a in self.paw.density.D_asp: r_g, vKS_g = self.get_spherical_ks_potential(a) setup = self.paw.density.setups[a] # Calculate also atomic LDA for reference g = AllElectron(setup.symbol, xcname='LDA',nofiles=True, scalarrel=True, txt=None) g.run() print >>f, r_g[0], vKS_g[0], g.vr[0], 0.0 for r, vKS,vr in zip(r_g[1:],vKS_g[1:], g.vr[1:]): print >> f, r, vKS,vr, (vKS-vr)/r f.close()
def _calculate_bound(self): from gpaw.atom.all_electron import AllElectron check_valid_quantum_number(self.Z, self.n, self.l) config_tuples = config_str_to_config_tuples( load_electronic_configurations()[chemical_symbols[self.Z]]) subshell_index = [shell[:2] for shell in config_tuples].index( (self.n, self.l)) with open(os.devnull, "w") as f, contextlib.redirect_stdout(f): ae = AllElectron(chemical_symbols[self.Z], xcname=self.xc) ae.run() # wave = interp1d(ae.r, ae.u_j[subshell_index], kind='cubic', fill_value='extrapolate', bounds_error=False) return ae.ETotal * units.Hartree, (ae.r, ae.u_j[subshell_index])
def __init__(self, generator, name=None, run=True, gtxt='-', non_relativistic_guess=False, xc='PBE', save_setup=False): if isinstance(generator, str): # treat 'generator' as symbol generator = Generator(generator, scalarrel=True, xcname=xc, txt=gtxt, nofiles=True) generator.N *= 4 self.generator = generator self.rgd = AERadialGridDescriptor(generator.beta / generator.N, 1.0 / generator.N, generator.N, default_spline_points=100) self.name = name if run: if non_relativistic_guess: ae0 = AllElectron(generator.symbol, scalarrel=False, nofiles=False, txt=gtxt, xcname=xc) ae0.N = generator.N ae0.beta = generator.beta ae0.run() # Now files will be stored such that they can # automagically be used by the next run() setup = generator.run(write_xml=False, use_restart_file=False, name=name, **parameters[generator.symbol]) if save_setup: setup.write_xml() else: if save_setup: raise ValueError('cannot save setup here because setup ' 'was already generated before basis ' 'generation.')
def write_spherical_ks_potentials(self, txt): f = open(txt, 'w') for a in self.paw.density.D_asp: r_g, vKS_g = self.get_spherical_ks_potential(a) setup = self.paw.density.setups[a] # Calculate also atomic LDA for reference g = AllElectron(setup.symbol, xcname='LDA', nofiles=True, scalarrel=True, txt=None) g.run() print(r_g[0], vKS_g[0], g.vr[0], 0.0, file=f) for r, vKS, vr in zip(r_g[1:], vKS_g[1:], g.vr[1:]): print(r, vKS, vr, (vKS - vr) / r, file=f) f.close()
def _calculate_continuum(self): from gpaw.atom.all_electron import AllElectron check_valid_quantum_number(self.Z, self.n, self.l) config_tuples = config_str_to_config_tuples( load_electronic_configurations()[chemical_symbols[self.Z]]) subshell_index = [shell[:2] for shell in config_tuples].index( (self.n, self.l)) with open(os.devnull, "w") as f, contextlib.redirect_stdout(f): ae = AllElectron(chemical_symbols[self.Z], xcname=self.xc) ae.f_j[subshell_index] -= 1. ae.run() vr = interp1d(ae.r, -ae.vr, fill_value='extrapolate', bounds_error=False) def schroedinger_derivative(y, r, l, e, vr): (u, up) = y return np.array([up, (l * (l + 1) / r**2 - 2 * vr(r) / r - e) * u]) r = np.geomspace(1e-7, 200, 1000000) continuum_waves = {} for lprime in self.lprimes: ur = integrate.odeint(schroedinger_derivative, [0.0, 1.], r, args=(lprime, self.epsilon, vr)) sqrt_k = 1 / ( 2 * self.epsilon / units.Hartree * (1 + units.alpha**2 * self.epsilon / units.Hartree / 2))**.25 ur = ur[:, 0] / ur[:, 0].max( ) / self.epsilon**.5 * units.Rydberg**0.25 / sqrt_k / np.sqrt( np.pi) continuum_waves[lprime] = ( r, ur ) # interp1d(r, ur, kind='cubic', fill_value='extrapolate', bounds_error=False) return ae.ETotal * units.Hartree, continuum_waves
from gpaw.atom.all_electron import AllElectron def check(name, atomE, atomeig, total, H**O): #print name, " ", atomE,"/", total, " | ", atomeig,"/",H**O," |" print "|",name, " |%12.4f/%12.4f | %12.4f/%12.4f |" % (atomE,total,atomeig,H**O) assert abs(atomE-total)<8e-3 assert abs(atomeig-H**O)<1e-3 A = AllElectron('He', 'KLI', False) A.run() HeE = A.Ekin+A.Epot+A.Exc Heeig = A.e_j[0] A = AllElectron('Be', 'KLI', False) A.run() BeE = A.Ekin+A.Epot+A.Exc Beeig = A.e_j[1] A = AllElectron('Ne', 'KLI', False) A.run() NeE = A.Ekin+A.Epot+A.Exc Neeig = A.e_j[2] A = AllElectron('Mg', 'KLI', False) A.run() MgE = A.Ekin+A.Epot+A.Exc Mgeig = A.e_j[3]
def run(self, core='', rcut=1.0, extra=None, logderiv=False, vbar=None, exx=False, name=None, normconserving='', filter=(0.4, 1.75), rcutcomp=None, write_xml=True, use_restart_file=True, empty_states=''): self.name = name self.core = core if type(rcut) is float: rcut_l = [rcut] else: rcut_l = rcut rcutmax = max(rcut_l) rcutmin = min(rcut_l) self.rcut_l = rcut_l if rcutcomp is None: rcutcomp = rcutmin self.rcutcomp = rcutcomp hfilter, xfilter = filter Z = self.Z n_j = self.n_j l_j = self.l_j f_j = self.f_j e_j = self.e_j if vbar is None: vbar = ('poly', rcutmin * 0.9) vbar_type, rcutvbar = vbar normconserving_l = [x in normconserving for x in 'spdf'] # Parse core string: j = 0 if core.startswith('['): a, core = core.split(']') core_symbol = a[1:] j = len(configurations[core_symbol][1]) while core != '': assert n_j[j] == int(core[0]) assert l_j[j] == 'spdf'.find(core[1]) if j != self.jcorehole: assert f_j[j] == 2 * (2 * l_j[j] + 1) j += 1 core = core[2:] njcore = j self.njcore = njcore lmaxocc = max(l_j[njcore:]) while empty_states != '': n = int(empty_states[0]) l = 'spdf'.find(empty_states[1]) assert n == 1 + l + l_j.count(l) n_j.append(n) l_j.append(l) f_j.append(0.0) e_j.append(-0.01) empty_states = empty_states[2:] if 2 in l_j[njcore:]: # We have a bound valence d-state. Add bound s- and # p-states if not already there: for l in [0, 1]: if l not in l_j[njcore:]: n_j.append(1 + l + l_j.count(l)) l_j.append(l) f_j.append(0.0) e_j.append(-0.01) if l_j[njcore:] == [0] and Z > 2: # We have only a bound valence s-state and we are not # hydrogen and not helium. Add bound p-state: n_j.append(n_j[njcore]) l_j.append(1) f_j.append(0.0) e_j.append(-0.01) nj = len(n_j) self.Nv = sum(f_j[njcore:]) self.Nc = sum(f_j[:njcore]) # Do all-electron calculation: AllElectron.run(self, use_restart_file) # Highest occupied atomic orbital: self.emax = max(e_j) N = self.N r = self.r dr = self.dr d2gdr2 = self.d2gdr2 beta = self.beta dv = r**2 * dr t = self.text t() t('Generating PAW setup') if core != '': t('Frozen core:', core) # So far - no ghost-states: self.ghost = False # Calculate the kinetic energy of the core states: Ekincore = 0.0 j = 0 for f, e, u in zip(f_j[:njcore], e_j[:njcore], self.u_j[:njcore]): u = np.where(abs(u) < 1e-160, 0, u) # XXX Numeric! k = e - np.sum((u**2 * self.vr * dr)[1:] / r[1:]) Ekincore += f * k if j == self.jcorehole: self.Ekincorehole = k j += 1 # Calculate core density: if njcore == 0: nc = np.zeros(N) else: uc_j = self.u_j[:njcore] uc_j = np.where(abs(uc_j) < 1e-160, 0, uc_j) # XXX Numeric! nc = np.dot(f_j[:njcore], uc_j**2) / (4 * pi) nc[1:] /= r[1:]**2 nc[0] = nc[1] self.nc = nc # Calculate core kinetic energy density if njcore == 0: tauc = np.zeros(N) else: tauc = self.radial_kinetic_energy_density(f_j[:njcore], l_j[:njcore], self.u_j[:njcore]) t('Kinetic energy of the core from tauc =', np.dot(tauc * r * r, dr) * 4 * pi) lmax = max(l_j[njcore:]) # Order valence states with respect to angular momentum # quantum number: self.n_ln = n_ln = [] self.f_ln = f_ln = [] self.e_ln = e_ln = [] for l in range(lmax + 1): n_n = [] f_n = [] e_n = [] for j in range(njcore, nj): if l_j[j] == l: n_n.append(n_j[j]) f_n.append(f_j[j]) e_n.append(e_j[j]) n_ln.append(n_n) f_ln.append(f_n) e_ln.append(e_n) # Add extra projectors: if extra is not None: if len(extra) == 0: lmaxextra = 0 else: lmaxextra = max(extra.keys()) if lmaxextra > lmax: for l in range(lmax, lmaxextra): n_ln.append([]) f_ln.append([]) e_ln.append([]) lmax = lmaxextra for l in extra: nn = -1 for e in extra[l]: n_ln[l].append(nn) f_ln[l].append(0.0) e_ln[l].append(e) nn -= 1 else: # Automatic: # Make sure we have two projectors for each occupied channel: for l in range(lmaxocc + 1): if len(n_ln[l]) < 2 and not normconserving_l[l]: # Only one - add one more: assert len(n_ln[l]) == 1 n_ln[l].append(-1) f_ln[l].append(0.0) e_ln[l].append(1.0 + e_ln[l][0]) if lmaxocc < 2 and lmaxocc == lmax: # Add extra projector for l = lmax + 1: n_ln.append([-1]) f_ln.append([0.0]) e_ln.append([0.0]) lmax += 1 self.lmax = lmax rcut_l.extend([rcutmin] * (lmax + 1 - len(rcut_l))) t('Cutoffs:') for rc, s in zip(rcut_l, 'spdf'): t('rc(%s)=%.3f' % (s, rc)) t('rc(vbar)=%.3f' % rcutvbar) t('rc(comp)=%.3f' % rcutcomp) t('rc(nct)=%.3f' % rcutmax) t() t('Kinetic energy of the core states: %.6f' % Ekincore) # Allocate arrays: self.u_ln = u_ln = [] # phi * r self.s_ln = s_ln = [] # phi-tilde * r self.q_ln = q_ln = [] # p-tilde * r for l in range(lmax + 1): nn = len(n_ln[l]) u_ln.append(np.zeros((nn, N))) s_ln.append(np.zeros((nn, N))) q_ln.append(np.zeros((nn, N))) # Fill in all-electron wave functions: for l in range(lmax + 1): # Collect all-electron wave functions: u_n = [self.u_j[j] for j in range(njcore, nj) if l_j[j] == l] for n, u in enumerate(u_n): u_ln[l][n] = u # Grid-index corresponding to rcut: gcut_l = [1 + int(rc * N / (rc + beta)) for rc in rcut_l] rcutfilter = xfilter * rcutmax self.rcutfilter = rcutfilter gcutfilter = 1 + int(rcutfilter * N / (rcutfilter + beta)) gcutmax = 1 + int(rcutmax * N / (rcutmax + beta)) # Outward integration of unbound states stops at 3 * rcut: gmax = int(3 * rcutmax * N / (3 * rcutmax + beta)) assert gmax > gcutfilter # Calculate unbound extra states: c2 = -(r / dr)**2 c10 = -d2gdr2 * r**2 for l, (n_n, e_n, u_n) in enumerate(zip(n_ln, e_ln, u_ln)): for n, e, u in zip(n_n, e_n, u_n): if n < 0: u[:] = 0.0 shoot(u, l, self.vr, e, self.r2dvdr, r, dr, c10, c2, self.scalarrel, gmax=gmax) u *= 1.0 / u[gcut_l[l]] charge = Z - self.Nv - self.Nc t('Charge: %.1f' % charge) t('Core electrons: %.1f' % self.Nc) t('Valence electrons: %.1f' % self.Nv) # Construct smooth wave functions: coefs = [] for l, (u_n, s_n) in enumerate(zip(u_ln, s_ln)): nodeless = True gc = gcut_l[l] for u, s in zip(u_n, s_n): s[:] = u if normconserving_l[l]: A = np.zeros((5, 5)) A[:4, 0] = 1.0 A[:4, 1] = r[gc - 2:gc + 2]**2 A[:4, 2] = A[:4, 1]**2 A[:4, 3] = A[:4, 1] * A[:4, 2] A[:4, 4] = A[:4, 2]**2 A[4, 4] = 1.0 a = u[gc - 2:gc + 3] / r[gc - 2:gc + 3]**(l + 1) a = np.log(a) def f(x): a[4] = x b = solve(A, a) r1 = r[:gc] r2 = r1**2 rl1 = r1**(l + 1) y = b[0] + r2 * (b[1] + r2 * (b[2] + r2 * (b[3] + r2 * b[4]))) y = np.exp(y) s[:gc] = rl1 * y return np.dot(s**2, dr) - 1 x1 = 0.0 x2 = 0.001 f1 = f(x1) f2 = f(x2) while abs(f1) > 1e-6: x0 = (x1 / f1 - x2 / f2) / (1 / f1 - 1 / f2) f0 = f(x0) if abs(f1) < abs(f2): x2, f2 = x1, f1 x1, f1 = x0, f0 else: A = np.ones((4, 4)) A[:, 0] = 1.0 A[:, 1] = r[gc - 2:gc + 2]**2 A[:, 2] = A[:, 1]**2 A[:, 3] = A[:, 1] * A[:, 2] a = u[gc - 2:gc + 2] / r[gc - 2:gc + 2]**(l + 1) if 0:#l < 2 and nodeless: a = np.log(a) a = solve(A, a) r1 = r[:gc] r2 = r1**2 rl1 = r1**(l + 1) y = a[0] + r2 * (a[1] + r2 * (a[2] + r2 * (a[3]))) if 0:#l < 2 and nodeless: y = np.exp(y) s[:gc] = rl1 * y coefs.append(a) if nodeless: if not np.alltrue(s[1:gc] > 0.0): raise RuntimeError( 'Error: The %d%s pseudo wave has a node!' % (n_ln[l][0], 'spdf'[l])) # Only the first state for each l must be nodeless: nodeless = False # Calculate pseudo core density: gcutnc = 1 + int(rcutmax * N / (rcutmax + beta)) self.nct = nct = nc.copy() A = np.ones((4, 4)) A[0] = 1.0 A[1] = r[gcutnc - 2:gcutnc + 2]**2 A[2] = A[1]**2 A[3] = A[1] * A[2] a = nc[gcutnc - 2:gcutnc + 2] a = solve(np.transpose(A), a) r2 = r[:gcutnc]**2 nct[:gcutnc] = a[0] + r2 * (a[1] + r2 * (a[2] + r2 * a[3])) t('Pseudo-core charge: %.6f' % (4 * pi * np.dot(nct, dv))) # ... and the pseudo core kinetic energy density: tauct = tauc.copy() a = tauc[gcutnc - 2:gcutnc + 2] a = solve(np.transpose(A), a) tauct[:gcutnc] = a[0] + r2 * (a[1] + r2 * (a[2] + r2 * a[3])) # ... and the soft valence density: nt = np.zeros(N) for f_n, s_n in zip(f_ln, s_ln): nt += np.dot(f_n, s_n**2) / (4 * pi) nt[1:] /= r[1:]**2 nt[0] = nt[1] nt += nct self.nt = nt # Calculate the shape function: x = r / rcutcomp gaussian = np.zeros(N) self.gamma = gamma = 10.0 gaussian[:gmax] = np.exp(-gamma * x[:gmax]**2) gt = 4 * (gamma / rcutcomp**2)**1.5 / sqrt(pi) * gaussian t('Shape function alpha=%.3f' % (gamma / rcutcomp**2)) norm = np.dot(gt, dv) #print norm, norm-1 assert abs(norm - 1) < 1e-2 gt /= norm # Calculate smooth charge density: Nt = np.dot(nt, dv) rhot = nt - (Nt + charge / (4 * pi)) * gt t('Pseudo-electron charge', 4 * pi * Nt) vHt = np.zeros(N) hartree(0, rhot * r * dr, self.beta, self.N, vHt) vHt[1:] /= r[1:] vHt[0] = vHt[1] vXCt = np.zeros(N) extra_xc_data = {} if self.xc.type != 'GLLB': Exct = self.xc.calculate_spherical(self.rgd, nt.reshape((1, -1)), vXCt.reshape((1, -1))) else: Exct = self.xc.get_smooth_xc_potential_and_energy_1d(vXCt) # Calculate extra-stuff for non-local functionals self.xc.get_extra_setup_data(extra_xc_data) vt = vHt + vXCt # Construct zero potential: gc = 1 + int(rcutvbar * N / (rcutvbar + beta)) if vbar_type == 'f': assert lmax == 2 uf = np.zeros(N) l = 3 # Solve for all-electron f-state: eps = 0.0 shoot(uf, l, self.vr, eps, self.r2dvdr, r, dr, c10, c2, self.scalarrel, gmax=gmax) uf *= 1.0 / uf[gc] # Fit smooth pseudo f-state polynomium: A = np.ones((4, 4)) A[:, 0] = 1.0 A[:, 1] = r[gc - 2:gc + 2]**2 A[:, 2] = A[:, 1]**2 A[:, 3] = A[:, 1] * A[:, 2] a = uf[gc - 2:gc + 2] / r[gc - 2:gc + 2]**(l + 1) a0, a1, a2, a3 = solve(A, a) r1 = r[:gc] r2 = r1**2 rl1 = r1**(l + 1) y = a0 + r2 * (a1 + r2 * (a2 + r2 * a3)) sf = uf.copy() sf[:gc] = rl1 * y # From 0 to gc, use analytic formula for kinetic energy operator: r4 = r2**2 r6 = r4 * r2 enumerator = (a0 * l * (l + 1) + a1 * (l + 2) * (l + 3) * r2 + a2 * (l + 4) * (l + 5) * r4 + a3 * (l + 6) * (l + 7) * r6) denominator = a0 + a1 * r2 + a2 * r4 + a3 * r6 ekin_over_phit = - 0.5 * (enumerator / denominator - l * (l + 1)) ekin_over_phit[1:] /= r2[1:] vbar = eps - vt vbar[:gc] -= ekin_over_phit vbar[0] = vbar[1] # Actually we can collect the terms into # a single fraction without poles, so as to avoid doing this, # but this is good enough # From gc to gmax, use finite-difference formula for kinetic # energy operator: vbar[gc:gmax] -= self.kin(l, sf)[gc:gmax] / sf[gc:gmax] vbar[gmax:] = 0.0 else: assert vbar_type == 'poly' A = np.ones((2, 2)) A[0] = 1.0 A[1] = r[gc - 1:gc + 1]**2 a = vt[gc - 1:gc + 1] a = solve(np.transpose(A), a) r2 = r**2 vbar = a[0] + r2 * a[1] - vt vbar[gc:] = 0.0 vt += vbar # Construct projector functions: for l, (e_n, s_n, q_n) in enumerate(zip(e_ln, s_ln, q_ln)): for e, s, q in zip(e_n, s_n, q_n): q[:] = self.kin(l, s) + (vt - e) * s q[gcutmax:] = 0.0 filter = Filter(r, dr, gcutfilter, hfilter).filter vbar = filter(vbar * r) # Calculate matrix elements: self.dK_lnn = dK_lnn = [] self.dH_lnn = dH_lnn = [] self.dO_lnn = dO_lnn = [] for l, (e_n, u_n, s_n, q_n) in enumerate(zip(e_ln, u_ln, s_ln, q_ln)): A_nn = np.inner(s_n, q_n * dr) # Do a LU decomposition of A: nn = len(e_n) L_nn = np.identity(nn, float) U_nn = A_nn.copy() # Keep all bound states normalized if sum([n > 0 for n in n_ln[l]]) <= 1: for i in range(nn): for j in range(i + 1, nn): L_nn[j, i] = 1.0 * U_nn[j, i] / U_nn[i, i] U_nn[j, :] -= U_nn[i, :] * L_nn[j, i] dO_nn = (np.inner(u_n, u_n * dr) - np.inner(s_n, s_n * dr)) e_nn = np.zeros((nn, nn)) e_nn.ravel()[::nn + 1] = e_n dH_nn = np.dot(dO_nn, e_nn) - A_nn q_n[:] = np.dot(inv(np.transpose(U_nn)), q_n) s_n[:] = np.dot(inv(L_nn), s_n) u_n[:] = np.dot(inv(L_nn), u_n) dO_nn = np.dot(np.dot(inv(L_nn), dO_nn), inv(np.transpose(L_nn))) dH_nn = np.dot(np.dot(inv(L_nn), dH_nn), inv(np.transpose(L_nn))) ku_n = [self.kin(l, u, e) for u, e in zip(u_n, e_n)] ks_n = [self.kin(l, s) for s in s_n] dK_nn = 0.5 * (np.inner(u_n, ku_n * dr) - np.inner(s_n, ks_n * dr)) dK_nn += np.transpose(dK_nn).copy() dK_lnn.append(dK_nn) dO_lnn.append(dO_nn) dH_lnn.append(dH_nn) for n, q in enumerate(q_n): q[:] = filter(q, l) * r**(l + 1) A_nn = np.inner(s_n, q_n * dr) q_n[:] = np.dot(inv(np.transpose(A_nn)), q_n) self.vt = vt self.vbar = vbar t('state eigenvalue norm') t('--------------------------------') for l, (n_n, f_n, e_n) in enumerate(zip(n_ln, f_ln, e_ln)): for n in range(len(e_n)): if n_n[n] > 0: f = '(%d)' % f_n[n] t('%d%s%-4s: %12.6f %12.6f' % ( n_n[n], 'spdf'[l], f, e_n[n], np.dot(s_ln[l][n]**2, dr))) else: t('*%s : %12.6f' % ('spdf'[l], e_n[n])) t('--------------------------------') self.logd = {} if logderiv: ni = 300 self.elog = np.linspace(-5.0, 1.0, ni) # Calculate logarithmic derivatives: gld = gcutmax + 10 self.rlog = r[gld] assert gld < gmax t('Calculating logarithmic derivatives at r=%.3f' % r[gld]) t('(skip with [Ctrl-C])') try: u = np.zeros(N) for l in range(4): self.logd[l] = (np.empty(ni), np.empty(ni)) if l <= lmax: dO_nn = dO_lnn[l] dH_nn = dH_lnn[l] q_n = q_ln[l] fae = open(self.symbol + '.ae.ld.' + 'spdf'[l], 'w') fps = open(self.symbol + '.ps.ld.' + 'spdf'[l], 'w') for i, e in enumerate(self.elog): # All-electron logarithmic derivative: u[:] = 0.0 shoot(u, l, self.vr, e, self.r2dvdr, r, dr, c10, c2, self.scalarrel, gmax=gld) dudr = 0.5 * (u[gld + 1] - u[gld - 1]) / dr[gld] ld = dudr / u[gld] - 1.0 / r[gld] print >> fae, e, ld self.logd[l][0][i] = ld # PAW logarithmic derivative: s = self.integrate(l, vt, e, gld) if l <= lmax: A_nn = dH_nn - e * dO_nn s_n = [self.integrate(l, vt, e, gld, q) for q in q_n] B_nn = np.inner(q_n, s_n * dr) a_n = np.dot(q_n, s * dr) B_nn = np.dot(A_nn, B_nn) B_nn.ravel()[::len(a_n) + 1] += 1.0 c_n = solve(B_nn, np.dot(A_nn, a_n)) s -= np.dot(c_n, s_n) dsdr = 0.5 * (s[gld + 1] - s[gld - 1]) / dr[gld] ld = dsdr / s[gld] - 1.0 / r[gld] print >> fps, e, ld self.logd[l][1][i] = ld except KeyboardInterrupt: pass self.write(nc,'nc') self.write(nt, 'nt') self.write(nct, 'nct') self.write(vbar, 'vbar') self.write(vt, 'vt') self.write(tauc, 'tauc') self.write(tauct, 'tauct') for l, (n_n, f_n, u_n, s_n, q_n) in enumerate(zip(n_ln, f_ln, u_ln, s_ln, q_ln)): for n, f, u, s, q in zip(n_n, f_n, u_n, s_n, q_n): if n < 0: self.write(u, 'ae', n=n, l=l) self.write(s, 'ps', n=n, l=l) self.write(q, 'proj', n=n, l=l) # Test for ghost states: for h in [0.05]: self.diagonalize(h) self.vn_j = vn_j = [] self.vl_j = vl_j = [] self.vf_j = vf_j = [] self.ve_j = ve_j = [] self.vu_j = vu_j = [] self.vs_j = vs_j = [] self.vq_j = vq_j = [] j_ln = [[0 for f in f_n] for f_n in f_ln] j = 0 for l, n_n in enumerate(n_ln): for n, nn in enumerate(n_n): if nn > 0: vf_j.append(f_ln[l][n]) vn_j.append(nn) vl_j.append(l) ve_j.append(e_ln[l][n]) vu_j.append(u_ln[l][n]) vs_j.append(s_ln[l][n]) vq_j.append(q_ln[l][n]) j_ln[l][n] = j j += 1 for l, n_n in enumerate(n_ln): for n, nn in enumerate(n_n): if nn < 0: vf_j.append(0) vn_j.append(nn) vl_j.append(l) ve_j.append(e_ln[l][n]) vu_j.append(u_ln[l][n]) vs_j.append(s_ln[l][n]) vq_j.append(q_ln[l][n]) j_ln[l][n] = j j += 1 nj = j self.dK_jj = np.zeros((nj, nj)) for l, j_n in enumerate(j_ln): for n1, j1 in enumerate(j_n): for n2, j2 in enumerate(j_n): self.dK_jj[j1, j2] = self.dK_lnn[l][n1, n2] if exx: X_p = constructX(self) ExxC = atomic_exact_exchange(self, 'core-core') else: X_p = None ExxC = None sqrt4pi = sqrt(4 * pi) setup = SetupData(self.symbol, self.xc.name, self.name, readxml=False) def divide_by_r(x_g, l): r = self.r #for x_g, l in zip(x_jg, l_j): p = x_g.copy() p[1:] /= self.r[1:] # XXXXX go to higher order!!!!! if l == 0:#l_j[self.jcorehole] == 0: p[0] = (p[2] + (p[1] - p[2]) * (r[0] - r[2]) / (r[1] - r[2])) return p def divide_all_by_r(x_jg): return [divide_by_r(x_g, l) for x_g, l in zip(x_jg, vl_j)] setup.l_j = vl_j setup.n_j = vn_j setup.f_j = vf_j setup.eps_j = ve_j setup.rcut_j = [rcut_l[l] for l in vl_j] setup.nc_g = nc * sqrt4pi setup.nct_g = nct * sqrt4pi setup.nvt_g = (nt - nct) * sqrt4pi setup.e_kinetic_core = Ekincore setup.vbar_g = vbar * sqrt4pi setup.tauc_g = tauc * sqrt4pi setup.tauct_g = tauct * sqrt4pi setup.extra_xc_data = extra_xc_data setup.Z = Z setup.Nc = self.Nc setup.Nv = self.Nv setup.e_kinetic = self.Ekin setup.e_xc = self.Exc setup.e_electrostatic = self.Epot setup.e_total = self.Epot + self.Exc + self.Ekin setup.rgd = self.rgd setup.rcgauss = self.rcutcomp / sqrt(self.gamma) setup.e_kin_jj = self.dK_jj setup.ExxC = ExxC setup.phi_jg = divide_all_by_r(vu_j) setup.phit_jg = divide_all_by_r(vs_j) setup.pt_jg = divide_all_by_r(vq_j) setup.X_p = X_p if self.jcorehole is not None: setup.has_corehole = True setup.lcorehole = l_j[self.jcorehole] # l_j or vl_j ????? XXX setup.ncorehole = n_j[self.jcorehole] setup.phicorehole_g = divide_by_r(self.u_j[self.jcorehole], setup.lcorehole) setup.core_hole_e = self.e_j[self.jcorehole] setup.core_hole_e_kin = self.Ekincorehole setup.fcorehole = self.fcorehole if self.ghost: raise RuntimeError('Ghost!') if self.scalarrel: reltype = 'scalar-relativistic' else: reltype = 'non-relativistic' attrs = [('type', reltype), ('name', 'gpaw-%s' % version)] data = 'Frozen core: '+ (self.core or 'none') setup.generatorattrs = attrs setup.generatordata = data self.id_j = [] for l, n in zip(vl_j, vn_j): if n > 0: id = '%s-%d%s' % (self.symbol, n, 'spdf'[l]) else: id = '%s-%s%d' % (self.symbol, 'spdf'[l], -n) self.id_j.append(id) setup.id_j = self.id_j if write_xml: setup.write_xml() return setup
# H**O energy in mHa for closed shell atoms e_HOMO_cs = { 'He': 851, 'Be': 321, 'Ne': 788, 'Ar': 577, 'Kr': 529, 'Xe': 474, 'Mg' : 281 + 8 } #e_HOMO_cs = { 'Ne': 788 } txt=None print('--- Comparing LB94 with', ref1) print('and', ref2) print('**** all electron calculations') print('atom [refs] -e_homo diff all in mHa') if rank == 0: for atom in e_HOMO_cs.keys(): ae = AllElectron(atom, 'LB94', txt=txt) ae.run() e_homo = int( ae.e_j[-1] * 10000 + .5 ) / 10. diff = e_HOMO_cs[atom] + e_homo print('%2s %8g %6.1f %4.1g' % (atom, e_HOMO_cs[atom], -e_homo, diff)) assert abs(diff) < 6 barrier() setup_paths.insert(0, '.') setups = {} print('**** 3D calculations') print('atom [refs] -e_homo diff all in mHa') for atom in e_HOMO_cs.keys(): e_ref = e_HOMO_cs[atom] # generate setup for the atom
# H**O energy in mHa for closed shell atoms e_HOMO_cs = { 'He': 851, 'Be': 321, 'Ne': 788, 'Ar': 577, 'Kr': 529, 'Xe': 474, 'Mg' : 281 + 8 } #e_HOMO_cs = { 'Ne': 788 } txt=None print '--- Comparing LB94 with', ref1 print 'and', ref2 print '**** all electron calculations' print 'atom [refs] -e_homo diff all in mHa' if rank == 0: for atom in e_HOMO_cs.keys(): ae = AllElectron(atom, 'LB94', txt=txt) ae.run() e_homo = int( ae.e_j[-1] * 10000 + .5 ) / 10. diff = e_HOMO_cs[atom] + e_homo print '%2s %8g %6.1f %4.1g' % (atom, e_HOMO_cs[atom], -e_homo, diff) assert abs(diff) < 6 barrier() setup_paths.insert(0, '.') setups = {} print '**** 3D calculations' print 'atom [refs] -e_homo diff all in mHa' for atom in e_HOMO_cs.keys(): e_ref = e_HOMO_cs[atom] # generate setup for the atom
from gpaw.atom.all_electron import AllElectron a = AllElectron("C") a.run()
from gpaw.atom.all_electron import AllElectron def check(name, atomE, atomeig, total, H**O): #print name, " ", atomE,"/", total, " | ", atomeig,"/",H**O," |" print "|", name, " |%12.4f/%12.4f | %12.4f/%12.4f |" % (atomE, total, atomeig, H**O) assert abs(atomE - total) < 8e-3 assert abs(atomeig - H**O) < 1e-3 A = AllElectron('He', 'KLI', False) A.run() HeE = A.Ekin + A.Epot + A.Exc Heeig = A.e_j[0] A = AllElectron('Be', 'KLI', False) A.run() BeE = A.Ekin + A.Epot + A.Exc Beeig = A.e_j[1] A = AllElectron('Ne', 'KLI', False) A.run() NeE = A.Ekin + A.Epot + A.Exc Neeig = A.e_j[2] A = AllElectron('Mg', 'KLI', False) A.run() MgE = A.Ekin + A.Epot + A.Exc
ETotal = { 'Be': -14.572 + 0.012, 'Ne': -128.548 - 0.029, 'Mg': -199.612 - 0.005 } EX = {'Be': -2.666 - 0.010, 'Ne': -12.107 - 0.122, 'Mg': -15.992 - 0.092} EHOMO = {'Be': -0.309 + 0.008, 'Ne': -0.851 + 0.098, 'Mg': -0.253 + 0.006} eignum = {'Be': 0, 'Ne': 3, 'Mg': 0} for xcname in ['GLLB', 'GLLBSC']: atoms = ['Be', 'Ne', 'Mg'] for atom in atoms: # Test AllElectron GLLB GLLB = AllElectron(atom, xcname=xcname, scalarrel=False, gpernode=600) GLLB.run() out("Total energy", xcname + "1D", atom, ETotal[atom], GLLB.ETotal, "Ha") out("Exchange energy", xcname + "1D", atom, EX[atom], GLLB.Exc, "Ha") out("H**O Eigenvalue", xcname + "1D", atom, EHOMO[atom], GLLB.e_j[-1], "Ha") if xcname == 'GLLB': equal(GLLB.ETotal, ETotal[atom], tolerance=1e-2) equal(GLLB.Exc, EX[atom], tolerance=1e-2) equal(GLLB.e_j[-1], EHOMO[atom], tolerance=1e-2) print( " Quanity Method Symbol Ref[1] GPAW Unit " ) for a, b, c, d, e, f in data:
data = [] def out(a,b,c,d,e,f): data.append( (a,b,c,d,e,f) ) ETotal = {'Be': -14.572+0.012, 'Ne': -128.548 -0.029, 'Mg': -199.612 - 0.005 } EX = {'Be': -2.666 - 0.010, 'Ne': -12.107 -0.122, 'Mg': -15.992 -0.092 } EHOMO = {'Be': -0.309 + 0.008, 'Ne': -0.851 + 0.098, 'Mg': -0.253 + 0.006} eignum = {'Be': 0, 'Ne':3, 'Mg':0 } for xcname in ['GLLB','GLLBSC']: atoms = ['Be','Ne','Mg'] for atom in atoms: # Test AllElectron GLLB GLLB = AllElectron(atom, xcname = xcname, scalarrel = False, gpernode = 600) GLLB.run() out("Total energy", xcname+"1D", atom, ETotal[atom] , GLLB.ETotal,"Ha") out("Exchange energy", xcname+"1D", atom, EX[atom], GLLB.Exc,"Ha") out("H**O Eigenvalue", xcname+"1D", atom, EHOMO[atom], GLLB.e_j[-1],"Ha") if xcname == 'GLLB': equal(GLLB.ETotal, ETotal[atom], tolerance=1e-2) equal(GLLB.Exc, EX[atom], tolerance=1e-2) equal(GLLB.e_j[-1], EHOMO[atom], tolerance=1e-2) print " Quanity Method Symbol Ref[1] GPAW Unit " for a,b,c,d,e,f in data: print "%20s %10s %10s %10.3f %10.3f %5s" % (a,b,c,d,e,f) print """References: [1] Self-consistent approximation to the Kohn-Sham exchange potential
from ase import * from gpaw.atom.all_electron import AllElectron # Calculate Helium atom using 3D-code he = Atoms(positions=[(0,0,0)], symbols='He') he.center(vacuum=3.0) calc = GPAW(h=0.17) he.set_calculator(calc) he.get_potential_energy() # Get the all-electron potential around the nucleus vKS_sLg = calc.nuclei[0].calculate_all_electron_potential(calc.hamiltonian.vHt_g) # Calculate Helium atom using 1D-code he_atom =AllElectron('He') he_atom.run() # Get the KS-potential vKS_atom = he_atom.vr / he_atom.r vKS_atom[0] = vKS_atom[1] # Get the spherical symmetric part and multiply with Y_00 vKS = vKS_sLg[0][0] / sqrt(4*pi) # Compare avg_diff = 0.0 for i, v in enumerate(vKS): avg_diff += abs(vKS_atom[i]-v) avg_diff /= len(vKS) print "Potential expansion is correct to", avg_diff * calc.Ha, " eV"
def main(): parser = build_parser() opt, args = parser.parse_args() import sys from gpaw.atom.generator import Generator from gpaw.atom.configurations import parameters, tf_parameters from gpaw.atom.all_electron import AllElectron from gpaw import ConvergenceError if args: atoms = args else: atoms = parameters.keys() bad_density_warning = """\ Problem with initial electron density guess! Try to run the program with the '-nw' option (non-scalar-relativistic calculation + write density) and then try again without the '-n' option (this will generate a good initial guess for the density).""" for symbol in atoms: scalarrel = not opt.non_scalar_relativistic corehole = None if opt.core_hole is not None: state, occ = opt.core_hole.split(',') # Translate corestate string ('1s') to n and l: ncorehole = int(state[0]) lcorehole = 'spdf'.find(state[1]) fcorehole = float(occ) corehole = (ncorehole, lcorehole, fcorehole) if opt.all_electron_only: a = AllElectron(symbol, opt.xcfunctional, scalarrel, corehole, opt.configuration, not opt.write_files, '-', opt.points_per_node, opt.orbital_free, opt.tw_coefficient) try: a.run() except ConvergenceError: print(bad_density_warning, file=sys.stderr) continue g = Generator(symbol, opt.xcfunctional, scalarrel, corehole, opt.configuration, not opt.write_files, '-', opt.points_per_node, orbital_free=opt.orbital_free, tw_coeff=opt.tw_coefficient) if opt.orbital_free: p = tf_parameters.get(symbol, {'rcut': 0.9}) else: p = parameters.get(symbol, {}) if opt.core is not None: p['core'] = opt.core if opt.radius is not None: p['rcut'] = [float(x) for x in opt.radius.split(',')] if opt.extra_projectors is not None: extra = {} if opt.extra_projectors != '': for l, x in enumerate(opt.extra_projectors.split(';')): if x != '': extra[l] = [float(y) for y in x.split(',')] p['extra'] = extra if opt.normconserving is not None: p['normconserving'] = opt.normconserving if opt.filter is not None: p['filter'] = [float(x) for x in opt.filter.split(',')] if opt.compensation_charge_radius is not None: p['rcutcomp'] = opt.compensation_charge_radius if opt.zero_potential is not None: vbar = opt.zero_potential.split(',') p['vbar'] = (vbar[0], float(vbar[1])) if opt.empty_states is not None: p['empty_states'] = opt.empty_states try: g.run(logderiv=opt.logarithmic_derivatives, exx=opt.exact_exchange, name=opt.name, use_restart_file=opt.use_restart_file, **p) except ConvergenceError: print(bad_density_warning, file=sys.stderr) except RuntimeError as m: if len(m.__str__()) == 0: raise print(m) if opt.plot: from gpaw.atom.analyse_setup import analyse analyse(g, show=True)
def run(self, core='', rcut=1.0, extra=None, logderiv=False, vbar=None, exx=False, name=None, normconserving='', filter=(0.4, 1.75), rcutcomp=None, write_xml=True, use_restart_file=True, empty_states=''): self.name = name self.core = core if type(rcut) is float: rcut_l = [rcut] else: rcut_l = rcut rcutmax = max(rcut_l) rcutmin = min(rcut_l) self.rcut_l = rcut_l if rcutcomp is None: rcutcomp = rcutmin self.rcutcomp = rcutcomp hfilter, xfilter = filter Z = self.Z n_j = self.n_j l_j = self.l_j f_j = self.f_j e_j = self.e_j if vbar is None: vbar = ('poly', rcutmin * 0.9) vbar_type, rcutvbar = vbar normconserving_l = [x in normconserving for x in 'spdf'] # Parse core string: j = 0 if core.startswith('['): a, core = core.split(']') core_symbol = a[1:] j = len(configurations[core_symbol][1]) while core != '': assert n_j[j] == int(core[0]) assert l_j[j] == 'spdf'.find(core[1]) if j != self.jcorehole: assert f_j[j] == 2 * (2 * l_j[j] + 1) j += 1 core = core[2:] njcore = j self.njcore = njcore lmaxocc = max(l_j[njcore:]) while empty_states != '': n = int(empty_states[0]) l = 'spdf'.find(empty_states[1]) print l_j assert n == 1 + l + l_j.count(l) n_j.append(n) l_j.append(l) f_j.append(0.0) e_j.append(-0.01) empty_states = empty_states[2:] if 2 in l_j[njcore:]: # We have a bound valence d-state. Add bound s- and # p-states if not already there: for l in [0, 1]: if l not in l_j[njcore:]: n_j.append(1 + l + l_j.count(l)) l_j.append(l) f_j.append(0.0) e_j.append(-0.01) if l_j[njcore:] == [0] and Z > 2: # We have only a bound valence s-state and we are not # hydrogen and not helium. Add bound p-state: n_j.append(n_j[njcore]) l_j.append(1) f_j.append(0.0) e_j.append(-0.01) nj = len(n_j) self.Nv = sum(f_j[njcore:]) self.Nc = sum(f_j[:njcore]) # Do all-electron calculation: AllElectron.run(self, use_restart_file) # Highest occupied atomic orbital: self.emax = max(e_j) N = self.N r = self.r dr = self.dr d2gdr2 = self.d2gdr2 beta = self.beta dv = r**2 * dr t = self.text t() t('Generating PAW setup') if core != '': t('Frozen core:', core) # So far - no ghost-states: self.ghost = False # Calculate the kinetic energy of the core states: Ekincore = 0.0 j = 0 for f, e, u in zip(f_j[:njcore], e_j[:njcore], self.u_j[:njcore]): u = np.where(abs(u) < 1e-160, 0, u) # XXX Numeric! k = e - np.sum((u**2 * self.vr * dr)[1:] / r[1:]) Ekincore += f * k if j == self.jcorehole: self.Ekincorehole = k j += 1 # Calculate core density: if njcore == 0: nc = np.zeros(N) else: uc_j = self.u_j[:njcore] uc_j = np.where(abs(uc_j) < 1e-160, 0, uc_j) # XXX Numeric! nc = np.dot(f_j[:njcore], uc_j**2) / (4 * pi) nc[1:] /= r[1:]**2 nc[0] = nc[1] self.nc = nc # Calculate core kinetic energy density if njcore == 0: tauc = np.zeros(N) else: tauc = self.radial_kinetic_energy_density(f_j[:njcore], l_j[:njcore], self.u_j[:njcore]) t('Kinetic energy of the core from tauc =', np.dot(tauc * r * r, dr) * 4 * pi) lmax = max(l_j[njcore:]) # Order valence states with respect to angular momentum # quantum number: self.n_ln = n_ln = [] self.f_ln = f_ln = [] self.e_ln = e_ln = [] for l in range(lmax + 1): n_n = [] f_n = [] e_n = [] for j in range(njcore, nj): if l_j[j] == l: n_n.append(n_j[j]) f_n.append(f_j[j]) e_n.append(e_j[j]) n_ln.append(n_n) f_ln.append(f_n) e_ln.append(e_n) # Add extra projectors: if extra is not None: if len(extra) == 0: lmaxextra = 0 else: lmaxextra = max(extra.keys()) if lmaxextra > lmax: for l in range(lmax, lmaxextra): n_ln.append([]) f_ln.append([]) e_ln.append([]) lmax = lmaxextra for l in extra: nn = -1 for e in extra[l]: n_ln[l].append(nn) f_ln[l].append(0.0) e_ln[l].append(e) nn -= 1 else: # Automatic: # Make sure we have two projectors for each occupied channel: for l in range(lmaxocc + 1): if len(n_ln[l]) < 2 and not normconserving_l[l]: # Only one - add one more: assert len(n_ln[l]) == 1 n_ln[l].append(-1) f_ln[l].append(0.0) e_ln[l].append(1.0 + e_ln[l][0]) if lmaxocc < 2 and lmaxocc == lmax: # Add extra projector for l = lmax + 1: n_ln.append([-1]) f_ln.append([0.0]) e_ln.append([0.0]) lmax += 1 self.lmax = lmax rcut_l.extend([rcutmin] * (lmax + 1 - len(rcut_l))) t('Cutoffs:') for rc, s in zip(rcut_l, 'spdf'): t('rc(%s)=%.3f' % (s, rc)) t('rc(vbar)=%.3f' % rcutvbar) t('rc(comp)=%.3f' % rcutcomp) t() t('Kinetic energy of the core states: %.6f' % Ekincore) # Allocate arrays: self.u_ln = u_ln = [] # phi * r self.s_ln = s_ln = [] # phi-tilde * r self.q_ln = q_ln = [] # p-tilde * r for l in range(lmax + 1): nn = len(n_ln[l]) u_ln.append(np.zeros((nn, N))) s_ln.append(np.zeros((nn, N))) q_ln.append(np.zeros((nn, N))) # Fill in all-electron wave functions: for l in range(lmax + 1): # Collect all-electron wave functions: u_n = [self.u_j[j] for j in range(njcore, nj) if l_j[j] == l] for n, u in enumerate(u_n): u_ln[l][n] = u # Grid-index corresponding to rcut: gcut_l = [1 + int(rc * N / (rc + beta)) for rc in rcut_l] rcutfilter = xfilter * rcutmax self.rcutfilter = rcutfilter gcutfilter = 1 + int(rcutfilter * N / (rcutfilter + beta)) gcutmax = 1 + int(rcutmax * N / (rcutmax + beta)) # Outward integration of unbound states stops at 3 * rcut: gmax = int(3 * rcutmax * N / (3 * rcutmax + beta)) assert gmax > gcutfilter # Calculate unbound extra states: c2 = -(r / dr)**2 c10 = -d2gdr2 * r**2 for l, (n_n, e_n, u_n) in enumerate(zip(n_ln, e_ln, u_ln)): for n, e, u in zip(n_n, e_n, u_n): if n < 0: u[:] = 0.0 shoot(u, l, self.vr, e, self.r2dvdr, r, dr, c10, c2, self.scalarrel, gmax=gmax) u *= 1.0 / u[gcut_l[l]] charge = Z - self.Nv - self.Nc t('Charge: %.1f' % charge) t('Core electrons: %.1f' % self.Nc) t('Valence electrons: %.1f' % self.Nv) # Construct smooth wave functions: coefs = [] for l, (u_n, s_n) in enumerate(zip(u_ln, s_ln)): nodeless = True gc = gcut_l[l] for u, s in zip(u_n, s_n): s[:] = u if normconserving_l[l]: A = np.zeros((5, 5)) A[:4, 0] = 1.0 A[:4, 1] = r[gc - 2:gc + 2]**2 A[:4, 2] = A[:4, 1]**2 A[:4, 3] = A[:4, 1] * A[:4, 2] A[:4, 4] = A[:4, 2]**2 A[4, 4] = 1.0 a = u[gc - 2:gc + 3] / r[gc - 2:gc + 3]**(l + 1) a = np.log(a) def f(x): a[4] = x b = solve(A, a) r1 = r[:gc] r2 = r1**2 rl1 = r1**(l + 1) y = b[0] + r2 * (b[1] + r2 * (b[2] + r2 * (b[3] + r2 * b[4]))) y = np.exp(y) s[:gc] = rl1 * y return np.dot(s**2, dr) - 1 x1 = 0.0 x2 = 0.001 f1 = f(x1) f2 = f(x2) while abs(f1) > 1e-6: x0 = (x1 / f1 - x2 / f2) / (1 / f1 - 1 / f2) f0 = f(x0) if abs(f1) < abs(f2): x2, f2 = x1, f1 x1, f1 = x0, f0 else: A = np.ones((4, 4)) A[:, 0] = 1.0 A[:, 1] = r[gc - 2:gc + 2]**2 A[:, 2] = A[:, 1]**2 A[:, 3] = A[:, 1] * A[:, 2] a = u[gc - 2:gc + 2] / r[gc - 2:gc + 2]**(l + 1) if 0: #l < 2 and nodeless: a = np.log(a) a = solve(A, a) r1 = r[:gc] r2 = r1**2 rl1 = r1**(l + 1) y = a[0] + r2 * (a[1] + r2 * (a[2] + r2 * (a[3]))) if 0: #l < 2 and nodeless: y = np.exp(y) s[:gc] = rl1 * y coefs.append(a) if nodeless: if not np.alltrue(s[1:gc] > 0.0): raise RuntimeError( 'Error: The %d%s pseudo wave has a node!' % (n_ln[l][0], 'spdf'[l])) # Only the first state for each l must be nodeless: nodeless = False # Calculate pseudo core density: gcutnc = 1 + int(rcutmax * N / (rcutmax + beta)) self.nct = nct = nc.copy() A = np.ones((4, 4)) A[0] = 1.0 A[1] = r[gcutnc - 2:gcutnc + 2]**2 A[2] = A[1]**2 A[3] = A[1] * A[2] a = nc[gcutnc - 2:gcutnc + 2] a = solve(np.transpose(A), a) r2 = r[:gcutnc]**2 nct[:gcutnc] = a[0] + r2 * (a[1] + r2 * (a[2] + r2 * a[3])) t('Pseudo-core charge: %.6f' % (4 * pi * np.dot(nct, dv))) # ... and the pseudo core kinetic energy density: tauct = tauc.copy() a = tauc[gcutnc - 2:gcutnc + 2] a = solve(np.transpose(A), a) tauct[:gcutnc] = a[0] + r2 * (a[1] + r2 * (a[2] + r2 * a[3])) # ... and the soft valence density: nt = np.zeros(N) for f_n, s_n in zip(f_ln, s_ln): nt += np.dot(f_n, s_n**2) / (4 * pi) nt[1:] /= r[1:]**2 nt[0] = nt[1] nt += nct self.nt = nt # Calculate the shape function: x = r / rcutcomp gaussian = np.zeros(N) self.gamma = gamma = 10.0 gaussian[:gmax] = np.exp(-gamma * x[:gmax]**2) gt = 4 * (gamma / rcutcomp**2)**1.5 / sqrt(pi) * gaussian norm = np.dot(gt, dv) #print norm, norm-1 assert abs(norm - 1) < 1e-2 gt /= norm # Calculate smooth charge density: Nt = np.dot(nt, dv) rhot = nt - (Nt + charge / (4 * pi)) * gt t('Pseudo-electron charge', 4 * pi * Nt) vHt = np.zeros(N) hartree(0, rhot * r * dr, self.beta, self.N, vHt) vHt[1:] /= r[1:] vHt[0] = vHt[1] vXCt = np.zeros(N) extra_xc_data = {} if self.xc.type != 'GLLB': Exct = self.xc.calculate_spherical(self.rgd, nt.reshape((1, -1)), vXCt.reshape((1, -1))) else: Exct = self.xc.get_smooth_xc_potential_and_energy_1d(vXCt) # Calculate extra-stuff for non-local functionals self.xc.get_extra_setup_data(extra_xc_data) vt = vHt + vXCt # Construct zero potential: gc = 1 + int(rcutvbar * N / (rcutvbar + beta)) if vbar_type == 'f': assert lmax == 2 uf = np.zeros(N) l = 3 # Solve for all-electron f-state: eps = 0.0 shoot(uf, l, self.vr, eps, self.r2dvdr, r, dr, c10, c2, self.scalarrel, gmax=gmax) uf *= 1.0 / uf[gc] # Fit smooth pseudo f-state polynomium: A = np.ones((4, 4)) A[:, 0] = 1.0 A[:, 1] = r[gc - 2:gc + 2]**2 A[:, 2] = A[:, 1]**2 A[:, 3] = A[:, 1] * A[:, 2] a = uf[gc - 2:gc + 2] / r[gc - 2:gc + 2]**(l + 1) a0, a1, a2, a3 = solve(A, a) r1 = r[:gc] r2 = r1**2 rl1 = r1**(l + 1) y = a0 + r2 * (a1 + r2 * (a2 + r2 * a3)) sf = uf.copy() sf[:gc] = rl1 * y # From 0 to gc, use analytic formula for kinetic energy operator: r4 = r2**2 r6 = r4 * r2 enumerator = (a0 * l * (l + 1) + a1 * (l + 2) * (l + 3) * r2 + a2 * (l + 4) * (l + 5) * r4 + a3 * (l + 6) * (l + 7) * r6) denominator = a0 + a1 * r2 + a2 * r4 + a3 * r6 ekin_over_phit = -0.5 * (enumerator / denominator - l * (l + 1)) ekin_over_phit[1:] /= r2[1:] vbar = eps - vt vbar[:gc] -= ekin_over_phit vbar[0] = vbar[1] # Actually we can collect the terms into # a single fraction without poles, so as to avoid doing this, # but this is good enough # From gc to gmax, use finite-difference formula for kinetic # energy operator: vbar[gc:gmax] -= self.kin(l, sf)[gc:gmax] / sf[gc:gmax] vbar[gmax:] = 0.0 else: assert vbar_type == 'poly' A = np.ones((2, 2)) A[0] = 1.0 A[1] = r[gc - 1:gc + 1]**2 a = vt[gc - 1:gc + 1] a = solve(np.transpose(A), a) r2 = r**2 vbar = a[0] + r2 * a[1] - vt vbar[gc:] = 0.0 vt += vbar # Construct projector functions: for l, (e_n, s_n, q_n) in enumerate(zip(e_ln, s_ln, q_ln)): for e, s, q in zip(e_n, s_n, q_n): q[:] = self.kin(l, s) + (vt - e) * s q[gcutmax:] = 0.0 filter = Filter(r, dr, gcutfilter, hfilter).filter vbar = filter(vbar * r) # Calculate matrix elements: self.dK_lnn = dK_lnn = [] self.dH_lnn = dH_lnn = [] self.dO_lnn = dO_lnn = [] for l, (e_n, u_n, s_n, q_n) in enumerate(zip(e_ln, u_ln, s_ln, q_ln)): A_nn = np.inner(s_n, q_n * dr) # Do a LU decomposition of A: nn = len(e_n) L_nn = np.identity(nn, float) U_nn = A_nn.copy() # Keep all bound states normalized if sum([n > 0 for n in n_ln[l]]) <= 1: for i in range(nn): for j in range(i + 1, nn): L_nn[j, i] = 1.0 * U_nn[j, i] / U_nn[i, i] U_nn[j, :] -= U_nn[i, :] * L_nn[j, i] dO_nn = (np.inner(u_n, u_n * dr) - np.inner(s_n, s_n * dr)) e_nn = np.zeros((nn, nn)) e_nn.ravel()[::nn + 1] = e_n dH_nn = np.dot(dO_nn, e_nn) - A_nn q_n[:] = np.dot(inv(np.transpose(U_nn)), q_n) s_n[:] = np.dot(inv(L_nn), s_n) u_n[:] = np.dot(inv(L_nn), u_n) dO_nn = np.dot(np.dot(inv(L_nn), dO_nn), inv(np.transpose(L_nn))) dH_nn = np.dot(np.dot(inv(L_nn), dH_nn), inv(np.transpose(L_nn))) ku_n = [self.kin(l, u, e) for u, e in zip(u_n, e_n)] ks_n = [self.kin(l, s) for s in s_n] dK_nn = 0.5 * (np.inner(u_n, ku_n * dr) - np.inner(s_n, ks_n * dr)) dK_nn += np.transpose(dK_nn).copy() dK_lnn.append(dK_nn) dO_lnn.append(dO_nn) dH_lnn.append(dH_nn) for n, q in enumerate(q_n): q[:] = filter(q, l) * r**(l + 1) A_nn = np.inner(s_n, q_n * dr) q_n[:] = np.dot(inv(np.transpose(A_nn)), q_n) self.vt = vt self.vbar = vbar t('state eigenvalue norm') t('--------------------------------') for l, (n_n, f_n, e_n) in enumerate(zip(n_ln, f_ln, e_ln)): for n in range(len(e_n)): if n_n[n] > 0: f = '(%d)' % f_n[n] t('%d%s%-4s: %12.6f %12.6f' % (n_n[n], 'spdf'[l], f, e_n[n], np.dot(s_ln[l][n]**2, dr))) else: t('*%s : %12.6f' % ('spdf'[l], e_n[n])) t('--------------------------------') self.logd = {} if logderiv: ni = 300 self.elog = np.linspace(-5.0, 1.0, ni) # Calculate logarithmic derivatives: gld = gcutmax + 10 self.rlog = r[gld] assert gld < gmax t('Calculating logarithmic derivatives at r=%.3f' % r[gld]) t('(skip with [Ctrl-C])') try: u = np.zeros(N) for l in range(4): self.logd[l] = (np.empty(ni), np.empty(ni)) if l <= lmax: dO_nn = dO_lnn[l] dH_nn = dH_lnn[l] q_n = q_ln[l] fae = open(self.symbol + '.ae.ld.' + 'spdf'[l], 'w') fps = open(self.symbol + '.ps.ld.' + 'spdf'[l], 'w') for i, e in enumerate(self.elog): # All-electron logarithmic derivative: u[:] = 0.0 shoot(u, l, self.vr, e, self.r2dvdr, r, dr, c10, c2, self.scalarrel, gmax=gld) dudr = 0.5 * (u[gld + 1] - u[gld - 1]) / dr[gld] ld = dudr / u[gld] - 1.0 / r[gld] print >> fae, e, ld self.logd[l][0][i] = ld # PAW logarithmic derivative: s = self.integrate(l, vt, e, gld) if l <= lmax: A_nn = dH_nn - e * dO_nn s_n = [ self.integrate(l, vt, e, gld, q) for q in q_n ] B_nn = np.inner(q_n, s_n * dr) a_n = np.dot(q_n, s * dr) B_nn = np.dot(A_nn, B_nn) B_nn.ravel()[::len(a_n) + 1] += 1.0 c_n = solve(B_nn, np.dot(A_nn, a_n)) s -= np.dot(c_n, s_n) dsdr = 0.5 * (s[gld + 1] - s[gld - 1]) / dr[gld] ld = dsdr / s[gld] - 1.0 / r[gld] print >> fps, e, ld self.logd[l][1][i] = ld except KeyboardInterrupt: pass self.write(nc, 'nc') self.write(nt, 'nt') self.write(nct, 'nct') self.write(vbar, 'vbar') self.write(vt, 'vt') self.write(tauc, 'tauc') self.write(tauct, 'tauct') for l, (n_n, f_n, u_n, s_n, q_n) in enumerate(zip(n_ln, f_ln, u_ln, s_ln, q_ln)): for n, f, u, s, q in zip(n_n, f_n, u_n, s_n, q_n): if n < 0: self.write(u, 'ae', n=n, l=l) self.write(s, 'ps', n=n, l=l) self.write(q, 'proj', n=n, l=l) # Test for ghost states: for h in [0.05]: self.diagonalize(h) self.vn_j = vn_j = [] self.vl_j = vl_j = [] self.vf_j = vf_j = [] self.ve_j = ve_j = [] self.vu_j = vu_j = [] self.vs_j = vs_j = [] self.vq_j = vq_j = [] j_ln = [[0 for f in f_n] for f_n in f_ln] j = 0 for l, n_n in enumerate(n_ln): for n, nn in enumerate(n_n): if nn > 0: vf_j.append(f_ln[l][n]) vn_j.append(nn) vl_j.append(l) ve_j.append(e_ln[l][n]) vu_j.append(u_ln[l][n]) vs_j.append(s_ln[l][n]) vq_j.append(q_ln[l][n]) j_ln[l][n] = j j += 1 for l, n_n in enumerate(n_ln): for n, nn in enumerate(n_n): if nn < 0: vf_j.append(0) vn_j.append(nn) vl_j.append(l) ve_j.append(e_ln[l][n]) vu_j.append(u_ln[l][n]) vs_j.append(s_ln[l][n]) vq_j.append(q_ln[l][n]) j_ln[l][n] = j j += 1 nj = j self.dK_jj = np.zeros((nj, nj)) for l, j_n in enumerate(j_ln): for n1, j1 in enumerate(j_n): for n2, j2 in enumerate(j_n): self.dK_jj[j1, j2] = self.dK_lnn[l][n1, n2] if exx: X_p = constructX(self) ExxC = atomic_exact_exchange(self, 'core-core') else: X_p = None ExxC = None sqrt4pi = sqrt(4 * pi) setup = SetupData(self.symbol, self.xc.name, self.name, readxml=False) def divide_by_r(x_g, l): r = self.r #for x_g, l in zip(x_jg, l_j): p = x_g.copy() p[1:] /= self.r[1:] # XXXXX go to higher order!!!!! if l == 0: #l_j[self.jcorehole] == 0: p[0] = (p[2] + (p[1] - p[2]) * (r[0] - r[2]) / (r[1] - r[2])) return p def divide_all_by_r(x_jg): return [divide_by_r(x_g, l) for x_g, l in zip(x_jg, vl_j)] setup.l_j = vl_j setup.n_j = vn_j setup.f_j = vf_j setup.eps_j = ve_j setup.rcut_j = [rcut_l[l] for l in vl_j] setup.nc_g = nc * sqrt4pi setup.nct_g = nct * sqrt4pi setup.nvt_g = (nt - nct) * sqrt4pi setup.e_kinetic_core = Ekincore setup.vbar_g = vbar * sqrt4pi setup.tauc_g = tauc * sqrt4pi setup.tauct_g = tauct * sqrt4pi setup.extra_xc_data = extra_xc_data setup.Z = Z setup.Nc = self.Nc setup.Nv = self.Nv setup.e_kinetic = self.Ekin setup.e_xc = self.Exc setup.e_electrostatic = self.Epot setup.e_total = self.Epot + self.Exc + self.Ekin setup.beta = self.beta setup.ng = self.N setup.rcgauss = self.rcutcomp / sqrt(self.gamma) setup.e_kin_jj = self.dK_jj setup.ExxC = ExxC setup.phi_jg = divide_all_by_r(vu_j) setup.phit_jg = divide_all_by_r(vs_j) setup.pt_jg = divide_all_by_r(vq_j) setup.X_p = X_p if self.jcorehole is not None: setup.has_corehole = True setup.lcorehole = l_j[self.jcorehole] # l_j or vl_j ????? XXX setup.ncorehole = n_j[self.jcorehole] setup.phicorehole_g = divide_by_r(self.u_j[self.jcorehole], setup.lcorehole) setup.core_hole_e = self.e_j[self.jcorehole] setup.core_hole_e_kin = self.Ekincorehole setup.fcorehole = self.fcorehole if self.ghost: raise RuntimeError('Ghost!') if self.scalarrel: reltype = 'scalar-relativistic' else: reltype = 'non-relativistic' attrs = [('type', reltype), ('name', 'gpaw-%s' % version)] data = 'Frozen core: ' + (self.core or 'none') setup.generatorattrs = attrs setup.generatordata = data self.id_j = [] for l, n in zip(vl_j, vn_j): if n > 0: id = '%s-%d%s' % (self.symbol, n, 'spdf'[l]) else: id = '%s-%s%d' % (self.symbol, 'spdf'[l], -n) self.id_j.append(id) setup.id_j = self.id_j if write_xml: setup.write_xml() return setup
#!/usr/bin/env python # coding: utf-8 # In[1]: from gpaw.atom.all_electron import AllElectron import numpy as np import matplotlib.pyplot as plt # In[2]: ti = AllElectron('Ti', xcname='LDA', scalarrel=True) ti.run() # In[4]: fig = plt.figure(figsize=(10, 7)) for i in range(len(ti.n_j)): plt.plot(ti.rgd.r_g, ti.u_j[i, :], label='n=%i, l=%i' % (ti.n_j[i], ti.l_j[i])) plt.legend() plt.xlim(0, 5) plt.show() # In[5]: rgd = ti.rgd phi, c0 = rgd.pseudize(ti.u_j[6], gc=rgd.ceil(3.0), l=0) # In[6]:
def main(): parser = build_parser() opt, args = parser.parse_args() import sys from gpaw.atom.generator import Generator from gpaw.atom.configurations import parameters, tf_parameters from gpaw.atom.all_electron import AllElectron from gpaw import ConvergenceError if args: atoms = args else: atoms = parameters.keys() bad_density_warning = """\ Problem with initial electron density guess! Try to run the program with the '-nw' option (non-scalar-relativistic calculation + write density) and then try again without the '-n' option (this will generate a good initial guess for the density).""" for symbol in atoms: scalarrel = not opt.non_scalar_relativistic corehole = None if opt.core_hole is not None: state, occ = opt.core_hole.split(',') # Translate corestate string ('1s') to n and l: ncorehole = int(state[0]) lcorehole = 'spdf'.find(state[1]) fcorehole = float(occ) corehole = (ncorehole, lcorehole, fcorehole) if opt.all_electron_only: a = AllElectron(symbol, opt.xcfunctional, scalarrel, corehole, opt.configuration, not opt.write_files, '-', opt.points_per_node, opt.orbital_free, opt.tf_coefficient) try: a.run() except ConvergenceError: print(bad_density_warning, file=sys.stderr) continue g = Generator(symbol, opt.xcfunctional, scalarrel, corehole, opt.configuration, not opt.write_files, '-', opt.points_per_node, orbital_free=opt.orbital_free, tf_coeff=opt.tf_coefficient) if opt.orbital_free: p = tf_parameters.get(symbol, {'rcut': 0.9}) else: p = parameters.get(symbol, {}) if opt.core is not None: p['core'] = opt.core if opt.radius is not None: p['rcut'] = [float(x) for x in opt.radius.split(',')] if opt.extra_projectors is not None: extra = {} if opt.extra_projectors != '': for l, x in enumerate(opt.extra_projectors.split(';')): if x != '': extra[l] = [float(y) for y in x.split(',')] p['extra'] = extra if opt.normconserving is not None: p['normconserving'] = opt.normconserving if opt.filter is not None: p['filter'] = [float(x) for x in opt.filter.split(',')] if opt.compensation_charge_radius is not None: p['rcutcomp'] = opt.compensation_charge_radius if opt.zero_potential is not None: vbar = opt.zero_potential.split(',') p['vbar'] = (vbar[0], float(vbar[1])) if opt.empty_states is not None: p['empty_states'] = opt.empty_states try: g.run(logderiv=opt.logarithmic_derivatives, exx=opt.exact_exchange, name=opt.name, use_restart_file=opt.use_restart_file, **p) except ConvergenceError: print(bad_density_warning, file=sys.stderr) except RuntimeError, m: if len(m.__str__()) == 0: raise print(m) if opt.plot: from gpaw.atom.analyse_setup import analyse analyse(g, show=True)