Esempio n. 1
0
def test_reorder_dmat1():
    # This test uses a very simple small matrix.
    dmat = numpy.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
    permutation = numpy.array([1,2,0])
    reorder_dmat(dmat, permutation)
    expected_dmat = numpy.array([2.0, 4.0, 5.0, 1.0, 3.0, 0.0])
    assert(abs(dmat-expected_dmat).max() < 1e-10)
Esempio n. 2
0
def test_pot_o2_cc_pvtz_pure():
    tmpdir, fn_fchk = setup_gaussian("o2_cc_pvtz_pure")
    fchk = FCHKFile(fn_fchk)
    shutil.rmtree(tmpdir)
    basis = GaussianBasis.from_fchk(fchk)
    dmat = fchk.fields["Total SCF Density"]
    reorder_dmat(dmat, basis.g03_permutation)
    points = ref_data_o2_cc_pvtz_pure_pot[:,:3]*angstrom
    ref_potential = ref_data_o2_cc_pvtz_pure_pot[:,3]
    nuc_potential = 0.0
    for i in xrange(fchk.molecule.size):
        center = fchk.molecule.coordinates[i]
        Z = fchk.molecule.numbers[i]
        radius = numpy.sqrt(((points-center)**2).sum(axis=1))
        nuc_potential += Z/radius
    ref_potential -= nuc_potential
    potential = -basis.call_gint_grid(gint2_nai_dmat, dmat, points)
    assert(abs(potential-ref_potential).max() < 1e-6)
Esempio n. 3
0
def test_pot_o2_cc_pvtz_pure():
    tmpdir, fn_fchk = setup_gaussian("o2_cc_pvtz_pure")
    fchk = FCHKFile(fn_fchk)
    shutil.rmtree(tmpdir)
    basis = GaussianBasis.from_fchk(fchk)
    dmat = fchk.fields["Total SCF Density"]
    reorder_dmat(dmat, basis.g03_permutation)
    points = ref_data_o2_cc_pvtz_pure_pot[:, :3] * angstrom
    ref_potential = ref_data_o2_cc_pvtz_pure_pot[:, 3]
    nuc_potential = 0.0
    for i in xrange(fchk.molecule.size):
        center = fchk.molecule.coordinates[i]
        Z = fchk.molecule.numbers[i]
        radius = numpy.sqrt(((points - center)**2).sum(axis=1))
        nuc_potential += Z / radius
    ref_potential -= nuc_potential
    potential = -basis.call_gint_grid(gint2_nai_dmat, dmat, points)
    assert (abs(potential - ref_potential).max() < 1e-6)
Esempio n. 4
0
def test_reorder_dmat2():
    # This is a test based on a randomly generated matrix.
    num_dof = 10
    size = (num_dof*(num_dof+1))/2
    dmat = numpy.random.normal(0, 1, size)
    permutation = numpy.random.permutation(num_dof)
    square = numpy.zeros((num_dof, num_dof), float)
    j = 0
    for i in xrange(num_dof):
        square[i,0:i+1] = dmat[j:j+i+1]
        square[0:i+1,i] = dmat[j:j+i+1]
        j += i+1
    square = square[permutation].transpose()[permutation]
    expected_dmat = numpy.zeros(size, float)
    j = 0
    for i in xrange(num_dof):
        expected_dmat[j:j+i+1] = square[i,0:i+1]
        j += i+1
    reorder_dmat(dmat, permutation)
    assert(abs(dmat-expected_dmat).max() < 1e-10)
