def relax_continuum_orbital(atomlist, phi0, potential_ee, E, max_iter=100, thresh=1.0e-6): # DOES NOT WORK """ improves an initial guess phi0 for a continuum orbital with energy E by iteratively solving the following linear equation 2 2 (H-E) dphi = -(H-E) phi 0 for the orbital correction dphi. The improved orbital is phi0+dphi. The linear equation has the form A.x = b and can be solve using by modified Richardson iteration (k+1) (k) (k) x = x + w (b - A.x ) for some parameter w > 0. Parameters ========== atomlist : list of tuples (Zat,[x,y,z]) with atomic numbers and positions phi0 : callable, phi0(x,y,z) evaluates the initial orbital potential_ee : callable, potential_ee(x,y,z) evaluates the effective molecular Kohn-Sham potential WITHOUT the nuclear attraction, Vee = Vcoul + Vxc E : float, energy of continuum orbital Optional ======== max_iter : int, maximum number of iterations thresh : float, the iteration is stopped as soon as the error falls below this threshold: |b-A.x| < thresh Returns ======= phi : callable, phi(x,y,z) evaluates the improved orbital """ w = 1.0 # right hand side of A.x = b # b = (H-E)^2 phi0 residual1 = residual_ee_func(atomlist, phi0, potential_ee, E) residual2 = residual_ee_func(atomlist, residual1, potential_ee, E) b = residual2 def null(x, y, z): return 0 * x x = null for i in range(0, max_iter): # compute A.x^(k) = (H-E)^2 phi_k print("residuals...") residual1 = residual_ee_func(atomlist, x, potential_ee, E) residual2 = residual_ee_func(atomlist, residual1, potential_ee, E) Ax = residual2 # b - A.x^(k) print("error...") bmAx = add_two_functions(atomlist, b, Ax, 1.0, -1.0) error = np.sqrt(overlap(atomlist, bmAx, bmAx)) print("iteration i= %d error |b-Ax|= %e" % (i, error)) if error < thresh: print("CONVERGED") break # x^(k+1) = x^(k) + w * (b - A.x^(k)) x_next = add_two_functions(atomlist, x, bmAx, 1.0, w) x = x_next phi = add_two_functions(atomlist, phi0, x, 1.0, 1.0) residual = residual_ee_func(atomlist, phi, potential_ee, E) import matplotlib.pyplot as plt plt.clf() plt.title("Iteration i=%d" % (i + 1)) plt.xlabel("z / bohr") plt.ylim((-0.6, +0.6)) r = np.linspace(-10.0, 10.0, 1000) plt.plot(r, phi(0 * r, 0 * r, r), label=r"$\phi$") plt.plot(r, residual(0 * r, 0 * r, r), ls="-.", label="residual $(H-E)\phi$") plt.legend() plt.savefig("/tmp/relaxation_%3.3d.png" % (i + 1)) plt.show() else: msg = "Orbital relaxation did not converge in '%s' iterations!" % max_iter print("WARNING: %s" % msg) #raise RuntimeError(msg) return phi
def improve_continuum_orbital(atomlist, phi0, potential_ee, E, max_iter=100, thresh=1.0e-6): """ improves an initial guess phi0 for a continuum orbital with energy E by repeatedly computing orbital corrections phi_{i+1} = a_i * phi_{i} + b_i * dphi_{i} The orbital correction delta_phi is obtained as the solution of the inhomogeneous Schroedinger equation (H-E) dphi = -(H-E) phi i i The Schroedinger equation is solved approximately by replacing the effective potential with a spherical average around each atom. The best linear combination of phi and the correction is chosen. The coefficients a_i and b_i minimize the expectation value of the residual 2 R = (H-E) 2 a ,b = argmin <phi |(H-E) |phi > i i i+1 i+1 Parameters ========== atomlist : list of tuples (Zat,[x,y,z]) with atomic numbers and positions phi0 : callable, phi0(x,y,z) evaluates the initial orbital potential_ee : callable, potential_ee(x,y,z) evaluates the effective molecular Kohn-Sham potential WITHOUT the nuclear attraction, Vee = Vcoul + Vxc E : float, energy of continuum orbital Optional ======== max_iter : int, maximum number of orbital correction thresh : float, the iteration is stopped as soon as the weight of the orbital correction falls below this threshold: 2 b_i <= thresh Returns ======= phi : callable, phi(x,y,z) evaluates the improved orbital """ # attraction potential between nuclei and electrons nuclear_potential = nuclear_potential_func(atomlist) # effective potential (electron-electron interaction + nuclei-electrons) def potential(x, y, z): return potential_ee(x, y, z) + nuclear_potential(x, y, z) print("orbital corrections...") phi = phi0 for i in range(0, max_iter): residual = residual_ee_func(atomlist, phi, potential_ee, E) def source(x, y, z): return -residual(x, y, z) # orbital correction delta_phi = inhomogeneous_schroedinger(atomlist, potential, source, E) a, b = variational_mixture_continuum(atomlist, phi, delta_phi, potential_ee, E) # The error is estimated as the norm^2 of the orbital correction: # error = <delta_phi|delta_phi> # For large radii, we cannot expect the solution to be correct since # the density of points is far too low. Therefore we exclude all points # outside the radius `rlarge` from the integration. rlarge = 10.0 def error_density(x, y, z): err = abs(delta_phi(x, y, z))**2 r = np.sqrt(x**2 + y**2 + z**2) err[rlarge < r] = 0.0 return err error = integral(atomlist, error_density) print(" iteration i= %d |dphi|^2= %e b^2= %e (threshold= %e )" % (i + 1, error, b**2, thresh)) # The solution is converged, when the weight of the # correction term b**2 is small enough. if b**2 < thresh: print("CONVERGED") break # next approximation for phi phi_next = add_two_functions(atomlist, phi, delta_phi, a, b) ### DEBUG import matplotlib.pyplot as plt plt.clf() # plot cuts along z-axis r = np.linspace(-40.0, 40.0, 10000) x = 0.0 * r y = 0.0 * r z = r plt.title("Iteration i=%d" % (i + 1)) plt.xlabel("z / bohr") plt.ylim((-0.6, +0.6)) plt.plot(r, phi(x, y, z), label=r"$\phi$") plt.plot(r, delta_phi(x, y, z), ls="--", label=r"$\Delta \phi$") plt.plot(r, phi_next(x, y, z), label=r"$a \phi + b \Delta \phi$") plt.plot(r, residual(x, y, z), ls="-.", label="residual $(H-E)\phi$") plt.legend() plt.savefig("/tmp/iteration_%3.3d.png" % (i + 1)) # plt.show() ### # prepare for next iteration phi = phi_next else: msg = "Orbital corrections did not converge in '%s' iterations!" % max_iter print("WARNING: %s" % msg) #raise RuntimeError(msg) return phi
def test_lcao_continuum(): import matplotlib.pyplot as plt # bond length in bohr dist = 2.0 # positions of protons posH1 = (0.0, 0.0, -dist / 2.0) posH2 = (0.0, 0.0, +dist / 2.0) atomlist = [(1, posH1), (1, posH2)] # Set resolution of multicenter grid settings.radial_grid_factor = 20 settings.lebedev_order = 23 # energy of continuum orbital E = 1.0 # same functional as used in the calculation of pseudo orbitals xc = XCFunctionals.libXCFunctional(Parameters.pseudo_orbital_x, Parameters.pseudo_orbital_c) dft = BasissetFreeDFT(atomlist, xc) print("initial orbital guess from DFTB calculation") orbitals = dft.getOrbitalGuess() norb = len(orbitals) # all orbitals are doubly occupied nelec = 2 * norb bound_orbitals = dft.getOrbitalGuess() # effective potential rho = density_func(bound_orbitals) veff = effective_potential_func(atomlist, rho, xc, nelec=nelec) ps = AtomicPotentialSet(atomlist) r = np.linspace(-15.0, 15.0, 10000) x = 0.0 * r y = 0.0 * r z = r for lmax in [0, 1, 2, 3]: bs = AtomicScatteringBasisSet(atomlist, E, lmax=lmax) #test_AO_basis(atomlist, bs, ps, E) R = residual2_matrix(atomlist, veff, ps, bs) S = continuum_overlap(bs.bfs, E) print("continuum overlap") print(S) print("residual^2 matrix") print(R) eigvals, eigvecs = sla.eigh(R, S) print(eigvals) print("eigenvector belonging to lowest eigenvalue") print(eigvecs[:, 0]) # LCAO continuum orbitals continuum_orbitals = orbital_transformation(atomlist, bs.bfs, eigvecs) # improve continuum orbital by adding a correction term # # phi = phi0 + dphi # # The orbital correction dphi is the solution of the inhomogeneous # Schroedinger equation # # (H-E)dphi = -(H-E)phi0 # print("orbital correction...") phi0 = continuum_orbitals[0] phi = improve_continuum_orbital(atomlist, phi0, veff, E) exit(-1) residual_0 = residual_func(atomlist, phi0, veff, E) def source(x, y, z): return -residual_0(x, y, z) delta_phi = inhomogeneous_schroedinger(atomlist, veff, source, E) residual_d = residual_func(atomlist, delta_phi, veff, E) a, b = variational_mixture_continuum(atomlist, phi0, delta_phi, veff, E) phi = add_two_functions(atomlist, phi0, delta_phi, a, b) residual = residual_func(atomlist, phi, veff, E) plt.plot(r, 1.0 / np.sqrt(2.0) * bs.bfs[0](x, y, z), label=r"AO") plt.plot(r, phi0(x, y, z), label=r"$\phi_0$") plt.plot(r, delta_phi(x, y, z), label=r"$\Delta \phi$") plt.plot(r, phi(x, y, z), label=r"$\phi_0 + \Delta \phi$") plt.legend() plt.show() """ dphi = delta_phi(x,y,z) imin = np.argmin(abs(r-1.0)) dphi[abs(r) < 1.0] = dphi[imin] - (dphi[abs(r) < 1.0] - dphi[imin]) plt.plot(r, dphi, label=r"$\Delta \phi$") """ plt.plot(r, residual_0(x, y, z), label=r"$(H-E) \phi_0$") plt.plot(r, residual_d(x, y, z), label=r"$(H-E)\Delta \phi$") plt.plot(r, residual(x, y, z), label=r"$(H-E)(a \phi_0 + b \Delta \phi)$") plt.plot(r, a * residual_0(x, y, z) + b * residual_d(x, y, z), ls="-.", label=r"$(H-E)(a \phi_0 + b \Delta \phi)$ (separate)") plt.legend() plt.show() averaged_angular_distribution(atomlist, bound_orbitals, continuum_orbitals, E) # save continuum MOs to cubefiles for i, phi in enumerate(continuum_orbitals): def func(grid, dV): x, y, z = grid return phi(x, y, z) Cube.function_to_cubefile( atomlist, func, filename="/tmp/cmo_lmax_%2.2d_orb%4.4d.cube" % (lmax, i), ppb=5.0) # for i, phi in enumerate(continuum_orbitals): residual = residual_func(atomlist, phi, veff, E) delta_e = energy_correction(atomlist, residual, phi, method="Becke") print(" orbital %d energy <%d|H-E|%d> = %e" % (i, i, i, delta_e)) l, = plt.plot(r, phi(x, y, z), label=r"$\phi_{%d}$ ($l_{max}$ = %d)" % (i, lmax)) plt.plot(r, residual(x, y, z), ls="-.", label=r"$(H-E)\phi_{%d}$" % i, color=l.get_color()) plt.legend() plt.show()
def lithium_cation_continuum(l, m, k): """ compute continuum orbital in the electrostatic potential of the Li^+ core Parameters ---------- l,m : angular quantum numbers of asymptotic solution e.g. l=0,m=0 s-orbital l=1,m=+1 px-orbital k : length of wavevector in a.u., the energy of the continuum orbital is E=1/2 k^2 """ # Li^+ atom atomlist = [(3, (0.0, 0.0, 0.0))] charge = +1 # choose resolution of multicenter grids for bound orbitals settings.radial_grid_factor = 20 # controls size of radial grid settings.lebedev_order = 25 # controls size of angular grid # 1s core orbitals for Li+^ atom RDFT = BasissetFreeDFT(atomlist, None, charge=charge) # bound_orbitals = RDFT.getOrbitalGuess() Etot, bound_orbitals, orbital_energies = RDFT.solveKohnSham() # choose resolution of multicenter grids for continuum orbitals settings.radial_grid_factor = 120 # controls size of radial grid settings.lebedev_order = 41 # controls size of angular grid # show number of radial and angular points in multicenter grid print_grid_summary(atomlist, settings.lebedev_order, settings.radial_grid_factor) print "electron density..." # electron density of two electrons in the 1s core orbital rho = density_func(bound_orbitals) print "effective potential..." # potential energy for Li nucleus and 2 core electrons potential = effective_potential_func(atomlist, rho, None, nelec=2) def v0(x, y, z): r = np.sqrt(x * x + y * y + z * z) return -1.0 / r def v1(x, y, z): return potential(x, y, z) - v0(x, y, z) # The continuum orbital is specified by its energy and asymptotic # angular momentum (E,l,m) # energy of continuum orbital E = 0.5 * k**2 # angular quantum numbers of asymptotic solution assert abs(m) <= l print " " print "Asymptotic continuum wavefunction" print "=================================" print " energy E= %e Hartree ( %e eV )" % ( E, E * AtomicData.hartree_to_eV) print " wavevector k= %e a.u." % k print " angular moment l= %d m= %+d" % (l, m) print " " # asymptotically correct solution for V0 = -1/r (hydrogen) Cf = regular_coulomb_func(E, charge, l, m, 0.0) phi0 = Cf # right-hand side of inhomogeneous Schroedinger equation def source(x, y, z): return -v1(x, y, z) * phi0(x, y, z) # # solve (H0 + V1 - E) dphi = - V1 phi0 # for orbital correction dphi atomic_numbers, atomic_coordinates = atomlist2arrays(atomlist) print "Schroedinger equation..." dphi = multicenter_inhomogeneous_schroedinger( potential, source, E, atomic_coordinates, atomic_numbers, radial_grid_factor=settings.radial_grid_factor, lebedev_order=settings.lebedev_order) # Combine asymptotically correct solution with correction # phi = phi0 + dphi phi = add_two_functions(atomlist, phi0, dphi, 1.0, 1.0) # residual for phi0 R0 = (H-E)phi0 residual0 = residual_func(atomlist, phi0, potential, E) # residual for final solution R = (H-E)phi residual = residual_func(atomlist, phi, potential, E) # spherical average of residual function residual_avg = spherical_average_residual_func(atomlist[0], residual) # The phase shift is determined by matching the radial wavefunction # to a shifted and scaled Coulomb function at a number of radial # sampling points drawn from the interval [rmin, rmax]. # On the one hand rmin < rmax should be chosen large enough, # so that the continuum orbital approaches its asymptotic form, # on the other hand rmax should be small enough that the accuracy # of the solution due to the sparse r-grid is still high enough. # A compromise has to be struck depending on the size of the radial grid. # The matching points are spread over several periods, # but not more than 30 bohr. wavelength = 2.0 * np.pi / k print "wavelength = %e" % wavelength rmin = 70.0 rmax = rmin + max(10 * wavelength, 30.0) Npts = 100 # determine phase shift and scaling factor by a least square # fit the the regular Coulomb function scale, delta = phaseshift_lstsq(atomlist, phi, E, charge, l, m, rmin, rmax, Npts) print "scale factor (relative to Coulomb wave) = %s" % scale print "phase shift (relative to Coulomb wave) = %e " % delta # normalize wavefunction, so that 1/scale phi(x,y,z) approaches # asymptotically a phase-shifted Coulomb wave phi_norm = multicenter_operation( [phi], lambda fs: fs[0] / scale, atomic_coordinates, atomic_numbers, radial_grid_factor=settings.radial_grid_factor, lebedev_order=settings.lebedev_order) # The continuum orbital should be orthogonal to the bound # orbitals belonging to the same Hamiltonian. I think this # should come out correctly by default. print " " print " Overlaps between bound orbitals and continuum orbital" print " =====================================================" for ib, bound_orbital in enumerate(bound_orbitals): olap_bc = overlap(atomlist, bound_orbital, phi_norm) print " <bound %d| continuum> = %e" % (ib + 1, olap_bc) print "" # shifted regular coulomb function Cf_shift = regular_coulomb_func(E, charge, l, m, delta) # save radial wavefunctions and spherically averaged residual # radial part of Coulomb wave without phase shift phi0_rad = radial_wave_func(atomlist, phi0, l, m) # radial part of shifted Coulomb wave Cf_shift_rad = radial_wave_func(atomlist, Cf_shift, l, m) # radial part of scattering solution phi_norm_rad = radial_wave_func(atomlist, phi_norm, l, m) print "" print "# RADIAL_WAVEFUNCTIONS" print "# Asymptotic wavefunction:" print "# charge Z= %+d" % charge print "# energy E= %e k= %e" % (E, k) print "# angular momentum l= %d m= %+d" % (l, m) print "# phase shift delta= %e rad" % delta print "# " print "# R/bohr Coulomb Coulomb radial wavefunction spherical avg. residual" print "# shifted R_{l,m}(r) <|(H-E)phi|^2>" import sys # write table to console r = np.linspace(1.0e-3, 100, 1000) data = np.vstack((r, phi0_rad(r), Cf_shift_rad(r), phi_rad(r), residual_avg(r))).transpose() np.savetxt(sys.stdout, data, fmt=" %+e ") print "# END" print ""
def test_hmi_continuum(): """ check that the continuum wavefunction of H2+ really are solutions of Schroedinger's equation, i.e. have (H-E)\phi = 0 everywhere starting from an LCAO guess for the continuum orbital, try to find the exact solution by adding orbital corrections iteratively """ # First we compute the exact wavefunction of the hydrogen molecular ion. from DFTB.Scattering import HMI # The bond length and charges cannot be changed, since the # separation constants were calculated only for the H2+ ion at R=2! R = 2.0 Za = 1.0 Zb = 1.0 # energy of continuum orbital E = 0.5 ## sigma (m=0) orbital m = 0 n = 0 trig = 'cos' # separation constant Lsep = HMI.SeparationConstants(R, Za, Zb) Lsep.load_separation_constants() Lfunc = Lsep.L_interpolated(m, n) c2 = 0.5 * E * R**2 mL, nL, L = Lfunc(c2) parity = (-1)**(mL + nL) phi_exact = HMI.create_wavefunction(mL, L, R * (Za + Zb), 0.0, R, c2, parity, trig) # Old implementation of H2+ wavefunctions, the wavefunction # looks indistinguishable from the exact wavefunction, but the # non-zero residue shows that is contains large errors. from DFTB.Scattering.hydrogen_molecular_ion import DimerWavefunctions wfn = DimerWavefunctions(R, Za, Zb, plot=False) delta, (Rfunc, Sfunc, Pfunc), wavefunction_exact = wfn.getContinuumOrbital( m, n, trig, E) def phi_exact_DW(x, y, z): return wavefunction_exact((x, y, z), None) # Set resolution of multicenter grid settings.radial_grid_factor = 10 settings.lebedev_order = 41 # Next we compute the wavefunction using the basis set free method atomlist = [(int(Za), (0.0, 0.0, -R / 2.0)), (int(Zb), (0.0, 0.0, +R / 2.0))] # no other electrons, only nuclear potential def potential(x, y, z): nuc = 0.0 * x for Zi, posi in atomlist: ri = np.sqrt((x - posi[0])**2 + (y - posi[1])**2 + (z - posi[2])**2) nuc += -Zi / ri return nuc # electron-electron interaction def potential_ee(x, y, z): return 0.0 * x # Set resolution of multicenter grid settings.radial_grid_factor = 10 settings.lebedev_order = 41 # residual of exact wavefunction (should be zero) residual_exact = residual_func(atomlist, phi_exact, potential, E) residual_ee_exact = residual_ee_func(atomlist, phi_exact, potential_ee, E) residual_exact_DW = residual_func(atomlist, phi_exact_DW, potential, E) # Laplacian laplacian_exact = laplacian_func(atomlist, phi_exact) import matplotlib.pyplot as plt plt.clf() r = np.linspace(-15.0, 15.0, 5000) x = 0 * r y = 0 * r z = r # plot exact wavefunction plt.plot(r, phi_exact(x, y, z), label="$\phi$ exact") # phi_exact_xyz = phi_exact(x, y, z) phi_exact_DW_xyz = phi_exact_DW(x, y, z) scale = phi_exact_xyz.max() / phi_exact_DW_xyz.max() plt.plot(r, scale * phi_exact_DW_xyz, label="$\phi$ exact (DimerWavefunction)") # and residual plt.plot(r, residual_exact(x, y, z), label=r"$(H-E)\phi$ (exact, old)") plt.plot(r, residual_exact_DW(x, y, z), ls="-.", label=r"$(H-E)\phi$ (exact, DimerWavefunction, old)") plt.plot(r, residual_ee_exact(x, y, z), ls="--", label=r"$(H-E)\phi$ (exact, new)") # kinetic energy plt.plot(r, -0.5 * laplacian_exact(x, y, z), ls="--", label=r"$-\frac{1}{2}\nabla^2 \phi$") # potential energy plt.plot(r, (potential(x, y, z) - E) * phi_exact(x, y, z), ls="--", label=r"$(V-E)\phi$") ## The initial guess for the \sigma continuum orbital ## is a regular Coulomb function centered on the midpoint ## between the two protons. #phi0 = regular_coulomb_func(E, +2, 0, 0, 0.0, center=(0.0, 0.0, 0.0)) ## The initial guess for the \sigma continuum orbital is ## the sum of two hydrogen s continuum orbitals bs = AtomicScatteringBasisSet(atomlist, E, lmax=0) phi0 = add_two_functions(atomlist, bs.bfs[0], bs.bfs[1], 1.0 / np.sqrt(2.0), 1.0 / np.sqrt(2.0)) ## start with exact wavefunction #phi0 = phi_exact plt.plot(r, phi0(x, y, z), ls="-.", label="guess $\phi_0$") plt.legend() plt.show() #phi = improve_continuum_orbital(atomlist, phi0, potential, E, thresh=1.0e-6) # place dummy atom at the center atomlist_grid = atomlist + [(1, (0.0, 0.0, 0.0))] phi = improve_continuum_orbital(atomlist_grid, phi0, potential, E, thresh=1.0e-6) import matplotlib.pyplot as plt plt.clf() r = np.linspace(-15.0, 15.0, 5000) x = 0 * r y = 0 * r z = r phi_exact_xyz = phi_exact(x, y, z) phi_xyz = phi(x, y, z) # scale numerical phi such that the maxima agree scale = phi_exact_xyz.max() / phi_xyz.max() phi_xyz *= scale print("scaling factor s = %s" % scale) plt.plot(r, phi_exact_xyz, label="exact") plt.plot(r, phi_xyz, label="numerical") plt.legend() plt.show()