def test_add_orbitals_to_dmat2(): num_dof = 10 num_orbitals = 5 size = (num_dof*(num_dof+1))/2 orbitals = numpy.random.normal(0, 1, (num_orbitals, num_dof)) dmat = numpy.zeros(size, float) add_orbitals_to_dmat(orbitals, dmat) expected_dmat = numpy.dot(orbitals.transpose(), orbitals) expected_dmat = numpy.concatenate([expected_dmat[i,:i+1] for i in xrange(num_dof)]) assert(abs(dmat - expected_dmat).max() < 1e-10)
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)
def test_add_orbitals_to_dmat1(): orbitals = numpy.array([[1, 2], [3, 4]]) expected_dmat = numpy.array([10, 14, 20]) dmat = numpy.zeros(3, float) add_orbitals_to_dmat(orbitals, dmat) assert(abs(dmat - expected_dmat).max() < 1e-10)
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)