Esempio n. 5
0
def test_dens_o2_cc_pvtz_pure():
    tmpdir, fn_fchk = setup_gaussian("o2_cc_pvtz_pure")
    fchk = FCHKFile(fn_fchk)
    shutil.rmtree(tmpdir)
    basis = GaussianBasis.from_fchk(fchk)
    num_dmat = (basis.num_dof*(basis.num_dof+1))/2

    points = ref_data_o2_cc_pvtz_pure_orb0[:,:3]

    dmat = fchk.fields["Total SCF Density"][:num_dmat]
    reorder_dmat(dmat, basis.g03_permutation)
    density = basis.call_gint_grid(gint1_fn_dmat, dmat, points*angstrom)

    num_alpha = fchk.fields["Number of alpha electrons"]
    weights = fchk.fields["Alpha MO coefficients"][:num_alpha*basis.num_dof]
    weights = weights.reshape((num_alpha, basis.num_dof))
    weights = weights[:,basis.g03_permutation]
    orbitals = basis.call_gint_grid(gint1_fns_basis, weights, points*angstrom, num_alpha)
    assert(orbitals.shape == (num_alpha, len(points)))
    expected_density = 2*(orbitals**2).sum(axis=0)

    assert(abs(density-expected_density).max() < 1e-6)
Esempio n. 6
0
def test_dens_o2_cc_pvtz_pure():
    tmpdir, fn_fchk = setup_gaussian("o2_cc_pvtz_pure")
    fchk = FCHKFile(fn_fchk)
    shutil.rmtree(tmpdir)
    basis = GaussianBasis.from_fchk(fchk)
    num_dmat = (basis.num_dof * (basis.num_dof + 1)) / 2

    points = ref_data_o2_cc_pvtz_pure_orb0[:, :3]

    dmat = fchk.fields["Total SCF Density"][:num_dmat]
    reorder_dmat(dmat, basis.g03_permutation)
    density = basis.call_gint_grid(gint1_fn_dmat, dmat, points * angstrom)

    num_alpha = fchk.fields["Number of alpha electrons"]
    weights = fchk.fields["Alpha MO coefficients"][:num_alpha * basis.num_dof]
    weights = weights.reshape((num_alpha, basis.num_dof))
    weights = weights[:, basis.g03_permutation]
    orbitals = basis.call_gint_grid(gint1_fns_basis, weights,
                                    points * angstrom, num_alpha)
    assert (orbitals.shape == (num_alpha, len(points)))
    expected_density = 2 * (orbitals**2).sum(axis=0)

    assert (abs(density - expected_density).max() < 1e-6)
