示例#1
0
文件: test.py 项目: chrinide/pyaim
    def build(self):

        t0 = (time.clock(), time.time())
        lib.logger.TIMER_LEVEL = 3

        cell = libpbc.chkfile.load_cell(self.chkfile)
        cell.ecp = None
        self.cell = cell
        self.a = self.cell.lattice_vectors()
        self.b = self.cell.reciprocal_vectors()
        self.vol = self.cell.vol
        self.nelectron = self.cell.nelectron 
        self.charge = self.cell.charge    
        self.spin = self.cell.spin      
        self.natm = self.cell.natm		
        self.kpts = lib.chkfile.load(self.chkfile, 'kcell/kpts')
        self.nkpts = len(self.kpts)
        self.ls = cell.get_lattice_Ls(dimension=3)
        self.ls = self.ls[numpy.argsort(lib.norm(self.ls, axis=1))]
        self.atm = numpy.asarray(cell._atm, dtype=numpy.int32, order='C')
        self.bas = numpy.asarray(cell._bas, dtype=numpy.int32, order='C')
        self.env = numpy.asarray(cell._env, dtype=numpy.double, order='C')
        self.nbas = self.bas.shape[0]
        self.ao_loc = cell.ao_loc_nr()
        self.shls_slice = (0, self.nbas)
        sh0, sh1 = self.shls_slice
        self.nao = self.ao_loc[sh1] - self.ao_loc[sh0]
        self.non0tab = numpy.empty((1,self.nbas), dtype=numpy.int8)
        # non0tab stores the number of images to be summed in real space.
        # Initializing it to 255 means all images are summed
        self.non0tab[:] = 0xff
        self.coords = numpy.asarray([(numpy.asarray(atom[1])).tolist() for atom in cell._atom])
        self.charges = cell.atom_charges()
        self.mo_coeff = lib.chkfile.load(self.chkfile, 'scf/mo_coeff')
        self.mo_occ = lib.chkfile.load(self.chkfile, 'scf/mo_occ')
        nprims, nmo = self.mo_coeff[0].shape 
        self.nprims = nprims
        self.nmo = nmo
        self.cart = cell.cart
        if (not self.leb):
            self.npang = self.npphi*self.nptheta

        self.rcut = _estimate_rcut(self)
        kpts = numpy.reshape(self.kpts, (-1,3))
        kpts_lst = numpy.reshape(kpts, (-1,3))
        self.explk = numpy.exp(1j * numpy.asarray(numpy.dot(self.ls, kpts_lst.T), order='C'))

        if (self.ntrial%2 == 0): self.ntrial += 1
        geofac = numpy.power(((self.rmaxsurf-0.1)/self.rprimer),(1.0/(self.ntrial-1.0)))
        self.rpru = numpy.zeros((self.ntrial))
        for i in range(self.ntrial): 
            self.rpru[i] = self.rprimer*numpy.power(geofac,(i+1)-1)
        self.rsurf = numpy.zeros((self.npang,self.ntrial), order='C')
        self.nlimsurf = numpy.zeros((self.npang), dtype=numpy.int32)

        if self.verbose >= logger.WARN:
            self.check_sanity()
        if self.verbose > logger.NOTE:
            self.dump_input()

        if (self.iqudt == 'legendre'):
            self.iqudt = 1
        
        if (self.leb):
            self.grids = grid.lebgrid(self.npang)
        else:
            self.grids = grid.anggrid(self.iqudt,self.nptheta,self.npphi)
         
        self.xyzrho = numpy.zeros((self.natm,3))
        t = time.time()
        logger.info(self,'Time finding nucleus %.3f (sec)' % (time.time()-t))

        if (self.backend == 'rkck'):
            backend = 1
        elif (self.backend == 'rkdp'):
            backend = 2
        else:
            raise NotImplementedError('Only rkck or rkdp ODE solver yet available') 
         
        ct_ = numpy.asarray(self.grids[:,0], order='C')
        st_ = numpy.asarray(self.grids[:,1], order='C')
        cp_ = numpy.asarray(self.grids[:,2], order='C')
        sp_ = numpy.asarray(self.grids[:,3], order='C')

        mo_coeff = numpy.zeros((self.nkpts,self.nprims,self.nmo), dtype=numpy.complex128)
        mo_occ = numpy.zeros((self.nkpts,self.nmo))
        for k in range(self.nkpts):
            mo_coeff[k,:,:] = self.mo_coeff[k][:,:]
            mo_occ[k,:] = self.mo_occ[k][:]
        
        t = time.time()
        feval = 'surf_driver'
        drv = getattr(libaim, feval)
        with lib.with_omp_threads(self.nthreads):
            drv(ctypes.c_int(self.inuc), 
                self.xyzrho.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_int(self.npang), 
                ct_.ctypes.data_as(ctypes.c_void_p),
                st_.ctypes.data_as(ctypes.c_void_p),
                cp_.ctypes.data_as(ctypes.c_void_p),
                sp_.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_int(self.ntrial), 
                self.rpru.ctypes.data_as(ctypes.c_void_p), 
                ctypes.c_double(self.epsiscp), 
                ctypes.c_double(self.epsroot), 
                ctypes.c_double(self.rmaxsurf), 
                ctypes.c_int(backend),
                ctypes.c_double(self.epsilon), 
                ctypes.c_double(self.step), 
                ctypes.c_int(self.mstep),
                ctypes.c_int(self.natm), 
                self.coords.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_int(self.cart),
                ctypes.c_int(self.nmo),  
                ctypes.c_int(self.nprims), 
                self.atm.ctypes.data_as(ctypes.c_void_p), 
                ctypes.c_int(self.nbas), 
                self.bas.ctypes.data_as(ctypes.c_void_p), 
                self.env.ctypes.data_as(ctypes.c_void_p), 
                self.ao_loc.ctypes.data_as(ctypes.c_void_p),
                mo_coeff.ctypes.data_as(ctypes.c_void_p),
                mo_occ.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_double(self.occdrop), 
                #
                self.a.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_int(len(self.ls)), 
                self.ls.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_int(self.nkpts), 
                self.explk.ctypes.data_as(ctypes.c_void_p),
                self.rcut.ctypes.data_as(ctypes.c_void_p),
                self.non0tab.ctypes.data_as(ctypes.c_void_p),
                #
                self.nlimsurf.ctypes.data_as(ctypes.c_void_p),
                self.rsurf.ctypes.data_as(ctypes.c_void_p))
        for i in range(self.nkpts):
            print k,self.mo_occ[k][:]
        logger.info(self,'Time finding surface %.3f (sec)' % (time.time()-t))
        r = numpy.array([0.00000, 0.00000, 0.00000]) 
        r = numpy.reshape(r, (-1,3))
        ao = dft.numint.eval_ao_kpts(self.cell, r, kpts=self.kpts, deriv=1)
        print rhograd(self,[0,0,0]) 
             
        logger.info(self,'Surface of atom %d saved',self.inuc)
        logger.timer(self,'BaderSurf build', *t0)

        return self
