votca 2026-dev
Loading...
Searching...
No Matches
activeregion.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2026 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Standard includes
21#include <algorithm>
22#include <iomanip>
23#include <set>
24#include <sstream>
25#include <stdexcept>
26
27// VOTCA includes
30
31// Local VOTCA includes
33#include "votca/xtp/aomatrix.h"
34
35namespace votca {
36namespace xtp {
37
38std::vector<std::vector<Index>> ActiveRegion::BondGraph(const QMMolecule& mol) {
39 tools::Elements elements;
40 const Index n = mol.size();
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");
44 }
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);
52 }
53 }
54 }
55 return graph;
56}
57
58Eigen::MatrixXd ActiveRegion::AtomPopulations(const Orbitals& orb,
59 const Eigen::MatrixXd& mos) {
60 const AOBasis& basis = orb.getDftBasis();
61 AOOverlap overlap;
62 overlap.Fill(basis);
63 // Mulliken: q_mu = c_mu (S c)_mu, summed over the functions of an atom
64 const Eigen::MatrixXd q = mos.cwiseProduct(overlap.Matrix() * mos);
65 const std::vector<Index>& per_atom = basis.getFuncPerAtom();
66 Eigen::MatrixXd pop(Index(per_atom.size()), mos.cols());
67 Index start = 0;
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();
70 start += per_atom[a];
71 }
72 return pop;
73}
74
75namespace {
76// connected components of the subgraph on `nodes`
77std::vector<std::vector<Index>> Components(
78 const std::vector<std::vector<Index>>& graph,
79 const std::vector<bool>& in_set) {
80 const Index n = Index(graph.size());
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) {
85 continue;
86 }
87 comps.emplace_back();
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();
92 stack.pop_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)];
97 stack.push_back(j);
98 }
99 }
100 }
101 }
102 return comps;
103}
104} // namespace
105
107 const std::vector<Index>& active) {
108 const auto graph = BondGraph(mol);
109 const Index n = mol.size();
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");
116 }
117 act[std::size_t(a)] = true;
118 }
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)];
123 }
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) {
130 msg << " {";
131 for (std::size_t k = 0; k < p.size(); ++k) {
132 msg << (k ? "," : "") << p[k];
133 }
134 msg << "}";
135 }
136 msg << ". Each molecule should contribute one connected piece.";
137 throw std::runtime_error(msg.str());
138 }
139 }
140}
141
143 const std::vector<Index>& active,
144 const Options& options) {
145 const QMMolecule& mol = full.QMAtoms();
146 const Index n = mol.size();
147 CheckConnected(mol, active);
148
149 // frontier orbitals of the full calculation
150 const Index homo = full.getNumberOfAlphaElectrons() - 1;
151 const Eigen::VectorXd& e = full.MOs().eigenvalues();
152 std::vector<Index> orbs;
153 for (Index k = homo; k >= 0 && e(k) >= e(homo) - options.window; --k) {
154 orbs.push_back(k);
155 }
156 if (options.include_virtuals) {
157 const Index lumo = homo + 1;
158 for (Index k = lumo; k < e.size() && e(k) <= e(lumo) + options.window;
159 ++k) {
160 orbs.push_back(k);
161 }
162 }
163 Eigen::MatrixXd mos(full.MOs().eigenvectors().rows(), Index(orbs.size()));
164 Report report;
165 report.energies.resize(Index(orbs.size()));
166 for (std::size_t k = 0; k < orbs.size(); ++k) {
167 mos.col(Index(k)) = full.MOs().eigenvectors().col(orbs[k]);
168 report.energies(Index(k)) = e(orbs[k]);
169 }
170 report.orbitals = orbs;
171 const Eigen::MatrixXd pop = AtomPopulations(full, mos);
172 // the largest population of an atom over the frontier orbitals
173 const Eigen::VectorXd maxpop = pop.rowwise().maxCoeff();
174
175 std::vector<bool> act(std::size_t(n), false);
176 for (Index a : active) {
177 act[std::size_t(a)] = true;
178 }
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();
184 }
185 }
186 return c;
187 };
188 report.coverage_input = coverage(act);
189
190 const auto graph = BondGraph(mol);
191 if (options.grow) {
192 const std::vector<bool> input = act;
193 // every atom above the threshold in a molecule that has active atoms
194 // joins, together with the shortest bond path that connects it to the
195 // region (an atom on a node of the orbital can lie on that path)
196 std::vector<bool> molecule_active(std::size_t(n), false);
197 {
198 std::vector<bool> all(std::size_t(n), true);
199 for (const auto& molecule : Components(graph, all)) {
200 bool any = false;
201 for (Index i : molecule) {
202 any = any || act[std::size_t(i)];
203 }
204 for (Index i : molecule) {
205 molecule_active[std::size_t(i)] = any;
206 }
207 }
208 }
209 bool changed = true;
210 while (changed) {
211 changed = false;
212 // breadth-first search from the region to the nearest atom above
213 // the threshold that is not yet active
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;
219 queue.push_back(i);
220 }
221 }
222 Index target = -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) {
226 continue;
227 }
228 parent[std::size_t(j)] = queue[q];
229 if (maxpop(j) > options.threshold &&
230 molecule_active[std::size_t(j)]) {
231 target = j;
232 break;
233 }
234 queue.push_back(j);
235 }
236 }
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;
240 changed = true;
241 }
242 }
243 // heavy atoms bonded to two or more active atoms (a ring atom between
244 // two active neighbours, an atom enclosed by the region)
245 changed = true;
246 while (changed) {
247 changed = false;
248 for (Index i = 0; i < n; ++i) {
249 if (act[std::size_t(i)] || mol[i].getElement() == "H") {
250 continue;
251 }
252 Index active_neighbours = 0;
253 for (Index j : graph[std::size_t(i)]) {
254 active_neighbours += act[std::size_t(j)] ? 1 : 0;
255 }
256 if (active_neighbours >= 2) {
257 act[std::size_t(i)] = true;
258 changed = true;
259 }
260 }
261 }
262 // hydrogens of active heavy atoms (also of the input ones: an X-H bond
263 // should never be cut)
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;
269 }
270 }
271 }
272 }
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);
276 }
277 }
278 }
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) {
283 report.unreachable.push_back(i);
284 }
285 }
286 if (!options.grow) {
287 report.unreachable.clear(); // only meaningful after growing
288 }
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)]) {
293 report.cut_hydrogens.push_back(i);
294 break;
295 }
296 }
297 }
298 }
299 for (Index i = 0; i < n; ++i) {
300 if (act[std::size_t(i)] || mol[i].getElement() == "H") {
301 continue;
302 }
303 Index active_neighbours = 0;
304 for (Index j : graph[std::size_t(i)]) {
305 active_neighbours += act[std::size_t(j)] ? 1 : 0;
306 }
307 if (active_neighbours >= 2) {
308 report.enclosed.push_back(i);
309 }
310 }
311 report.coverage_final = coverage(act);
312 CheckConnected(mol, report.atoms);
313 return report;
314}
315
316std::string ActiveRegion::Report::Format() const {
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
322 << std::setprecision(4) << energies(Index(k)) * tools::conv::hrt2ev
323 << " eV input " << std::setprecision(3) << coverage_input(Index(k));
324 if (!added.empty()) {
325 out << " grown " << coverage_final(Index(k));
326 }
327 out << "\n";
328 }
329 if (!added.empty()) {
330 out << " added " << added.size() << " atoms:";
331 for (Index a : added) {
332 out << " " << a;
333 }
334 out << "\n";
335 }
336 if (!cut_hydrogens.empty()) {
337 out << " warning: inactive hydrogens bonded to active atoms (X-H bonds "
338 "cut; grow adds them):";
339 for (Index a : cut_hydrogens) {
340 out << " " << a;
341 }
342 out << "\n";
343 }
344 if (!enclosed.empty()) {
345 out << " warning: inactive atoms bonded to two or more active atoms "
346 "(several bonds cut):";
347 for (Index a : enclosed) {
348 out << " " << a;
349 }
350 out << "\n";
351 }
352 if (!unreachable.empty()) {
353 out << " above the threshold in molecules without active atoms:";
354 for (Index a : unreachable) {
355 out << " " << a;
356 }
357 out << "\n";
358 }
359 return out.str();
360}
361
362} // namespace xtp
363} // namespace votca
const Eigen::VectorXd & eigenvalues() const
Definition eigensystem.h:30
const Eigen::MatrixXd & eigenvectors() const
Definition eigensystem.h:33
information about an element
Definition elements.h:42
double getCovRad(std::string name, std::string unit)
Returns the covalent Radii of the atom.
Definition elements.cc:109
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
const std::vector< Index > & getFuncPerAtom() const
Definition aobasis.h:72
void Fill(const AOBasis &aobasis) final
const Eigen::MatrixXd & Matrix() const
Definition aomatrix.h:52
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.
Definition orbitals.h:47
Index getNumberOfAlphaElectrons() const
Return the stored number of alpha electrons.
Definition orbitals.h:159
const tools::EigenSystem & MOs() const
Return read-only access to alpha/restricted molecular orbitals.
Definition orbitals.h:192
const QMMolecule & QMAtoms() const
Return read-only access to the molecular geometry.
Definition orbitals.h:262
const AOBasis & getDftBasis() const
Return the DFT AO basis, throwing if it has not been initialized.
Definition orbitals.h:372
const double hrt2ev
Definition constants.h:53
Charge transport classes.
Definition ERIs.h:28
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
std::vector< Index > enclosed
std::vector< Index > orbitals
std::vector< Index > cut_hydrogens
std::vector< Index > unreachable