41 std::vector<double> radius(
static_cast<std::size_t
>(n));
42 for (
Index i = 0; i < n; ++i) {
43 radius[std::size_t(i)] = elements.
getCovRad(mol[i].getElement(),
"bohr");
45 std::vector<std::vector<Index>> graph(
static_cast<std::size_t
>(n));
46 for (
Index i = 0; i < n; ++i) {
47 for (
Index j = i + 1; j < n; ++j) {
48 const double d = (mol[i].
getPos() - mol[j].getPos()).norm();
49 if (d < 1.2 * (radius[std::size_t(i)] + radius[std::size_t(j)])) {
50 graph[std::size_t(i)].push_back(j);
51 graph[std::size_t(j)].push_back(i);
59 const Eigen::MatrixXd& mos) {
64 const Eigen::MatrixXd q = mos.cwiseProduct(overlap.
Matrix() * mos);
66 Eigen::MatrixXd pop(
Index(per_atom.size()), mos.cols());
68 for (std::size_t a = 0; a < per_atom.size(); ++a) {
69 pop.row(
Index(a)) = q.middleRows(start, per_atom[a]).colwise().sum();
77std::vector<std::vector<Index>> Components(
78 const std::vector<std::vector<Index>>& graph,
79 const std::vector<bool>& in_set) {
81 std::vector<Index> label(std::size_t(n), -1);
82 std::vector<std::vector<Index>> comps;
83 for (
Index s = 0; s < n; ++s) {
84 if (!in_set[std::size_t(s)] || label[std::size_t(s)] >= 0) {
88 std::vector<Index> stack{s};
89 label[std::size_t(s)] =
Index(comps.size()) - 1;
90 while (!stack.empty()) {
91 const Index i = stack.back();
93 comps.back().push_back(i);
94 for (
Index j : graph[std::size_t(i)]) {
95 if (in_set[std::size_t(j)] && label[std::size_t(j)] < 0) {
96 label[std::size_t(j)] = label[std::size_t(s)];
107 const std::vector<Index>& active) {
110 std::vector<bool> all(std::size_t(n),
true);
111 std::vector<bool> act(std::size_t(n),
false);
112 for (
Index a : active) {
113 if (a < 0 || a >= n) {
114 throw std::runtime_error(
"dft_in_dft: active atom index " +
115 std::to_string(a) +
" out of range");
117 act[std::size_t(a)] =
true;
119 for (
const auto& molecule : Components(graph, all)) {
120 std::vector<bool> act_here(std::size_t(n),
false);
121 for (
Index i : molecule) {
122 act_here[std::size_t(i)] = act[std::size_t(i)];
124 const auto pieces = Components(graph, act_here);
125 if (pieces.size() > 1) {
126 std::ostringstream msg;
127 msg <<
"dft_in_dft: the active atoms split a molecule into "
128 << pieces.size() <<
" disconnected pieces:";
129 for (
const auto& p : pieces) {
131 for (std::size_t k = 0; k < p.size(); ++k) {
132 msg << (k ?
"," :
"") << p[k];
136 msg <<
". Each molecule should contribute one connected piece.";
137 throw std::runtime_error(msg.str());
143 const std::vector<Index>& active,
152 std::vector<Index> orbs;
153 for (
Index k = homo; k >= 0 &&
e(k) >=
e(homo) - options.
window; --k) {
157 const Index lumo = homo + 1;
158 for (
Index k = lumo; k <
e.size() &&
e(k) <=
e(lumo) + options.
window;
166 for (std::size_t k = 0; k < orbs.size(); ++k) {
173 const Eigen::VectorXd maxpop = pop.rowwise().maxCoeff();
175 std::vector<bool> act(std::size_t(n),
false);
176 for (
Index a : active) {
177 act[std::size_t(a)] =
true;
179 auto coverage = [&](
const std::vector<bool>& in) {
180 Eigen::VectorXd c = Eigen::VectorXd::Zero(pop.cols());
181 for (
Index i = 0; i < n; ++i) {
182 if (in[std::size_t(i)]) {
183 c += pop.row(i).transpose();
192 const std::vector<bool> input = act;
196 std::vector<bool> molecule_active(std::size_t(n),
false);
198 std::vector<bool> all(std::size_t(n),
true);
199 for (
const auto& molecule : Components(graph, all)) {
201 for (
Index i : molecule) {
202 any = any || act[std::size_t(i)];
204 for (
Index i : molecule) {
205 molecule_active[std::size_t(i)] = any;
214 std::vector<Index> parent(std::size_t(n), -2);
215 std::vector<Index> queue;
216 for (
Index i = 0; i < n; ++i) {
217 if (act[std::size_t(i)]) {
218 parent[std::size_t(i)] = -1;
223 for (std::size_t q = 0; q < queue.size() && target < 0; ++q) {
224 for (
Index j : graph[std::size_t(queue[q])]) {
225 if (parent[std::size_t(j)] != -2) {
228 parent[std::size_t(j)] = queue[q];
230 molecule_active[std::size_t(j)]) {
237 for (
Index i = target; i >= 0 && !act[std::size_t(i)];
238 i = parent[std::size_t(i)]) {
239 act[std::size_t(i)] =
true;
248 for (
Index i = 0; i < n; ++i) {
249 if (act[std::size_t(i)] || mol[i].getElement() ==
"H") {
252 Index active_neighbours = 0;
253 for (
Index j : graph[std::size_t(i)]) {
254 active_neighbours += act[std::size_t(j)] ? 1 : 0;
256 if (active_neighbours >= 2) {
257 act[std::size_t(i)] =
true;
264 for (
Index i = 0; i < n; ++i) {
265 if (act[std::size_t(i)] && mol[i].getElement() !=
"H") {
266 for (
Index j : graph[std::size_t(i)]) {
267 if (mol[j].getElement() ==
"H") {
268 act[std::size_t(j)] =
true;
273 for (
Index i = 0; i < n; ++i) {
274 if (act[std::size_t(i)] && !input[std::size_t(i)]) {
275 report.
added.push_back(i);
279 for (
Index i = 0; i < n; ++i) {
280 if (act[std::size_t(i)]) {
281 report.
atoms.push_back(i);
282 }
else if (maxpop(i) > options.
threshold) {
289 for (
Index i = 0; i < n; ++i) {
290 if (!act[std::size_t(i)] && mol[i].getElement() ==
"H") {
291 for (
Index j : graph[std::size_t(i)]) {
292 if (act[std::size_t(j)]) {
299 for (
Index i = 0; i < n; ++i) {
300 if (act[std::size_t(i)] || mol[i].getElement() ==
"H") {
303 Index active_neighbours = 0;
304 for (
Index j : graph[std::size_t(i)]) {
305 active_neighbours += act[std::size_t(j)] ? 1 : 0;
307 if (active_neighbours >= 2) {
317 std::ostringstream out;
318 out <<
"Frontier orbitals of the full calculation on the active region "
319 "(Mulliken population):\n";
320 for (std::size_t k = 0; k <
orbitals.size(); ++k) {
321 out <<
" MO " << std::setw(5) <<
orbitals[k] <<
" " << std::fixed
324 if (!
added.empty()) {
329 if (!
added.empty()) {
330 out <<
" added " <<
added.size() <<
" atoms:";
337 out <<
" warning: inactive hydrogens bonded to active atoms (X-H bonds "
338 "cut; grow adds them):";
345 out <<
" warning: inactive atoms bonded to two or more active atoms "
346 "(several bonds cut):";
353 out <<
" above the threshold in molecules without active atoms:";
Container to hold Basisfunctions for all atoms.
const std::vector< Index > & getFuncPerAtom() const
void Fill(const AOBasis &aobasis) final
const Eigen::MatrixXd & Matrix() const
static Report Evaluate(const Orbitals &full, const std::vector< Index > &active, const Options &options)
Checks the input region, and with options.grow adds every atom whose population in a frontier orbital...
static Eigen::MatrixXd AtomPopulations(const Orbitals &orb, const Eigen::MatrixXd &mos)
static std::vector< std::vector< Index > > BondGraph(const QMMolecule &mol)
Bonded neighbours of each atom (covalent radii, factor 1.2).
static void CheckConnected(const QMMolecule &mol, const std::vector< Index > &active)
Throws unless the active atoms of every molecule (connected component of the bond graph) form a singl...
const Eigen::Vector3d & getPos() const
Container for molecular orbitals and derived one-particle data.
Index getNumberOfAlphaElectrons() const
Return the stored number of alpha electrons.
const tools::EigenSystem & MOs() const
Return read-only access to alpha/restricted molecular orbitals.
const QMMolecule & QMAtoms() const
Return read-only access to the molecular geometry.
const AOBasis & getDftBasis() const
Return the DFT AO basis, throwing if it has not been initialized.
Charge transport classes.
Provides a means for comparing floating point numbers.
std::string Format() const
Eigen::VectorXd coverage_final
Eigen::VectorXd coverage_input
std::vector< Index > added
std::vector< Index > enclosed
std::vector< Index > orbitals
std::vector< Index > cut_hydrogens
std::vector< Index > unreachable
std::vector< Index > atoms