示例#2
0
    def build(self):

        t0 = (time.clock(), time.time())
        lib.logger.TIMER_LEVEL = 3

        mol = lib.chkfile.load_mol(self.chkfile)
        self.nelectron = mol.nelectron
        self.charge = mol.charge
        self.spin = mol.spin
        self.natm = mol.natm
        self.atm = numpy.asarray(mol._atm, dtype=numpy.int32, order='C')
        self.bas = numpy.asarray(mol._bas, dtype=numpy.int32, order='C')
        self.nbas = self.bas.shape[0]
        self.env = numpy.asarray(mol._env, dtype=numpy.double, order='C')
        self.ao_loc = mol.ao_loc_nr()
        self.shls_slice = (0, self.nbas)
        sh0, sh1 = self.shls_slice
        self.nao = self.ao_loc[sh1] - self.ao_loc[sh0]
        self.non0tab = numpy.ones((1, self.nbas), dtype=numpy.int8)
        self.coords = numpy.asarray([(numpy.asarray(atom[1])).tolist()
                                     for atom in mol._atom])
        self.charges = mol.atom_charges()
        #if (self.cas):
        #    self.mo_coeff = lib.chkfile.load(self.chkfile, 'mcscf/mo_coeff')
        #else:
        #    self.mo_coeff = lib.chkfile.load(self.chkfile, 'scf/mo_coeff')
        self.mo_coeff = lib.chkfile.load(self.chkfile, 'scf/mo_coeff')
        self.mo_occ = lib.chkfile.load(self.chkfile, 'scf/mo_occ')
        nprims, nmo = self.mo_coeff.shape
        self.nprims = nprims
        self.nmo = nmo
        self.cart = mol.cart
        if (not self.leb):
            self.npang = self.npphi * self.nptheta

        if (self.corr):
            self.rdm1 = lib.chkfile.load(self.chkfile, 'rdm/rdm1')
            nmo = self.rdm1.shape[0]
            natocc, natorb = numpy.linalg.eigh(self.rdm1)
            if (self.cas):
                self.mo_coeff = lib.chkfile.load(self.chkfile,
                                                 'mcscf/mo_coeff')
            natorb = numpy.dot(self.mo_coeff, natorb)
            self.mo_coeff = natorb
            self.mo_occ = natocc

        nocc = self.mo_occ[abs(self.mo_occ) > self.occdrop]
        nocc = len(nocc)
        self.nocc = nocc

        if (self.ntrial % 2 == 0): self.ntrial += 1
        geofac = numpy.power(((self.rmaxsurf - 0.1) / self.rprimer),
                             (1.0 / (self.ntrial - 1.0)))
        self.rpru = numpy.zeros((self.ntrial))
        for i in range(self.ntrial):
            self.rpru[i] = self.rprimer * numpy.power(geofac, (i + 1) - 1)
        self.rsurf = numpy.zeros((self.npang, self.ntrial), order='C')
        self.nlimsurf = numpy.zeros((self.npang), dtype=numpy.int32)

        if self.verbose >= logger.WARN:
            self.check_sanity()
        if self.verbose > logger.NOTE:
            self.dump_input()

        if (self.iqudt == 'legendre'):
            self.iqudt = 1

        if (self.leb):
            self.grids = grid.lebgrid(self.npang)
        else:
            self.grids = grid.anggrid(self.iqudt, self.nptheta, self.npphi)

        self.xyzrho = numpy.zeros((self.natm, 3))
        t = time.time()
        for i in range(self.natm):
            self.xyzrho[i], gradmod = gradrho(self, self.coords[i], self.step)
            if (gradmod > 1e-4):
                if (self.charges[i] > 2.0):
                    logger.info(self, 'Good rho position %.6f %.6f %.6f',
                                *self.xyzrho[i])
                else:
                    raise RuntimeError('Failed finding nucleus:',
                                       *self.xyzrho[i])
            else:
                logger.info(self, 'Check rho position %.6f %.6f %.6f',
                            *self.xyzrho[i])
                logger.info(self, 'Setting xyzrho for atom to imput coords')
                self.xyzrho[i] = self.coords[i]
        self.xnuc = numpy.asarray(self.xyzrho[self.inuc])
        logger.info(self,
                    'Time finding nucleus %.3f (sec)' % (time.time() - t))

        if (self.backend == 'rkck'):
            backend = 1
        elif (self.backend == 'rkdp'):
            backend = 2
        else:
            raise NotImplementedError(
                'Only rkck or rkdp ODE solver yet available')

        ct_ = numpy.asarray(self.grids[:, 0], order='C')
        st_ = numpy.asarray(self.grids[:, 1], order='C')
        cp_ = numpy.asarray(self.grids[:, 2], order='C')
        sp_ = numpy.asarray(self.grids[:, 3], order='C')

        t = time.time()
        feval = 'surf_driver'
        drv = getattr(libaim, feval)
        with lib.with_omp_threads(self.nthreads):
            drv(ctypes.c_int(self.inuc),
                self.xyzrho.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_int(self.npang), ct_.ctypes.data_as(ctypes.c_void_p),
                st_.ctypes.data_as(ctypes.c_void_p),
                cp_.ctypes.data_as(ctypes.c_void_p),
                sp_.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(self.ntrial),
                self.rpru.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_double(self.epsiscp), ctypes.c_double(self.epsroot),
                ctypes.c_double(self.rmaxsurf), ctypes.c_int(backend),
                ctypes.c_double(self.epsilon), ctypes.c_double(self.step),
                ctypes.c_int(self.mstep), ctypes.c_int(self.natm),
                self.coords.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_int(self.cart), ctypes.c_int(self.nmo),
                ctypes.c_int(self.nprims),
                self.atm.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_int(self.nbas),
                self.bas.ctypes.data_as(ctypes.c_void_p),
                self.env.ctypes.data_as(ctypes.c_void_p),
                self.ao_loc.ctypes.data_as(ctypes.c_void_p),
                self.mo_coeff.ctypes.data_as(ctypes.c_void_p),
                self.mo_occ.ctypes.data_as(ctypes.c_void_p),
                ctypes.c_double(self.occdrop),
                self.nlimsurf.ctypes.data_as(ctypes.c_void_p),
                self.rsurf.ctypes.data_as(ctypes.c_void_p))
        logger.info(self,
                    'Time finding surface %.3f (sec)' % (time.time() - t))

        self.rmin = 1000.0
        self.rmax = 0.0
        for i in range(self.npang):
            nsurf = int(self.nlimsurf[i])
            self.rmin = numpy.minimum(self.rmin, self.rsurf[i, 0])
            self.rmax = numpy.maximum(self.rmax, self.rsurf[i, nsurf - 1])
        logger.info(self, 'Rmin for surface %.6f', self.rmin)
        logger.info(self, 'Rmax for surface %.6f', self.rmax)

        logger.info(self, 'Write HDF5 surface file')
        atom_dic = {
            'inuc': self.inuc,
            'xnuc': self.xnuc,
            'xyzrho': self.xyzrho,
            'coords': self.grids,
            'npang': self.npang,
            'ntrial': self.ntrial,
            'rmin': self.rmin,
            'rmax': self.rmax,
            'nlimsurf': self.nlimsurf,
            'rsurf': self.rsurf
        }
        lib.chkfile.save(self.surfile, 'atom' + str(self.inuc), atom_dic)
        logger.info(self, 'Surface of atom %d saved', self.inuc)
        logger.timer(self, 'BaderSurf build', *t0)

        return self