Debian Codegraph @

contrib/iapbs/src/apbs_driver.c

raw
1 /**
2 *
3 * @file apbs_driver.c
4 * @author Robert Konecny
5 * @brief The main iAPBS driver code
6 * @note Energy is returned in kJ/mol, forces in kJ/(mol/A).
7 *
8 *
9 */
10
11 #include "apbs.h"
12 #include "routines.h"
13 #include "generic/nosh.h"
14 #include "generic/mgparm.h"
15 #include "generic/pbeparm.h"
16 #include "generic/femparm.h"
17 #include "generic/vhal.h"
18
19 #include "apbs_driver.h"
20
21 #ifdef WIN32
22 #include <process.h>
23 #else
24 #include <unistd.h>
25 #endif
26
27
28 /*! \def MAX_BUF_SIZE
29 \brief Buffer size for internal APBS string input.
30 */
31 #define MAX_BUF_SIZE 4096
32
33 VEMBED(rcsid="$Id: apbs_driver.c rok $")
34
35 /**
36 * @brief Wrapper iAPBS function
37 * @author Robert Konecny
38 *
39 */
40 int apbsdrv_(
41 int *nat,
42 double x[NATOMS],
43 double y[NATOMS],
44 double z[NATOMS],
45 double radius[NATOMS],
46 double charge[NATOMS],
47 double r_param[9],
48 int i_param[25],
49 double grid[3],
50 int dime[3],
51 int pdime[3],
52 double glen[3],
53 double center[3],
54 double cglen[3],
55 double fglen[3],
56 double ccenter[3],
57 double fcenter[3],
58 double *ofrac,
59 int *dbg,
60 double ionq[MAXION],
61 double ionc[MAXION],
62 double ionr[MAXION],
63 double esenergy[1],
64 double npenergy[1],
65 double apbsdx[NATOMS], double apbsdy[NATOMS], double apbsdz[NATOMS],
66 double apbsqfx[NATOMS], double apbsqfy[NATOMS], double apbsqfz[NATOMS],
67 double apbsibx[NATOMS], double apbsiby[NATOMS], double apbsibz[NATOMS],
68 double apbsnpx[NATOMS], double apbsnpy[NATOMS], double apbsnpz[NATOMS],
69 double apbsdbx[NATOMS], double apbsdby[NATOMS], double apbsdbz[NATOMS],
70 double apbsgrid_meta[13],
71 double *apbsgrid[]
72 )
73 {
74 int i,k,j;
75
76 // 1 if grid data to be written to files (traditional), 0 to return via apbsgrid**.
77 int is_grid2file = 1;
78
79 // Memory must be allocated for the outputs if they are to be returned in-memory.
80 if( apbsgrid_meta[0] > 0 ) {
81 is_grid2file = 0;
82 VASSERT( apbsgrid != VNULL );
83 for(i=0; i<apbsgrid_meta[0]; i++) {
84 VASSERT( apbsgrid[i] != VNULL );
85 }
86 }
87
88 NOsh *nosh = VNULL;
89
90 MGparm *mgparm = VNULL;
91 FEMparm *feparm = VNULL;
92 PBEparm *pbeparm = VNULL;
93 APOLparm *apolparm = VNULL;
94 Vparam *param = VNULL;
95
96 Vmem *mem = VNULL;
97 Vcom *com = VNULL;
98 Vio *sock = VNULL;
99 #ifdef HAVE_MC
100 Vfetk *fetk[NOSH_MAXCALC];
101 Gem *gm[NOSH_MAXMOL];
102 #else
103 void *fetk[NOSH_MAXCALC];
104 void *gm[NOSH_MAXMOL];
105 #endif
106 Vpmg *pmg[NOSH_MAXCALC];
107 Vpmgp *pmgp[NOSH_MAXCALC];
108 Vpbe *pbe[NOSH_MAXCALC];
109 Valist *alist[NOSH_MAXMOL];
110 Vgrid *dielXMap[NOSH_MAXMOL],*dielYMap[NOSH_MAXMOL],*dielZMap[NOSH_MAXMOL];
111 Vgrid *kappaMap[NOSH_MAXMOL];
112 Vgrid *potMap[NOSH_MAXMOL];
113 Vgrid *chargeMap[NOSH_MAXMOL];
114 // char *input_path = VNULL;
115 char *output_path = VNULL;
116 int rank, size, isolve, debug, natom;
117 // unsigned long int bytesTotal, highWater;
118 size_t bytesTotal, highWater;
119 Voutput_Format outputformat;
120
121 int bufsize = MAX_BUF_SIZE;
122 int rc = 0;
123 double coord[3];
124 char *inputString;
125
126 /* These variables require some explaining... The energy double arrays
127 * store energies from the various calculations. The energy int array
128 * stores either a flag (0,1) displaying whether energies were calculated
129 * or if PCE_COMPS is used, the number of atom energies stored
130 * for the given calculation. Likewise, the
131 * force double arrays store forces from the various calcualtions. The
132 * force int array stores an integer which either says no calculation was
133 * performed (0) or gives the number of entries in the force array for each
134 * calculation */
135 double qfEnergy[NOSH_MAXCALC], qmEnergy[NOSH_MAXCALC];
136 double dielEnergy[NOSH_MAXCALC], totEnergy[NOSH_MAXCALC];
137 AtomForce *atomForce[NOSH_MAXCALC];
138 double *atomEnergy[NOSH_MAXCALC];
139 int nenergy[NOSH_MAXCALC], nforce[NOSH_MAXCALC];
140 /* The real partition centers */
141 double realCenter[3];
142
143
144
145 /* ************** CHECK PARALLEL STATUS *************** */
146 // init is done by the calling program
147 // VASSERT(Vcom_init(&argc, &argv));
148 com = Vcom_ctor(1);
149 rank = Vcom_rank(com);
150 size = Vcom_size(com);
151 startVio();
152 Vnm_setIoTag(rank, size);
153 Vnm_tprint( 0, "Hello world from PE %d\n", rank);
154
155 /* A bit of array/pointer initialization */
156 mem = Vmem_ctor("MAIN");
157 for (i=0; i<NOSH_MAXCALC; i++) {
158 pmg[i] = VNULL;
159 pmgp[i] = VNULL;
160 fetk[i] = VNULL;
161 pbe[i] = VNULL;
162 qfEnergy[i] = 0;
163 qmEnergy[i] = 0;
164 dielEnergy[i] = 0;
165 totEnergy[i] = 0;
166 atomForce[i] = VNULL;
167 nenergy[i] = 0;
168 nforce[i] = 0;
169 }
170 for (i=0; i<NOSH_MAXMOL; i++) {
171 alist[i] = VNULL;
172 dielXMap[i] = VNULL;
173 dielYMap[i] = VNULL;
174 dielZMap[i] = VNULL;
175 kappaMap[i] = VNULL;
176 potMap[i] = VNULL;
177 chargeMap[i] = VNULL;
178 }
179
180
181 /* *************** PARSE INPUT FILE ******************* */
182 nosh = NOsh_ctor(rank, size);
183
184 // sock = Vio_ctor("FILE", "ASC", VNULL, input_path, "r");
185 // Vnm_tprint( 1, "Parsing input file %s...\n", input_path);
186
187 VASSERT( bufsize <= VMAX_BUFSIZE );
188 sock = Vio_ctor("BUFF","ASC",VNULL,"0","r");
189
190 debug = *dbg;
191
192 /* generate input string */
193 inputString = VNULL;
194 inputString = setupString(r_param, i_param, grid, dime, ionq, ionc,
195 ionr, glen, center, cglen, fglen, ccenter, fcenter, ofrac,
196 pdime, debug);
197 if(debug>2) Vnm_tprint(1, "debug: Input string:\n%s\n", inputString);
198 Vio_bufTake(sock, inputString, bufsize);
199
200 if (!NOsh_parseInput(nosh, sock)) {
201 Vnm_tprint( 2, "Error while parsing input string.\n");
202 VJMPERR1(0);
203 } else if(debug>1) Vnm_tprint( 1, "Parsed input string.\n");
204
205 sock->VIObuffer = VNULL;
206 Vio_dtor(&sock);
207
208
209 /* *************** LOAD PARAMETERS AND MOLECULES ******************* */
210 //nosh->gotparm = 0; // not using param file for now
211 /*
212 *
213 param = loadParameter(nosh);
214 if (loadMolecules(nosh, param, alist) != 1) {
215 Vnm_tprint(2, "Error reading molecules!\n");
216 VJMPERR1(0);
217 }
218 */
219
220 /* alist fills nosh */
221 alist[0] = Valist_ctor();
222
223 alist[0]->center[0] = 0.;
224 alist[0]->center[1] = 0.;
225 alist[0]->center[2] = 0.;
226 alist[0]->maxcrd[0] = -VLARGE;
227 alist[0]->maxcrd[1] = -VLARGE;
228 alist[0]->maxcrd[2] = -VLARGE;
229 alist[0]->mincrd[0] = VLARGE;
230 alist[0]->mincrd[1] = VLARGE;
231 alist[0]->mincrd[2] = VLARGE;
232 alist[0]->maxrad = 0.;
233 alist[0]->charge = 0.;
234
235 alist[0]->number = *nat;
236 natom = alist[0]->number;
237 /* Allocate the necessary space for the atom array */
238 alist[0]->atoms = Vmem_malloc(alist[0]->vmem, alist[0]->number,
239 (sizeof(Vatom)));
240 VASSERT(alist[0]->atoms != VNULL);
241
242
243 for (i=0; i<alist[0]->number; i++) {
244 /* Fill atoms in the atom list */
245
246 /* atom[i]->partID = 1; FIXME */
247
248 if (x[i] < alist[0]->mincrd[0]) alist[0]->mincrd[0] = x[i];
249 if (y[i] < alist[0]->mincrd[1]) alist[0]->mincrd[1] = y[i];
250 if (z[i] < alist[0]->mincrd[2]) alist[0]->mincrd[2] = z[i];
251 if (x[i] > alist[0]->maxcrd[0]) alist[0]->maxcrd[0] = x[i];
252 if (y[i] > alist[0]->maxcrd[1]) alist[0]->maxcrd[1] = y[i];
253 if (z[i] > alist[0]->maxcrd[2]) alist[0]->maxcrd[2] = z[i];
254 if (radius[i] > alist[0]->maxrad) alist[0]->maxrad = radius[i];
255 alist[0]->charge = alist[0]->charge + charge[i];
256
257 coord[0] = x[i];
258 coord[1] = y[i];
259 coord[2] = z[i];
260 Vatom_setPosition(&(alist[0]->atoms)[i], coord);
261 Vatom_setCharge(&(alist[0]->atoms)[i], charge[i]);
262 Vatom_setRadius(&(alist[0]->atoms)[i], radius[i]);
263 Vatom_setAtomID(&(alist[0]->atoms)[i], i);
264 // not necessary? Vatom_setPartID(&(alist[0]->atoms)[i, );
265 }
266
267 alist[0]->center[0] = 0.5*(alist[0]->maxcrd[0] + alist[0]->mincrd[0]);
268 alist[0]->center[1] = 0.5*(alist[0]->maxcrd[1] + alist[0]->mincrd[1]);
269 alist[0]->center[2] = 0.5*(alist[0]->maxcrd[2] + alist[0]->mincrd[2]);
270
271 /* *************** SETUP CALCULATIONS *************** */
272 if (NOsh_setupElecCalc(nosh, alist) != 1) {
273 Vnm_tprint(2, "Error setting up ELEC calculations\n");
274 VJMPERR1(0);
275 }
276
277 if ((rc = NOsh_setupApolCalc(nosh, alist)) == ACD_ERROR) {
278 Vnm_tprint(2, "Error setting up APOL calculations\n");
279 VJMPERR1(0);
280 }
281
282 /* ******************* CHECK APOL********************** */
283 //if((nosh->gotparm == 0) && (rc == ACD_YES)){
284 // Vnm_print(1,"\nError you must provide a parameter file if you\n" \
285 // " are performing an APOLAR calculation\n");
286 // VJMPERR1(0);
287 //}
288
289 /* *************** LOAD MAPS ******************* */
290 if (loadDielMaps(nosh, dielXMap, dielYMap, dielZMap) != 1) {
291 Vnm_tprint(2, "Error reading dielectric maps!\n");
292 VJMPERR1(0);
293 }
294 if (loadKappaMaps(nosh, kappaMap) != 1) {
295 Vnm_tprint(2, "Error reading kappa maps!\n");
296 VJMPERR1(0);
297 }
298 if (loadPotMaps(nosh, potMap) != 1) {
299 Vnm_tprint(2, "Error reading potential maps!\n");
300 VJMPERR1(0);
301 }
302 if (loadChargeMaps(nosh, chargeMap) != 1) {
303 Vnm_tprint(2, "Error reading charge maps!\n");
304 VJMPERR1(0);
305 }
306
307 /* *************** Initialization ******************* */
308 // Do this initialization only if calcforce is requested.
309 for (i=0; i<nosh->ncalc; i++){
310 pbeparm = nosh->calc[i]->pbeparm;
311 if(pbeparm->calcforce >0) {
312 for (i=0; i < natom; i++) {
313 apbsdx[i] = 0.0;
314 apbsdy[i] = 0.0;
315 apbsdz[i] = 0.0;
316 apbsqfx[i] = 0.0;
317 apbsqfy[i] = 0.0;
318 apbsqfz[i] = 0.0;
319 apbsibx[i] = 0.0;
320 apbsiby[i] = 0.0;
321 apbsibz[i] = 0.0;
322 apbsdbx[i] = 0.0;
323 apbsdby[i] = 0.0;
324 apbsdbz[i] = 0.0;
325 apbsnpx[i] = 0.0;
326 apbsnpy[i] = 0.0;
327 apbsnpz[i] = 0.0;
328 }
329 // once is enough
330 break;
331 }
332 }
333
334 /* *************** DO THE CALCULATIONS ******************* */
335 if(debug>1) Vnm_tprint( 1, "Preparing to run %d PBE calculations.\n",
336 nosh->ncalc);
337 for (i=0; i<nosh->ncalc; i++) {
338 if(debug>1) Vnm_tprint( 1, "----------------------------------------\n");
339
340 switch (nosh->calc[i]->calctype) {
341 case NCT_MG:
342 /* What is this? This seems like a very awkward way to find
343 the right ELEC statement... */
344 for (k=0; k<nosh->nelec; k++) {
345 if (nosh->elec2calc[k] >= i) {
346 break;
347 }
348 }
349 if (Vstring_strcasecmp(nosh->elecname[k], "") == 0) {
350 if(debug>1) Vnm_tprint( 1, "CALCULATION #%d: MULTIGRID\n", i+1);
351 } else {
352 if(debug>1) Vnm_tprint( 1, "CALCULATION #%d (%s): MULTIGRID\n",
353 i+1, nosh->elecname[k]);
354 }
355 /* Useful local variables */
356 mgparm = nosh->calc[i]->mgparm;
357 pbeparm = nosh->calc[i]->pbeparm;
358 natom = alist[0]->number;
359
360 /* Set up problem */
361 if(debug>1) Vnm_tprint( 1, " Setting up problem...\n");
362 if (!initMG(i, nosh, mgparm, pbeparm, realCenter, pbe,
363 alist, dielXMap, dielYMap, dielZMap, kappaMap, chargeMap,
364 pmgp, pmg, potMap)) {
365 Vnm_tprint( 2, "Error setting up MG calculation!\n");
366 VJMPERR1(0);
367 }
368
369 /* Print problem parameters */
370 if(debug>0) printMGPARM(mgparm, realCenter);
371 if(debug>0) printPBEPARM(pbeparm);
372
373 /* Solve PDE */
374 if (solveMG(nosh, pmg[i], mgparm->type) != 1) {
375 Vnm_tprint(2, "Error solving PDE!\n");
376 VJMPERR1(0);
377 }
378
379 /* Set partition information for observables and I/O */
380 if (setPartMG(nosh, mgparm, pmg[i]) != 1) {
381 Vnm_tprint(2, "Error setting partition info!\n");
382 VJMPERR1(0);
383 }
384
385 /* Write out energies */
386 energyMG(nosh, i, pmg[i],
387 &(nenergy[i]), &(totEnergy[i]), &(qfEnergy[i]),
388 &(qmEnergy[i]), &(dielEnergy[i]));
389 esenergy[0] = 0.0;
390 esenergy[0] = getElecEnergy(com, nosh, totEnergy, i);
391
392 // if(debug>3) printElecEnergy(com, nosh, totEnergy, i);
393
394 /* Write out forces */
395 forceMG(mem, nosh, pbeparm, mgparm, pmg[i], &(nforce[i]),
396 &(atomForce[i]), alist);
397
398 //if (pbeparm->calcforce > 0 && i == nosh->ncalc-1) {
399 if (pbeparm->calcforce == PCF_TOTAL) {
400 // Vnm_tprint(2, "pcf_total\n");
401 apbsdx[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
402 (atomForce[i][0].qfForce[0] +
403 atomForce[i][0].ibForce[0] +
404 atomForce[i][0].dbForce[0]);
405 apbsdy[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
406 (atomForce[i][0].qfForce[1] +
407 atomForce[i][0].ibForce[1] +
408 atomForce[i][0].dbForce[1]);
409 apbsdz[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
410 (atomForce[i][0].qfForce[2] +
411 atomForce[i][0].ibForce[2] +
412 atomForce[i][0].dbForce[2]);
413
414 apbsqfx[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
415 atomForce[i][0].qfForce[0];
416 apbsqfy[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
417 atomForce[i][0].qfForce[1];
418 apbsqfz[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
419 atomForce[i][0].qfForce[2];
420
421 apbsibx[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
422 atomForce[i][0].ibForce[0];
423 apbsiby[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
424 atomForce[i][0].ibForce[1];
425 apbsibz[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
426 atomForce[i][0].ibForce[2];
427
428 apbsdbx[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
429 atomForce[i][0].dbForce[0];
430 apbsdby[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
431 atomForce[i][0].dbForce[1];
432 apbsdbz[0] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
433 atomForce[i][0].dbForce[2];
434 }
435
436 if (pbeparm->calcforce == PCF_COMPS) {
437 // Vnm_tprint(2, "pcf_comps\n");
438 for (j=0; j < natom; j++) {
439
440 apbsdx[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
441 (atomForce[i][j].qfForce[0] +
442 atomForce[i][j].ibForce[0] +
443 atomForce[i][j].dbForce[0]);
444 apbsdy[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
445 (atomForce[i][j].qfForce[1] +
446 atomForce[i][j].ibForce[1] +
447 atomForce[i][j].dbForce[1]);
448 apbsdz[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
449 (atomForce[i][j].qfForce[2] +
450 atomForce[i][j].ibForce[2] +
451 atomForce[i][j].dbForce[2]);
452
453 /* individual components */
454 apbsqfx[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
455 atomForce[i][j].qfForce[0];
456 apbsqfy[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
457 atomForce[i][j].qfForce[1];
458 apbsqfz[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
459 atomForce[i][j].qfForce[2];
460
461 apbsibx[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
462 atomForce[i][j].ibForce[0];
463 apbsiby[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
464 atomForce[i][j].ibForce[1];
465 apbsibz[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
466 atomForce[i][j].ibForce[2];
467
468 apbsdbx[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
469 atomForce[i][j].dbForce[0];
470 apbsdby[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
471 atomForce[i][j].dbForce[1];
472 apbsdbz[j] = Vunit_kb*pbeparm->temp*(1e-3)*Vunit_Na *
473 atomForce[i][j].dbForce[2];
474 }
475 // if(debug>3) printElecForce(com, nosh, nforce, atomForce, i);
476 }
477 /* Write grid-dimensioned data to file if that's what user wants*/
478 if( is_grid2file ) {
479 writedataMG(rank, nosh, pbeparm, pmg[i]);
480 }
481 /* Return in-memeory instead */
482 else {
483 for( k=0; k<pbeparm->numwrite; k++ ) {
484 int nx = pmg[i]->pmgp->nx;
485 int ny = pmg[i]->pmgp->ny;
486 int nz = pmg[i]->pmgp->nz;
487 double hx = pmg[i]->pmgp->hx;
488 double hy = pmg[i]->pmgp->hy;
489 double hz = pmg[i]->pmgp->hzed;
490 double centx = pmg[i]->pmgp->xcent;
491 double centy = pmg[i]->pmgp->ycent;
492 double centz = pmg[i]->pmgp->zcent;
493 double parm;
494 Vdata_Type data_type = pbeparm->writetype[k];
495 switch(data_type) {
496 case VDT_SMOL:
497 parm = pbeparm->srad;
498 break;
499 case VDT_SSPL:
500 parm = pbeparm->swin;
501 break;
502 case VDT_IVDW:
503 parm = pmg[i]->pbe->maxIonRadius;
504 break;
505 case VDT_DIELX:
506 centx +=0.5*hz;
507 parm = 0;
508 break;
509 case VDT_DIELY:
510 centy +=0.5*hz;
511 parm = 0;
512 break;
513 case VDT_DIELZ:
514 centz +=0.5*hz;
515 parm = 0;
516 break;
517 case VDT_CHARGE:
518 case VDT_POT:
519 case VDT_VDW:
520 case VDT_LAP:
521 case VDT_EDENS:
522 case VDT_NDENS:
523 case VDT_QDENS:
524 case VDT_KAPPA:
525 case VDT_ATOMPOT:
526 parm = 0;
527 break;
528 default:
529 Vnm_print(2, "Warning! Skipping invalid data type for writing: %d\n", k);
530 continue;
531 }
532
533 apbsgrid_meta[1] = nx;
534 apbsgrid_meta[2] = ny;
535 apbsgrid_meta[3] = nz;
536 apbsgrid_meta[4] = hx;
537 apbsgrid_meta[5] = hy;
538 apbsgrid_meta[6] = hz;
539 apbsgrid_meta[7] = centx;
540 apbsgrid_meta[8] = centy;
541 apbsgrid_meta[9] = centz;
542 apbsgrid_meta[10] = centx - 0.5*(nx-1)*hx;
543 apbsgrid_meta[11] = centy - 0.5*(ny-1)*hy;
544 apbsgrid_meta[12] = centz - 0.5*(nz-1)*hz;
545
546 Vpmg_fillArray(pmg[i], apbsgrid[k], data_type, parm,
547 pbeparm->pbetype, pbeparm);
548 }
549 }
550 /* Write matrix */
551 writematMG(rank, nosh, pbeparm, pmg[i]);
552
553 /* If needed, cache atom energies */
554 nenergy[i] = 0;
555 if ((pbeparm->calcenergy == PCE_COMPS) && (outputformat != OUTPUT_NULL)){
556 storeAtomEnergy(pmg[i], i, &(atomEnergy[i]), &(nenergy[i]));
557 }
558
559 /* clean up memory after final run - gets around APBS memory leak */
560 if (i == nosh->elec2calc[k] ) {
561 if(debug>4) Vnm_tprint( 1, "Cleaning %d pmg memory segment\n", i);
562 Vpmg_dtor(&pmg[i]);
563 killMG(nosh, pbe, pmgp, pmg);
564 }
565
566 fflush(stdout);
567 fflush(stderr);
568
569 break;
570
571 /* ***** Do FEM calculation ***** */
572 case NCT_FEM:
573 #ifdef HAVE_MC
574 for (k=0; k<nosh->nelec; k++) {
575 if (nosh->elec2calc[k] >= i) break;
576 }
577 if (Vstring_strcasecmp(nosh->elecname[i+1], "") == 0) {
578 Vnm_tprint( 1, "CALCULATION #%d: FINITE ELEMENT\n", i+1);
579 } else {
580 Vnm_tprint( 1, "CALCULATION #%d (%s): FINITE ELEMENT\n", i+1, nosh->elecname[k+1]);
581 }
582
583 /* Useful local variables */
584 feparm = nosh->calc[i]->femparm;
585 pbeparm = nosh->calc[i]->pbeparm;
586
587 /* Warn the user about some things */
588 Vnm_tprint(2, "#################### WARNING ###################\n");
589 Vnm_tprint(2, "## FE support is currently very experimental! ##\n");
590 Vnm_tprint(2, "#################### WARNING ###################\n");
591
592 /* Set up problem */
593 Vnm_tprint( 1, " Setting up problem...\n");
594 if (!initFE(i, nosh, feparm, pbeparm, pbe, alist, fetk)) {
595 Vnm_tprint( 2, "Error setting up FE calculation!\n");
596 VJMPERR1(0);
597 }
598
599 /* Print problem parameters */
600 printFEPARM(i, nosh, feparm, fetk);
601 printPBEPARM(pbeparm);
602
603 /* Refine mesh */
604 if (!preRefineFE(i, feparm, fetk)) {
605 Vnm_tprint( 2, "Error pre-refining mesh!\n");
606 VJMPERR1(0);
607 }
608
609 /* Solve-estimate-refine */
610 Vnm_tprint(2, "\n\nWARNING! DO NOT EXPECT PERFORMANCE OUT OF THE APBS/FEtk\n");
611 Vnm_tprint(2, "INTERFACE AT THIS TIME. THE FINITE ELEMENT SOLVER IS\n");
612 Vnm_tprint(2, "CURRENTLY NOT OPTIMIZED FOR THE PB EQUATION. IF YOU WANT\n");
613 Vnm_tprint(2, "PERFORMANCE, PLEASE USE THE MULTIGRID-BASED METHODS, E.G.\n");
614 Vnm_tprint(2, "MG-AUTO, MG-PARA, and MG-MANUAL (SEE DOCS.)\n\n");
615 Vnm_tprint(1, " Beginning solve-estimate-refine cycle:\n");
616 for (isolve=0; isolve<feparm->maxsolve; isolve++) {
617 Vnm_tprint(1, " Solve #%d...\n", isolve);
618 if (!solveFE(i, pbeparm, feparm, fetk)) {
619 Vnm_tprint(2, "ERROR SOLVING EQUATION!\n");
620 VJMPERR1(0);
621 }
622 if (!energyFE(nosh, i, fetk, &(nenergy[i]),
623 &(totEnergy[i]), &(qfEnergy[i]),
624 &(qmEnergy[i]), &(dielEnergy[i]))) {
625 Vnm_tprint(2, "ERROR SOLVING EQUATION!\n");
626 VJMPERR1(0);
627 }
628 /* We're not going to refine if we've hit the max number
629 * of solves */
630 if (isolve < (feparm->maxsolve)-1) {
631 if (!postRefineFE(i, feparm, fetk)) break;
632 }
633 bytesTotal = Vmem_bytesTotal();
634 highWater = Vmem_highWaterTotal();
635 Vnm_tprint(1, " Currently memory use: %g MB\n",
636 ((double)bytesTotal/(1024.)/(1024.)));
637 Vnm_tprint(1, " High-water memory use: %g MB\n",
638 ((double)highWater/(1024.)/(1024.)));
639 }
640
641 Vnm_tprint(1, " Writing FEM data to files.\n");
642 if (!writedataFE(rank, nosh, pbeparm, fetk[i])) {
643 Vnm_tprint(2, " Error while writing FEM data!\n");
644 }
645 #else /* ifdef HAVE_MC */
646 Vnm_print(2, "Error! APBS not compiled with FEtk!\n");
647 exit(2);
648 #endif /* ifdef HAVE_MC */
649 break;
650
651 /* Do an apolar calculation */
652 case NCT_APOL:
653 /* Copied from NCT_MG. See the note above (top of loop) for
654 information about this loop.
655 */
656 for (k=0; k<nosh->napol; k++) {
657 if (nosh->apol2calc[k] >= i) {
658 break;
659 }
660 }
661
662 if (Vstring_strcasecmp(nosh->apolname[k], "") == 0) {
663 if(debug>1) Vnm_tprint( 1, "CALCULATION #%d: APOLAR\n", i+1);
664 } else {
665 if(debug>1) Vnm_tprint( 1, "CALCULATION #%d (%s): APOLAR\n",
666 i+1, nosh->apolname[k]);
667 }
668
669 apolparm = nosh->calc[i]->apolparm;
670 rc = initAPOL(nosh, mem, param, apolparm, &(nforce[i]), &(atomForce[i]),
671 alist[(apolparm->molid)-1]);
672 if(rc == 0) {
673 Vnm_tprint(2, "Error calculating apolar solvation quantities!\n");
674 VJMPERR1(0);
675 }
676
677 if(debug>3) printf("energyAPOL: %1.12E\n", apolparm->gamma*apolparm->sasa);
678 npenergy[0] = 0.0;
679 npenergy[0] = apolparm->gamma*apolparm->sasa;
680
681 //if (pbeparm->calcforce > 0 && i == nosh->ncalc-1) {
682 // if (pbeparm->calcforce > 1) {
683 if (apolparm->calcforce == ACF_COMPS) {
684 for (j=0; j < natom; j++) {
685 apbsnpx[j] = (atomForce[i][j]).sasaForce[0];
686 apbsnpy[j] = (atomForce[i][j]).sasaForce[1];
687 apbsnpz[j] = (atomForce[i][j]).sasaForce[2];
688 }
689 }
690 //if(debug>3) printApolEnergy(nosh, i);
691 //if(debug>3) printApolForce(com, nosh, nforce, atomForce, i);
692 break;
693 default:
694 Vnm_tprint(2, " Unknown calculation type (%d)!\n",
695 nosh->calc[i]->calctype);
696 exit(2);
697 }
698 }
699
700 //Clear out the parameter file memory
701 if(param != VNULL) Vparam_dtor(&param);
702
703 /* *************** HANDLE PRINT STATEMENTS ******************* */
704 if (nosh->nprint > 0) {
705 Vnm_tprint( 1, "----------------------------------------\n");
706 Vnm_tprint( 1, "PRINT STATEMENTS\n");
707 }
708 for (i=0; i<nosh->nprint; i++) {
709 /* Print energy */
710 if (nosh->printwhat[i] == NPT_ENERGY) {
711 printEnergy(com, nosh, totEnergy, i);
712 /* Print force */
713 } else if (nosh->printwhat[i] == NPT_FORCE) {
714 if(debug>6) printForce(com, nosh, nforce, atomForce, i);
715 } else if (nosh->printwhat[i] == NPT_ELECENERGY) {
716 if(debug>3) printElecEnergy(com, nosh, totEnergy, i);
717 //esEnergy[0] = getElecEnergy(com, nosh, totEnergy, i);
718 } else if (nosh->printwhat[i] == NPT_ELECFORCE) {
719 if(debug>6) printElecForce(com, nosh, nforce, atomForce, i);
720 } else if (nosh->printwhat[i] == NPT_APOLENERGY) {
721 if(debug>3) printApolEnergy(nosh, i);
722 } else if (nosh->printwhat[i] == NPT_APOLFORCE) {
723 if(debug>6) printApolForce(com, nosh, nforce, atomForce, i);
724 } else {
725 Vnm_tprint( 2, "Undefined PRINT keyword!\n");
726 break;
727 }
728 }
729 if(debug>1) Vnm_tprint( 1, "----------------------------------------\n");
730
731 /* *************** HANDLE LOGGING *********************** */
732
733 if (outputformat == OUTPUT_FLAT) {
734 Vnm_tprint(2," Writing data to flat file %s...\n\n", output_path);
735 writedataFlat(nosh, com, output_path, totEnergy, qfEnergy, qmEnergy,
736 dielEnergy, nenergy, atomEnergy, nforce, atomForce);
737 }
738
739 /* Destroy energy arrays if they still exist */
740
741 for (i=0; i<nosh->ncalc; i++) {
742 if (nenergy[i] > 0) Vmem_free(mem, nenergy[i], sizeof(double),
743 (void **)&(atomEnergy[i]));
744 }
745
746 /* *************** GARBAGE COLLECTION ******************* */
747
748 //Vnm_tprint( 1, "CLEANING UP AND SHUTTING DOWN...\n");
749 /* Clean up APBS structures */
750 killForce(mem, nosh, nforce, atomForce);
751 killEnergy();
752 killMG(nosh, pbe, pmgp, pmg);
753 #ifdef HAVE_MC
754 killFE(nosh, pbe, fetk, gm);
755 #endif
756 killChargeMaps(nosh, chargeMap);
757 killKappaMaps(nosh, kappaMap);
758 killDielMaps(nosh, dielXMap, dielYMap, dielZMap);
759 killMolecules(nosh, alist);
760 NOsh_dtor(&nosh);
761
762 /* Memory statistics */
763 bytesTotal = Vmem_bytesTotal();
764 highWater = Vmem_highWaterTotal();
765 if(debug>1) Vnm_tprint( 1, "Final memory usage: %4.3f MB total, %4.3f MB high water\n",
766 (double)(bytesTotal)/(1024.*1024.),
767 (double)(highWater)/(1024.*1024.));
768
769 /* Clean up MALOC structures */
770 Vcom_dtor(&com);
771 Vmem_dtor(&mem);
772
773 /* And now it's time to so "so long"... */
774 //Vnm_tprint(1, "\n\n");
775 //Vnm_tprint( 1, "Thanks for using APBS!\n\n");
776
777 /* This should be last */
778 Vnm_tstop(APBS_TIMER_WALL_CLOCK, "APBS WALL CLOCK");
779 Vnm_flush(1);
780 Vnm_flush(2);
781 // Vcom_finalize();
782 fflush(NULL);
783 return 0;
784
785 VERROR1:
786 Vcom_finalize();
787 Vcom_dtor(&com);
788 Vmem_dtor(&mem);
789 return APBSRC;
790 }
791
792
793 /**
794 * @brief Creates APBS input string
795 * @author Robert Konecny
796 */
797 char *setupString(double r_param[9], int i_param[25], double grid[3],
798 int dime[3], double ionq[MAXION], double ionc[MAXION],
799 double ionr[MAXION], double glen[3], double center[3],
800 double cglen[3], double fglen[3],
801 double ccenter[3], double fcenter[3], double *ofrac,
802 int pdime[3], int debug)
803 {
804 static char string[MAX_BUF_SIZE];
805 static char mybuf[MAX_BUF_SIZE];
806 int i;
807 // read section
808 strcpy(string, "read\n mol pqr ion.pqr\n");
809
810 if(i_param[22] == 1){
811 strcat(string, "charge dx iapbs-charge.dx\n");
812 }
813 if(i_param[23] == 1){
814 strcat(string, "kappa dx iapbs-kappa.dx\n");
815 }
816 if(i_param[24] == 1){
817 strcat(string, "diel dx iapbs-dielx.dx iapbs-diely.dx iapbs-dielz.dx\n");
818 }
819 if(i_param[17] == 1){
820 strcat(string, "pot dx iapbs-pot.dx\n");
821 }
822 strcat(string, "end\n\n");
823
824 // elec section
825 strcat(string, "elec name elec\n");
826 switch(i_param[0]){
827 case 0: sprintf(mybuf, "mg-manual\n"); break;
828 case 1: sprintf(mybuf, "mg-auto\n"); break;
829 case 2: sprintf(mybuf, "mg-para\n"); break;
830 default: printf("iAPBS: mg- keyword error\n"); exit(2);
831 }
832 strcat(string, mybuf);
833
834 sprintf(mybuf, "dime %i %i %i\n", dime[0], dime[1], dime[2]);
835 strcat(string,mybuf);
836 if (i_param[0] == 0 && grid[0] > 0.) {
837 sprintf(mybuf, "grid %.3f %.3f %.3f\n", grid[0], grid[1], grid[2]);
838 strcat(string,mybuf);
839 // nlev is deprecated now (2009/12)
840 sprintf(mybuf, "# nlev %i\n", i_param[1]);
841 strcat(string,mybuf);
842 }
843 if (i_param[0] == 0 && glen[0] > 0.) {
844 sprintf(mybuf, "glen %.3f %.3f %.3f\n", glen[0], glen[1], glen[2]);
845 strcat(string,mybuf);
846 }
847
848 if (i_param[0] == 0 && i_param[2] == 0) {
849 sprintf(mybuf, "gcent %.3f %.3f %.3f\n", center[0], center[1], center[2]);
850 strcat(string,mybuf);
851 } else if (i_param[0] == 0 && i_param[2] == 1) {
852 strcat(string, "gcent mol 1\n");
853 }
854 if (i_param[0] != 0 && cglen[0] > 0.) {
855 sprintf(mybuf, "cglen %.3f %.3f %.3f\n", cglen[0], cglen[1], cglen[2]);
856 strcat(string,mybuf);
857 }
858 if (i_param[0] != 0 && fglen[0] > 0.) {
859 sprintf(mybuf, "fglen %.3f %.3f %.3f\n", fglen[0], fglen[1], fglen[2]);
860 strcat(string,mybuf);
861 }
862 if (i_param[0] != 0 && i_param[3] == 0) {
863 sprintf(mybuf, "cgcent %.3f %.3f %.3f\n",
864 ccenter[0], ccenter[1], ccenter[2]);
865 strcat(string,mybuf);
866 } else if (i_param[0] != 0 && i_param[3] == 1) {
867 strcat(string, "cgcent mol 1\n");
868 }
869 if (i_param[0] != 0 && i_param[4] == 0) {
870 sprintf(mybuf, "fgcent %.3f %.3f %.3f\n",
871 fcenter[0], fcenter[1], fcenter[2]);
872 strcat(string,mybuf);
873 } else if (i_param[0] != 0 && i_param[4] == 1) {
874 strcat(string, "fgcent mol 1\n");
875 }
876
877 if (i_param[0] == 2) {
878 sprintf(mybuf, "pdime %i %i %i\n", pdime[0], pdime[1], pdime[2]);
879 strcat(string,mybuf);
880 sprintf(mybuf, "ofrac %.3f\n", *ofrac);
881 strcat(string,mybuf);
882 }
883
884 for (i=0; i < i_param[21]; i++) {
885 sprintf(mybuf, "ion charge %.3f conc %.3f radius %.3f\n",
886 ionq[i], ionc[i], ionr[i]);
887 strcat(string,mybuf);
888 }
889 sprintf(mybuf, "pdie %.3f \nsdie %.3f\nsrad %.3f\nswin %.3f\n\
890 temp %.3f\nsdens %.3f\nmol 1\n", r_param[0], r_param[1], r_param[2],
891 r_param[3], r_param[4], r_param[5]);
892 strcat(string, mybuf);
893
894 switch(i_param[5]){
895 case 0: sprintf(mybuf, "chgm spl0\n"); break;
896 case 1: sprintf(mybuf, "chgm spl2\n"); break;
897 case 2: sprintf(mybuf, "chgm spl4\n"); break;
898 default: printf("iAPBS: chgm keyword error\n"); exit(2);
899 }
900 strcat(string, mybuf);
901 switch(i_param[6]){
902 case 0: sprintf(mybuf, "lpbe\n"); break;
903 case 1: sprintf(mybuf, "npbe\n"); break;
904 case 2: sprintf(mybuf, "lrpbe\n"); break;
905 case 3: sprintf(mybuf, "nrpbe\n"); break;
906 case 4: sprintf(mybuf, "smpbe vol %.3f size %.3f\n",
907 r_param[7], r_param[8]); break;
908 default: printf("iAPBS: PBE keyword error\n"); exit(2);
909 }
910 strcat(string, mybuf);
911 switch(i_param[7]){
912 case 0: sprintf(mybuf, "bcfl zero\n"); break;
913 case 1: sprintf(mybuf, "bcfl sdh\n"); break;
914 case 2: sprintf(mybuf, "bcfl mdh\n"); break;
915 case 4: sprintf(mybuf, "bcfl focus\n"); break;
916 default: printf("iAPBS: bcfl keyword error\n"); exit(2);
917 }
918 strcat(string, mybuf);
919 switch(i_param[8]){
920 case 0: sprintf(mybuf, "srfm mol\n"); break;
921 case 1: sprintf(mybuf, "srfm smol\n"); break;
922 case 2: sprintf(mybuf, "srfm spl2\n"); break;
923 case 3: sprintf(mybuf, "srfm spl4\n"); break;
924 default: printf("iAPBS: srfm keyword error\n"); exit(2);
925 }
926 strcat(string, mybuf);
927 switch(i_param[9]){
928 case 0: sprintf(mybuf, "calcenergy no\n"); break;
929 case 1: sprintf(mybuf, "calcenergy total\n"); break;
930 case 2: sprintf(mybuf, "calcenergy comps\n"); break;
931 default: printf("iAPBS: calcenergy keyword error\n"); exit(2);
932 }
933 strcat(string, mybuf);
934 switch(i_param[10]){
935 case 0: sprintf(mybuf, "calcforce no\n"); break;
936 case 1: sprintf(mybuf, "calcforce total\n"); break;
937 case 2: sprintf(mybuf, "calcforce comps\n"); break;
938 default: printf("iAPBS: calcforce keyword error\n"); exit(2);
939 }
940 strcat(string, mybuf);
941
942 if(i_param[11] == 1){
943 // strcat(string, "write pot dx iapbs-pot\n");
944 sprintf(string, "%swrite pot dx iapbs-pot-%d\n", string, getpid());
945 }
946 if(i_param[12] == 1){
947 strcat(string, "write charge dx iapbs-charge\n");
948 }
949 if(i_param[13] == 1){
950 strcat(string, "write smol dx iapbs-smol\n");
951 }
952 if(i_param[14] == 1){
953 strcat(string, "write kappa dx iapbs-kappa\n");
954 }
955 if(i_param[15] == 1){
956 strcat(string, "write dielx dx iapbs-dielx\n");
957 strcat(string, "write diely dx iapbs-diely\n");
958 strcat(string, "write dielz dx iapbs-dielz\n");
959 }
960 if(i_param[16] == 1){
961 strcat(string, "write atompot dx iapbs-atompot\n");
962 }
963
964
965 if(i_param[22] == 1){
966 strcat(string, "usemap charge 1\n");
967 }
968 if(i_param[23] == 1){
969 strcat(string, "usemap kappa 1\n");
970 }
971 if(i_param[24] == 1){
972 strcat(string, "usemap diel 1\n");
973 }
974 if(i_param[17] == 1){
975 strcat(string, "usemap pot 1\n");
976 }
977 strcat(string, "end\n\n");
978
979 // apolar section
980
981 if(i_param[20] > 0){
982 strcat(string, "apolar name npolar\n");
983 strcat(string, "bconc 0.0\npress 0.0\ndpos 0.2\n");
984 strcat(string, "mol 1\nsrfm sacc\n");
985 strcat(string, "grid 0.2 0.2 0.2\n");
986 sprintf(mybuf, "gamma %.3f\n", r_param[6]);
987 strcat(string, mybuf);
988 sprintf(mybuf, "srad %.3f\nswin %.3f\ntemp %.3f\nsdens %.3f\n", \
989 r_param[2], r_param[3], r_param[4], r_param[5]);
990 strcat(string, mybuf);
991
992 switch(i_param[20]){
993 case 1: sprintf(mybuf, "calcenergy total\n"); break;
994 case 2: sprintf(mybuf, "calcenergy comps\n"); break;
995 default: printf("iAPBS: Unknown calcnpenergy option!\n"); exit(2);
996 }
997 strcat(string, mybuf);
998
999 switch(i_param[19]){
1000 case 0: sprintf(mybuf, "calcforce no\n"); break;
1001 case 1: sprintf(mybuf, "calcforce total\n"); break;
1002 case 2: sprintf(mybuf, "calcforce comps\n"); break;
1003 default: printf("iAPBS: Unknown calcnpforce option!\n"); exit(2);
1004 }
1005 strcat(string, mybuf);
1006
1007 strcat(string, "end\n\n");
1008 }
1009
1010 // print section
1011 if (debug > 3) {
1012 strcat(string, "print elecEnergy elec end\n");
1013 if(i_param[20] > 0 ){
1014 strcat(string, "print apolEnergy npolar end\n");
1015 }
1016 }
1017 if (debug > 6) {
1018 if(i_param[10] > 0 ){
1019 strcat(string, "print elecForce elec end\n");
1020 }
1021 if(i_param[20] > 0 && i_param[19] > 0 ) {
1022 strcat(string, "print apolForce npolar end\n");
1023 }
1024 }
1025
1026 strcat(string, "\nquit\n");
1027 //printf("%s", string);
1028 return string;
1029 }
1030
1031 /**
1032 * @note Collect Elect Energy
1033 *
1034 *
1035 * @author Robert Konecny
1036 *
1037 */
1038 //VPUBLIC double getElecEnergy(Vcom *com, NOsh *nosh,
1039 double getElecEnergy(Vcom *com, NOsh *nosh,
1040 double totEnergy[NOSH_MAXCALC], int iprint)
1041 {
1042
1043 int iarg, calcid;
1044 double ltenergy, gtenergy, scalar;
1045
1046 calcid = nosh->elec2calc[nosh->printcalc[iprint][0]];
1047 if (nosh->calc[calcid]->pbeparm->calcenergy != PCE_NO) {
1048 ltenergy = Vunit_kb * (1e-3) * Vunit_Na *
1049 nosh->calc[calcid]->pbeparm->temp * totEnergy[calcid];
1050 } else {
1051 Vnm_tprint( 2, " Didn't calculate energy in Calculation \
1052 #%d\n", calcid+1);
1053 return 0;
1054 }
1055 for (iarg=1; iarg<nosh->printnarg[iprint]; iarg++) {
1056 calcid = nosh->elec2calc[nosh->printcalc[iprint][iarg]];
1057 /* Add or subtract? */
1058 if (nosh->printop[iprint][iarg-1] == 0) scalar = 1.0;
1059 else if (nosh->printop[iprint][iarg-1] == 1) scalar = -1.0;
1060 /* Accumulate */
1061 ltenergy += (scalar * Vunit_kb * (1e-3) * Vunit_Na *
1062 nosh->calc[calcid]->pbeparm->temp * totEnergy[calcid]);
1063 }
1064
1065 // Vnm_tprint( 1, " Local net energy (PE %d) = %1.12E kJ/mol\n",
1066 // Vcom_rank(com), ltenergy);
1067 // Vnm_tprint( 0, "printEnergy: Performing global reduction (sum)\n");
1068 Vcom_reduce(com, &ltenergy, &gtenergy, 1, 2, 0);
1069 // Vnm_tprint( 1, " Global net ELEC energy = %1.12E kJ/mol\n", gtenergy);
1070 //printf("esenF %f\n", gtenergy);
1071 return gtenergy;
1072 }
1073