| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_CORE_BASISSET_H |
| 7 |
#define AVOGADRO_CORE_BASISSET_H |
| 8 |
|
| 9 |
#include "avogadrocoreexport.h" |
| 10 |
#include "core/variant.h" |
| 11 |
|
| 12 |
#include <array> |
| 13 |
#include <string> |
| 14 |
|
| 15 |
namespace Avogadro::Core { |
| 16 |
|
| 17 |
class Molecule; |
| 18 |
|
| 19 |
/** |
| 20 |
* @class BasisSet basisset.h <avogadro/core/basisset.h> |
| 21 |
* @brief BasisSet contains basis set data. |
| 22 |
* @author Marcus D. Hanwell |
| 23 |
* |
| 24 |
* This is the base class for basis sets, and has two derived classes - |
| 25 |
* GaussianSet and SlaterSet. It must be populated with data, with other classes |
| 26 |
* capable of performing calculations on the data or writing it out. |
| 27 |
*/ |
| 28 |
|
| 29 |
class AVOGADROCORE_EXPORT BasisSet |
| 30 |
{ |
| 31 |
public: |
| 32 |
/** |
| 33 |
* Constructor. |
| 34 |
*/ |
| 35 |
BasisSet() = default; |
| 36 |
|
| 37 |
/** |
| 38 |
* Destructor. |
| 39 |
*/ |
| 40 |
virtual ~BasisSet() = default; |
| 41 |
|
| 42 |
/** |
| 43 |
* Clone. |
| 44 |
*/ |
| 45 |
virtual BasisSet* clone() const = 0; |
| 46 |
|
| 47 |
/** |
| 48 |
* @brief The ElectronType enum describes the type of electrons being set or |
| 49 |
* retrieved. If Paired, then Alpha and Beta cannot be set, if Alpha or Beta |
| 50 |
* then both must be set. |
| 51 |
*/ |
| 52 |
enum ElectronType |
| 53 |
{ |
| 54 |
Paired, |
| 55 |
Alpha, |
| 56 |
Beta |
| 57 |
}; |
| 58 |
|
| 59 |
/** |
| 60 |
* Set the number of electrons in the BasisSet. |
| 61 |
* @param n The number of electrons in the BasisSet. |
| 62 |
* @param type The type of the electrons (Alpha, Beta, or Paired). |
| 63 |
*/ |
| 64 |
virtual void setElectronCount(unsigned int n, ElectronType type = Paired); |
| 65 |
|
| 66 |
/** |
| 67 |
* @param type The type of the electrons (Alpha, Beta, or Paired). |
| 68 |
* @return The number of electrons in the molecule. |
| 69 |
*/ |
| 70 |
unsigned int electronCount(ElectronType type = Paired) const; |
| 71 |
|
| 72 |
/** |
| 73 |
* Set the molecule for the basis set. |
| 74 |
*/ |
| 75 |
void setMolecule(Molecule* molecule_) { m_molecule = molecule_; } |
| 76 |
|
| 77 |
/** |
| 78 |
* Get the molecule this basis set belongs to. |
| 79 |
*/ |
| 80 |
Molecule* molecule() { return m_molecule; } |
| 81 |
const Molecule* molecule() const { return m_molecule; } |
| 82 |
|
| 83 |
/** |
| 84 |
* Set the name of the basis set. |
| 85 |
*/ |
| 86 |
void setName(const std::string& name) { m_name = name; } |
| 87 |
|
| 88 |
/** |
| 89 |
* Get the name of the basis set. |
| 90 |
*/ |
| 91 |
std::string name() const { return m_name; } |
| 92 |
|
| 93 |
/** |
| 94 |
* Set the name of the basis set. |
| 95 |
*/ |
| 96 |
void setTheoryName(const std::string& name) { m_theoryName = name; } |
| 97 |
|
| 98 |
/** |
| 99 |
* Get the name of the basis set. |
| 100 |
*/ |
| 101 |
std::string theoryName() const { return m_theoryName; } |
| 102 |
|
| 103 |
/** |
| 104 |
* @return The number of molecular orbitals in the BasisSet. |
| 105 |
*/ |
| 106 |
virtual unsigned int molecularOrbitalCount( |
| 107 |
ElectronType type = Paired) const = 0; |
| 108 |
|
| 109 |
/** |
| 110 |
* @return The molecular orbital number corresponding to the HOMO orbital. |
| 111 |
*/ |
| 112 |
unsigned int homo(ElectronType type = Paired) const { return lumo(type) - 1; } |
| 113 |
|
| 114 |
/** |
| 115 |
* @return The molecular orbital number corresponding to the LUMO orbital. |
| 116 |
*/ |
| 117 |
unsigned int lumo(ElectronType type = Paired) const; |
| 118 |
|
| 119 |
/** |
| 120 |
* @return True of the basis set is valid, false otherwise. |
| 121 |
* Default is true, if false then the basis set is likely unusable. |
| 122 |
*/ |
| 123 |
virtual bool isValid() = 0; |
| 124 |
|
| 125 |
/** |
| 126 |
* @return the orbital symmetry labels (if they exist) for the MOs |
| 127 |
*/ |
| 128 |
std::vector<std::string> symmetryLabels(ElectronType type = Paired) const |
| 129 |
{ |
| 130 |
if (type == Paired || type == Alpha) |
| 131 |
return m_symmetryLabels[0]; |
| 132 |
else |
| 133 |
return m_symmetryLabels[1]; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Set the orbital symmetry labels (a1, t2g, etc.) for the molecular |
| 138 |
* orbitals |
| 139 |
*/ |
| 140 |
void setSymmetryLabels(const std::vector<std::string>& labels, |
| 141 |
ElectronType type = Paired); |
| 142 |
|
| 143 |
/** |
| 144 |
* @brief Set the molecular orbital energies, expected in Hartrees. |
| 145 |
* @param energies The vector containing energies for the MOs of type |
| 146 |
* @param type The type of the electrons being set. |
| 147 |
*/ |
| 148 |
void setMolecularOrbitalEnergy(const std::vector<double>& energies, |
| 149 |
ElectronType type = Paired); |
| 150 |
|
| 151 |
/** |
| 152 |
* @brief Set the molecular orbital occupancies. |
| 153 |
* @param occ The occupancies for the MOs of type. |
| 154 |
* @param type The type of the electrons being set. |
| 155 |
*/ |
| 156 |
void setMolecularOrbitalOccupancy(const std::vector<unsigned char>& occ, |
| 157 |
ElectronType type = Paired); |
| 158 |
|
| 159 |
std::vector<double>& moEnergy(ElectronType type = Paired) |
| 160 |
{ |
| 161 |
if (type == Paired || type == Alpha) |
| 162 |
return m_moEnergy[0]; |
| 163 |
else |
| 164 |
return m_moEnergy[1]; |
| 165 |
} |
| 166 |
|
| 167 |
std::vector<double> moEnergy(ElectronType type = Paired) const |
| 168 |
{ |
| 169 |
if (type == Paired || type == Alpha) |
| 170 |
return m_moEnergy[0]; |
| 171 |
else |
| 172 |
return m_moEnergy[1]; |
| 173 |
} |
| 174 |
|
| 175 |
std::vector<unsigned char>& moOccupancy(ElectronType type = Paired) |
| 176 |
{ |
| 177 |
if (type == Paired || type == Alpha) |
| 178 |
return m_moOccupancy[0]; |
| 179 |
else |
| 180 |
return m_moOccupancy[1]; |
| 181 |
} |
| 182 |
|
| 183 |
std::vector<unsigned char> moOccupancy(ElectronType type = Paired) const |
| 184 |
{ |
| 185 |
if (type == Paired || type == Alpha) |
| 186 |
return m_moOccupancy[0]; |
| 187 |
else |
| 188 |
return m_moOccupancy[1]; |
| 189 |
} |
| 190 |
|
| 191 |
protected: |
| 192 |
/** |
| 193 |
* Total number of electrons, 0 is alpha electrons and 1 is beta electrons. |
| 194 |
* For closed shell calculations alpha is doubly occupied and there are no |
| 195 |
* beta electrons. |
| 196 |
*/ |
| 197 |
std::array<unsigned int, 2> m_electrons = {}; |
| 198 |
|
| 199 |
/** |
| 200 |
* The Molecule holds the atoms (and possibly bonds) read in from the output |
| 201 |
* file. Most basis sets have orbitals around these atoms, but this is not |
| 202 |
* necessarily the case. |
| 203 |
*/ |
| 204 |
Molecule* m_molecule = nullptr; |
| 205 |
|
| 206 |
/** |
| 207 |
* The name of the basis set, this is usually a string identifier referencing |
| 208 |
* a standard basis set when only one is used. |
| 209 |
*/ |
| 210 |
std::string m_name; |
| 211 |
|
| 212 |
/** |
| 213 |
* The name of the theory used for the calculation. |
| 214 |
*/ |
| 215 |
std::string m_theoryName; |
| 216 |
|
| 217 |
/** |
| 218 |
* The orbital symmetry labels (if they exist) for the MOs |
| 219 |
*/ |
| 220 |
std::vector<std::string> m_symmetryLabels[2]; |
| 221 |
|
| 222 |
/** |
| 223 |
* @brief This block stores energies for the molecular orbitals (same |
| 224 |
* convention as the molecular orbital coefficients). |
| 225 |
*/ |
| 226 |
std::vector<double> m_moEnergy[2]; |
| 227 |
|
| 228 |
/** |
| 229 |
* @brief The occupancy of the molecular orbitals. |
| 230 |
*/ |
| 231 |
std::vector<unsigned char> m_moOccupancy[2]; |
| 232 |
}; |
| 233 |
|
| 234 |
inline unsigned int BasisSet::lumo(ElectronType type) const |
| 235 |
{ |
| 236 |
if (type == Beta) { |
| 237 |
// check if we have occupancy data |
| 238 |
if (m_moOccupancy[1].size() > 0) { |
| 239 |
for (unsigned int i = 0; i < m_moOccupancy[1].size(); ++i) { |
| 240 |
if (m_moOccupancy[1][i] == 0) |
| 241 |
return i; |
| 242 |
} |
| 243 |
} |
| 244 |
} else { |
| 245 |
// alpha or paired |
| 246 |
// check if we have occupancy data - more accurate |
| 247 |
if (m_moOccupancy[0].size() > 0) { |
| 248 |
for (unsigned int i = 0; i < m_moOccupancy[0].size(); ++i) { |
| 249 |
if (m_moOccupancy[0][i] == 0) |
| 250 |
return i; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
// fall back to electron count |
| 255 |
// (supports odd number of electrons)) |
| 256 |
if (type == Beta) { |
| 257 |
return m_electrons[0] / 2 + 1; |
| 258 |
} else { |
| 259 |
return m_electrons[0] / 2 + (m_electrons[0] % 2) + 1; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
inline void BasisSet::setElectronCount(unsigned int n, ElectronType type) |
| 264 |
{ |
| 265 |
switch (type) { |
| 266 |
case Paired: |
| 267 |
m_electrons[0] = n; |
| 268 |
m_electrons[1] = 0; |
| 269 |
break; |
| 270 |
case Alpha: |
| 271 |
m_electrons[0] = n; |
| 272 |
break; |
| 273 |
case Beta: |
| 274 |
m_electrons[1] = n; |
| 275 |
break; |
| 276 |
default: |
| 277 |
// Shouldn't hit this condition. |
| 278 |
; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
inline unsigned int BasisSet::electronCount(ElectronType type) const |
| 283 |
{ |
| 284 |
switch (type) { |
| 285 |
case Paired: |
| 286 |
case Alpha: |
| 287 |
return m_electrons[0]; |
| 288 |
case Beta: |
| 289 |
return m_electrons[1]; |
| 290 |
default: |
| 291 |
// Shouldn't hit this condition. |
| 292 |
return 0; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
inline void BasisSet::setSymmetryLabels(const std::vector<std::string>& labels, |
| 297 |
ElectronType type) |
| 298 |
{ |
| 299 |
if (type == Paired || type == Alpha) |
| 300 |
m_symmetryLabels[0] = labels; |
| 301 |
else |
| 302 |
m_symmetryLabels[1] = labels; |
| 303 |
} |
| 304 |
|
| 305 |
inline void BasisSet::setMolecularOrbitalEnergy( |
| 306 |
const std::vector<double>& energies, ElectronType type) |
| 307 |
{ |
| 308 |
if (type == Beta) |
| 309 |
m_moEnergy[1] = energies; |
| 310 |
else |
| 311 |
m_moEnergy[0] = energies; |
| 312 |
} |
| 313 |
|
| 314 |
inline void BasisSet::setMolecularOrbitalOccupancy( |
| 315 |
const std::vector<unsigned char>& occ, ElectronType type) |
| 316 |
{ |
| 317 |
if (type == Beta) |
| 318 |
m_moOccupancy[1] = occ; |
| 319 |
else |
| 320 |
m_moOccupancy[0] = occ; |
| 321 |
} |
| 322 |
|
| 323 |
} // namespace Avogadro::Core |
| 324 |
|
| 325 |
#endif |