-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdftd_dispersion.cpp
268 lines (231 loc) · 6.08 KB
/
dftd_dispersion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* This file is part of cpp-d4.
*
* Copyright (C) 2019 Sebastian Ehlert, Marvin Friede
*
* cpp-d4 is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cpp-d4 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with cpp-d4. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* D4(EEQ)-ATM implementation
*/
#include <cmath>
#include "damping/dftd_atm.h"
#include "damping/dftd_rational.h"
#include "dftd_cblas.h"
#include "dftd_dispersion.h"
#include "dftd_eeq.h"
#include "dftd_geometry.h"
#include "dftd_matrix.h"
#include "dftd_model.h"
#include "dftd_ncoord.h"
#include "dftd_parameters.h"
namespace dftd4 {
int get_dispersion(
const TMolecule &mol,
const int charge,
const TD4Model &d4,
const dparam &par,
const TCutoff cutoff,
double &energy,
double *GRAD
) {
TVector<int> realIdx;
initializeRealIdx(mol.NAtoms, realIdx);
return get_dispersion(mol, realIdx, charge, d4, par, cutoff, energy, GRAD);
}
int get_dispersion(
const TMolecule &mol,
const TIVector &realIdx,
const int charge,
const TD4Model &d4,
const dparam &par,
const TCutoff cutoff,
double &energy,
double *GRAD
) {
// setup variables
int info{0};
bool lmbd = (par.s9 != 0.0);
bool lgrad = !!GRAD;
int nat = realIdx.Max() + 1;
// distances
TMatrix<double> dist;
dist.NewMatrix(nat, nat);
calc_distances(mol, realIdx, dist);
TVector<double> cn; // D4 coordination number
TVector<double> q; // partial charges from EEQ model
TMatrix<double> dcndr; // derivative of D4-CN
TMatrix<double> dqdr; // derivative of partial charges
TVector<double> gradient; // derivative of dispersion energy
cn.NewVector(nat);
q.NewVector(nat);
if (lgrad) {
dcndr.NewMatrix(3 * nat, nat);
dqdr.NewMatrix(3 * nat, nat);
gradient.NewVector(3 * nat);
}
// calculate partial charges from EEQ model
info = get_charges(mol, realIdx, dist, charge, cutoff.cn_eeq, q, dqdr, lgrad);
if (info != EXIT_SUCCESS) return info;
// get the D4 coordination number
info = get_ncoord_d4(mol, realIdx, dist, cutoff.cn, cn, dcndr, lgrad);
if (info != EXIT_SUCCESS) return info;
// maximum number of reference systems
int mref{0};
info = get_max_ref(mol, mref);
if (info != EXIT_SUCCESS) return info;
// reference charges
TMatrix<double> refq;
refq.NewMat(mref, nat);
info = d4.set_refq_eeq(mol, realIdx, refq);
if (info != EXIT_SUCCESS) return info;
TMatrix<double> gwvec;
TMatrix<double> dgwdcn;
TMatrix<double> dgwdq;
gwvec.NewMatrix(mref, nat);
if (lgrad) {
dgwdcn.NewMatrix(mref, nat);
dgwdq.NewMatrix(mref, nat);
}
info = d4.weight_references(
mol, realIdx, cn, q, refq, gwvec, dgwdcn, dgwdq, lgrad
);
if (info != EXIT_SUCCESS) return info;
TMatrix<double> c6;
TMatrix<double> dc6dcn;
TMatrix<double> dc6dq;
c6.NewMatrix(nat, nat);
if (lgrad) {
dc6dcn.NewMatrix(nat, nat);
dc6dq.NewMatrix(nat, nat);
}
info = d4.get_atomic_c6(
mol, realIdx, gwvec, dgwdcn, dgwdq, c6, dc6dcn, dc6dq, lgrad
);
if (info != EXIT_SUCCESS) return info;
// --------------------------
// Two-body dispersion energy
// --------------------------
TVector<double> dEdcn;
TVector<double> dEdq;
TVector<double> energies;
energies.NewVector(nat);
if (lgrad) {
dEdcn.NewVector(nat);
dEdq.NewVector(nat);
}
info = get_dispersion2(
mol,
realIdx,
dist,
cutoff.disp2,
par,
c6,
dc6dcn,
dc6dq,
energies,
dEdcn,
dEdq,
gradient,
lgrad
);
if (info != EXIT_SUCCESS) return info;
// blas function have no return in orca
if (lgrad) BLAS_Add_Mat_x_Vec(gradient, dqdr, dEdq, false, 1.0);
dqdr.DelMat();
// ----------------------------
// Three-body dispersion energy
// ----------------------------
if (lmbd) {
// Three-body term is independent of charges
for (int i = 0; i != nat; i++) {
q(i) = 0.0;
}
// calculate weight references
gwvec.NewMatrix(mref, nat);
if (lgrad) {
dgwdcn.NewMatrix(mref, nat);
dgwdq.NewMatrix(mref, nat);
}
info = d4.weight_references(
mol, realIdx, cn, q, refq, gwvec, dgwdcn, dgwdq, lgrad
);
if (info != EXIT_SUCCESS) return info;
cn.Delete();
q.Delete();
refq.Delete();
// calculate reference C6 coefficients
c6.NewMatrix(nat, nat);
if (lgrad) {
dc6dcn.NewMatrix(nat, nat);
dc6dq.NewMatrix(nat, nat);
}
info = d4.get_atomic_c6(
mol, realIdx, gwvec, dgwdcn, dgwdq, c6, dc6dcn, dc6dq, lgrad
);
if (info != EXIT_SUCCESS) return info;
gwvec.DelMat();
dgwdcn.DelMat();
dgwdq.DelMat();
// calculate three-body dispersion
info = get_dispersion3(
mol,
realIdx,
dist,
cutoff.disp3,
par,
c6,
dc6dcn,
dc6dq,
energies,
dEdcn,
dEdq,
gradient,
lgrad
);
if (info != EXIT_SUCCESS) return info;
} else {
cn.Delete();
q.Delete();
refq.Delete();
gwvec.Delete();
dgwdcn.Delete();
dgwdq.Delete();
}
dist.DelMat();
c6.DelMat();
dc6dcn.DelMat();
dc6dq.DelMat();
if (lgrad) { BLAS_Add_Mat_x_Vec(gradient, dcndr, dEdcn, false, 1.0); }
dcndr.DelMat();
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++) {
ii = realIdx(i);
if (ii < 0) continue;
GRAD[3 * i] = gradient(3 * ii);
GRAD[3 * i + 1] = gradient(3 * ii + 1);
GRAD[3 * i + 2] = gradient(3 * ii + 2);
}
gradient.DelVec();
}
return EXIT_SUCCESS;
}
} // namespace dftd4