Exemplo n.º 1
0
    def solve_static_iterative(self, maxiter=20, conv=1.e-9, use_diis=True):

        # Init JK object
        jk = psi4.core.JK.build(self.scf_wfn.basisset())
        jk.initialize()

        # Add blank matrices to the jk object and numpy hooks to C_right
        npC_right = []
        for xyz in range(3):
            jk.C_left_add(self.Co)
            mC = psi4.core.Matrix(self.nbf, self.nocc)
            npC_right.append(np.asarray(mC))
            jk.C_right_add(mC)

        # Build initial guess, previous vectors, diis object, and C_left updates
        self.x = []
        x_old = []
        diis = []
        ia_denom = -self.epsilon[:self.nocc].reshape(
            -1, 1) + self.epsilon[self.nocc:]
        for xyz in range(3):
            self.x.append(self.dipoles_xyz[xyz] / ia_denom)
            x_old.append(np.zeros(ia_denom.shape))
            diis.append(DIIS_helper())

        # Convert Co and Cv to numpy arrays
        Co = np.asarray(self.Co)
        Cv = np.asarray(self.Cv)

        print('\nStarting CPHF iterations:')
        t = time.time()
        for CPHF_ITER in range(1, maxiter + 1):

            # Update jk's C_right
            for xyz in range(3):
                npC_right[xyz][:] = Cv.dot(self.x[xyz].T)

            # Compute JK objects
            jk.compute()

            # Update amplitudes
            for xyz in range(3):
                # Build J and K objects
                J = np.asarray(jk.J()[xyz])
                K = np.asarray(jk.K()[xyz])

                # Bulid new guess
                X = self.dipoles_xyz[xyz].copy()
                X -= (Co.T).dot(4 * J - K.T - K).dot(Cv)
                X /= ia_denom

                # DIIS for good measure
                if use_diis:
                    diis[xyz].add(X, X - x_old[xyz])
                    X = diis[xyz].extrapolate()
                self.x[xyz] = X.copy()

            # Check for convergence
            rms = []
            for xyz in range(3):
                rms.append(np.max((self.x[xyz] - x_old[xyz])**2))
                x_old[xyz] = self.x[xyz]

            avg_RMS = sum(rms) / 3
            max_RMS = max(rms)

            if max_RMS < conv:
                print('CPHF converged in %d iterations and %.2f seconds.' %
                      (CPHF_ITER, time.time() - t))
                self.rhsvecs = []
                for numx in range(3):
                    rhsvec = self.dipoles_xyz[numx].reshape(-1)
                    self.rhsvecs.append(rhsvec)
                    self.x[numx] = self.x[numx].reshape(-1)
                break

            print(
                'CPHF Iteration %3d: Average RMS = %3.8f  Maximum RMS = %3.8f'
                % (CPHF_ITER, avg_RMS, max_RMS))
