Exemplo n.º 1
0
 def solve(self,H,**opts):
     from PyQuante.LA2 import mkdens_spinavg,simx,geigh
     from PyQuante.NumWrap import matrixmultiply,eigh
     if self.first_iteration:
         self.first_iteration = False
         self.orbe,self.orbs = geigh(H,self.S)
     else:
         Ht = simx(H,self.orbs)
         if self.pass_nroots:
             self.orbe,orbs = self.solver(Ht,self.nroots)
         else:
             self.orbe,orbs = self.solver(Ht)
         self.orbs = matrixmultiply(self.orbs,orbs)
     self.D = mkdens_spinavg(self.orbs,self.nclosed,self.nopen)
     self.entropy = 0
     return self.D,self.entropy
Exemplo n.º 2
0
 def solve(self, H, **kwargs):
     from PyQuante.LA2 import mkdens_spinavg, simx, geigh
     from PyQuante.NumWrap import matrixmultiply, eigh
     if self.first_iteration:
         self.first_iteration = False
         self.orbe, self.orbs = geigh(H, self.S)
     else:
         Ht = simx(H, self.orbs)
         if self.pass_nroots:
             self.orbe, orbs = self.solver(Ht, self.nroots)
         else:
             self.orbe, orbs = self.solver(Ht)
         self.orbs = matrixmultiply(self.orbs, orbs)
     self.D = mkdens_spinavg(self.orbs, self.nclosed, self.nopen)
     self.entropy = 0
     return self.D, self.entropy
Exemplo n.º 3
0
def test_grad(atoms,**opts):
    basis = opts.get('basis',None)
    verbose = opts.get('verbose',False)

    bfs = getbasis(atoms,basis)
    S,h,Ints = getints(bfs,atoms)

    nel = atoms.get_nel()
    enuke = atoms.get_enuke()

    energy, orbe, orbs = dft(atoms,return_flag=1)

    nclosed,nopen = atoms.get_closedopen()
    D = mkdens_spinavg(orbs,nclosed,nopen)

    # Now set up a minigrid on which to evaluate the density and gradients:
    npts = opts.get('npts',1)
    d = opts.get('d',1e-4)
    # generate any number of random points, and compute
    # analytical and numeric gradients:
    print "Computing Grad Rho for Molecule %s" % atoms.name
    maxerr = -1
    for i in range(npts):
        x,y,z = rand_xyz()
        rho = get_rho(x,y,z,D,bfs)
        rho_px = get_rho(x+d,y,z,D,bfs)
        rho_mx = get_rho(x-d,y,z,D,bfs)
        rho_py = get_rho(x,y+d,z,D,bfs)
        rho_my = get_rho(x,y-d,z,D,bfs)
        rho_pz = get_rho(x,y,z+d,D,bfs)
        rho_mz = get_rho(x,y,z-d,D,bfs)
        gx,gy,gz = get_grad_rho(x,y,z,D,bfs)
        gx_num = (rho_px-rho_mx)/2/d
        gy_num = (rho_py-rho_my)/2/d
        gz_num = (rho_pz-rho_mz)/2/d

        dx,dy,dz = gx-gx_num,gy-gy_num,gz-gz_num
        error = sqrt(dx*dx+dy*dy+dz*dz)
        maxerr = max(error,maxerr)
        print " Point  %10.6f %10.6f %10.6f %10.6f" % (x,y,z,error)
        print "  Numerical %10.6f %10.6f %10.6f" % (gx_num,gy_num,gz_num)
        print "  Analytic  %10.6f %10.6f %10.6f" % (gx,gy,gz)
    print "The maximum error in the gradient calculation is ",maxerr
    return
Exemplo n.º 4
0
 def solve(self,H,**opts):
     from PyQuante.LA2 import geigh,mkdens_spinavg
     self.orbe,self.orbs = geigh(H,self.S)
     self.D = mkdens_spinavg(self.orbs,self.nclosed,self.nopen)
     self.entropy = 0
     return self.D,self.entropy
Exemplo n.º 5
0
 def solve(self, H, **kwargs):
     from PyQuante.LA2 import geigh, mkdens_spinavg
     self.orbe, self.orbs = geigh(H, self.S)
     self.D = mkdens_spinavg(self.orbs, self.nclosed, self.nopen)
     self.entropy = 0
     return self.D, self.entropy
Exemplo n.º 6
0
def rhf(mol,
        bfs,
        S,
        Hcore,
        Ints,
        mu=0,
        MaxIter=100,
        eps_SCF=1E-4,
        _diis_=True):

    ##########################################################
    ##					Get the system information
    ##########################################################

    # size
    nbfs = len(bfs)

    # get the nuclear energy
    enuke = mol.get_enuke()

    # determine the number of electrons
    # and occupation numbers
    nelec = mol.get_nel()
    nclosed, nopen = mol.get_closedopen()
    nocc = nclosed

    # orthogonalization matrix
    X = SymOrthCutoff(S)

    if _ee_inter_ == 0:
        print '\t\t ==================================================='
        print '\t\t == Electrons-Electrons interactions desactivated =='
        print '\t\t ==================================================='

    if nopen != 0:
        print '\t\t ================================================================='
        print '\t\t Warning : using restricted HF with open shell is not recommended'
        print "\t\t Use only if you know what you're doing"
        print '\t\t ================================================================='

    # get a first DM
    #D = np.zeros((nbfs,nbfs))
    L, C = scla.eigh(Hcore, b=S)
    D = mkdens(C, 0, nocc)

    # initialize the old energy
    eold = 0.

    # initialize the DIIS
    if _diis_:
        avg = DIIS(S)

    #print '\t SCF Calculations'
    for iiter in range(MaxIter):

        # form the G matrix from the
        # density matrix and  the 2electron integrals
        G = get2JmK(Ints, D)

        # form the Fock matrix
        F = Hcore + _ee_inter_ * G + mu

        # if DIIS
        if _diis_:
            F = avg.getF(F, D)

        # orthogonalize the Fock matrix
        Fp = np.dot(X.T, np.dot(F, X))

        # diagonalize the Fock matrix
        Lp, Cp = scla.eigh(Fp)

        # form the density matrix in the OB
        if nopen == 0:
            Dp = mkdens(Cp, 0, nocc)
        else:
            Dp = mkdens_spinavg(Cp, nclosed, nopen)

        # pass the eigenvector back to the AO
        C = np.dot(X, Cp)

        # form the density matrix in the AO
        if nopen == 0:
            D = mkdens(C, 0, nocc)
        #else:
        #	D = mkdens_spinavg(C,nclosed,nopen)

        # compute the total energy
        e = np.sum(D * (Hcore + F)) + enuke

        print "\t\t Iteration: %d    Energy: %f    EnergyVar: %f" % (
            iiter, e.real, np.abs((e - eold).real))

        # stop if done
        if (np.abs(e - eold) < eps_SCF):
            break
        else:
            eold = e

    if iiter < MaxIter:
        print(
            "\t\t SCF for HF has converged in %d iterations, Final energy %1.3f Ha\n"
            % (iiter, e.real))

    else:
        print("\t\t SCF for HF has failed to converge after %d iterations")

    # compute the density matrix in the
    # eigenbasis of F
    P = np.dot(Cp.T, np.dot(Dp, Cp))
    #print D

    return Lp, C, Cp, F, Fp, D, Dp, P, X