votca 2026-dev
Loading...
Searching...
No Matches
libint2_calls.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2024 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 <chrono>
23#include <numeric>
24
25// Local VOTCA includes
26#include "votca/xtp/ERIs.h"
27#include "votca/xtp/aobasis.h"
29#include "votca/xtp/aomatrix.h"
32
33// include libint last otherwise it overrides eigen
35#define LIBINT2_CONSTEXPR_STATICS 0
36#if defined(__clang__)
37#pragma clang diagnostic push
38#pragma clang diagnostic ignored "-W#warnings"
39#elif defined(__GNUC__)
40#pragma GCC diagnostic push
41#pragma GCC diagnostic ignored "-Warray-bounds"
42#pragma GCC diagnostic ignored "-Wcpp"
43#endif
44#include <libint2.hpp>
45#include <libint2/statics_definition.h>
46#if defined(__clang__)
47#pragma clang diagnostic pop
48#elif defined(__GNUC__)
49#pragma GCC diagnostic pop
50#endif
51
52namespace votca {
53namespace xtp {
54
55std::vector<std::vector<Index>> AOBasis::ComputeShellPairs(
56 double threshold) const {
57
58 Index nthreads = OPENMP::getMaxThreads();
59
60 std::vector<libint2::Shell> shells = GenerateLibintBasis();
61
62 // construct the 2-electron repulsion integrals engine
63 std::vector<libint2::Engine> engines;
64 engines.reserve(nthreads);
65 engines.emplace_back(libint2::Operator::overlap, getMaxNprim(), getMaxL(), 0);
66 for (Index i = 1; i != nthreads; ++i) {
67 engines.push_back(engines[0]);
68 }
69
70 std::vector<std::vector<Index>> pairs(shells.size());
71
72#pragma omp parallel for schedule(dynamic)
73 for (Index s1 = 0; s1 < Index(shells.size()); ++s1) {
74 Index thread_id = OPENMP::getThreadId();
75
76 libint2::Engine& engine = engines[thread_id];
77 const libint2::Engine::target_ptr_vec& buf = engine.results();
78 Index n1 = shells[s1].size();
79
80 for (Index s2 = 0; s2 <= s1; ++s2) {
81 bool on_same_center = (shells[s1].O == shells[s2].O);
82 bool significant = on_same_center;
83 if (!on_same_center) {
84 Index n2 = shells[s2].size();
85 engine.compute(shells[s1], shells[s2]);
86 Eigen::Map<const Eigen::MatrixXd> buf_mat(buf[0], n1, n2);
87 significant = (buf_mat.norm() >= threshold);
88 }
89 if (significant) {
90 pairs[s1].push_back(s2);
91 }
92 }
93 std::sort(pairs[s1].begin(), pairs[s1].end());
94 }
95 return pairs;
96}
97
98using MatrixLibInt =
99 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
100
101template <libint2::Operator obtype,
102 typename OperatorParams =
103 typename libint2::operator_traits<obtype>::oper_params_type>
104std::array<MatrixLibInt, libint2::operator_traits<obtype>::nopers>
106 OperatorParams oparams = OperatorParams()) {
107
108 Index nthreads = OPENMP::getMaxThreads();
109 std::vector<libint2::Shell> shells = aobasis.GenerateLibintBasis();
110
111 std::vector<std::vector<Index>> shellpair_list = aobasis.ComputeShellPairs();
112
113 Index nopers = static_cast<Index>(libint2::operator_traits<obtype>::nopers);
114 std::array<MatrixLibInt, libint2::operator_traits<obtype>::nopers> result;
115 for (MatrixLibInt& r : result) {
116 r = MatrixLibInt::Zero(aobasis.AOBasisSize(), aobasis.AOBasisSize());
117 }
118
119 std::vector<libint2::Engine> engines(nthreads);
120 engines[0] = libint2::Engine(obtype, aobasis.getMaxNprim(),
121 static_cast<int>(aobasis.getMaxL()), 0);
122 engines[0].set_params(oparams);
123 for (Index i = 1; i < nthreads; ++i) {
124 engines[i] = engines[0];
125 }
126
127 std::vector<Index> shell2bf = aobasis.getMapToBasisFunctions();
128
129#pragma omp parallel for schedule(dynamic)
130 for (Index s1 = 0; s1 < aobasis.getNumofShells(); ++s1) {
131 Index thread_id = OPENMP::getThreadId();
132 libint2::Engine& engine = engines[thread_id];
133 const libint2::Engine::target_ptr_vec& buf = engine.results();
134
135 Index bf1 = shell2bf[s1];
136 Index n1 = shells[s1].size();
137
138 for (Index s2 : shellpair_list[s1]) {
139
140 engine.compute(shells[s1], shells[s2]);
141 if (buf[0] == nullptr) {
142 continue; // if all integrals screened out, skip to next shell set
143 }
144 Index bf2 = shell2bf[s2];
145 Index n2 = shells[s2].size();
146 for (unsigned int op = 0; op != nopers; ++op) {
147 Eigen::Map<const MatrixLibInt> buf_mat(buf[op], n1, n2);
148 result[op].block(bf1, bf2, n1, n2) = buf_mat;
149 if (s1 != s2) { // if s1 >= s2, copy {s1,s2} to the corresponding
150 // {s2,s1} block, note the transpose!
151 result[op].block(bf2, bf1, n2, n1) = buf_mat.transpose();
152 }
153 }
154 }
155 }
156 return result;
157}
158
159/***********************************
160 * OVERLAP BETWEEN TWO BASES
161 ***********************************/
162Eigen::MatrixXd ComputeMixedOverlap(const AOBasis& row, const AOBasis& col) {
163 libint2::Shell::do_enforce_unit_normalization(false);
164 const std::vector<libint2::Shell> sa = row.GenerateLibintBasis();
165 const std::vector<libint2::Shell> sb = col.GenerateLibintBasis();
166 const std::vector<Index> ba = row.getMapToBasisFunctions();
167 const std::vector<Index> bb = col.getMapToBasisFunctions();
168 Eigen::MatrixXd result =
169 Eigen::MatrixXd::Zero(row.AOBasisSize(), col.AOBasisSize());
170 const Index nthreads = OPENMP::getMaxThreads();
171 std::vector<libint2::Engine> engines(nthreads);
172 engines[0] = libint2::Engine(
173 libint2::Operator::overlap,
174 std::max(row.getMaxNprim(), col.getMaxNprim()),
175 static_cast<int>(std::max(row.getMaxL(), col.getMaxL())), 0);
176 for (Index i = 1; i < nthreads; ++i) {
177 engines[i] = engines[0];
178 }
179#pragma omp parallel for schedule(dynamic)
180 for (Index s1 = 0; s1 < Index(sa.size()); ++s1) {
181 libint2::Engine& engine = engines[OPENMP::getThreadId()];
182 const libint2::Engine::target_ptr_vec& buf = engine.results();
183 const Index n1 = Index(sa[s1].size());
184 for (Index s2 = 0; s2 < Index(sb.size()); ++s2) {
185 engine.compute(sa[s1], sb[s2]);
186 if (buf[0] == nullptr) {
187 continue;
188 }
189 const Index n2 = Index(sb[s2].size());
190 Eigen::Map<const MatrixLibInt> m(buf[0], n1, n2);
191 result.block(ba[s1], bb[s2], n1, n2) = m;
192 }
193 }
194 return result;
195}
196
197/***********************************
198 * KINETIC
199 ***********************************/
203
204/***********************************
205 * OVERLAP
206 ***********************************/
210
211Eigen::MatrixXd AOOverlap::singleShellOverlap(const AOShell& shell) const {
212 libint2::Shell::do_enforce_unit_normalization(false);
213 libint2::Operator obtype = libint2::Operator::overlap;
214 try {
215 libint2::Engine engine(obtype, shell.getSize(),
216 static_cast<int>(shell.getL()), 0);
217
218 const libint2::Engine::target_ptr_vec& buf = engine.results();
219
220 libint2::Shell s = shell.LibintShell();
221 engine.compute(s, s);
222
223 Eigen::Map<const MatrixLibInt> buf_mat(buf[0], shell.getNumFunc(),
224 shell.getNumFunc());
225 return buf_mat;
226 } catch (const libint2::Engine::lmax_exceeded& error) {
227 std::ostringstream oss;
228 oss << "\nA libint error occured:\n"
229 << error.what() << "\n"
230 << "\nYou requested a computation for a shell with angular momentum "
231 << error.lmax_requested()
232 << ",\nbut your libint package only supports angular momenta upto "
233 << error.lmax_limit() - 1 << ".\n"
234 << "\nTo fix this error you will need to reinstall libint with "
235 "support\n"
236 "for higher angular momenta. If you installed your own libint it\n"
237 "should be reconfigured and installed with the option "
238 "--with-max-am=<maxAngularMomentum>.\n"
239 "If you installed libint with your OS package manager, you will\n"
240 "need to setup you own libint installation with the \n"
241 "--with-max-am=<maxAngularMomentum> option set."
242 << std::endl;
243 throw std::runtime_error(oss.str());
244 }
245}
246
247/***********************************
248 * DIPOLE
249 ***********************************/
250void AODipole::Fill(const AOBasis& aobasis) {
251 auto results = computeOneBodyIntegrals<libint2::Operator::emultipole1,
252 std::array<libint2::Shell::real_t, 3>>(
253 aobasis, r_);
254
255 for (Index i = 0; i < 3; i++) {
256 aomatrix_[i] = results[1 + i]; // emultipole1 returns: overlap, x-dipole,
257 // y-dipole, z-dipole
258 }
259}
260
261/***********************************
262 * COULOMB
263 ***********************************/
264void AOCoulomb::Fill(const AOBasis& aobasis) {
266}
267
269 Index nthreads = OPENMP::getMaxThreads();
270 std::vector<libint2::Shell> shells = aobasis.GenerateLibintBasis();
271 std::vector<Index> shell2bf = aobasis.getMapToBasisFunctions();
272
273 aomatrix_ =
274 Eigen::MatrixXd::Zero(aobasis.AOBasisSize(), aobasis.AOBasisSize());
275
276 // build engines for each thread
277 std::vector<libint2::Engine> engines(nthreads);
278 engines[0] =
279 libint2::Engine(libint2::Operator::coulomb, aobasis.getMaxNprim(),
280 static_cast<int>(aobasis.getMaxL()), 0);
281 engines[0].set(libint2::BraKet::xs_xs);
282 for (Index i = 1; i != nthreads; ++i) {
283 engines[i] = engines[0];
284 }
285
286#pragma omp parallel for schedule(dynamic)
287 for (Index s1 = 0; s1 < aobasis.getNumofShells(); ++s1) {
288 libint2::Engine& engine = engines[OPENMP::getThreadId()];
289 const libint2::Engine::target_ptr_vec& buf = engine.results();
290
291 Index bf1 = shell2bf[s1];
292 Index n1 = shells[s1].size();
293 // cannot use shellpairs because this is still a two-center integral and
294 // overlap screening would give wrong result
295 for (Index s2 = 0; s2 <= s1; ++s2) {
296
297 engine.compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xs, 0>(
298 shells[s1], libint2::Shell::unit(), shells[s2],
299 libint2::Shell::unit());
300
301 if (buf[0] == nullptr) {
302 continue; // if all integrals screened out, skip to next shell set
303 }
304 Index bf2 = shell2bf[s2];
305 Index n2 = shells[s2].size();
306
307 Eigen::Map<const MatrixLibInt> buf_mat(buf[0], n1, n2);
308 aomatrix_.block(bf1, bf2, n1, n2) = buf_mat;
309 if (s1 != s2) { // if s1 >= s2, copy {s1,s2} to the corresponding
310 // {s2,s1} block, note the transpose!
311 aomatrix_.block(bf2, bf1, n2, n1) = buf_mat.transpose();
312 }
313 }
314 }
315}
316
317Eigen::MatrixXd ERIs::ComputeSchwarzShells(const AOBasis& basis) const {
318
319 Index noshells = basis.getNumofShells();
320
321 Eigen::MatrixXd result = Eigen::MatrixXd::Zero(noshells, noshells);
322 Index nthreads = OPENMP::getMaxThreads();
323 std::vector<libint2::Engine> engines(nthreads);
324 double epsilon = 0.0;
325 engines[0] = libint2::Engine(libint2::Operator::coulomb, basis.getMaxNprim(),
326 static_cast<int>(basis.getMaxL()), 0, epsilon);
327
328 for (Index i = 1; i < nthreads; ++i) {
329 engines[i] = engines[0];
330 }
331
332 std::vector<libint2::Shell> shells = basis.GenerateLibintBasis();
333
334#pragma omp parallel for schedule(dynamic)
335 for (Index s1 = 0l; s1 < basis.getNumofShells(); ++s1) {
336 Index thread_id = OPENMP::getThreadId();
337 libint2::Engine& engine = engines[thread_id];
338 const libint2::Engine::target_ptr_vec& buf = engine.results();
339 Index n1 = shells[s1].size();
340
341 for (Index s2 = 0l; s2 <= s1; ++s2) {
342 Index n2 = shells[s2].size();
343 Index n12 = n1 * n2;
344
345 engines[thread_id]
346 .compute2<libint2::Operator::coulomb, libint2::BraKet::xx_xx, 0>(
347 shells[s1], shells[s2], shells[s1], shells[s2]);
348
349 Eigen::Map<const MatrixLibInt> buf_mat(buf[0], n12, n12);
350
351 result(s2, s1) = std::sqrt(buf_mat.cwiseAbs().maxCoeff());
352 }
353 }
354 return result.selfadjointView<Eigen::Upper>();
355}
356
357template <bool with_exchange>
358std::array<Eigen::MatrixXd, 2> ERIs::Compute4c(const Eigen::MatrixXd& dmat,
359 double error) const {
360 assert(schwarzscreen_.rows() > 0 && schwarzscreen_.cols() > 0 &&
361 "Please call Initialize_4c before running this");
362 Index nthreads = OPENMP::getMaxThreads();
363
364 Eigen::MatrixXd hartree = Eigen::MatrixXd::Zero(dmat.rows(), dmat.cols());
365 Eigen::MatrixXd exchange;
366 if (with_exchange) {
367 exchange = Eigen::MatrixXd::Zero(dmat.rows(), dmat.cols());
368 }
369 Eigen::MatrixXd dnorm_block = ComputeShellBlockNorm(dmat);
370 double fock_precision = error;
371 // engine precision controls primitive truncation, assume worst-case scenario
372 // (all primitive combinations add up constructively)
373 Index max_nprim4 = maxnprim_ * maxnprim_ * maxnprim_ * maxnprim_;
374 double engine_precision = std::min(fock_precision / dnorm_block.maxCoeff(),
375 std::numeric_limits<double>::epsilon()) /
376 double(max_nprim4);
377 std::vector<libint2::Engine> engines(nthreads);
378 engines[0] = libint2::Engine(libint2::Operator::coulomb, int(maxnprim_),
379 int(maxL_), 0);
380 engines[0].set_precision(engine_precision); // shellset-dependent precision
381 // control will likely break
382 // positive definiteness
383 // stick with this simple recipe
384 for (Index i = 1; i < nthreads; ++i) {
385 engines[i] = engines[0];
386 }
387 Index nshells = basis_.size();
388
389#pragma omp parallel for schedule(dynamic) reduction(+ : hartree) \
390 reduction(+ : exchange)
391 for (Index s1 = 0; s1 < nshells; ++s1) {
392 Index thread_id = OPENMP::getThreadId();
393 libint2::Engine& engine = engines[thread_id];
394 const auto& buf = engine.results();
395 Index start_1 = starts_[s1];
396 const libint2::Shell& shell1 = basis_[s1];
397 Index n1 = shell1.size();
398
399 auto sp12_iter = shellpairdata_[s1].begin();
400 for (Index s2 : shellpairs_[s1]) {
401 Index start_2 = starts_[s2];
402 const libint2::Shell& shell2 = basis_[s2];
403 Index n2 = shell2.size();
404 double dnorm_12 = dnorm_block(s1, s2);
405 const libint2::ShellPair* sp12 = &(*sp12_iter);
406 ++sp12_iter;
407
408 for (Index s3 = 0; s3 <= s1; ++s3) {
409
410 Index start_3 = starts_[s3];
411 const libint2::Shell& shell3 = basis_[s3];
412 Index n3 = shell3.size();
413 auto sp34_iter = shellpairdata_[s3].begin();
414 double dnorm_123 = std::max(dnorm_block(s1, s3),
415 std::max(dnorm_block(s2, s3), dnorm_12));
416 Index s4max = (s1 == s3) ? s2 : s3;
417 for (Index s4 : shellpairs_[s3]) {
418 if (s4 > s4max) {
419 break;
420 } // for each s3, s4 are stored in monotonically increasing
421 // order
422
423 const libint2::ShellPair* sp34 = &(*sp34_iter);
424 // must update the iter even if going to skip s4
425 ++sp34_iter;
426 double dnorm_1234 =
427 std::max(dnorm_block(s1, s4),
428 std::max(dnorm_block(s2, s4),
429 std::max(dnorm_block(s3, s4), dnorm_123)));
430
431 if (dnorm_1234 * schwarzscreen_(s1, s2) * schwarzscreen_(s3, s4) <
432 fock_precision) {
433 continue;
434 }
435
436 const libint2::Shell& shell4 = basis_[s4];
437 engine
438 .compute2<libint2::Operator::coulomb, libint2::BraKet::xx_xx, 0>(
439 shell1, shell2, shell3, shell4, sp12, sp34);
440 const auto* buf_1234 = buf[0];
441 if (buf_1234 == nullptr) {
442 continue; // if all integrals screened out, skip to next quartet
443 }
444 Index start_4 = starts_[s4];
445 Index n4 = shell4.size();
446 Index s12_deg = (s1 == s2) ? 1 : 2;
447 Index s34_deg = (s3 == s4) ? 1 : 2;
448 Index s12_34_deg = (s1 == s3) ? (s2 == s4 ? 1 : 2) : 2;
449 Index s1234_deg = s12_deg * s34_deg * s12_34_deg;
450
451 for (Index f1 = 0, f1234 = 0; f1 != n1; ++f1) {
452 const Index bf1 = f1 + start_1;
453 for (Index f2 = 0; f2 != n2; ++f2) {
454 const Index bf2 = f2 + start_2;
455 for (Index f3 = 0; f3 != n3; ++f3) {
456 const Index bf3 = f3 + start_3;
457 for (Index f4 = 0; f4 != n4; ++f4, ++f1234) {
458 const Index bf4 = f4 + start_4;
459
460 const double value = buf_1234[f1234];
461
462 const double value_scal_by_deg = value * double(s1234_deg);
463
464 hartree(bf1, bf2) += dmat(bf3, bf4) * value_scal_by_deg;
465 hartree(bf3, bf4) += dmat(bf1, bf2) * value_scal_by_deg;
466 if (with_exchange) {
467 exchange(bf1, bf3) -= dmat(bf2, bf4) * value_scal_by_deg;
468 exchange(bf2, bf3) -= dmat(bf1, bf4) * value_scal_by_deg;
469 exchange(bf2, bf4) -= dmat(bf1, bf3) * value_scal_by_deg;
470 exchange(bf1, bf4) -= dmat(bf2, bf3) * value_scal_by_deg;
471 }
472 }
473 }
474 }
475 }
476 }
477 }
478 }
479 }
480 std::array<Eigen::MatrixXd, 2> result2;
481 // 0.25=0.5(symmetrisation)*0.5(our dmat has a factor 2)
482 result2[0] = 0.25 * (hartree + hartree.transpose());
483 if (with_exchange) {
484 // prefactor
485 result2[1] = 0.125 * (exchange + exchange.transpose());
486 }
487 return result2;
488}
489
490template std::array<Eigen::MatrixXd, 2> ERIs::Compute4c<true>(
491 const Eigen::MatrixXd& dmat, double error) const;
492template std::array<Eigen::MatrixXd, 2> ERIs::Compute4c<false>(
493 const Eigen::MatrixXd& dmat, double error) const;
494
495std::vector<std::vector<Index>> TCMatrix_dft::SignificantPairs(
496 const AOBasis& dftbasis, const Eigen::MatrixXd& metric,
497 double pair_threshold) const {
498 const Index nshells = dftbasis.getNumofShells();
499 std::vector<std::vector<Index>> kept(nshells);
500 if (pair_threshold <= 0.0) {
501 for (Index a = 0; a < nshells; ++a) {
502 kept[a].resize(a + 1);
503 std::iota(kept[a].begin(), kept[a].end(), Index(0));
504 }
505 return kept;
506 }
507 // |(P|ab)| <= sqrt((P|P)) sqrt((ab|ab))
508 const double max_aux = std::sqrt(metric.diagonal().cwiseAbs().maxCoeff());
509 const Index nthreads = OPENMP::getMaxThreads();
510 std::vector<libint2::Engine> engines(nthreads);
511 engines[0] =
512 libint2::Engine(libint2::Operator::coulomb, dftbasis.getMaxNprim(),
513 static_cast<int>(dftbasis.getMaxL()), 0, 0.0);
514 for (Index i = 1; i < nthreads; ++i) {
515 engines[i] = engines[0];
516 }
517 const std::vector<libint2::Shell> shells = dftbasis.GenerateLibintBasis();
518#pragma omp parallel for schedule(dynamic)
519 for (Index a = nshells - 1; a >= 0; --a) {
520 libint2::Engine& engine = engines[OPENMP::getThreadId()];
521 const libint2::Engine::target_ptr_vec& buf = engine.results();
522 for (Index b = 0; b < a; ++b) {
523 engine.compute2<libint2::Operator::coulomb, libint2::BraKet::xx_xx, 0>(
524 shells[a], shells[b], shells[a], shells[b]);
525 if (buf[0] == nullptr) {
526 continue;
527 }
528 const Index n = Index(shells[a].size() * shells[b].size());
529 const double schwarz =
530 std::sqrt(Eigen::Map<const Eigen::MatrixXd>(buf[0], n, n)
531 .cwiseAbs()
532 .maxCoeff());
533 if (max_aux * schwarz >= pair_threshold) {
534 kept[a].push_back(b);
535 }
536 }
537 kept[a].push_back(a); // diagonal shell pairs are always kept
538 }
539 return kept;
540}
541
542void TCMatrix_dft::Fill(const AOBasis& auxbasis, const AOBasis& dftbasis,
543 double pair_threshold, Index build_columns) {
544 std::vector<std::vector<Index>> kept;
545 {
546 auto metric_start = std::chrono::steady_clock::now();
547 AOCoulomb auxAOcoulomb;
548 auxAOcoulomb.Fill(auxbasis);
549 inv_sqrt_ = auxAOcoulomb.Pseudo_InvSqrt(1e-8);
550 removedfunctions_ = auxAOcoulomb.Removedfunctions();
551 metric_seconds_ = std::chrono::duration<double>(
552 std::chrono::steady_clock::now() - metric_start)
553 .count();
554 kept = SignificantPairs(dftbasis, auxAOcoulomb.Matrix(), pair_threshold);
555 }
556 SetupLayout(dftbasis, kept);
557 const Index naux = auxbasis.AOBasisSize();
558 // every stored entry is written below
559 data_.resize(
560 Index(runs_.empty() ? 0 : runs_.back().offset + runs_.back().length),
561 naux);
562
563 const std::vector<Index> shell2bf = dftbasis.getMapToBasisFunctions();
564 const std::vector<Index> auxshell2bf = auxbasis.getMapToBasisFunctions();
565 const std::vector<libint2::Shell> dftshells = dftbasis.GenerateLibintBasis();
566 const std::vector<libint2::Shell> auxshells = auxbasis.GenerateLibintBasis();
567
568 // Work unit: shell a with the kept shells kept[a][first, last). Its pair
569 // columns are built for all aux functions at once, at most about
570 // build_columns of them, which bounds the scratch memory per thread.
571 struct Unit {
572 Index a;
573 Index first;
574 Index last;
575 };
576 std::vector<Unit> units;
577 Index max_rows = 0;
578 for (Index a = dftbasis.getNumofShells() - 1; a >= 0; --a) {
579 const Index na = Index(dftshells[a].size());
580 Index first = 0;
581 while (first < Index(kept[a].size())) {
582 Index last = first;
583 Index rows = 0;
584 while (last < Index(kept[a].size()) &&
585 (last == first ||
586 rows + na * Index(dftshells[kept[a][last]].size()) <=
587 build_columns)) {
588 rows += na * Index(dftshells[kept[a][last]].size());
589 ++last;
590 }
591 max_rows = std::max(max_rows, rows);
592 units.push_back({a, first, last});
593 first = last;
594 }
595 }
596
597 const Index nthreads = OPENMP::getMaxThreads();
598 std::vector<libint2::Engine> engines(nthreads);
599 engines[0] = libint2::Engine(
600 libint2::Operator::coulomb,
601 std::max(dftbasis.getMaxNprim(), auxbasis.getMaxNprim()),
602 static_cast<int>(std::max(dftbasis.getMaxL(), auxbasis.getMaxL())), 0);
603 engines[0].set(libint2::BraKet::xs_xx);
604 for (Index i = 1; i < nthreads; ++i) {
605 engines[i] = engines[0];
606 }
607
608#pragma omp parallel
609 {
610 libint2::Engine& engine = engines[OPENMP::getThreadId()];
611 const libint2::Engine::target_ptr_vec& buf = engine.results();
612 // rows: pair columns (nu, mu) of the unit, columns: aux functions
613 Eigen::MatrixXd raw(max_rows, naux);
614 Eigen::MatrixXd transformed(max_rows, naux);
615 std::vector<Index> row_start;
616 std::vector<Index> chunk_pos;
617
618#pragma omp for schedule(dynamic)
619 for (Index u = 0; u < Index(units.size()); ++u) {
620 const Unit& unit = units[u];
621 const libint2::Shell& shell_a = dftshells[unit.a];
622 const Index na = Index(shell_a.size());
623 const Index start_a = shell2bf[unit.a];
624 const bool has_diagonal = kept[unit.a][unit.last - 1] == unit.a;
625
626 // position of each kept shell in the unit's part of a column
627 chunk_pos.assign(1, 0);
628 for (Index k = unit.first; k < unit.last; ++k) {
629 chunk_pos.push_back(chunk_pos.back() +
630 Index(dftshells[kept[unit.a][k]].size()));
631 }
632 const Index width = chunk_pos.back();
633 // column mu = start_a + i holds width rows, fewer on the diagonal
634 row_start.assign(1, 0);
635 for (Index i = 0; i < na; ++i) {
636 const Index len = has_diagonal ? width - (na - i - 1) : width;
637 row_start.push_back(row_start.back() + len);
638 }
639 const Index rows = row_start.back();
640 raw.topRows(rows).setZero();
641
642 for (Index aux = 0; aux < Index(auxshells.size()); ++aux) {
643 const libint2::Shell& auxshell = auxshells[aux];
644 const Index aux_start = auxshell2bf[aux];
645 for (Index k = unit.first; k < unit.last; ++k) {
646 const Index b = kept[unit.a][k];
647 const libint2::Shell& shell_b = dftshells[b];
648 engine
649 .compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xx, 0>(
650 auxshell, libint2::Shell::unit(), shell_a, shell_b);
651 if (buf[0] == nullptr) {
652 continue;
653 }
654 Eigen::TensorMap<
655 Eigen::Tensor<const double, 3, Eigen::RowMajor> const>
656 result(buf[0], auxshell.size(), shell_a.size(), shell_b.size());
657 const Index nb = Index(shell_b.size());
658 const Index pos = chunk_pos[k - unit.first];
659 for (Index i = 0; i < na; ++i) {
660 // nu <= mu
661 const Index ncol = (b == unit.a) ? std::min(nb, i + 1) : nb;
662 for (Index c = 0; c < ncol; ++c) {
663 for (Index f = 0; f < Index(auxshell.size()); ++f) {
664 raw(row_start[i] + pos + c, aux_start + f) = result(f, i, c);
665 }
666 }
667 }
668 }
669 }
670
671 // B_P = sum_Q V^-1/2_PQ (Q|..)
672 transformed.topRows(rows).noalias() =
673 raw.topRows(rows) * inv_sqrt_.transpose();
674 // unit rows of column mu continue the column after the shells before
675 Index before = 0;
676 for (Index k = 0; k < unit.first; ++k) {
677 before += Index(dftshells[kept[unit.a][k]].size());
678 }
679 for (Index i = 0; i < na; ++i) {
680 const Index len = row_start[i + 1] - row_start[i];
681 data_.block(col_offset_[start_a + i] + before, 0, len, naux) =
682 transformed.middleRows(row_start[i], len);
683 }
684 }
685 }
686}
687
688/*
689 * Determines the 3-center integrals for a given shell in the aux basis
690 * by calculating the 3-center repulsion integral of the functions in the
691 * aux shell with ALL functions in the DFT basis set
692 */
693std::vector<Eigen::MatrixXd> ComputeAO3cBlock(const libint2::Shell& auxshell,
694 const AOBasis& dftbasis,
695 libint2::Engine& engine) {
696 std::vector<Eigen::MatrixXd> ao3c = std::vector<Eigen::MatrixXd>(
697 auxshell.size(),
698 Eigen::MatrixXd::Zero(dftbasis.AOBasisSize(), dftbasis.AOBasisSize()));
699
700 std::vector<libint2::Shell> dftshells = dftbasis.GenerateLibintBasis();
701 std::vector<Index> shell2bf = dftbasis.getMapToBasisFunctions();
702
703 const libint2::Engine::target_ptr_vec& buf = engine.results();
704 // alpha-loop over the "left" DFT basis function
705 for (Index row = 0; row < Index(dftshells.size()); row++) {
706
707 const libint2::Shell& shell_row = dftshells[row];
708 const Index row_start = shell2bf[row];
709 // ThreecMatrix is symmetric, restrict explicit calculation to triangular
710 // matrix
711 for (Index col = 0; col <= row; col++) {
712 const libint2::Shell& shell_col = dftshells[col];
713 const Index col_start = shell2bf[col];
714
715 engine.compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xx, 0>(
716 auxshell, libint2::Shell::unit(), shell_col, shell_row);
717
718 if (buf[0] == nullptr) {
719 continue;
720 }
721 Eigen::TensorMap<Eigen::Tensor<const double, 3, Eigen::RowMajor> const>
722 result(buf[0], auxshell.size(), shell_col.size(), shell_row.size());
723
724 for (size_t aux_c = 0; aux_c < auxshell.size(); aux_c++) {
725 for (size_t col_c = 0; col_c < shell_col.size(); col_c++) {
726 for (size_t row_c = 0; row_c < shell_row.size(); row_c++) {
727 // ao3c is col major result is row-major so this is ideal
728 ao3c[aux_c](row_start + row_c, col_start + col_c) =
729 result(aux_c, col_c, row_c);
730 } // ROW copy
731 } // COL copy
732 } // AUX copy
733
734 } // gamma-loop
735 } // alpha-loop
736
737 for (Eigen::MatrixXd& mat : ao3c) {
738 mat.triangularView<Eigen::Upper>() =
739 mat.triangularView<Eigen::Lower>().transpose();
740 }
741 return ao3c;
742}
743
744namespace {
745// libint2 shells of a Gaussian charge (s) and a Gaussian dipole (cartesian
746// p) of exponent beta at pos, and the factors that turn the L2-normalized
747// primitives libint2 makes of them into the unit charge g (beta/pi)^3/2
748// exp(-beta r^2) and the unit dipole density 2 beta (r - R) g:
749// s: N_s = (2 beta/pi)^3/4, charge N_s (pi/beta)^3/2;
750// p: N_p = (2 beta/pi)^3/4 2 sqrt(beta).
751struct GaussianSiteShells {
752 libint2::Shell s;
753 libint2::Shell p;
754 double s_factor;
755 double p_factor;
756 GaussianSiteShells(const Eigen::Vector3d& pos, double width) {
757 const double beta = 1.0 / (width * width);
758 const std::array<double, 3> O = {pos.x(), pos.y(), pos.z()};
759 s = libint2::Shell{{beta}, {{0, false, {1.0}}}, O};
760 p = libint2::Shell{{beta}, {{1, false, {1.0}}}, O};
761 const double n_s = std::pow(2.0 * beta / M_PI, 0.75);
762 s_factor = 1.0 / (n_s * std::pow(M_PI / beta, 1.5));
763 const double n_p = n_s * 2.0 * std::sqrt(beta);
764 p_factor = 2.0 * beta * std::pow(beta / M_PI, 1.5) / n_p;
765 }
766};
767
768std::vector<libint2::Engine> GaussianSiteEngines(const AOBasis& basis) {
769 std::vector<libint2::Engine> engines(OPENMP::getMaxThreads());
770 engines[0] = libint2::Engine(libint2::Operator::coulomb,
771 int(std::max<Index>(basis.getMaxNprim(), 1)),
772 int(std::max<Index>(basis.getMaxL(), 1)), 0);
773 engines[0].set(libint2::BraKet::xs_xx);
774 for (std::size_t i = 1; i < engines.size(); ++i) {
775 engines[i] = engines[0];
776 }
777 return engines;
778}
779} // namespace
780
781Eigen::MatrixXd AOGaussianSites::Potential(const AOBasis& basis,
782 const std::vector<Source>& sources) {
783 const Index nao = basis.AOBasisSize();
784 Eigen::MatrixXd V = Eigen::MatrixXd::Zero(nao, nao);
785 for (const Source& src : sources) {
786 if (!(src.width > 0.0)) {
787 throw std::runtime_error(
788 "AOGaussianSites: Gaussian sites need a positive width");
789 }
790 }
791 std::vector<libint2::Engine> engines = GaussianSiteEngines(basis);
792#pragma omp parallel for schedule(dynamic) reduction(+ : V)
793 for (Index k = 0; k < Index(sources.size()); ++k) {
794 const Source& src = sources[std::size_t(k)];
795 const GaussianSiteShells sh(src.pos, src.width);
796 libint2::Engine& engine = engines[OPENMP::getThreadId()];
797 if (src.charge != 0.0) {
798 V +=
799 (src.charge * sh.s_factor) * ComputeAO3cBlock(sh.s, basis, engine)[0];
800 }
801 if (src.dipole.squaredNorm() > 0.0) {
802 std::vector<Eigen::MatrixXd> p = ComputeAO3cBlock(sh.p, basis, engine);
803 for (Index c = 0; c < 3; ++c) {
804 V += (src.dipole(c) * sh.p_factor) * p[std::size_t(c)];
805 }
806 }
807 }
808 return V;
809}
810
812 const AOBasis& basis, const Eigen::MatrixXd& D,
813 const std::vector<Eigen::Vector3d>& points,
814 const std::vector<double>& widths) {
815 if (widths.size() != points.size()) {
816 throw std::runtime_error(
817 "AOGaussianSites: " + std::to_string(widths.size()) + " widths for " +
818 std::to_string(points.size()) + " points");
819 }
820 const Index n = Index(points.size());
821 for (double w : widths) {
822 if (!(w > 0.0)) {
823 throw std::runtime_error(
824 "AOGaussianSites: Gaussian sites need a positive width");
825 }
826 }
827 Eigen::MatrixXd result = Eigen::MatrixXd::Zero(n, 4);
828 std::vector<libint2::Engine> engines = GaussianSiteEngines(basis);
829#pragma omp parallel for schedule(dynamic)
830 for (Index k = 0; k < n; ++k) {
831 const GaussianSiteShells sh(points[std::size_t(k)], widths[std::size_t(k)]);
832 libint2::Engine& engine = engines[OPENMP::getThreadId()];
833 result(k, 0) =
834 sh.s_factor *
835 ComputeAO3cBlock(sh.s, basis, engine)[0].cwiseProduct(D).sum();
836 // d/dR (mn|g_R) = (mn| 2 beta (r - R) g_R): the dipole density
837 std::vector<Eigen::MatrixXd> p = ComputeAO3cBlock(sh.p, basis, engine);
838 for (Index c = 0; c < 3; ++c) {
839 result(k, 1 + c) = sh.p_factor * p[std::size_t(c)].cwiseProduct(D).sum();
840 }
841 }
842 return result;
843}
844
845namespace {
846// L * A * R evaluated in the cheaper order. For the AO->MO transformation of
847// the three-centre integrals L = C_n^T (n x N), A = (mu nu|P) (N x N) and
848// R = C_m (N x m) with m < n, so A * R first saves a factor of about n/m on
849// the dominant product.
850Eigen::MatrixXd MultiplyLeftRight(const Eigen::MatrixXd& L,
851 const Eigen::MatrixXd& A,
852 const Eigen::MatrixXd& R) {
853 const double left_first =
854 double(L.rows()) * double(A.rows()) * double(A.cols()) +
855 double(L.rows()) * double(A.cols()) * double(R.cols());
856 const double right_first =
857 double(A.rows()) * double(A.cols()) * double(R.cols()) +
858 double(L.rows()) * double(A.rows()) * double(R.cols());
859 if (right_first < left_first) {
860 return L * (A * R);
861 }
862 return (L * A) * R;
863}
864} // namespace
865
866void TCMatrix_gwbse::Fill3cMO(const AOBasis& auxbasis, const AOBasis& dftbasis,
867 const Eigen::MatrixXd& dft_orbitals) {
868
869 // excluded holes (TCMatrix_gwbse::setExcludedHoles) have no slices
870 const Index skip = std::min(excluded_holes_, mtotal_);
871 const Eigen::MatrixXd dftm =
872 dft_orbitals.middleCols(mmin_ + skip, mtotal_ - skip);
873 const Eigen::MatrixXd dftn =
874 dft_orbitals.middleCols(nmin_, ntotal_).transpose();
875
876 Index nthreads = OPENMP::getMaxThreads();
877
878 std::vector<libint2::Shell> auxshells = auxbasis.GenerateLibintBasis();
879 std::vector<libint2::Engine> engines(nthreads);
880 engines[0] = libint2::Engine(
881 libint2::Operator::coulomb,
882 std::max(dftbasis.getMaxNprim(), auxbasis.getMaxNprim()),
883 static_cast<int>(std::max(dftbasis.getMaxL(), auxbasis.getMaxL())), 0);
884 engines[0].set(libint2::BraKet::xs_xx);
885 for (Index i = 1; i < nthreads; ++i) {
886 engines[i] = engines[0];
887 }
888 std::vector<Index> auxshell2bf = auxbasis.getMapToBasisFunctions();
889
890#pragma omp parallel
891 {
892 Index threadid = OPENMP::getThreadId();
893#pragma omp for schedule(dynamic)
894 for (Index aux = 0; aux < Index(auxshells.size()); aux++) {
895 const libint2::Shell& auxshell = auxshells[aux];
896
897 std::vector<Eigen::MatrixXd> ao3c =
898 ComputeAO3cBlock(auxshell, dftbasis, engines[threadid]);
899
900 // AO->MO transformation of each function of the aux shell, written
901 // straight into its column of every m-slice. Each aux function is a
902 // different column, so the threads write to disjoint memory.
903 const Index dim = static_cast<Index>(ao3c.size());
904 const Index aux0 = auxshell2bf[aux];
905 for (Index k = 0; k < dim; ++k) {
906 ao3c[k] = MultiplyLeftRight(dftn, ao3c[k], dftm);
907 for (Index m_level = skip; m_level < mtotal_; m_level++) {
908 matrix_[m_level].col(aux0 + k) = ao3c[k].col(m_level - skip);
909 }
910 ao3c[k].resize(0, 0);
911 }
912 } // shells of GW basis set
913 }
914}
915
916// EnvironmentScreening: the two functions that need libint2 engines live
917// here, so that environmentscreening.cc does not include libint2.hpp (a
918// single include of it takes 2-5 GB to compile, depending on the libint2
919// version).
920
922 const AOBasis& auxbasis, const std::vector<Eigen::Vector3d>& points,
923 const std::vector<double>& widths) {
924 if (!widths.empty() && widths.size() != points.size()) {
925 throw std::runtime_error("EnvironmentScreening::AuxFieldAtPoints: " +
926 std::to_string(widths.size()) + " widths for " +
927 std::to_string(points.size()) + " points.");
928 }
929
930 // Same shells, same function offsets, same normalization as
931 // TCMatrix_gwbse::Fill3cMO -- which is what makes the rows of F line up
932 // with the auxiliary index of the stored three-centre integrals.
933 const std::vector<libint2::Shell> shells = auxbasis.GenerateLibintBasis();
934 const std::vector<Index> shell2bf = auxbasis.getMapToBasisFunctions();
935 const Index n_points = Index(points.size());
936
937 Eigen::MatrixXd F =
938 Eigen::MatrixXd::Zero(auxbasis.AOBasisSize(), 3 * n_points);
939 if (n_points == 0 || shells.empty()) {
940 return F;
941 }
942
943 const Index nthreads = OPENMP::getMaxThreads();
944 std::vector<libint2::Engine> engines(nthreads);
945 engines[0] =
946 libint2::Engine(libint2::Operator::nuclear, int(auxbasis.getMaxNprim()),
947 int(auxbasis.getMaxL()), 0);
948 for (Index i = 1; i < nthreads; ++i) {
949 engines[i] = engines[0];
950 }
951 // Smeared sites: (chi_Q | g) with g a single s primitive, as the
952 // auxiliary metric is computed (xs_xs, unit shells on the other side).
953 std::vector<libint2::Engine> coulomb(nthreads);
954 coulomb[0] =
955 libint2::Engine(libint2::Operator::coulomb, int(auxbasis.getMaxNprim()),
956 int(auxbasis.getMaxL()), 0);
957 coulomb[0].set(libint2::BraKet::xs_xs);
958 for (Index i = 1; i < nthreads; ++i) {
959 coulomb[i] = coulomb[0];
960 }
961
962 // Four-point central difference for d/dC, error O(h^4):
963 // f'(C) = [-f(C+2h) + 8 f(C+h) - 8 f(C-h) + f(C-2h)] / (12 h)
964 const double h = 1e-3;
965 const std::array<double, 4> offset = {2.0 * h, h, -h, -2.0 * h};
966 const std::array<double, 4> weight = {-1.0 / (12.0 * h), 8.0 / (12.0 * h),
967 -8.0 / (12.0 * h), 1.0 / (12.0 * h)};
968
969 // Sign. libint2's nuclear operator with a unit charge at C is
970 // -1/|r - C|, so the integral against Shell::unit() is -phi_P(C), minus
971 // the potential of the density chi_P at C (verified against the closed
972 // form for an s-Gaussian). The field is E = -grad_C phi = +grad_C of
973 // that integral, so the stencil is applied to the raw integral as is.
974#pragma omp parallel for schedule(dynamic)
975 for (Index p = 0; p < n_points; ++p) {
976 const double width = widths.empty() ? 0.0 : widths[std::size_t(p)];
977 if (width > 0.0) {
978 // Field of chi_Q averaged over g: E = -grad_C (chi_Q | g_C), with
979 // (chi_Q | g_C) the potential of chi_Q averaged over g (positive
980 // for a positive chi_Q), so the stencil is applied with a minus.
981 // libint2 normalizes the primitive to unit L2 norm, N = (2b/pi)^3/4;
982 // dividing by its charge N (pi/b)^3/2 makes it a unit charge.
983 libint2::Engine& engine = coulomb[OPENMP::getThreadId()];
984 const libint2::Engine::target_ptr_vec& buf = engine.results();
985 const double beta = 1.0 / (width * width);
986 const double charge =
987 std::pow(2.0 * beta / M_PI, 0.75) * std::pow(M_PI / beta, 1.5);
988 for (Index k = 0; k < 3; ++k) {
989 for (std::size_t s = 0; s < offset.size(); ++s) {
990 Eigen::Vector3d C = points[std::size_t(p)];
991 C(k) += offset[s];
992 const libint2::Shell g{
993 {beta}, {{0, false, {1.0}}}, {{C.x(), C.y(), C.z()}}};
994 for (std::size_t sh = 0; sh < shells.size(); ++sh) {
995 engine.compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xs,
996 0>(shells[sh], libint2::Shell::unit(), g,
997 libint2::Shell::unit());
998 if (buf[0] == nullptr) {
999 continue;
1000 }
1001 const Index start = shell2bf[sh];
1002 for (std::size_t f = 0; f < shells[sh].size(); ++f) {
1003 F(start + Index(f), 3 * p + k) -= weight[s] * buf[0][f] / charge;
1004 }
1005 }
1006 }
1007 }
1008 continue;
1009 }
1010 libint2::Engine& engine = engines[OPENMP::getThreadId()];
1011 const libint2::Engine::target_ptr_vec& buf = engine.results();
1012 for (Index k = 0; k < 3; ++k) {
1013 for (std::size_t s = 0; s < offset.size(); ++s) {
1014 Eigen::Vector3d C = points[std::size_t(p)];
1015 C(k) += offset[s];
1016 std::vector<std::pair<double, std::array<double, 3>>> charge{
1017 {1.0, {C.x(), C.y(), C.z()}}};
1018 engine.set_params(charge);
1019 for (std::size_t sh = 0; sh < shells.size(); ++sh) {
1020 engine.compute(shells[sh], libint2::Shell::unit());
1021 if (buf[0] == nullptr) {
1022 continue; // screened out by libint2: exactly zero
1023 }
1024 const Index start = shell2bf[sh];
1025 for (std::size_t f = 0; f < shells[sh].size(); ++f) {
1026 F(start + Index(f), 3 * p + k) += weight[s] * buf[0][f];
1027 }
1028 }
1029 }
1030 }
1031 }
1032 return F;
1033}
1034
1035// Net charge of every auxiliary function, integral chi_Q d^3r: an overlap
1036// integral against the unit shell, in the order and normalization of the
1037// three-centre integrals (same shells, same offsets).
1039 const AOBasis& auxbasis, const std::vector<Eigen::Vector3d>& points,
1040 const std::vector<double>& widths) {
1041 if (widths.size() != points.size()) {
1042 throw std::runtime_error("EnvironmentScreening::AuxPotentialAtPoints: " +
1043 std::to_string(widths.size()) + " widths for " +
1044 std::to_string(points.size()) + " points.");
1045 }
1046 const std::vector<libint2::Shell> shells = auxbasis.GenerateLibintBasis();
1047 const std::vector<Index> shell2bf = auxbasis.getMapToBasisFunctions();
1048 const Index n_points = Index(points.size());
1049 Eigen::MatrixXd P = Eigen::MatrixXd::Zero(auxbasis.AOBasisSize(), n_points);
1050 if (n_points == 0 || shells.empty()) {
1051 return P;
1052 }
1053 const Index nthreads = OPENMP::getMaxThreads();
1054 std::vector<libint2::Engine> engines(nthreads);
1055 engines[0] =
1056 libint2::Engine(libint2::Operator::nuclear, int(auxbasis.getMaxNprim()),
1057 int(auxbasis.getMaxL()), 0);
1058 std::vector<libint2::Engine> coulomb(nthreads);
1059 coulomb[0] =
1060 libint2::Engine(libint2::Operator::coulomb, int(auxbasis.getMaxNprim()),
1061 int(auxbasis.getMaxL()), 0);
1062 coulomb[0].set(libint2::BraKet::xs_xs);
1063 for (Index i = 1; i < nthreads; ++i) {
1064 engines[i] = engines[0];
1065 coulomb[i] = coulomb[0];
1066 }
1067 // Signs and normalization as in AuxFieldAtPoints: the nuclear integral
1068 // against Shell::unit() with a unit charge at C is -phi_Q(C); (chi_Q|g_C)
1069 // divided by the charge of libint2's L2-normalized primitive is +phi_Q
1070 // averaged over the unit Gaussian g_C.
1071#pragma omp parallel for schedule(dynamic)
1072 for (Index p = 0; p < n_points; ++p) {
1073 const Eigen::Vector3d& C = points[std::size_t(p)];
1074 const double width = widths[std::size_t(p)];
1075 if (width > 0.0) {
1076 libint2::Engine& engine = coulomb[OPENMP::getThreadId()];
1077 const libint2::Engine::target_ptr_vec& buf = engine.results();
1078 const double beta = 1.0 / (width * width);
1079 const double charge =
1080 std::pow(2.0 * beta / M_PI, 0.75) * std::pow(M_PI / beta, 1.5);
1081 const libint2::Shell g{
1082 {beta}, {{0, false, {1.0}}}, {{C.x(), C.y(), C.z()}}};
1083 for (std::size_t sh = 0; sh < shells.size(); ++sh) {
1084 engine.compute2<libint2::Operator::coulomb, libint2::BraKet::xs_xs, 0>(
1085 shells[sh], libint2::Shell::unit(), g, libint2::Shell::unit());
1086 if (buf[0] == nullptr) {
1087 continue;
1088 }
1089 for (std::size_t f = 0; f < shells[sh].size(); ++f) {
1090 P(shell2bf[sh] + Index(f), p) = buf[0][f] / charge;
1091 }
1092 }
1093 continue;
1094 }
1095 libint2::Engine& engine = engines[OPENMP::getThreadId()];
1096 const libint2::Engine::target_ptr_vec& buf = engine.results();
1097 std::vector<std::pair<double, std::array<double, 3>>> charge{
1098 {1.0, {C.x(), C.y(), C.z()}}};
1099 engine.set_params(charge);
1100 for (std::size_t sh = 0; sh < shells.size(); ++sh) {
1101 engine.compute(shells[sh], libint2::Shell::unit());
1102 if (buf[0] == nullptr) {
1103 continue;
1104 }
1105 for (std::size_t f = 0; f < shells[sh].size(); ++f) {
1106 P(shell2bf[sh] + Index(f), p) = -buf[0][f];
1107 }
1108 }
1109 }
1110 return P;
1111}
1112
1113Eigen::VectorXd EnvironmentScreening::AuxCharges(const AOBasis& auxbasis) {
1114 const std::vector<libint2::Shell> shells = auxbasis.GenerateLibintBasis();
1115 const std::vector<Index> shell2bf = auxbasis.getMapToBasisFunctions();
1116 Eigen::VectorXd q = Eigen::VectorXd::Zero(auxbasis.AOBasisSize());
1117 libint2::Engine engine(libint2::Operator::overlap,
1118 int(auxbasis.getMaxNprim()), int(auxbasis.getMaxL()),
1119 0);
1120 const libint2::Engine::target_ptr_vec& buf = engine.results();
1121 for (std::size_t sh = 0; sh < shells.size(); ++sh) {
1122 engine.compute(shells[sh], libint2::Shell::unit());
1123 if (buf[0] == nullptr) {
1124 continue;
1125 }
1126 for (std::size_t f = 0; f < shells[sh].size(); ++f) {
1127 q(shell2bf[sh] + Index(f)) = buf[0][f];
1128 }
1129 }
1130 return q;
1131}
1132
1133} // namespace xtp
1134} // namespace votca
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
Index getMaxL() const
Definition aobasis.cc:29
AOShellIterator begin() const
Definition aobasis.h:49
Index AOBasisSize() const
Definition aobasis.h:46
AOShellIterator end() const
Definition aobasis.h:50
std::vector< Index > getMapToBasisFunctions() const
Definition aobasis.cc:45
Index getNumofShells() const
Definition aobasis.h:56
Index getMaxNprim() const
Definition aobasis.cc:37
std::vector< std::vector< Index > > ComputeShellPairs(double threshold=1e-20) const
std::vector< libint2::Shell > GenerateLibintBasis() const
Definition aobasis.cc:119
void Fill(const AOBasis &aobasis) final
Eigen::MatrixXd aomatrix_
Definition aomatrix.h:86
const Eigen::MatrixXd & Matrix() const
Definition aomatrix.h:76
void computeCoulombIntegrals(const AOBasis &aobasis)
Eigen::MatrixXd Pseudo_InvSqrt(double etol)
Definition aomatrix.cc:88
Index Removedfunctions() const
Definition aomatrix.h:81
std::array< Eigen::MatrixXd, 3 > aomatrix_
Definition aomatrix.h:105
std::array< libint2::Shell::real_t, 3 > r_
Definition aomatrix.h:106
void Fill(const AOBasis &aobasis) final
static Eigen::MatrixXd Potential(const AOBasis &basis, const std::vector< Source > &sources)
static Eigen::MatrixXd PotentialAndGradientAt(const AOBasis &basis, const Eigen::MatrixXd &D, const std::vector< Eigen::Vector3d > &points, const std::vector< double > &widths)
void Fill(const AOBasis &aobasis) final
Eigen::MatrixXd aomatrix_
Definition aomatrix.h:44
void Fill(const AOBasis &aobasis) final
Eigen::MatrixXd singleShellOverlap(const AOShell &shell) const
Eigen::MatrixXd aomatrix_
Definition aomatrix.h:64
libint2::Shell LibintShell() const
Definition aoshell.cc:65
Index getSize() const
Definition aoshell.h:109
Index getNumFunc() const
Definition aoshell.h:103
std::vector< std::vector< libint2::ShellPair > > shellpairdata_
Definition ERIs.h:107
std::vector< libint2::Shell > basis_
Definition ERIs.h:103
Eigen::MatrixXd ComputeSchwarzShells(const AOBasis &dftbasis) const
std::vector< std::vector< Index > > shellpairs_
Definition ERIs.h:106
std::array< Eigen::MatrixXd, 2 > Compute4c(const Eigen::MatrixXd &dmat, double error) const
Index maxL_
Definition ERIs.h:109
Eigen::MatrixXd schwarzscreen_
Definition ERIs.h:136
Eigen::MatrixXd ComputeShellBlockNorm(const Eigen::MatrixXd &dmat) const
Definition ERIs.cc:70
std::vector< Index > starts_
Definition ERIs.h:104
Index maxnprim_
Definition ERIs.h:108
static Eigen::VectorXd AuxCharges(const AOBasis &auxbasis)
static Eigen::MatrixXd AuxPotentialAtPoints(const AOBasis &auxbasis, const std::vector< Eigen::Vector3d > &points, const std::vector< double > &widths)
The electrostatic potential of each auxiliary function (as a charge density) at each point,...
static Eigen::MatrixXd AuxFieldAtPoints(const AOBasis &auxbasis, const std::vector< Eigen::Vector3d > &points, const std::vector< double > &widths={})
F: the electric field at each point produced by each auxiliary basis function, taken as a charge dens...
std::vector< Index > col_offset_
std::vector< Run > runs_
std::vector< std::vector< Index > > SignificantPairs(const AOBasis &dftbasis, const Eigen::MatrixXd &metric, double pair_threshold) const
Index size() const
Number of aux functions.
Definition threecenter.h:85
void Fill(const AOBasis &auxbasis, const AOBasis &dftbasis, double pair_threshold=0.0, Index build_columns=kBuildColumns)
void SetupLayout(const AOBasis &dftbasis, const std::vector< std::vector< Index > > &kept)
Eigen::MatrixXd data_
void Fill3cMO(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &dft_orbitals)
std::vector< Eigen::MatrixXd > matrix_
Eigen::MatrixXd inv_sqrt_
Definition threecenter.h:60
Index getMaxThreads()
Definition eigen.h:145
Index getThreadId()
Definition eigen.h:160
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixLibInt
Definition aoecp.cc:56
Eigen::MatrixXd ComputeMixedOverlap(const AOBasis &row, const AOBasis &col)
std::vector< Eigen::MatrixXd > ComputeAO3cBlock(const libint2::Shell &auxshell, const AOBasis &dftbasis, libint2::Engine &engine)
std::array< MatrixLibInt, libint2::operator_traits< obtype >::nopers > computeOneBodyIntegrals(const AOBasis &aobasis, OperatorParams oparams=OperatorParams())
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26