Beispiel #1
0
  def test_dens_elec(self):
    """ Compute density in coordinate space with system_vars_c, integrate and compare with number of electrons """
    from pyscf.nao import system_vars_c
    from pyscf.nao.m_comp_dm import comp_dm
    from timeit import default_timer as timer
    
    sv = system_vars_c().init_siesta_xml(label='water', cd=os.path.dirname(os.path.abspath(__file__)))
    dm = comp_dm(sv.wfsx.x, sv.get_occupations())
    #print(sv.get_occupations())
    #print(dir(sv.ao_log) )
    #print((sv.ao_log.psi_log[0][0]**2 *sv.ao_log.rr**3 * np.log(sv.ao_log.rr[1]/sv.ao_log.rr[0])).sum())
    #print((sv.ao_log.psi_log[0][1]**2 *sv.ao_log.rr**3 * np.log(sv.ao_log.rr[1]/sv.ao_log.rr[0])).sum())
    #print((sv.ao_log.psi_log[0][2]**2 *sv.ao_log.rr**3 * np.log(sv.ao_log.rr[1]/sv.ao_log.rr[0])).sum())
    #print((sv.ao_log.psi_log[0][3]**2 *sv.ao_log.rr**3 * np.log(sv.ao_log.rr[1]/sv.ao_log.rr[0])).sum())
    #print((sv.ao_log.psi_log[0][4]**2 *sv.ao_log.rr**3 * np.log(sv.ao_log.rr[1]/sv.ao_log.rr[0])).sum())
    #
    #print((sv.ao_log.psi_log[1][0]**2 *sv.ao_log.rr**3 * np.log(sv.ao_log.rr[1]/sv.ao_log.rr[0])).sum())
    #print((sv.ao_log.psi_log[1][1]**2 *sv.ao_log.rr**3 * np.log(sv.ao_log.rr[1]/sv.ao_log.rr[0])).sum())
    #print((sv.ao_log.psi_log[1][2]**2 *sv.ao_log.rr**3 * np.log(sv.ao_log.rr[1]/sv.ao_log.rr[0])).sum())
    
    grid = sv.build_3dgrid_pp(level=5)
    
    #t1 = timer()
    #dens1 = sv.dens_elec_vec(grid.coords, dm)
    #t2 = timer(); print(t2-t1); t1 = timer()
    
    t1 = timer()
    dens = sv.dens_elec(grid.coords, dm)
    #t2 = timer(); print(t2-t1); t1 = timer()
    
    nelec = np.einsum("is,i", dens, grid.weights)[0]
    #t2 = timer(); print(t2-t1, nelec, sv.hsx.nelec, dens.shape); t1 = timer()

    self.assertAlmostEqual(nelec, sv.hsx.nelec, 2)
Beispiel #2
0
  def test_exc(self):
    """ Compute exchange-correlation energy """
    from timeit import default_timer as timer

    from pyscf.nao import system_vars_c
    from pyscf.nao.m_comp_dm import comp_dm
    from timeit import default_timer as timer
    
    sv = system_vars_c().init_siesta_xml(label='water', cd=os.path.dirname(os.path.abspath(__file__)))
    dm = comp_dm(sv.wfsx.x, sv.get_occupations())
    exc = sv.exc(dm, xc_code='1.0*LDA,1.0*PZ', level=4)
    self.assertAlmostEqual(exc, -4.14222392763)
Beispiel #3
0
  def test_fxc(self):
    """ Compute TDDFT interaction kernel  """
    from timeit import default_timer as timer

    from pyscf.nao import system_vars_c, prod_basis_c
    from pyscf.nao.m_comp_dm import comp_dm
    from timeit import default_timer as timer
    
    sv = system_vars_c().init_siesta_xml(label='water', cd=os.path.dirname(os.path.abspath(__file__)))
    pb = prod_basis_c().init_prod_basis_pp(sv)
    dm = comp_dm(sv.wfsx.x, sv.get_occupations())
    fxc = pb.comp_fxc_lil(dm, xc_code='1.0*LDA,1.0*PZ', level=4)