Esempio n. 7
0
    def __init__(self, filename, options):
        if len(options) == 1:
            density_type = options[0]
        elif len(options) == 0:
            density_type = None
        else:
            raise ValueError("Only one option is supported for the FCHKWaveFunction")
        self.filename = filename
        self.options = options
        self.prefix = filename[:-5]
        fchk = FCHKFile(filename)
        # for use by the rest of the program
        self.charge = fchk.fields["Charge"]
        self.num_orbitals = fchk.fields["Number of basis functions"]
        self.dipole = fchk.fields["Dipole Moment"]
        self.num_electrons = fchk.fields["Number of electrons"]
        self.num_alpha = fchk.fields["Number of alpha electrons"]
        self.num_beta = fchk.fields["Number of beta electrons"]
        self.restricted = "Beta Orbital Energies" not in fchk.fields
        self.molecule = fchk.molecule
        self.nuclear_charges = fchk.fields["Nuclear charges"]
        # for internal usage
        if density_type is None:
            if "mp2" in fchk.lot.lower():
                density_type = "mp2"
            elif "mp3" in fchk.lot.lower():
                density_type = "mp3"
            elif "mp4" in fchk.lot.lower():
                density_type = "mp4"
            else:
                density_type = "scf"
        # electronic structure data
        self.basis = GaussianBasis.from_fchk(fchk)
        # orbitals
        self.alpha_orbital_energies = None
        self.beta_orbital_energies = None
        self.natural_orbitals = None
        self.alpha_orbitals = None
        self.beta_orbitals = None
        self.alpha_occupations = None
        self.beta_occupations = None
        self.natural_occupations = None
        if density_type == "scf":
            # Load orbital stuff only for scf computations.
            self.alpha_orbital_energies = fchk.fields["Alpha Orbital Energies"]
            self.beta_orbital_energies = fchk.fields.get("Beta Orbital Energies", self.alpha_orbital_energies)
            self.alpha_orbitals = fchk.fields["Alpha MO coefficients"].reshape((-1,self.basis.num_dof))
            self.alpha_orbitals = self.alpha_orbitals[:,self.basis.g03_permutation]
            self.beta_orbitals = fchk.fields.get("Beta MO coefficients")
            if self.beta_orbitals is None:
                self.beta_orbitals = self.alpha_orbitals
            else:
                self.beta_orbitals = self.beta_orbitals.reshape((-1,self.basis.num_dof))[:,self.basis.g03_permutation]
            self.alpha_occupations = numpy.zeros(self.num_orbitals, float)
            self.alpha_occupations[:self.num_alpha] = 1.0
            if self.beta_orbitals is None:
                self.beta_occupations = self.alpha_occupations
            else:
                self.beta_occupations = numpy.zeros(self.num_orbitals, float)
                self.beta_occupations[:self.num_beta] = 1.0
            if self.restricted:
                self.natural_orbitals = self.alpha_orbitals
                self.natural_occupations = self.alpha_occupations + self.beta_occupations
                self.num_natural = max(self.num_alpha, self.num_beta)

        # density matrices
        hack_fchk = self.restricted and (self.num_alpha != self.num_beta) and density_type == "scf"
        if hack_fchk:
            # construct density matrices manually because the fchk file
            # contains the wrong one. we only do this for scf computations.
            n = self.basis.num_dof
            size = (n*(n+1))/2
            self.density_matrix = numpy.zeros(size, float)
            add_orbitals_to_dmat(self.alpha_orbitals[:self.num_alpha], self.density_matrix)
            add_orbitals_to_dmat(self.beta_orbitals[:self.num_beta], self.density_matrix)
            self.spin_density_matrix = numpy.zeros(size, float)
            num_min = min(self.num_alpha, self.num_beta)
            num_max = max(self.num_alpha, self.num_beta)
            add_orbitals_to_dmat(self.alpha_orbitals[num_min:num_max], self.spin_density_matrix)
        else:
            self.density_matrix = None
            self.spin_density_matrix = None
            # load the density matrices
            for key in fchk.fields:
                if key.startswith("Total") and key.endswith("Density"):
                    if key[6:-8].lower() != density_type:
                        continue
                    assert self.density_matrix is None
                    dmat = fchk.fields[key]
                    reorder_dmat(dmat, self.basis.g03_permutation)
                    self.density_matrix = dmat
                elif key.startswith("Spin") and key.endswith("Density"):
                    if key[5:-8].lower() != density_type:
                        continue
                    assert self.spin_density_matrix is None
                    dmat = fchk.fields[key]
                    reorder_dmat(dmat, self.basis.g03_permutation)
                    self.spin_density_matrix = dmat
            if self.density_matrix is None:
                raise ValueError("Could not find the '%s' density matrix in the fchk file." % self._density_type)
