-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelectric.cpp
509 lines (384 loc) · 16.2 KB
/
electric.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//
// Created by lurker on 3/22/17.
//
#include "electric.h"
using namespace bbfmm;
void electric(Grid& g, levelset& ls, Surface& surf, Molecule& mol, scalar_t rescale, Config& cfg){
vector<point> source, target;
vector<scalar_t > weight, normalX, normalY, normalZ;
scalar_t dx = ls.dx / rescale;
/*
* map all points back to the actual protein surface.
*/
for (index_t id = 0; id < surf.nodes.size(); ++id) {
source.push_back(
{
surf.nodes[id].data[0]/rescale,
surf.nodes[id].data[1]/rescale,
surf.nodes[id].data[2]/rescale
}
);
target.push_back(
{
surf.nodes[id].data[0]/rescale,
surf.nodes[id].data[1]/rescale,
surf.nodes[id].data[2]/rescale
}
);
weight.push_back(surf.weight[id] * rescale * dx * SQR(dx));
/*
* normal vectors do not rescale.
*/
normalX.push_back(surf.normals[id].data[0]);
normalY.push_back(surf.normals[id].data[1]);
normalZ.push_back(surf.normals[id].data[2]);
}
std::cout << std::setw(15) <<"POINTS NUM" << " " << std::setw(8) << source.size() << std::endl;
/*
* setup for FMM.
*/
index_t np = atoi(cfg.options["fmm_np"].c_str());
index_t maxPoint = atoi(cfg.options["fmm_maxpoint"].c_str());
index_t maxLevel = atoi(cfg.options["fmm_maxlevel"].c_str());
scalar_t kappa =atof(cfg.options["solvent_kappa"].c_str());
scalar_t dE = atof(cfg.options["solvent_dE"].c_str());
scalar_t dI = atof(cfg.options["solvent_dI"].c_str());
index_t N = (index_t) source.size();
scalar_t vacant_radius = atof(cfg.options["tau"].c_str()) * dx;
scalar_t area = std::accumulate(weight.begin(), weight.end(), 0.);
std::cout << std::setw(15)<< "AREA APPROX" << " " << std::setw(8)<< area << " A^2" <<std::fixed<<std::endl;
/*
* dBIE, singular part can be regularized as anything, e.g. 0. First order is observed.
*/
auto K1x = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
if (r < vacant_radius) return 0.;
else return (a.x - b.x) * (dE / dI * exp(-kappa * r) * (kappa * r + 1) - 1.) / 4.0 / M_PI / r / d;
};
auto K1y = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
if (r < vacant_radius) return 0.;
else return (a.y - b.y) * (dE / dI * exp(-kappa * r) * (kappa * r + 1) - 1.) / 4.0 / M_PI / r / d;
};
auto K1z = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
if (r < vacant_radius) return 0.;
else return (a.z - b.z) * (dE / dI * exp(-kappa * r) * (kappa * r + 1) - 1.) / 4.0 / M_PI / r / d;
};
auto K2 = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
if (r < vacant_radius) {
if (kappa == 0.) return 0.;
return (exp(-kappa * vacant_radius) - 1.0 + kappa * vacant_radius) / kappa / 2.0 / M_PI / vacant_radius /
vacant_radius;
} else {
return (1.0 - exp(-kappa * r)) / 4.0 / M_PI / r;
}
};
auto K3xx = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.x - b.x;
scalar_t df2 = a.x - b.x;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (1 - exp(-t) * (t + 1)) * delta / d / r +
(exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K3yy = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.y - b.y;
scalar_t df2 = a.y - b.y;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (1 - exp(-t) * (t + 1)) * delta / d / r +
(exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K3zz = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.z - b.z;
scalar_t df2 = a.z - b.z;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (1 - exp(-t) * (t + 1)) * delta / d / r +
(exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K3xy = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.x - b.x;
scalar_t df2 = a.y - b.y;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K3yz = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.y - b.y;
scalar_t df2 = a.z - b.z;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K3zx = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.z - b.z;
scalar_t df2 = a.x - b.x;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K3yx = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.x - b.x;
scalar_t df2 = a.y - b.y;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K3zy = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.y - b.y;
scalar_t df2 = a.z - b.z;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K3xz = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
scalar_t df1 = a.z - b.z;
scalar_t df2 = a.x - b.x;
scalar_t delta = 1.0;
scalar_t t = kappa * r;
if (r < vacant_radius) {
return 0.;
} else {
return (exp(-t) * ((t + 3) * t + 3) - 3.0) * df1 * df2 / d / d / r;
}
};
auto K4x = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
if (r < vacant_radius) return 0.;
else return -(a.x - b.x) * (dI / dE * exp(-kappa * r) * (kappa * r + 1) - 1.) / 4.0 / M_PI / r / d;
};
auto K4y = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
if (r < vacant_radius) return 0.;
else return -(a.y - b.y) * (dI / dE * exp(-kappa * r) * (kappa * r + 1) - 1.) / 4.0 / M_PI / r / d;
};
auto K4z = [&](point &a, point &b) {
scalar_t d = SQR(a.x - b.x) + SQR(a.y - b.y) + SQR(a.z - b.z);
scalar_t r = sqrt(d);
if (r < vacant_radius) return 0.;
else return -(a.z - b.z) * (dI / dE * exp(-kappa * r) * (kappa * r + 1) - 1.) / 4.0 / M_PI / r / d;
};
auto dmapping = [&](vector<point> &_source, vector<point> &_target, vector<scalar_t> &_weight,
vector<scalar_t> &_normalX, vector<scalar_t> &_normalY, vector<scalar_t> &_normalZ,
Vector &_phi) {
auto _N = (index_t) _source.size();
assert(_phi.row() == 2 * _N);
kernel _K1x, _K1y, _K1z, _K2, _K3xx, _K3yy, _K3zz, _K3xy, _K3yz, _K3zx, _K3yx, _K3xz, _K3zy, _K4x, _K4y, _K4z;
_K1x.eval = K1x;
_K1y.eval = K1y;
_K1z.eval = K1z;
_K2.eval = K2;
_K3xx.eval = K3xx;
_K3yy.eval = K3yy;
_K3zz.eval = K3zz;
_K3xy.eval = K3xy;
_K3yz.eval = K3yz;
_K3zx.eval = K3zx;
_K3yx.eval = K3yx;
_K3zy.eval = K3zy;
_K3xz.eval = K3xz;
_K4x.eval = K4x;
_K4y.eval = K4y;
_K4z.eval = K4z;
Vector pphi_pn(_N);
Vector phiX(_N), phiY(_N), phiZ(_N);
for (auto id = 0; id < _N; ++id) {
pphi_pn(id) = _phi(id + _N) * _weight[id];
phiX(id) = _phi(id) * _normalX[id] * _weight[id];
phiY(id) = _phi(id) * _normalY[id] * _weight[id];
phiZ(id) = _phi(id) * _normalZ[id] * _weight[id];
}
// todo: check all charges.
_K1x.initialize(np, _source, _target, phiX, _N, _N, maxPoint, maxLevel);
_K1y.initialize(np, _source, _target, phiY, _N, _N, maxPoint, maxLevel);
_K1z.initialize(np, _source, _target, phiZ, _N, _N, maxPoint, maxLevel);
_K2.initialize(np, _source, _target, pphi_pn, _N, _N, maxPoint, maxLevel);
_K3xx.initialize(np, _source, _target, phiX, _N, _N, maxPoint, maxLevel);
_K3yy.initialize(np, _source, _target, phiY, _N, _N, maxPoint, maxLevel);
_K3zz.initialize(np, _source, _target, phiZ, _N, _N, maxPoint, maxLevel);
_K3xy.initialize(np, _source, _target, phiX, _N, _N, maxPoint, maxLevel);
_K3yz.initialize(np, _source, _target, phiY, _N, _N, maxPoint, maxLevel);
_K3zx.initialize(np, _source, _target, phiZ, _N, _N, maxPoint, maxLevel);
_K3yx.initialize(np, _source, _target, phiY, _N, _N, maxPoint, maxLevel);
_K3zy.initialize(np, _source, _target, phiZ, _N, _N, maxPoint, maxLevel);
_K3xz.initialize(np, _source, _target, phiX, _N, _N, maxPoint, maxLevel);
_K4x.initialize(np, _source, _target, pphi_pn, _N, _N, maxPoint, maxLevel);
_K4y.initialize(np, _source, _target, pphi_pn, _N, _N, maxPoint, maxLevel);
_K4z.initialize(np, _source, _target, pphi_pn, _N, _N, maxPoint, maxLevel);
Vector ret1x, ret1y, ret1z, ret2, ret3xx, ret3yy, ret3zz, ret3xy, ret3yz, ret3zx, ret3yx, ret3zy, ret3xz, ret4x, ret4y, ret4z;
_K1x.run(ret1x);
_K1y.run(ret1y);
_K1z.run(ret1z);
_K2.run(ret2);
_K3xx.run(ret3xx);
_K3yy.run(ret3yy);
_K3zz.run(ret3zz);
_K3xy.run(ret3xy);
_K3yx.run(ret3yx);
_K3xz.run(ret3xz);
_K3zx.run(ret3zx);
_K3yz.run(ret3yz);
_K3zy.run(ret3zy);
_K4x.run(ret4x);
_K4y.run(ret4y);
_K4z.run(ret4z);
Vector tmp1(_N), tmp2(_N);
for (auto id = 0; id < _N; ++id) {
tmp1(id) = normalX[id] * (ret3xx(id) + ret3yx(id) + ret3zx(id)) +
normalY[id] * (ret3yy(id) + ret3xy(id) + ret3zy(id)) +
normalZ[id] * (ret3zz(id) + ret3xz(id) + ret3yz(id));
tmp2(id) = normalX[id] * ret4x(id) + normalY[id] * ret4y(id) + normalZ[id] * ret4z(id);
}
Vector output(2 * _N);
for (auto id = 0; id < _N; ++id) {
output(id) = _phi(id) + (ret1x(id) + ret1y(id) + ret1z(id) - ret2(id)) / (0.5 * (1 + dE / dI));
output(id + _N) = _phi(id + _N) + (tmp1(id) - tmp2(id)) / (0.5 * (1 + dI / dE));
}
return output;
};
auto FullMap = [&](Vector& phi) {
return dmapping(source, target, weight, normalX, normalY, normalZ, phi);
};
Vector start(2 * N); setValue(start, 0.);
Vector load(2 * N); setValue(load, 0.);
for (auto id = 0; id < N; ++id) {
for (auto atom_id = 0; atom_id < mol.N; ++atom_id) {
scalar_t d = SQR(source[id].x - mol.centers[atom_id].data[0] / rescale) +
SQR(source[id].y - mol.centers[atom_id].data[1] / rescale) +
SQR(source[id].z - mol.centers[atom_id].data[2] / rescale);
scalar_t r = sqrt(d);
load(id) += mol.charges[atom_id] / dI / 4.0 / M_PI / r / (0.5 * (1 + dE / dI));
load(id + N) -= mol.charges[atom_id] / dI / 4.0 / M_PI / d / r *
(normalX[id] * (source[id].x - mol.centers[atom_id].data[0] / rescale) +
normalY[id] * (source[id].y - mol.centers[atom_id].data[1] / rescale) +
normalZ[id] * (source[id].z - mol.centers[atom_id].data[2] / rescale)) /
(0.5 * (1 + dI / dE));
}
}
GMRES(FullMap, start, load, atoi(cfg.options["gmres_restart"].c_str()), atoi(cfg.options["gmres_maxiter"].c_str()), atof(cfg.options["gmres_tol"].c_str()));
/*
* solution is stored in start.
* output to file.
*/
std::ofstream potentialFile;
potentialFile.open(cfg.options["potent_file"]);
for (int id = 0; id < 2 * N; ++id) {
potentialFile << start(id) << "\n";
}
potentialFile.close();
/*
* calculate polarization energy
*/
vector<point> target_centers;
for (int id = 0; id < mol.N; ++id) {
target_centers.emplace_back(mol.centers[id].data[0] / rescale, mol.centers[id].data[1] / rescale,
mol.centers[id].data[2] / rescale
);
}
auto polarizeMap = [&](vector<point>& _source, vector<point>& _target, vector<scalar_t>& _weight,
vector<scalar_t>& _normalX, vector<scalar_t>& _normalY, vector<scalar_t>& _normalZ, Vector& _phi) {
auto _N = (index_t) _source.size();
auto _M = (index_t) _target.size();
assert(_phi.row() == 2 * _N);
kernel _K1x, _K1y, _K1z, _K2;
_K1x.eval = K1x;
_K1y.eval = K1y;
_K1z.eval = K1z;
_K2.eval = K2;
Vector pphi_pn(_N);
Vector phiX(_N), phiY(_N), phiZ(_N);
for (auto id = 0; id < _N; ++id) {
pphi_pn(id) = _phi(id + _N) * _weight[id];
phiX(id) = _phi(id) * _normalX[id] * _weight[id];
phiY(id) = _phi(id) * _normalY[id] * _weight[id];
phiZ(id) = _phi(id) * _normalZ[id] * _weight[id];
}
_K1x.initialize(np, _source, _target, phiX, _N, _M, maxPoint, maxLevel);
_K1y.initialize(np, _source, _target, phiY, _N, _M, maxPoint, maxLevel);
_K1z.initialize(np, _source, _target, phiZ, _N, _M, maxPoint, maxLevel);
_K2.initialize(np, _source, _target, pphi_pn, _N, _M, maxPoint, maxLevel);
Vector ret1x, ret1y, ret1z, ret2;
_K1x.run(ret1x);
_K1y.run(ret1y);
_K1z.run(ret1z);
_K2.run(ret2);
Vector output(_M); setValue(output, 0.);
for (auto id = 0; id < _M; ++id) {
output(id) = ret2(id) - (ret1x(id) + ret1y(id) + ret1z(id));
}
return output;
};
Vector energy(mol.N); setValue(energy, 0.);
energy = polarizeMap(source, target_centers, weight, normalX, normalY, normalZ, start);
std::ofstream energyFile;
energyFile.open(cfg.options["energy_file"]);
for (int id = 0; id < energy.row(); ++id) {
energyFile << energy(id) << "\n";
}
energyFile.close();
scalar_t polarizedEnergy = 0.;
for (auto id = 0; id < energy.row(); ++id) {
polarizedEnergy += energy(id) * mol.charges[id];
}
polarizedEnergy *= 0.5;
std::cout << "polarized energy: " << std::setw(20) << std::scientific <<polarizedEnergy <<std::fixed << std::endl;
}