Beispiel #4
0
    def test_dft_sv(self):
        """ Try to run DFT with system_vars_c """
        from pyscf.nao import system_vars_c
        from pyscf.nao.m_comp_dm import comp_dm
        from pyscf.nao.m_fermi_dirac import fermi_dirac_occupations

        sv = system_vars_c().init_siesta_xml(label='water',
                                             cd=os.path.dirname(
                                                 os.path.abspath(__file__)))
        ksn2fd = fermi_dirac_occupations(sv.hsx.telec, sv.wfsx.ksn2e,
                                         sv.fermi_energy)
        ksn2f = (3 - sv.nspin) * ksn2fd
        dm = comp_dm(sv.wfsx.x, ksn2f)
        vxc = sv.vxc_lil(dm, 'LDA,PZ')
 def comp_dm(self):
     """ Computes the density matrix """
     from pyscf.nao.m_comp_dm import comp_dm
     return comp_dm(self.wfsx.x, self.get_occupations())
 def comp_dm(self):
   """ Computes the density matrix """
   from pyscf.nao.m_comp_dm import comp_dm
   return comp_dm(self.wfsx.x, self.get_occupations())
Beispiel #7
0
    def __init__(self,
                 sv,
                 pb,
                 tddft_iter_tol=1e-2,
                 tddft_iter_broadening=0.00367493,
                 nfermi_tol=1e-5,
                 telec=None,
                 nelec=None,
                 fermi_energy=None,
                 xc_code='LDA,PZ',
                 GPU=False,
                 precision="single",
                 **kvargs):
        """ Iterative TDDFT a la PK, DF, OC JCTC """
        from pyscf.nao.m_fermi_dirac import fermi_dirac_occupations
        from pyscf.nao.m_comp_dm import comp_dm
        import sys

        assert tddft_iter_tol > 1e-6
        assert type(tddft_iter_broadening) == float
        assert sv.wfsx.x.shape[-1] == 1  # i.e. real eigenvectors we accept here

        if precision == "single":
            self.dtype = np.float32
            self.dtypeComplex = np.complex64
        elif precision == "double":
            self.dtype = np.float64
            self.dtypeComplex = np.complex128
        else:
            raise ValueError("precision can be only single or double")

        self.rf0_ncalls = 0
        self.l0_ncalls = 0
        self.matvec_ncalls = 0
        self.tddft_iter_tol = tddft_iter_tol
        self.eps = tddft_iter_broadening
        self.sv, self.pb, self.norbs, self.nspin = sv, pb, sv.norbs, sv.nspin
        self.v_dab = pb.get_dp_vertex_coo(dtype=self.dtype).tocsr()
        self.cc_da = pb.get_da2cc_coo(dtype=self.dtype).tocsr()
        self.moms0, self.moms1 = pb.comp_moments(dtype=self.dtype)
        self.nprod = self.moms0.size
        self.kernel, self.kernel_dim = pb.comp_coulomb_pack(dtype=self.dtype)

        if xc_code.upper() != 'RPA':
            dm = comp_dm(sv.wfsx.x, sv.get_occupations())

            pb.comp_fxc_pack(dm,
                             xc_code,
                             kernel=self.kernel,
                             dtype=self.dtype,
                             **kvargs)

        self.telec = sv.hsx.telec if telec is None else telec
        self.nelec = sv.hsx.nelec if nelec is None else nelec
        self.fermi_energy = sv.fermi_energy if fermi_energy is None else fermi_energy
        self.x = np.require(sv.wfsx.x, dtype=self.dtype, requirements='CW')
        self.ksn2e = np.require(sv.wfsx.ksn2e,
                                dtype=self.dtype,
                                requirements='CW')
        ksn2fd = fermi_dirac_occupations(self.telec, self.ksn2e,
                                         self.fermi_energy)
        self.ksn2f = (3 - self.nspin) * ksn2fd
        self.nfermi = np.argmax(ksn2fd[0, 0, :] < nfermi_tol)
        self.vstart = np.argmax(1.0 - ksn2fd[0, 0, :] > nfermi_tol)
        self.xocc = self.x[0, 0, 0:self.nfermi, :,
                           0]  # does python creates a copy at this point ?
        self.xvrt = self.x[0, 0, self.vstart:, :,
                           0]  # does python creates a copy at this point ?

        self.tddft_iter_gpu = tddft_iter_gpu_c(GPU, self.v_dab, self.ksn2f,
                                               self.ksn2e, self.norbs,
                                               self.nfermi, self.vstart)