Esempio n. 8
0
    def __init__(self, filename, options):
        if len(options) == 1:
            density_type = options[0]
        elif len(options) == 0:
            density_type = None
        else:
            raise ValueError(
                "Only one option is supported for the FCHKWaveFunction")
        self.filename = filename
        self.options = options
        self.prefix = filename[:-5]
        fchk = FCHKFile(filename)
        # for use by the rest of the program
        self.charge = fchk.fields["Charge"]
        self.num_orbitals = fchk.fields["Number of basis functions"]
        self.dipole = fchk.fields["Dipole Moment"]
        self.num_electrons = fchk.fields["Number of electrons"]
        self.num_alpha = fchk.fields["Number of alpha electrons"]
        self.num_beta = fchk.fields["Number of beta electrons"]
        self.restricted = "Beta Orbital Energies" not in fchk.fields
        self.molecule = fchk.molecule
        self.nuclear_charges = fchk.fields["Nuclear charges"]
        # for internal usage
        if density_type is None:
            if "mp2" in fchk.lot.lower():
                density_type = "mp2"
            elif "mp3" in fchk.lot.lower():
                density_type = "mp3"
            elif "mp4" in fchk.lot.lower():
                density_type = "mp4"
            else:
                density_type = "scf"
        # electronic structure data
        self.basis = GaussianBasis.from_fchk(fchk)
        # orbitals
        self.alpha_orbital_energies = None
        self.beta_orbital_energies = None
        self.natural_orbitals = None
        self.alpha_orbitals = None
        self.beta_orbitals = None
        self.alpha_occupations = None
        self.beta_occupations = None
        self.natural_occupations = None
        if density_type == "scf":
            # Load orbital stuff only for scf computations.
            self.alpha_orbital_energies = fchk.fields["Alpha Orbital Energies"]
            self.beta_orbital_energies = fchk.fields.get(
                "Beta Orbital Energies", self.alpha_orbital_energies)
            self.alpha_orbitals = fchk.fields["Alpha MO coefficients"].reshape(
                (-1, self.basis.num_dof))
            self.alpha_orbitals = self.alpha_orbitals[:, self.basis.
                                                      g03_permutation]
            self.beta_orbitals = fchk.fields.get("Beta MO coefficients")
            if self.beta_orbitals is None:
                self.beta_orbitals = self.alpha_orbitals
            else:
                self.beta_orbitals = self.beta_orbitals.reshape(
                    (-1, self.basis.num_dof))[:, self.basis.g03_permutation]
            self.alpha_occupations = numpy.zeros(self.num_orbitals, float)
            self.alpha_occupations[:self.num_alpha] = 1.0
            if self.beta_orbitals is None:
                self.beta_occupations = self.alpha_occupations
            else:
                self.beta_occupations = numpy.zeros(self.num_orbitals, float)
                self.beta_occupations[:self.num_beta] = 1.0
            if self.restricted:
                self.natural_orbitals = self.alpha_orbitals
                self.natural_occupations = self.alpha_occupations + self.beta_occupations
                self.num_natural = max(self.num_alpha, self.num_beta)

        # density matrices
        hack_fchk = self.restricted and (
            self.num_alpha != self.num_beta) and density_type == "scf"
        if hack_fchk:
            # construct density matrices manually because the fchk file
            # contains the wrong one. we only do this for scf computations.
            n = self.basis.num_dof
            size = (n * (n + 1)) / 2
            self.density_matrix = numpy.zeros(size, float)
            add_orbitals_to_dmat(self.alpha_orbitals[:self.num_alpha],
                                 self.density_matrix)
            add_orbitals_to_dmat(self.beta_orbitals[:self.num_beta],
                                 self.density_matrix)
            self.spin_density_matrix = numpy.zeros(size, float)
            num_min = min(self.num_alpha, self.num_beta)
            num_max = max(self.num_alpha, self.num_beta)
            add_orbitals_to_dmat(self.alpha_orbitals[num_min:num_max],
                                 self.spin_density_matrix)
        else:
            self.density_matrix = None
            self.spin_density_matrix = None
            # load the density matrices
            for key in fchk.fields:
                if key.startswith("Total") and key.endswith("Density"):
                    if key[6:-8].lower() != density_type:
                        continue
                    assert self.density_matrix is None
                    dmat = fchk.fields[key]
                    reorder_dmat(dmat, self.basis.g03_permutation)
                    self.density_matrix = dmat
                elif key.startswith("Spin") and key.endswith("Density"):
                    if key[5:-8].lower() != density_type:
                        continue
                    assert self.spin_density_matrix is None
                    dmat = fchk.fields[key]
                    reorder_dmat(dmat, self.basis.g03_permutation)
                    self.spin_density_matrix = dmat
            if self.density_matrix is None:
                raise ValueError(
                    "Could not find the '%s' density matrix in the fchk file."
                    % self._density_type)