-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBEproblem.c
448 lines (380 loc) · 18.7 KB
/
PBEproblem.c
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
#include "PBEproblem.h"
#include "Overlap.h"
#include "Integration.h"
#include <sys/time.h>
#include <sys/resource.h>
PBEproblem PBEproblem_allocate(char *PDBfilename, char *SRFfilename, int compress) {
PBEproblem problem = NULL;
problem = (PBEproblem)(calloc(1, sizeof(_PBEproblem)));
PBEproblem_allocateButDontCompress(problem, PDBfilename, SRFfilename);
if (compress)
PBEproblem_initialize(problem);
return problem;
}
PBEproblem PBEproblem_loadOnlyPDB(char *PDBfilename) {
PBEproblem problem = NULL;
problem = (PBEproblem)(calloc(1, sizeof(_PBEproblem)));
problem->preconditioner = NULL;
problem->qualocationoperator = NULL;
problem->pbesurfaceoperator = NULL;
readPDB(PDBfilename, &(problem->numpdbentries), &(problem->pdbentries));
assignRadiiCharges(problem->pdbentries, problem->numpdbentries, SIZentries, numSIZentries,
CRGentries, numCRGentries);
return problem;
}
void PBEproblem_allocateButDontCompress(PBEproblem problem, char *PDBfilename, char *SRFfilename) {
problem->preconditioner = NULL;
problem->qualocationoperator = NULL;
problem->pbesurfaceoperator = NULL;
problem->useOverlap = 0; // default preconditioner is the diagonal
// or block-diagonal; set manually to get
// overlap preconditioner!
readPDB(PDBfilename, &(problem->numpdbentries), &(problem->pdbentries));
assignRadiiCharges(problem->pdbentries, problem->numpdbentries, SIZentries, numSIZentries,
CRGentries, numCRGentries);
readSRF(SRFfilename,
&(problem->saltpanels), &(problem->numsaltpanels), &(problem->numsalts),
&(problem->dielectricpanels), &(problem->numdielectricpanels),
&(problem->numdielectrics), &(problem->dielectricparent),
&(problem->dielectriccavitypanels), &(problem->numdielectriccavitypanels),
&(problem->numdielectriccavities), &(problem->dielectriccavityparent),
&(problem->saltcavitypanels), &(problem->numsaltcavitypanels),
&(problem->numsaltcavities), &(problem->saltcavityparent),
&(problem->numtotalpanels));
problem->numvariablecharges = 0;
PBEproblem_findVariableChargeIndices(problem, variablechain);
problem->numfixedligandcharges = 0;
PBEproblem_findFixedLigandChargeIndices(problem, fixedligandchain);
problem->numfixedreceptorcharges = 0;
PBEproblem_findFixedReceptorChargeIndices(problem, variablechain, fixedligandchain);
problem->globalCharges = Vector_allocate(problem->numpdbentries);
if (usequalocation)
problem->numtotalsurfacevariables = problem->numtotalpanels;
else
problem->numtotalsurfacevariables = 2 * problem->numtotalpanels;
problem->RHS = Vector_allocate(problem->numtotalsurfacevariables);
problem->Sol = Vector_allocate(problem->numtotalsurfacevariables);
problem->globalPhiReact = Vector_allocate(problem->numpdbentries);
problem->variablePhiReact = Vector_allocate(problem->numvariablecharges);
}
void PBEproblem_initialize(PBEproblem problem)
{
printf("found %d %d %d charges\n",
problem->numvariablecharges,
problem->numfixedligandcharges,
problem->numfixedreceptorcharges);
struct rusage ruse;
struct timeval tval;
real starttime, endtime, startwalltime, endwalltime;
if (usequalocation) {
printf("generating qualocation operator\n");
getrusage(RUSAGE_SELF, &ruse);
starttime = ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec +
1e-6 * (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec);
gettimeofday(&tval, NULL);
startwalltime = tval.tv_sec + 1e-6 * tval.tv_usec;
generateQualocationOperator(&(problem->qualocationoperator),
problem->pdbentries, problem->numpdbentries,
problem->dielectricpanels, problem->numdielectricpanels, problem->numdielectrics,
problem->dielectriccavitypanels, problem->numdielectriccavitypanels, problem->numdielectriccavities,
problem->dielectriccavityparent, problem->numtotalpanels);
getrusage(RUSAGE_SELF, &ruse);
endtime = ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec +
1e-6 * (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec);
gettimeofday(&tval, NULL);
endwalltime = tval.tv_sec + 1e-6 * tval.tv_usec;
printf("time to initialize operator: %.2f s (%.2f)\n", endtime - starttime, endwalltime - startwalltime);
generateQualocationOperatorPreconditioner(&problem->preconditioner,
problem->qualocationoperator,
problem->numtotalpanels);
} else {
printf("generating Green's theorem operator\n");
generateSurfaceOperator(&(problem->pbesurfaceoperator), problem->pdbentries, problem->numpdbentries,
problem->saltpanels, problem->numsaltpanels, problem->numsalts,
problem->dielectricpanels, problem->numdielectricpanels,
problem->numdielectrics, problem->dielectricparent,
problem->dielectriccavitypanels, problem->numdielectriccavitypanels,
problem->numdielectriccavities, problem->dielectriccavityparent,
problem->saltcavitypanels, problem->numsaltcavitypanels,
problem->numsaltcavities, problem->saltcavityparent,
problem->numtotalpanels);
generateSurfaceOperatorPreconditioner(&(problem->preconditioner), problem->pbesurfaceoperator, problem->numtotalpanels);
}
}
void PBEproblem_generateLhatOnTheFly(PBEproblem problem, Matrix Lhat) {
/* generateQualocationOperatorMinimal(&(problem->qualocationoperator), */
/* problem->pdbentries, problem->numpdbentries, */
/* problem->dielectricpanels, problem->numdielectricpanels, problem->numdielectrics, */
/* problem->dielectriccavitypanels, problem->numdielectriccavitypanels, problem->numdielectriccavities, */
/* problem->dielectriccavityparent, problem->numtotalpanels); */
/* problem->preconditioner = NULL; */
/* PBEproblem_generateDiagonalPreconditionerOnTheFly(problem, &(problem->preconditioner)); */
printf("generateLhatOnTheFly is commented out.\n");exit(-1);
}
void PBEproblem_generateDiagonalPreconditionerOnTheFly(PBEproblem problem, Preconditioner *P) {
unsigned int i, j, count = 0;
if (*P != NULL) {
Preconditioner_free(*P);
}
*P = Preconditioner_allocate(problem->numtotalsurfacevariables, problem->numtotalsurfacevariables);
if (usequalocation) { // basically Preconditioner_fill_diagonal_solv_ecf_qual without Tree_extractdiagonal
Vector diag = Vector_allocate(problem->numtotalpanels);
Panel* allpanels = (Panel *)calloc(problem->numtotalpanels, sizeof(Panel));
for (i = 0; i < problem->numdielectrics; i++)
for (j = 0; j < problem->numdielectricpanels[i]; j++) {
allpanels[count] = problem->dielectricpanels[i][j];
count++;
}
for (i = 0; i < problem->numdielectriccavities; i++) {
if (problem->dielectriccavityparent[i] == 999)
continue;
for (j = 0; j < problem->numdielectriccavitypanels[i]; j++) {
allpanels[count] = problem->dielectriccavitypanels[i][j];
count++;
}
}
real idiel = innerdielectric;
real odiel = outerdielectric;
for (i = 0; i < problem->numtotalpanels; i++) {
diag[i] = Integration(allpanels[i]->centroid, allpanels[i], POISSON_KERNEL, NULL, DOUBLE_LAYER_INT);
Preconditioner_set(*P, i, i, (-odiel / ((odiel - idiel) * idiel) + (diag[i]) / (4.0 * M_PI * idiel)) * allpanels[i]->area);
}
Vector_free(diag);
} else {
printf("PBEproblem_generateDiagonalPreconditionerOnTheFly: Green's theorem case not implemented yet!\n");
}
Preconditioner_factor(*P);
}
void PBEproblem_loadNewChargeDistribution(PBEproblem problem, char *PDBfilename, char *CRGfilename) {
readCRG(CRGfilename, &numCRGentries, &CRGentries);
printf("loaded CRG\n");
readPDB(PDBfilename, &(problem->numpdbentries), &(problem->pdbentries));
printf("loaded new PDB\n");
assignRadiiCharges(problem->pdbentries, problem->numpdbentries, SIZentries, numSIZentries,
CRGentries, numCRGentries);
printf("assignRadiiCharges has returned\n");
problem->numvariablecharges = 0;
PBEproblem_findVariableChargeIndices(problem, variablechain);
problem->numfixedligandcharges = 0;
PBEproblem_findFixedLigandChargeIndices(problem, fixedligandchain);
problem->numfixedreceptorcharges = 0;
PBEproblem_findFixedReceptorChargeIndices(problem, variablechain, fixedligandchain);
printf("indices updated\n");
free(problem->globalCharges);
free(problem->globalPhiReact);
free(problem->variablePhiReact);
problem->globalCharges = Vector_allocate(problem->numpdbentries);
problem->globalPhiReact = Vector_allocate(problem->numpdbentries);
problem->variablePhiReact = Vector_allocate(problem->numvariablecharges);
}
void PBEproblem_findVariableChargeIndices(PBEproblem problem, char variablechain) {
unsigned int i, currentcount = 0;
problem->numvariablecharges = 0;
for (i = 0; i < problem->numpdbentries; i++)
if (variablechain == problem->pdbentries[i].chain)
problem->numvariablecharges++;
problem->variablechargeindextoglobalindex = (unsigned int *)calloc(problem->numvariablecharges, sizeof(unsigned int));
for (i = 0; i < problem->numpdbentries; i++) {
if (variablechain == problem->pdbentries[i].chain) {
problem->variablechargeindextoglobalindex[currentcount] = i;
currentcount++;
}
}
}
void PBEproblem_findFixedLigandChargeIndices(PBEproblem problem, char fixedligandchain) {
unsigned int i, currentcount = 0;
for (i = 0; i < problem->numpdbentries; i++)
if (fixedligandchain == problem->pdbentries[i].chain)
problem->numfixedligandcharges++;
problem->fixedligandchargeindextoglobalindex = (unsigned int *)calloc(problem->numfixedligandcharges, sizeof(unsigned int));
for (i = 0; i < problem->numpdbentries; i++) {
if (fixedligandchain == problem->pdbentries[i].chain) {
problem->fixedligandchargeindextoglobalindex[currentcount] = i;
currentcount++;
}
}
}
void PBEproblem_findFixedReceptorChargeIndices(PBEproblem problem, char variablechain, char fixedligandchain) {
unsigned int i, currentcount = 0;
for (i = 0; i < problem->numpdbentries; i++)
if (! ((variablechain == problem->pdbentries[i].chain) || (fixedligandchain==problem->pdbentries[i].chain)) )
problem->numfixedreceptorcharges++;
problem->fixedreceptorchargeindextoglobalindex = (unsigned int *)calloc(problem->numfixedreceptorcharges, sizeof(unsigned int));
for (i = 0; i < problem->numpdbentries; i++) {
if (! ((variablechain == problem->pdbentries[i].chain) || (fixedligandchain==problem->pdbentries[i].chain)) ) {
problem->fixedreceptorchargeindextoglobalindex[currentcount] = i;
currentcount++;
}
}
}
void PBEproblem_free(PBEproblem problem) {
if (usequalocation) {
if (problem->qualocationoperator)
QualocationOperator_free(problem->qualocationoperator);
} else {
if (problem->pbesurfaceoperator)
SurfaceOperator_free(problem->pbesurfaceoperator);
}
if (problem->preconditioner)
Preconditioner_free(problem->preconditioner);
// what about freeing panels, PDBentries?
free(problem->variablechargeindextoglobalindex);
Vector_free(problem->globalCharges);
Vector_free(problem->RHS);
Vector_free(problem->Sol);
Vector_free(problem->globalPhiReact);
Vector_free(problem->variablePhiReact);
free(problem);
}
void PBEproblem_setVariableChargeVector(PBEproblem problem, Vector variablechargevec) {
unsigned int i;
if (problem->globalCharges == variablechargevec) {
return;
}
// assumes arguments are appropriately dimensioned
for (i = 0; i < problem->numpdbentries; i++)
problem->globalCharges[i] = 0.0;
for (i = 0; i < problem->numvariablecharges; i++) {
problem->globalCharges[problem->variablechargeindextoglobalindex[i]] = variablechargevec[i];
}
}
void PBEproblem_getVariableReactionPotentials(PBEproblem problem, Vector localreactionvector) {
unsigned int i;
for (i = 0; i < problem->numvariablecharges; i++)
localreactionvector[i] = problem->globalPhiReact[problem->variablechargeindextoglobalindex[i]];
}
void PBEproblem_solve(PBEproblem problem) {
unsigned int i;
struct rusage ruse;
struct timeval tval;
real starttime, endtime, startwalltime, endwalltime;
getrusage(RUSAGE_SELF, &ruse);
starttime = ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec +
1e-6 * (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec);
gettimeofday(&tval, NULL);
startwalltime = tval.tv_sec + 1e-6 * tval.tv_usec;
Vector_zero(problem->RHS, problem->numtotalsurfacevariables);
Vector_zero(problem->Sol, problem->numtotalsurfacevariables);
if (usequalocation) {
QualocationOperator_makeRHS_fromVector(problem->qualocationoperator,
problem->RHS, problem->globalCharges, problem->numpdbentries);
// Vector_writefile("b_qual_known.m", problem->RHS, problem->numtotalsurfacevariables);
/* printf("pbeproblem_solve_RHS = ["); */
/* for (i = 0; i < problem->numtotalsurfacevariables; i++) { */
/* printf("%f\n", problem->RHS[i]); */
/* } */
/* printf("];\n"); */
GMRES_QualocationOperator(problem->qualocationoperator, problem->preconditioner,
problem->RHS, problem->Sol,
problem->numtotalsurfacevariables, errortolerance);
// Vector_writefile("x_qual_known.m",problem->Sol,problem->numtotalsurfacevariables);
/* printf("pbeproblem_solve_Sol = ["); */
/* for (i = 0; i < problem->numtotalsurfacevariables; i++) { */
/* printf("%f\n", problem->Sol[i]); */
/* } */
/* printf("];\n"); */
QualocationOperator_collectPotentials_toVector(problem->qualocationoperator,
problem->globalPhiReact, problem->Sol);
} else {
SurfaceOperator_makeRHS_fromVector(problem->pbesurfaceoperator, problem->RHS,
problem->globalCharges, problem->numpdbentries);
// Vector_writefile("b_yoon_coll.m", problem->RHS, problem->numtotalsurfacevariables);
/* printf("rhs=["); */
/* for (i = 0; i < problem->numtotalsurfacevariables; i++) { */
/* printf("%f\n", problem->RHS[i]); */
/* } */
/* printf("];\n"); */
GMRES_SurfaceOperator(problem->pbesurfaceoperator, problem->preconditioner,
problem->RHS, problem->Sol,
problem->numtotalsurfacevariables, errortolerance);
Vector_zero(problem->globalPhiReact, problem->numpdbentries);
SurfaceOperator_collectPotentials_toVector(problem->pbesurfaceoperator,
problem->globalPhiReact, problem->Sol);
// Vector_writefile("x_yoon_coll.m", problem->Sol, problem->numtotalsurfacevariables);
/* printf("x=["); */
/* for (i = 0; i < problem->numtotalsurfacevariables; i++) { */
/* printf("%f\n", problem->Sol[i]); */
/* } */
/* printf("];\n"); */
}
getrusage(RUSAGE_SELF, &ruse);
endtime = ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec +
1e-6 * (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec);
gettimeofday(&tval, NULL);
endwalltime = tval.tv_sec + 1e-6 * tval.tv_usec;
printf("time for PBEproblem_solve(): %.2f s (%.2f)\n", endtime - starttime, endwalltime - startwalltime);
for (i = 0; i < problem->numpdbentries; i++)
problem->globalPhiReact[i] *= .592 * KT_CONVERSION / innerdielectric;
}
void PBEproblem_applyPreconditioner(PBEproblem problem, Vector Px, Vector x) {
if (problem->useOverlap) {
Preconditioner_multiply(Px, problem->overlapPreconditioner, x, problem->numtotalsurfacevariables);
} else {
Preconditioner_solve(Px, problem->preconditioner, x);
}
}
void PBEproblem_applyA1(PBEproblem problem, Vector A1x, Vector x) {
PBEproblem_setVariableChargeVector(problem, x);
if (usequalocation) {
QualocationOperator_makeRHS_fromVector(problem->qualocationoperator,
A1x, problem->globalCharges,
problem->numpdbentries);
} else {
SurfaceOperator_makeRHS_fromVector(problem->pbesurfaceoperator,
A1x, problem->globalCharges,
problem->numpdbentries);
}
// need to negate the calculated vector?
}
void PBEproblem_applyA2(PBEproblem problem, Vector A2x, Vector x) {
if (usequalocation) {
QualocationOperator_multiply(problem->qualocationoperator, A2x, x);
} else {
SurfaceOperator_topmultiply(problem->pbesurfaceoperator, A2x, x);
}
}
void PBEproblem_applyA3(PBEproblem problem, Vector A3x, Vector x) {
unsigned int i;
Vector_zero(problem->globalPhiReact, problem->numpdbentries);
if (usequalocation) {
QualocationOperator_collectPotentials_toVector(problem->qualocationoperator,
problem->globalPhiReact, x);
} else {
SurfaceOperator_collectPotentials_toVector(problem->pbesurfaceoperator,
problem->globalPhiReact, x);
}
for (i = 0; i < problem->numpdbentries; i++)
problem->globalPhiReact[i] *= .592 * KT_CONVERSION / innerdielectric;
PBEproblem_getVariableReactionPotentials(problem, A3x);
}
void PBEproblem_setupOverlapPreconditioner(PBEproblem problem) {
problem->useOverlap = 1;
problem->overlapPreconditioner = Preconditioner_allocate(problem->numtotalsurfacevariables,
problem->numtotalsurfacevariables);
Preconditioner_fill_overlap_ecf_qual_cav(problem->overlapPreconditioner, problem->qualocationoperator->tree,
problem->qualocationoperator->tree->panels,
problem->numtotalsurfacevariables, problem->numtotalsurfacevariables,
problem->qualocationoperator->innerdielectric,
problem->qualocationoperator->outerdielectric);
}
void PBEproblem_saveOverlapPreconditioner(PBEproblem problem, char *filename) {
unsigned int i,j;
if (problem->overlapPreconditioner == NULL) {
printf("ERROR: no overlap preconditioner has been created yet.\n");
return;
}
Vector Pqq = Vector_allocate(problem->numtotalsurfacevariables);
Vector qq = Vector_allocate(problem->numtotalsurfacevariables);
FILE *OUTFILE = fopen(filename, "w");
for (i = 0; i < problem->numtotalsurfacevariables; i++) {
Vector_zero(qq, problem->numtotalsurfacevariables); qq[i]=1.0;
Preconditioner_multiply(Pqq, problem->overlapPreconditioner, qq, problem->numtotalsurfacevariables);
for (j = 0; j < problem->numtotalsurfacevariables; j++) {
fprintf(OUTFILE, "%f ", Pqq[j]);
}
fprintf(OUTFILE, "\n");
}
fclose(OUTFILE);
Vector_free(qq);
Vector_free(Pqq);
}