Beispiel #8
0
    def __init__(self,
                 sv,
                 pb,
                 tddft_iter_tol=1e-2,
                 tddft_iter_broadening=0.00367493,
                 nfermi_tol=1e-5,
                 telec=None,
                 nelec=None,
                 fermi_energy=None,
                 xc_code='LDA,PZ',
                 GPU=False,
                 precision="single",
                 load_kernel=False,
                 **kvargs):
        """ Iterative TDDFT a la PK, DF, OC JCTC """
        from pyscf.nao.m_fermi_dirac import fermi_dirac_occupations
        from pyscf.nao.m_comp_dm import comp_dm

        assert tddft_iter_tol > 1e-6
        assert type(tddft_iter_broadening) == float
        assert sv.wfsx.x.shape[-1] == 1  # i.e. real eigenvectors we accept here

        if precision == "single":
            self.dtype = np.float32
            self.dtypeComplex = np.complex64
            self.gemm = blas.sgemm
            if scipy_ver > 0:
                self.spmv = blas.sspmv
            else:
                self.spmv = spmv_wrapper
        elif precision == "double":
            self.dtype = np.float64
            self.dtypeComplex = np.complex128
            self.gemm = blas.dgemm
            if scipy_ver > 0:
                self.spmv = blas.dspmv
            else:
                self.spmv = spmv_wrapper
        else:
            raise ValueError("precision can be only single or double")

        self.rf0_ncalls = 0
        self.l0_ncalls = 0
        self.matvec_ncalls = 0
        self.tddft_iter_tol = tddft_iter_tol
        self.eps = tddft_iter_broadening
        self.sv, self.pb, self.norbs, self.nspin = sv, pb, sv.norbs, sv.nspin

        self.v_dab = pb.get_dp_vertex_sparse(dtype=self.dtype,
                                             sparseformat=coo_matrix).tocsr()
        self.cc_da = pb.get_da2cc_sparse(dtype=self.dtype,
                                         sparseformat=coo_matrix).tocsr()

        self.moms0, self.moms1 = pb.comp_moments(dtype=self.dtype)
        self.nprod = self.moms0.size

        if load_kernel:
            self.load_kernel(**kvargs)
        else:
            self.kernel, self.kernel_dim = pb.comp_coulomb_pack(
                dtype=self.dtype)  # Lower Triangular Part of the kernel
            assert self.nprod == self.kernel_dim, "%r %r " % (self.nprod,
                                                              self.kernel_dim)

            if xc_code.upper() != 'RPA':
                dm = comp_dm(sv.wfsx.x, sv.get_occupations())
                pb.comp_fxc_pack(dm,
                                 xc_code,
                                 kernel=self.kernel,
                                 dtype=self.dtype,
                                 **kvargs)

        self.telec = sv.hsx.telec if telec is None else telec
        self.nelec = sv.hsx.nelec if nelec is None else nelec
        self.fermi_energy = sv.fermi_energy if fermi_energy is None else fermi_energy

        # probably unnecessary, require probably does a copy
        # problematic for the dtype, must there should be another option
        #self.x  = np.require(sv.wfsx.x, dtype=self.dtype, requirements='CW')

        self.ksn2e = np.require(sv.wfsx.ksn2e,
                                dtype=self.dtype,
                                requirements='CW')
        ksn2fd = fermi_dirac_occupations(self.telec, self.ksn2e,
                                         self.fermi_energy)
        self.ksn2f = (3 - self.nspin) * ksn2fd
        self.nfermi = np.argmax(ksn2fd[0, 0, :] < nfermi_tol)
        self.vstart = np.argmax(1.0 - ksn2fd[0, 0, :] > nfermi_tol)

        self.xocc = sv.wfsx.x[0, 0, 0:self.nfermi, :,
                              0]  # does python creates a copy at this point ?
        self.xvrt = sv.wfsx.x[0, 0, self.vstart:, :,
                              0]  # does python creates a copy at this point ?

        self.tddft_iter_gpu = tddft_iter_gpu_c(GPU, self.v_dab, self.ksn2f,
                                               self.ksn2e, self.norbs,
                                               self.nfermi, self.vstart)