Skip to content

Commit 8a966c0

Browse files
haneugHagen Neugebauer
andauthored
Overload get_dispersion for atom-wise energies (#41)
-Also added a typedef for TRVector --------- Co-authored-by: Hagen Neugebauer <[email protected]>
1 parent 3895196 commit 8a966c0

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

include/dftd_dispersion.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,34 @@ extern int get_dispersion(
7171
double *GRAD
7272
);
7373

74+
/**
75+
* @brief Wrapper to handle the evaluation of dispersion energy and derivatives.
76+
*
77+
* This function calculates the atom-wise dispersion energy and gradients
78+
* for the given molecular geometry, considering only the atoms specified
79+
* in `realIdx`.
80+
*
81+
* @param mol Molecular geometry.
82+
* @param realIdx List for real atoms excluding ghost/non atoms.
83+
* @param charge Molecular charge.
84+
* @param par DFT-D4 parameters.
85+
* @param d4 Base D4 dispersion model.
86+
* @param cutoff Real-space cutoffs for CN and dispersion.
87+
* @param energies atom-wise dispersion energies (inout).
88+
* @param GRAD Dispersion gradient (inout).
89+
* @return Exit status.
90+
*/
91+
extern int get_dispersion(
92+
const TMolecule &mol,
93+
const TIVector &realIdx,
94+
int charge,
95+
const TD4Model &d4,
96+
const dparam &par,
97+
TCutoff cutoff,
98+
TRVector &energies,
99+
double *GRAD
100+
);
101+
74102
/**
75103
* @brief Wrapper to handle the evaluation of dispersion energy and derivatives.
76104
*

include/dftd_matrix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,6 @@ template <class T> class TMatrix {
231231
};
232232

233233
typedef TVector<int> TIVector;
234+
typedef TVector<double> TRVector;
234235

235236
} // namespace dftd4

src/dftd_dispersion.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,37 @@ int get_dispersion(
5959
double &energy,
6060
double *GRAD
6161
) {
62+
63+
int info{0};
64+
65+
int nat = realIdx.Max() + 1;
66+
67+
TRVector energies; // atom-wise energies
68+
energies.NewVector(nat);
69+
70+
info = get_dispersion(mol, realIdx, charge, d4, par, cutoff, energies, GRAD);
71+
if (info != EXIT_SUCCESS) return info;
72+
73+
// sum up atom-wise energies
74+
for (int i = 0; i != nat; i++) {
75+
energy += energies(i);
76+
}
77+
78+
energies.DelVec();
79+
80+
return EXIT_SUCCESS;
81+
}
82+
83+
int get_dispersion(
84+
const TMolecule &mol,
85+
const TIVector &realIdx,
86+
const int charge,
87+
const TD4Model &d4,
88+
const dparam &par,
89+
const TCutoff cutoff,
90+
TRVector &energies,
91+
double *GRAD
92+
) {
6293
// setup variables
6394
int info{0};
6495
bool lmbd = (par.s9 != 0.0);
@@ -137,8 +168,6 @@ int get_dispersion(
137168

138169
TVector<double> dEdcn;
139170
TVector<double> dEdq;
140-
TVector<double> energies;
141-
energies.NewVector(nat);
142171
if (lgrad) {
143172
dEdcn.NewVector(nat);
144173
dEdq.NewVector(nat);
@@ -242,12 +271,6 @@ int get_dispersion(
242271
dEdcn.DelVec();
243272
dEdq.DelVec();
244273

245-
// sum up atom-wise energies
246-
for (int i = 0; i != nat; i++) {
247-
energy += energies(i);
248-
}
249-
energies.DelVec();
250-
251274
// write to input gradient
252275
if (lgrad) {
253276
for (int i = 0, ii = 0; i != mol.NAtoms; i++) {

0 commit comments

Comments
 (0)