Skip to content

Commit

Permalink
Overload get_dispersion for atom-wise energies
Browse files Browse the repository at this point in the history
-added a typedef for TRVector
  • Loading branch information
Hagen Neugebauer committed Jan 21, 2025
1 parent 3895196 commit 39c8e20
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
28 changes: 28 additions & 0 deletions include/dftd_dispersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ extern int get_dispersion(
double *GRAD
);

/**
* @brief Wrapper to handle the evaluation of dispersion energy and derivatives.
*
* This function calculates the atom-wise dispersion energy and gradients
* for the given molecular geometry, considering only the atoms specified
* in `realIdx`.
*
* @param mol Molecular geometry.
* @param realIdx List for real atoms excluding ghost/non atoms.
* @param charge Molecular charge.
* @param par DFT-D4 parameters.
* @param d4 Base D4 dispersion model.
* @param cutoff Real-space cutoffs for CN and dispersion.
* @param energies atom-wise dispersion energies (inout).
* @param GRAD Dispersion gradient (inout).
* @return Exit status.
*/
extern int get_dispersion(
const TMolecule &mol,
const TIVector &realIdx,
int charge,
const TD4Model &d4,
const dparam &par,
TCutoff cutoff,
TRVector &energies,
double *GRAD
);

/**
* @brief Wrapper to handle the evaluation of dispersion energy and derivatives.
*
Expand Down
1 change: 1 addition & 0 deletions include/dftd_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,6 @@ template <class T> class TMatrix {
};

typedef TVector<int> TIVector;
typedef TVector<double> TRVector;

} // namespace dftd4
37 changes: 30 additions & 7 deletions src/dftd_dispersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ int get_dispersion(
double &energy,
double *GRAD
) {

int info{0};

int nat = realIdx.Max() + 1;

TRVector energies; // atom-wise energies
info = get_dispersion(mol, realIdx, charge, d4, par, cutoff, energies, GRAD);

if (info != EXIT_SUCCESS) return info;

// sum up atom-wise energies
for (int i = 0; i != nat; i++) {
energy += energies(i);
}

energies.DelVec();

return EXIT_SUCCESS;
}

int get_dispersion(
const TMolecule &mol,
const TIVector &realIdx,
const int charge,
const TD4Model &d4,
const dparam &par,
const TCutoff cutoff,
TRVector &energies,
double *GRAD
) {
// setup variables
int info{0};
bool lmbd = (par.s9 != 0.0);
Expand Down Expand Up @@ -137,7 +167,6 @@ int get_dispersion(

TVector<double> dEdcn;
TVector<double> dEdq;
TVector<double> energies;
energies.NewVector(nat);
if (lgrad) {
dEdcn.NewVector(nat);
Expand Down Expand Up @@ -242,12 +271,6 @@ int get_dispersion(
dEdcn.DelVec();
dEdq.DelVec();

// sum up atom-wise energies
for (int i = 0; i != nat; i++) {
energy += energies(i);
}
energies.DelVec();

// write to input gradient
if (lgrad) {
for (int i = 0, ii = 0; i != mol.NAtoms; i++) {
Expand Down

0 comments on commit 39c8e20

Please sign in to comment.