コード例 #1
0
ファイル: orbital_hessian.py プロジェクト: Dayou-Zhang/mrh
 def _get_tFock1_2b(self, p, q, kappa):
     rsH = self.rs.conjugate().T
     rs2q = r2q = rsH @ q
     q2rs = q2r = rs2q.conjugate().T
     svals_rinq = linalg.svd(rs2q)[1].sum()
     rs2p = rsH @ p
     svals_rinp = linalg.svd(rs2p)[1].sum()
     rinq = abs(svals_rinq - self.rs.shape[-1]) < 1e-5
     rinp = abs(svals_rinp - self.rs.shape[-1]) < 1e-5
     if rinp:
         return np.zeros(
             (p.shape[-1], q.shape[-1]
              ))  # Hack to keep both F_pq and F_qp in the rinq case
     elif not rinq:
         raise RuntimeError(
             "Totally off-diagonal Hessian elements not supported ({} {} {})"
             .format(svals_rinq, svals_rinp, self.rs.shape[-1]))
     m2p = self.ints.ao2loc @ p
     m2q = self.ints.ao2loc @ q
     p2m = m2p.conjugate().T
     ao2loc = self.ints.ao2loc
     loc2ao = ao2loc.conjugate().T
     dm1 = self.oneRDMs[0] + self.oneRDMs[1]
     tdm1 = ao2loc @ (kappa @ dm1 - dm1 @ kappa) @ loc2ao
     tdm1 = numpy_helper.pack_tril(tdm1 + tdm1.T - np.diag(np.diag(tdm1)))
     vj_pq = np.zeros((p.shape[-1], q.shape[-1]), dtype=tdm1.dtype)
     for cderi in self.ints.with_df.loop():
         rho = np.dot(cderi, tdm1)
         vj_pq += p2m @ numpy_helper.unpack_tril(np.dot(rho, cderi)) @ m2q
     pH = p.conjugate().T
     qH = q.conjugate().T
     dm1_pp = pH @ dm1 @ p
     dm1_qq = qH @ dm1 @ q
     tFock1 = vj_pq @ dm1_qq - dm1_pp @ vj_pq
     return tFock1
コード例 #2
0
ファイル: diis.py プロジェクト: zzy2014/pyscf
    def restore(self, filename, inplace=True):
        '''Read diis contents from a diis file and replace the attributes of
        current diis object if needed, then construct the vector.
        '''
        fdiis = misc.H5TmpFile(filename)
        if inplace:
            self.filename = filename
            self._diisfile = fdiis

        diis_keys = fdiis.keys()
        x_keys = [k for k in diis_keys if k[0] == 'x']
        e_keys = [k for k in diis_keys if k[0] == 'e']
        # errvec may be incomplete if program is terminated when generating errvec.
        # The last vector or errvec should be excluded.
        nd = min(len(x_keys), len(e_keys))
        if nd == 0:
            return self

        if inplace:
            if fdiis[x_keys[0]].size < INCORE_SIZE or self.incore:
                for key in diis_keys:
                    self._buffer[key] = numpy.asarray(fdiis[key])

            if 'xprev' in diis_keys:
                self._xprev = fdiis['xprev']

        else:
            for key in diis_keys:
                self._store(key, fdiis[key].value)

            if 'xprev' in diis_keys:
                self._store('xprev', numpy.asarray(fdiis['xprev']))
                if 'xprev' in self._buffer:  # incore
                    self._xprev = self._buffer['xprev']
                else:
                    self._xprev = self._diisfile['xprev']

        self._bookkeep = list(range(nd))
        self._head = nd
        vecsize = 0

        e_mat = []
        for i in range(nd):
            dti = numpy.asarray(self.get_err_vec(i))
            vecsize = dti.size
            for j in range(i + 1):
                dtj = self.get_err_vec(j)
                assert (dtj.size == vecsize)
                tmp = 0
                for p0, p1 in misc.prange(0, vecsize, BLOCK_SIZE):
                    tmp += numpy.dot(dti[p0:p1].conj(), dtj[p0:p1])
                e_mat.append(tmp)
            dti = dtj = None
        e_mat = numpy_helper.unpack_tril(e_mat)

        space = max(nd, self.space)
        self._H = numpy.zeros((space + 1, space + 1), e_mat.dtype)
        self._H[0, 1:] = self._H[1:, 0] = 1
        self._H[1:nd + 1, 1:nd + 1] = e_mat
        return self