43constexpr double kWeightCutoff = 1
e-14;
47Eigen::VectorXd Weights(
48 const std::vector<HirshfeldPartition::AtomicReference>& atoms,
49 const Eigen::Vector3d& point) {
50 Eigen::VectorXd rho(
Index(atoms.size()));
51 for (
Index j = 0; j < rho.size(); ++j) {
53 atoms[std::size_t(j)].basis, atoms[std::size_t(j)].density, point);
55 const double denominator = rho.sum();
56 constexpr double kNegligibleDenominator = 1.e-12;
57 if (denominator < kNegligibleDenominator) {
58 return Eigen::VectorXd::Zero(rho.size());
60 return rho / denominator;
65 const std::vector<HirshfeldPartition::AtomicReference>& atoms,
69 std::vector<Eigen::MatrixXd> W(std::size_t(n_atoms),
70 Eigen::MatrixXd::Zero(n_ao, n_ao));
71 std::vector<Eigen::MatrixXd*> targets;
72 for (Eigen::MatrixXd& m : W) {
73 targets.push_back(&m);
78 std::exception_ptr eptr =
nullptr;
79#pragma omp parallel for schedule(guided)
86 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
89 Eigen::MatrixXd ao(box.
Matrixsize(), n_points);
90 Eigen::MatrixXd w(n_points, n_atoms);
91 for (
Index p = 0; p < n_points; ++p) {
93 w.row(p) = weights[std::size_t(p)] *
94 Weights(atoms, points[std::size_t(p)]).transpose();
101 Eigen::Map<const Eigen::VectorXd>(weights.data(), n_points)
104 std::vector<Eigen::MatrixXd> local(
static_cast<std::size_t
>(n_atoms));
105 for (
Index k = 0; k < n_atoms; ++k) {
106 if (w.col(k).cwiseAbs().maxCoeff() <= kWeightCutoff * wmax) {
109 local[std::size_t(k)].noalias() =
110 ao * w.col(k).asDiagonal() * ao.transpose();
112 std::vector<const Eigen::MatrixXd*> blocks;
113 for (
const Eigen::MatrixXd& m : local) {
114 blocks.push_back(&m);
116 acc.
Add(box, blocks);
121 eptr = std::current_exception();
127 std::rethrow_exception(eptr);
130 for (Eigen::MatrixXd& m : W) {
131 m = 0.5 * (m + m.transpose()).eval();
137 const std::vector<HirshfeldPartition::AtomicReference>& atoms,
138 const std::vector<Eigen::Vector3d>& positions,
const AOBasis& basis,
139 const Vxc_Grid& grid,
const std::array<Eigen::MatrixXd, 3>& dP_field) {
141 if (
Index(positions.size()) != n_atoms) {
142 throw std::runtime_error(
143 "ChargeResponseFit: positions and Hirshfeld references differ in "
146 for (
const Eigen::MatrixXd& m : dP_field) {
148 throw std::runtime_error(
149 "ChargeResponseFit: density response does not match the basis");
152 std::vector<Eigen::Matrix3d> alpha(std::size_t(n_atoms),
153 Eigen::Matrix3d::Zero());
154 std::exception_ptr eptr =
nullptr;
155#pragma omp parallel for schedule(guided)
162 std::array<Eigen::MatrixXd, 3> dP_box;
163 for (
Index c = 0; c < 3; ++c) {
164 dP_box[std::size_t(c)] =
167 const std::vector<Eigen::Vector3d>& points = box.
getGridPoints();
169 std::vector<Eigen::Matrix3d> local(std::size_t(n_atoms),
170 Eigen::Matrix3d::Zero());
171 for (std::size_t p = 0; p < points.size(); ++p) {
172 const Eigen::VectorXd w = Weights(atoms, points[p]);
173 if (w.cwiseAbs().maxCoeff() == 0.0) {
178 for (
Index c = 0; c < 3; ++c) {
179 rho(c) = ao.dot(dP_box[std::size_t(c)] * ao);
181 for (
Index k = 0; k < n_atoms; ++k) {
182 const Eigen::Vector3d d = points[p] - positions[std::size_t(k)];
183 local[std::size_t(k)] -= (weights[p] * w(k)) * d * rho.transpose();
188 for (
Index k = 0; k < n_atoms; ++k) {
189 alpha[std::size_t(k)] += local[std::size_t(k)];
196 eptr = std::current_exception();
202 std::rethrow_exception(eptr);
208 const std::vector<Eigen::Vector3d>& positions,
209 const std::vector<Eigen::MatrixXd>& W,
210 const std::vector<Eigen::MatrixXd>& dP_potential,
211 const std::array<Eigen::MatrixXd, 3>& dipole,
212 const std::array<Eigen::MatrixXd, 3>& dP_field,
213 const std::vector<Eigen::Matrix3d>& alpha_atoms) {
215 if (
Index(W.size()) != n ||
Index(dP_potential.size()) != n ||
216 Index(alpha_atoms.size()) != n || n < 2) {
217 throw std::runtime_error(
218 "ChargeResponseFit::Analyse: inconsistent number of atoms");
221 Eigen::MatrixXd pi(n, n);
222 for (
Index j = 0; j < n; ++j) {
223 for (
Index k = 0; k < n; ++k) {
225 -W[std::size_t(j)].cwiseProduct(dP_potential[std::size_t(k)]).sum();
228 r.
pi_asymmetry = (pi - pi.transpose()).norm() / pi.norm();
229 const Eigen::MatrixXd projector =
230 Eigen::MatrixXd::Identity(n, n) -
231 Eigen::MatrixXd::Constant(n, n, 1.0 /
double(n));
232 r.
pi = projector * (0.5 * (pi + pi.transpose())) * projector;
233 r.
pi = 0.5 * (r.
pi + r.
pi.transpose()).eval();
234 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(r.
pi,
235 Eigen::EigenvaluesOnly);
238 std::stringstream message;
239 message <<
"ChargeResponseFit: Pi is not positive semidefinite (lowest "
242 <<
"); tighten the SCF convergence or change the step";
243 throw std::runtime_error(message.str());
246 for (
Index a = 0; a < 3; ++a) {
247 for (
Index c = 0; c < 3; ++c) {
249 -dipole[std::size_t(a)].cwiseProduct(dP_field[std::size_t(c)]).sum();
254 Eigen::MatrixX3d X(n, 3);
255 for (
Index k = 0; k < n; ++k) {
256 X.row(k) = positions[std::size_t(k)].transpose();
258 X.rowwise() -= X.colwise().mean();
261 Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eres(r.
alpha_res,
262 Eigen::EigenvaluesOnly);
263 if (eres.eigenvalues()(0) <= 0.0) {
264 std::stringstream message;
265 message <<
"ChargeResponseFit: alpha - X^T Pi X is not positive definite "
267 << eres.eigenvalues().transpose()
268 <<
" bohr^3); the charge response over-polarizes";
269 throw std::runtime_error(message.str());
274 for (
Index k = 0; k < n; ++k) {
275 r.
res_weights(k) = std::max(alpha_atoms[std::size_t(k)].trace(), 0.0);
278 throw std::runtime_error(
279 "ChargeResponseFit: no positive Hirshfeld atomic polarizability");
282 for (
Index k = 0; k < n; ++k) {
292 Eigen::Vector3d centre = Eigen::Vector3d::Zero();
293 for (
const QMAtom& atom : mol) {
294 centre += atom.getPos();
297 std::vector<Index> map(
static_cast<std::size_t
>(n), -1);
298 for (
Index k = 0; k < n; ++k) {
299 const Eigen::Vector3d image = 2.0 * centre - mol[k].
getPos();
300 for (
Index j = 0; j < n; ++j) {
301 if ((mol[j].getPos() - image).norm() < tol &&
302 mol[j].getElement() == mol[k].getElement()) {
303 map[std::size_t(k)] = j;
307 if (map[std::size_t(k)] < 0) {
312 Eigen::MatrixXd T = Eigen::MatrixXd::Zero(n_ao, n_ao);
313 for (
Index k = 0; k < n; ++k) {
315 const std::vector<const AOShell*> to =
317 if (from.size() != to.size()) {
320 for (std::size_t s = 0; s < from.size(); ++s) {
321 if (from[s]->getL() != to[s]->getL()) {
324 const double parity = (
Index(from[s]->getL()) % 2 == 0) ? 1.0 : -1.0;
325 for (
Index f = 0; f < from[s]->getNumFunc(); ++f) {
326 T(to[s]->getStartIndex() + f, from[s]->getStartIndex() + f) = parity;
339 throw std::runtime_error(
340 "ChargeResponseFit: closed-shell molecules only (spin 1)");
343 throw std::runtime_error(
"ChargeResponseFit: step must be positive");
346 throw std::runtime_error(
"ChargeResponseFit: tolerance must be positive");
348 const double step = fit.
step;
357 auto scf = [&](
const Eigen::MatrixXd& v,
Orbitals& target) {
366 const bool ok = engine.
Evaluate(target);
369 throw std::runtime_error(
"ChargeResponseFit: SCF did not converge");
374 <<
" Charge-response fit: ground state"
376 scf(Eigen::MatrixXd(), orb);
384 bs.
Load(options.
get(
"basisset").
as<std::string>());
389 const std::vector<HirshfeldPartition::AtomicReference> atoms =
391 const std::vector<Eigen::MatrixXd> W =
WeightMatrices(atoms, basis, grid);
395 Eigen::MatrixXd sum = -overlap.
Matrix();
396 for (
const Eigen::MatrixXd& w : W) {
400 <<
TimeStamp() <<
" Hirshfeld weight matrices: max |sum_k W_k - S| = "
401 << sum.cwiseAbs().maxCoeff() << std::flush;
408 auto response = [&](
const Eigen::MatrixXd& h,
const std::string& label) {
409 Eigen::MatrixXd dP = Eigen::MatrixXd::Zero(h.rows(), h.cols());
410 for (
double sign : {1.0, -1.0}) {
412 <<
TimeStamp() <<
" Perturbation " << label
413 << (sign > 0 ?
" +" :
" -") << step << std::flush;
415 scf(sign * step * h, perturbed);
416 dP += sign * perturbed.DensityMatrixGroundState();
418 return Eigen::MatrixXd(dP / (2.0 * step));
426 <<
" Inversion centre: one atom of each pair is perturbed"
431 std::vector<Index> perturbed;
432 for (
Index k = 0; k < n_atoms; ++k) {
434 perturbed.push_back(k);
437 std::vector<Eigen::MatrixXd> dP_potential(
static_cast<std::size_t
>(n_atoms));
438 std::array<Eigen::MatrixXd, 3> dP_field;
440 std::vector<Eigen::MatrixXd> h;
441 for (
Index k : perturbed) {
442 h.push_back(W[std::size_t(k)]);
444 for (
Index c = 0; c < 3; ++c) {
445 h.push_back(dipole.
Matrix()[std::size_t(c)]);
448 << h.size() <<
" perturbations" << std::flush;
452 const std::vector<Eigen::MatrixXd> dP =
454 for (std::size_t j = 0; j < perturbed.size(); ++j) {
455 dP_potential[std::size_t(perturbed[j])] = dP[j];
457 for (std::size_t c = 0; c < 3; ++c) {
458 dP_field[c] = dP[perturbed.size() + c];
461 for (
Index k : perturbed) {
462 dP_potential[std::size_t(k)] =
463 response(W[std::size_t(k)],
464 "w_" + std::to_string(k) +
" (" + mol[k].getElement() +
")");
466 const std::array<std::string, 3> axes = {
"x",
"y",
"z"};
467 for (
Index c = 0; c < 3; ++c) {
468 dP_field[std::size_t(c)] = response(dipole.
Matrix()[std::size_t(c)],
469 "field " + axes[std::size_t(c)]);
472 for (
Index k = 0; k < n_atoms; ++k) {
474 dP_potential[std::size_t(k)] =
475 inv.
T * dP_potential[std::size_t(inv.
atom_map[std::size_t(k)])] *
480 std::vector<Eigen::Vector3d> positions;
481 for (
const QMAtom& atom : mol) {
482 positions.push_back(atom.getPos());
484 const std::vector<Eigen::Matrix3d> alpha_atoms =
486 return Analyse(positions, W, dP_potential, dipole.
Matrix(), dP_field,
Container to hold Basisfunctions for all atoms.
const std::vector< const AOShell * > getShellsofAtom(Index AtomId) const
Index AOBasisSize() const
void Fill(const BasisSet &bs, const QMMolecule &atoms)
void setCenter(const Eigen::Vector3d &r)
const std::array< Eigen::MatrixXd, 3 > & Matrix() const
void Fill(const AOBasis &aobasis) final
void Fill(const AOBasis &aobasis) final
const Eigen::MatrixXd & Matrix() const
const Eigen::Vector3d & getPos() const
void Load(const std::string &name)
static Inversion FindInversion(const QMMolecule &mol, const AOBasis &basis, double tol=1e-4)
static std::vector< Eigen::Matrix3d > AtomicPolarizabilities(const std::vector< HirshfeldPartition::AtomicReference > &atoms, const std::vector< Eigen::Vector3d > &positions, const AOBasis &basis, const Vxc_Grid &grid, const std::array< Eigen::MatrixXd, 3 > &dP_field)
static Result Analyse(const std::vector< Eigen::Vector3d > &positions, const std::vector< Eigen::MatrixXd > &W, const std::vector< Eigen::MatrixXd > &dP_potential, const std::array< Eigen::MatrixXd, 3 > &dipole, const std::array< Eigen::MatrixXd, 3 > &dP_field, const std::vector< Eigen::Matrix3d > &alpha_atoms)
static std::vector< Eigen::MatrixXd > WeightMatrices(const std::vector< HirshfeldPartition::AtomicReference > &atoms, const AOBasis &basis, const Vxc_Grid &grid)
static Result Fit(const tools::Property &dftoptions, Orbitals &orb, const FitOptions &fit, Logger &log)
Electronic ground-state via Density-Functional Theory.
void setWarmStart(bool warm_start)
void setLogger(Logger *pLog)
Attach the logger used for SCF progress and diagnostics.
std::vector< HirshfeldPartition::AtomicReference > HirshfeldReferences(const QMMolecule &mol) const
void setExternalPotentialMatrix(const Eigen::MatrixXd &v)
void Initialize(tools::Property &options)
Read DFT, grid, and SCF settings from the user options tree.
const std::string & getGridName() const
Name of the integration grid of the SCF (xtpdft.integration_grid).
std::vector< Eigen::MatrixXd > StaticResponse(const Orbitals &orb, const std::vector< Eigen::MatrixXd > &perturbations, double tolerance=1e-7, Index max_iterations=100)
bool Evaluate(Orbitals &orb)
void setSetupCache(DFTSetupCache *cache)
Adds per-grid-box blocks into a set of AO matrices from inside an OpenMP loop over grid boxes.
void Finish()
sums the thread-local copies into the targets (call outside the loop)
void Add(const GridBox &box, const std::vector< const Eigen::MatrixXd * > &small)
const std::vector< Eigen::Vector3d > & getGridPoints() const
Eigen::MatrixXd ReadFromBigMatrix(const Eigen::MatrixXd &bigmatrix) const
const std::vector< double > & getGridWeights() const
AOShell::AOValues CalcAOValues(const Eigen::Vector3d &point) const
static double EvaluateAtomicDensity(const AOBasis &atom_basis, const Eigen::MatrixXd &reference_density, const Eigen::Vector3d &point)
Logger is used for thread-safe output of messages.
Container for molecular orbitals and derived one-particle data.
Index getSpin() const
Return the stored spin multiplicity.
const QMMolecule & QMAtoms() const
Return read-only access to the molecular geometry.
Timestamp returns the current time as a string Example: cout << TimeStamp().
void GridSetup(const std::string &type, const QMMolecule &atoms, const AOBasis &basis)
Index getBoxesSize() const
#define XTP_LOG(level, log)
Charge transport classes.
Provides a means for comparing floating point numbers.
double step
finite differences: strength of each perturbation
double tolerance
relative CPKS residual
std::vector< Index > atom_map
std::vector< Eigen::Matrix3d > alpha_res_atoms
Eigen::Matrix3d alpha_res
std::vector< Eigen::Matrix3d > alpha_atoms
Eigen::VectorXd res_weights
Eigen::VectorXd pi_eigenvalues
Parts of the DFT setup that stay the same between runs on the same molecule with the same basis sets,...