Exemplo n.º 2
0
    def solve_dynamic_iterative(self,
                                omega=0.0,
                                maxiter=20,
                                conv=1.e-9,
                                use_diis=True):

        # Init JK object
        jk = psi4.core.JK.build(self.scf_wfn.basisset())
        jk.initialize()

        # Add blank matrices to the JK object and NumPy hooks to
        # C_right; there are 6 sets of matrices to account for X and Y
        # vectors separately.
        npC_right = []
        for xyz in range(6):
            jk.C_left_add(self.Co)
            mC = psi4.core.Matrix(self.nbf, self.nocc)
            npC_right.append(np.asarray(mC))
            jk.C_right_add(mC)

        # Build initial guess, previous vectors, diis object, and C_left updates
        x_l, x_r = [], []
        x_l_old, x_r_old = [], []
        diis_l, diis_r = [], []
        ia_denom_l = self.epsilon[self.nocc:] - self.epsilon[:self.
                                                             nocc].reshape(
                                                                 -1, 1) - omega
        ia_denom_r = self.epsilon[self.nocc:] - self.epsilon[:self.
                                                             nocc].reshape(
                                                                 -1, 1) + omega
        for xyz in range(3):
            x_l.append(self.dipoles_xyz[xyz] / ia_denom_l)
            x_r.append(self.dipoles_xyz[xyz] / ia_denom_r)
            x_l_old.append(np.zeros(ia_denom_l.shape))
            x_r_old.append(np.zeros(ia_denom_r.shape))
            diis_l.append(DIIS_helper())
            diis_r.append(DIIS_helper())

        # Convert Co and Cv to numpy arrays
        Co = np.asarray(self.Co)
        Cv = np.asarray(self.Cv)

        print('\nStarting CPHF iterations:')
        t = time.time()
        for CPHF_ITER in range(1, maxiter + 1):

            # Update jk's C_right; ordering is Xx, Xy, Xz, Yx, Yy, Yz
            for xyz in range(3):
                npC_right[xyz][:] = Cv.dot(x_l[xyz].T)
                npC_right[xyz + 3][:] = Cv.dot(x_r[xyz].T)

            # Perform generalized J/K build
            jk.compute()

            # Update amplitudes
            for xyz in range(3):
                # Build J and K objects
                J_l = np.asarray(jk.J()[xyz])
                K_l = np.asarray(jk.K()[xyz])
                J_r = np.asarray(jk.J()[xyz + 3])
                K_r = np.asarray(jk.K()[xyz + 3])

                # Bulid new guess
                X_l = self.dipoles_xyz[xyz].copy()
                X_r = self.dipoles_xyz[xyz].copy()
                X_l -= (Co.T).dot(2 * J_l - K_l).dot(Cv)
                X_r -= (Co.T).dot(2 * J_r - K_r).dot(Cv)
                X_l /= ia_denom_l
                X_r /= ia_denom_r

                # DIIS for good measure
                if use_diis:
                    diis_l[xyz].add(X_l, X_l - x_l_old[xyz])
                    X_l = diis_l[xyz].extrapolate()
                    diis_r[xyz].add(X_r, X_r - x_r_old[xyz])
                    X_r = diis_r[xyz].extrapolate()
                x_l[xyz] = X_l.copy()
                x_r[xyz] = X_r.copy()

            # Check for convergence
            rms = []
            for xyz in range(3):
                rms_l = np.max((x_l[xyz] - x_l_old[xyz])**2)
                rms_r = np.max((x_r[xyz] - x_r_old[xyz])**2)
                rms.append(max(rms_l, rms_r))
                x_l_old[xyz] = x_l[xyz]
                x_r_old[xyz] = x_r[xyz]

            avg_RMS = sum(rms) / 3
            max_RMS = max(rms)

            if max_RMS < conv:
                print('CPHF converged in %d iterations and %.2f seconds.' %
                      (CPHF_ITER, time.time() - t))
                self.rhsvecs = []
                for numx in range(3):
                    rhsvec = self.dipoles_xyz[numx].reshape(-1)
                    self.rhsvecs.append(np.concatenate((rhsvec, -rhsvec)))
                    self.x.append(
                        np.concatenate(
                            (x_l[numx].reshape(-1), x_r[numx].reshape(-1))))
                break

            print(
                'CPHF Iteration %3d: Average RMS = %3.8f  Maximum RMS = %3.8f'
                % (CPHF_ITER, avg_RMS, max_RMS))
Exemplo n.º 3
0
    npC_right = []
    for xyz in range(3):
        jk.C_left_add(Co)
        mC = psi4.core.Matrix(nbf, nocc)
        npC_right.append(np.asarray(mC))
        jk.C_right_add(mC)

    # Build initial guess, previous vectors, diis object, and C_left updates
    x = []
    x_old = []
    diis = []
    ia_denom = -epsilon[:nocc].reshape(-1, 1) + epsilon[nocc:]
    for xyz in range(3):
        x.append(dipoles_xyz[xyz] / ia_denom)
        x_old.append(np.zeros(ia_denom.shape))
        diis.append(DIIS_helper())

    # Convert Co and Cv to numpy arrays
    mCo = Co
    Co = np.asarray(Co)
    Cv = np.asarray(Cv)

    print('\nStarting CPHF iterations:')
    t = time.time()
    for CPHF_ITER in range(1, maxiter + 1):

        # Update jk's C_right
        for xyz in range(3):
            npC_right[xyz][:] = Cv.dot(x[xyz].T)

        # Compute JK objects