Debian Codegraph @

contrib/iapbs/modules/NAMD/GlobalMasterAPBS.C

raw
1 /**
2 *** Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by
3 *** The Board of Trustees of the University of Illinois.
4 *** All rights reserved.
5 **/
6
7 /**
8 *
9 * @file GlobalMasterAPBS.C
10 * @author Justin Gullingsrud and Robert Konecny
11 * @brief NAMD/APBS module
12 * @note Energy is returned in kcal/mol, forces in kcal/(mol/A).
13 *
14 *
15 */
16
17 /*
18 * This is iAPBS/NAMD module for performing APBS calculations in namd.
19 * For more information please see http://mccammon.ucsd.edu/iapbs/
20 *
21 */
22
23 #ifdef NAMD_APBS
24
25 #include "GlobalMasterAPBS.h"
26 #include "InfoStream.h"
27 #include "Node.h"
28 #include "SimParameters.h"
29 #include "Vector.h"
30 #include "Molecule.h"
31 #include "ReductionMgr.h"
32 #include "ParseOptions.h"
33 #include "Parameters.h"
34
35 #include "apbs/apbs.h"
36 #include "apbs/routines.h"
37 #include "apbs/apbscfg.h"
38 #include "iapbs/apbs_driver.h"
39
40 //#define DEBUGM
41 #define MIN_DEBUG_LEVEL 1
42 #include "Debug.h"
43
44 // convert from APBS energy units (kJ) to NAMD energy units (kcal)
45 #define APBS_ENERGY_UNITS 0.2390057
46
47 static void parsePQRFile(const char *path, int numAtoms,
48 double *charges, double *radii);
49
50 class APBSParameters {
51 public:
52 int debug;
53 int calc_type;
54 int nonlin;
55 int bcfl;
56 int srfm;
57 int nlev;
58 BigReal pdie;
59 BigReal sdie;
60 BigReal sdens;
61 BigReal srad;
62 BigReal swin;
63 BigReal temp;
64 BigReal gamma;
65 BigReal smvolume;
66 BigReal smsize;
67 int chgm;
68 int calcenergy;
69 int calcforce;
70 int calcnpenergy;
71 int calcnpforce;
72 Bool wpot;
73 Bool wchg;
74 Bool wsmol;
75 Bool wkappa;
76 Bool wdiel;
77 Bool watompot;
78 Bool rpot;
79 Bool rchg;
80 Bool rkappa;
81 Bool rdiel;
82 BigReal ionq[MAXION];
83 BigReal ionc[MAXION];
84 BigReal ionr[MAXION];
85 int cmeth;
86 Vector center;
87 int ccmeth;
88 Vector ccenter;
89 int fcmeth;
90 Vector fcenter;
91 Vector grid;
92 Vector glen;
93 Vector cglen;
94 Vector fglen;
95 BigReal ofrac;
96 Bool sp_apbs;
97 int verbose;
98 Bool recalculateGrid;
99
100 double r_param[9];
101 int i_param[25];
102
103 int dime[3];
104 int pdime[3];
105
106 APBSParameters();
107 void parse(StringList *script);
108
109 private:
110 void config_parser(ParseOptions &opts);
111 void check_config(ParseOptions &opts, ConfigList &config);
112 };
113
114 APBSParameters::APBSParameters() {
115 memset(this, 0, sizeof(APBSParameters));
116 }
117
118 void APBSParameters::config_parser(ParseOptions &opts) {
119 opts.optional("main", "calc_type", "Calculation type: 0: mg-manual; 1: mg-auto; 2: mg-para", &calc_type, 1);
120 opts.optional("main", "nlev", " Levels in multigrid hierarchy nlev?", &nlev, 0);
121 opts.optional("main", "debug", "APBS Debugging level", &debug, 0);
122 opts.optional("main", "nonlin", "Use NLPB?", &nonlin, 0);
123 opts.optional("main", "bcfl", "Boundary condition method", &bcfl, 1);
124 opts.optional("main", "srfm", "Surface calculation method", &srfm, 2);
125 opts.optional("main", "pdie", "Solute dielectric", &pdie, 2.0);
126 opts.optional("main", "sdie", "Solvent dielectric", &sdie, 78.4);
127 opts.optional("main", "sdens", "Vacc sphere density", &sdens, 10.0);
128 opts.optional("main", "srad", "Solvent radius", &srad, 1.4);
129 opts.optional("main", "swin", "Cubic spline window", &swin, 0.3);
130 opts.optional("main", "temp", "Temperature (in K)", &temp, 298.15);
131 opts.optional("main", "gamma", "Surface tension for apolar energies/forces (in kJ/mol/A^2)", &gamma, 0.105);
132 opts.optional("main", "smvolume", "smvolume", &smvolume, 10.0);
133 opts.optional("main", "smsize", "smsize", &smsize, 1000.0);
134 opts.optional("main", "chgm", "Charge discretization method: 0: spl0; 1: spl2", &chgm, 1);
135 opts.optional("main", "calcenergy", "Energy calculation flag: 0: Do not perform energy calculation; 1: Calculate total energy only; 2: Calculate per-atom energy components", &calcenergy, 2);
136 opts.optional("main", "calcforce", "Atomic forces calculation: 0: Do not perform force calculation; 1: Calculate total force only; 2: Calculate per-atom force components", &calcforce, 2);
137 opts.optional("main", "calcnpenergy", "NP energy calculation: 0: Do not perform NP energy calculation; 1: Calculate total NP energy only; 2: Calculate per-atom NP energy components", &calcnpenergy, 1);
138 opts.optional("main", "calcnpforce", "NP force calculation: 0: Do not perform NP force calculation; 1: Calculate total NP force only; 2: Calculate per-atom NP force components", &calcnpforce, 2);
139 opts.optionalB("main", "wpot", "Write electrostatic potential to iapbs-pot.dx?", &wpot, FALSE);
140 opts.optionalB("main", "watompot", "Write electrostatic potential at each atom to iapbs-atompot.dx?", &watompot, FALSE);
141 opts.optionalB("main", "wchg", "Write charge data to iapbs-charge.dx?", &wchg, FALSE);
142 opts.optionalB("main", "wsmol", "Write molecular surface data to iapbs-smol.dx?", &wsmol, FALSE);
143 opts.optionalB("main", "wkappa", "Write kappa data to iapbs-kappa.dx?", &wkappa, FALSE);
144 opts.optionalB("main", "wdiel", "Write the dielectric map to iapbs-diel[x,y,z].dx?", &wdiel, FALSE);
145 opts.optionalB("main", "rkappa", "Read kappa map from iapbs-kappa.dx?", &rkappa, FALSE);
146 opts.optionalB("main", "rchg", "Read charge map from iapbs-charge.dx?", &rchg, FALSE);
147 opts.optionalB("main", "rdiel", "Reade the dielectric map from iapbs-diel[x,y,z].dx?", &rdiel, FALSE);
148 opts.optionalB("main", "rpot", "Reade pot map from iapbs-pot[x,y,z].dx?", &rpot, FALSE);
149 opts.optional("main", "ion", "Counterion charge [e], conc [M], radius [A]", PARSE_MULTIPLES);
150 opts.optional("main", "dime", "Grid dimensions (in x, y and z)", PARSE_STRING);
151 opts.optional("main", "cmeth", "Centering method: 0: Center on a point 1: Center on a molecule", &cmeth, 1);
152 opts.optional("main", "center", "Grid center if cmeth=0", &center);
153 opts.optional("main", "ccmeth", "Coarse grid centering method: 0: Center on a point 1: Center on a molecule", &ccmeth, 1);
154 opts.optional("main", "ccenter", "Coarse grid center if ccmeth=0", &ccenter);
155 opts.optional("main", "fcmeth", "Fine grid centering method: 0: Center on a point 1: Center on a molecule", &fcmeth, 1);
156 opts.optional("main", "fcenter", "Fine grid center if fcmeth=0", &fcenter);
157 opts.optional("main", "grid", "Grid spacings", &grid);
158 opts.optional("main", "glen", "Grid side lengths", &glen);
159 opts.optional("main", "cglen", "Coarse grid side lengths", &cglen);
160 opts.optional("main", "fglen", "Fine grid side lengths", &fglen);
161 opts.optional("main", "pdime", "Grid of processors to be used in calculation", PARSE_STRING);
162 opts.optional("main", "ofrac", "Overlap fraction between processors", &ofrac, 0.1);
163 opts.optionalB("main", "sp_apbs", "Perform single point energy calculation", &sp_apbs, FALSE);
164 opts.optionalB("main", "recalculateGrid", "Recalculate grid size on the fly", &recalculateGrid, FALSE);
165 opts.optional("main", "verbose", "APBS verbosity level", &verbose, 0);
166 }
167
168 void APBSParameters::check_config(ParseOptions &opts, ConfigList &config) {
169 // parse ions; may be more than one "ion" directive
170 int nion = 0;
171 if (opts.defined("ion")) {
172 StringList *ion = config.find("ion");
173 for ( ; ion; ion = ion->next) {
174 if (nion >= MAXION) {
175 NAMD_die("Too many ions specified");
176 }
177 if (sscanf(ion->data, "%lf %lf %lf", ionq+nion, ionc+nion, ionr+nion) != 3) {
178 NAMD_die("Unable to parse ion line");
179 }
180 nion++;
181 }
182 }
183
184
185 // populate arrays to pass to APBS
186 i_param[0] = calc_type;
187 i_param[1] = nlev;
188 i_param[2] = cmeth;
189 i_param[3] = ccmeth;
190 i_param[4] = fcmeth;
191 i_param[5] = chgm;
192 i_param[6] = nonlin;
193 i_param[7] = bcfl;
194 i_param[8] = srfm;
195 i_param[9] = calcenergy;
196 i_param[10] = calcforce;
197 i_param[11] = wpot;
198 i_param[12] = wchg;
199 i_param[13] = wsmol;
200 i_param[14] = wkappa;
201 i_param[15] = wdiel;
202 i_param[16] = watompot;
203 i_param[17] = rpot;
204 i_param[18] = 0;
205 i_param[19] = calcnpforce;
206 i_param[20] = calcnpenergy;
207 i_param[21] = nion;
208 i_param[22] = rchg;
209 i_param[23] = rkappa;
210 i_param[24] = rdiel;
211
212 r_param[0] = pdie;
213 r_param[1] = sdie;
214 r_param[2] = srad;
215 r_param[3] = swin;
216 r_param[4] = temp;
217 r_param[5] = sdens;
218 r_param[6] = gamma;
219 r_param[7] = smvolume;
220 r_param[8] = smsize;
221
222 if (opts.defined("dime")) {
223 char s[129];
224 opts.get("dime", s);
225 if (sscanf(s, "%d %d %d", dime+0, dime+1, dime+2) != 3) {
226 NAMD_die("APBS: Unable to parse dime option");
227 }
228 }
229
230 if (opts.defined("pdime")) {
231 char s[129];
232 opts.get("pdime", s);
233 if (sscanf(s, "%d %d %d", pdime+0, pdime+1, pdime+2) != 3) {
234 NAMD_die("APBS: Unable to parse pdime option");
235 }
236 }
237
238 // consistency check
239 if (opts.defined("grid")) {
240 if (calc_type == 1 && (!recalculateGrid)) {
241 NAMD_die("APBS: Wrong combination of options (grid/mg-auto).");
242 }
243 }
244 if ((calcforce == 0) || (calcforce == 1)) {
245 iout << "APBS: WARNING: Calculation of all-atom solvation forces is not turned on (calcforce)!\n";
246 iout << "APBS: WARNING: This may leed to unpredictable results during minimization or MD.\n";
247 }
248
249
250 // printout some info
251 if (verbose > 0) {
252 if (calc_type == 0) {
253 iout << "APBS: MG-MANUAL calculation.\n";
254 } else if (calc_type == 1) {
255 iout << "APBS: MG-AUTO calculation.\n";
256 } else if (calc_type == 2) {
257 iout << "APBS: MG-PARA calculation.\n";
258 }
259 if (nonlin == 0) {
260 iout << "APBS: Linearized traditional PBE.\n";
261 } else if (nonlin == 1) {
262 iout << "APBS: Nonlinear traditional PBE.\n";
263 } else if (nonlin == 2) {
264 iout << "APBS: Linearized RPBE.\n";
265 } else if (nonlin == 3) {
266 iout << "APBS: Nonlinear RPBE.\n";
267 } else if (nonlin == 4) {
268 iout << "APBS: Size-Modified PBE.\n";
269 } else {
270 NAMD_die("APBS: Unknown PBE option.");
271 }
272 if (bcfl == 0) {
273 iout << "APBS: Zero boundary conditions.\n";
274 } else if (bcfl == 1) {
275 iout << "APBS: Single Debye-Huckel sphere boundary conditions.\n";
276 } else if (bcfl == 2) {
277 iout << "APBS: Multiple Debye-Huckel sphere boundary conditions.\n";
278 } else if (bcfl == 3) {
279 iout << "APBS: Focusing boundary conditions.\n";
280 } else {
281 NAMD_die("APBS: Unknown boundary conditions option.");
282 }
283 if (srfm == 0) {
284 iout << "APBS: Using molecular surface definition.\n";
285 } else if (srfm == 1) {
286 iout << "APBS: Using smoothed molecular surface definition.\n";
287 } else if (srfm == 2) {
288 iout << "APBS: Using cubic-spline surface definition.\n";
289 } else if (srfm == 3) {
290 iout << "APBS: Using 7-order polynomial spline surface definition.\n";
291 } else {
292 NAMD_die("APBS: Unknown surface definition.");
293 }
294 if (chgm == 0) {
295 iout << "APBS: Using trilinear interpolation (linear splines).\n";
296 } else if (chgm == 1) {
297 iout << "APBS: Using cubic B-spline charge discretization.\n";
298 } else if (chgm == 2) {
299 iout << "APBS: Using quintic B-spline charge discretization.\n";
300 } else {
301 NAMD_die("APBS: Unknown charge discretization.");
302 }
303
304 iout << "APBS: Solute dielectric (pdie): " << pdie << "\n";
305 iout << "APBS: Solvent dielectric (sdie): " << sdie << "\n";
306 iout << "APBS: Temperature: " << temp << " K\n";
307 iout << "APBS: Surface sphere density (sdens): " << sdens << " grid points/A^2\n";
308 iout << "APBS: Surface tension: " << gamma << " kJ/mol/A\n";
309 iout << "APBS: Verbosity level: " << verbose << "\n";
310 iout << "APBS: Debug level: " << debug << "\n";
311
312 if (recalculateGrid) {
313 iout << "APBS: Requesting grid size re-calculation on the fly. \n" << endi;
314 }
315 iout << "APBS: Grid values: \n";
316 if (opts.defined("fglen")){
317 iout << "APBS: Grid lengths (fglen): " << fglen[0] << " "
318 << fglen[1] << " " << fglen[2] << "\n";
319 }
320 if (opts.defined("cglen")){
321 iout << "APBS: Grid lenghts (cglen): " << cglen[0] << " "
322 << cglen[1] << " " << cglen[2] << "\n";
323 }
324 if (opts.defined("dime")){
325 iout << "APBS: Grid dimensions: " << dime[0] << " "
326 << dime[1] << " " << dime[2] << "\n";
327 }
328 if (opts.defined("grid")){
329 iout << "APBS: Grid spacings (in A): " << grid[0] << " "
330 << grid[1] << " " << grid[2] << "\n";
331 } else if((opts.defined("fglen")) && (opts.defined("dime"))) {
332 iout << "APBS: Grid spacings (in A): " <<
333 fglen[0]/(dime[0]-1) << " " << fglen[1]/(dime[1]-1)
334 << " " << fglen[2]/(dime[2]-1) << "\n";
335 }
336 if((opts.defined("dime")) && (opts.defined("grid"))) {
337 iout << "APBS: Grid lengths: " << dime[0]*grid[0]-grid[0] << " "
338 << dime[1]*grid[1]-grid[1] << " "
339 << dime[2]*grid[2]-grid[2] << "\n";
340 }
341 if (opts.defined("dime")){
342 iout << "APBS: Required memory: " <<
343 dime[0]*dime[1]*dime[2]*200.0/1024/1024 << " MB\n";
344 }
345
346 if (calcenergy == 0) {
347 iout << "APBS: Electrostatic energies will not be calculated.\n";
348 } else if (calcenergy == 1) {
349 iout << "APBS: Total electrostatic energies will be calculated.\n";
350 } else if (calcenergy == 2) {
351 iout << "APBS: All-atom electrostatic energies will be calculated.\n";
352 }
353 if (calcforce == 0) {
354 iout << "APBS: Solvent forces will not be calculated.\n";
355 } else if (calcforce == 1) {
356 iout << "APBS: Total solvent forces will be calculated.\n";
357 } else if (calcforce == 2) {
358 iout << "APBS: All-atom solvent forces will be calculated.\n";
359 }
360 if (calcnpenergy == 0) {
361 iout << "APBS: Non-polar energies will not be calculated.\n";
362 } else if (calcnpenergy == 1) {
363 iout << "APBS: Total non-polar energies will be calculated.\n";
364 } else if (calcnpenergy == 2) {
365 iout << "APBS: All-atom non-polar energies will be calculated.\n";
366 }
367
368
369 if (wpot == 1){
370 iout << "APBS: Writing potential to iapbs-pot.dx.\n";
371 }
372 if (wchg == 1){
373 iout << "APBS: Writing charge distribution to iapbs-charge.dx.\n";
374 }
375 if (wsmol == 1){
376 iout << "APBS: Writing molecular accessibility to iapbs-smol.dx.\n";
377 }
378 if (wkappa == 1){
379 iout << "APBS: Writing kappa map to iapbs-kappa.dx.\n";
380 }
381 if (wdiel == 1){
382 iout << "APBS: Writing dielectric map to iapbs-dielXYZ.dx\n";
383 }
384 if (rchg == 1){
385 iout << "APBS: Reading charge map data from iapbs-charge.dx.\n";
386 }
387 if (rkappa == 1){
388 iout << "APBS: Reading kappa map data from iapbs-kappa.dx.\n";
389 }
390 if (rdiel == 1){
391 iout << "APBS: Reading dielectric map data from iapbs-dielXYZ.dx.\n";
392 }
393 }
394 }
395
396 void APBSParameters::parse(StringList *script) {
397 ConfigList config;
398 for ( ; script; script = script->next ) {
399 const char *block = script->data;
400 // split block into lines
401 int lastline = 0;
402 while (!lastline) {
403 const char *newline = strchr(block, '\n');
404 if (!newline) {
405 lastline = 1;
406 }
407 // skip initial whitespace
408 block += strspn(block, " ");
409 // word is everything up to space or '='. Also skip comments
410 size_t wordlen = strcspn(block, " =#\n");
411 if (!wordlen) {
412 block = newline + 1;
413 continue;
414 }
415 // args are everything after the = or ' ', until a comment or end of line
416 // there may be trailing spaces, but that shouldn't hurt anything.
417 const char *args = block + wordlen + 1;
418 args += strspn(args, " =");
419 size_t arglen = strcspn(args, "#\n");
420 config.add_element(block, wordlen, args, arglen);
421
422 // advance to next line
423 block = newline+1;
424 }
425 }
426
427 // we've lex'ed all the options; now parse them.
428 ParseOptions opts;
429 config_parser(opts);
430 if (!opts.check_consistency()) {
431 NAMD_die("Internal error in APBS configuration parser");
432 }
433 // feed the options to the parser
434 if (!opts.set(config)) {
435 NAMD_die("ERROR(S) IN THE APBS CONFIGURATION FILE");
436 }
437 // do other checks that ParseOptions can't do
438 check_config(opts, config);
439 }
440
441 GlobalMasterAPBS::GlobalMasterAPBS() {
442 DebugM(3,"Constructing\n");
443
444 // fetch input parameters
445 SimParameters *simparams = Node::Object()->simParameters;
446 StringList *script = Node::Object()->configList->find("apbsForcesConfig");
447
448 params = new APBSParameters;
449 params->parse(script);
450
451 // indicate that we will submit an energy calculation each step
452 reduction = ReductionMgr::Object()->willSubmit(REDUCTIONS_BASIC);
453
454 outputFreq = simparams->outputEnergies;
455 step = simparams->firstTimestep;
456
457 // fetch charges and radii
458 Molecule *mol = Node::Object()->molecule;
459 numAtoms = mol->numAtoms;
460 charges = new double[numAtoms];
461 radii = new double[numAtoms];
462 positionx = new double[numAtoms];
463 positiony = new double[numAtoms];
464 positionz = new double[numAtoms];
465
466 {
467 for (int i=0; i<3; i++) {
468 solvent_forces[i] = new double[numAtoms];
469 vacuum_forces[i] = new double[numAtoms];
470 qfForce[i] = new double[numAtoms];
471 ibForce[i] = new double[numAtoms];
472 dbForce[i] = new double[numAtoms];
473 npForce[i] = new double[numAtoms];
474 }
475 }
476 for (int i=0; i<13; i++) {
477 apbsgrid_meta[i] = 0;
478 }
479 apbsgrid[0] = 0;
480
481 // we always read charges and radii from a pqr file
482 // NAMD stores vdw parameters for _pairs_ of atoms, and I don't
483 // understand how to map those back to single atom radii. Note that
484 // we have to deal with at least four different force fields here as
485 // well (Charmm, Amber, Gromacs, OPLS...)
486
487 parsePQRFile(simparams->apbsPQRFile, numAtoms, charges, radii);
488 double totalcharge = 0;
489
490 // request coordinates of all atoms on each update
491 for (int i=0; i<numAtoms; i++) {
492 modifyRequestedAtoms().add(i);
493 totalcharge += charges[i];
494 }
495 iout << "APBS: Total charge: " << totalcharge << "\n" << endi;
496 }
497
498 GlobalMasterAPBS::~GlobalMasterAPBS() {
499 delete [] charges;
500 delete [] radii;
501 delete [] positionx;
502 delete [] positiony;
503 delete [] positionz;
504 {
505 for (int i=0; i<3; i++) {
506 delete solvent_forces[i];
507 delete vacuum_forces[i];
508 delete qfForce[i];
509 delete ibForce[i];
510 delete dbForce[i];
511 delete npForce[i];
512 }
513 }
514 }
515
516 void GlobalMasterAPBS::calculate() {
517 // fetch the coordinates. It's complicated like this because atoms
518 // don't always arrive in the same order
519 AtomIDList::const_iterator a_i = getAtomIdBegin();
520 AtomIDList::const_iterator a_e = getAtomIdEnd();
521 PositionList::const_iterator p_i = getAtomPositionBegin();
522 for ( ; a_i != a_e; ++a_i, ++p_i ) {
523 size_t index = *a_i;
524 positionx[index] = p_i->x;
525 positiony[index] = p_i->y;
526 positionz[index] = p_i->z;
527 }
528 solvent_elec_energy = solvent_np_energy = vacuum_elec_energy = 0;
529
530 call_apbs(0);
531 // For sp (single-point) calculation, that's all we need. Otherwise,
532 // for dynamics, we do a second calculation in vacuum and store forces.
533 if (!params->sp_apbs) {
534 call_apbs(1);
535 // store forces back to nodes
536 // clear out the requested forces first!
537 modifyAppliedForces().resize(0);
538 modifyForcedAtoms().resize(0);
539
540 modifyAppliedForces().resize(numAtoms);
541 modifyForcedAtoms().resize(numAtoms);
542
543 for (int i=0; i<numAtoms; i++) {
544 modifyForcedAtoms().item(i) = i;
545 BigReal f[3];
546 for (int j=0; j<3; j++) {
547 f[j] = (solvent_forces[j][i] - vacuum_forces[j][i])*APBS_ENERGY_UNITS;
548 }
549 modifyAppliedForces().item(i) = Force(f[0], f[1], f[2]);
550 }
551 }
552
553 // store solvation energy. A separate category could be created for this.
554 BigReal solvationEnergy = solvent_elec_energy+solvent_np_energy-vacuum_elec_energy;
555 reduction->item(REDUCTION_MISC_ENERGY) += solvationEnergy;
556 reduction->submit();
557
558 // if needed, output separate elec and np energies.
559 if (outputFreq && !(step % outputFreq)) {
560 BigReal elec = solvent_elec_energy - vacuum_elec_energy;
561 iout << "APBS ENERGIES: ELEC: " << elec
562 << " NONPOLAR: " << solvent_np_energy << "\n" << endi;
563 }
564 ++step;
565 }
566
567 // this routine is currently reentrant as long as in_vacuum is different
568 // for each call. In principle we could perform the needed two APBS calls
569 // simultaneously, storing the solvent and vacuum results in different arrays.
570 void GlobalMasterAPBS::call_apbs(int in_vacuum) {
571
572 //
573 // do computation with radii, charges, and current positions
574 //
575 double esEnergy[10];
576 double npEnergy[10];
577
578 // in vacuum, ion concentrations get set to zero, and solvent dielectric
579 // gets set to 1.0.
580
581 BigReal ionc[MAXION];
582 int nion = params->i_param[21];
583 for (int i=0; i<nion; i++) {
584 ionc[i] = in_vacuum ? 0 : params->ionc[i];
585 }
586 double **forcePtr = in_vacuum ? vacuum_forces : solvent_forces;
587
588 double r_param[9];
589 memcpy(r_param, params->r_param, 9*sizeof(double));
590 if (in_vacuum) {
591 // set solvent dielectric to 1.0
592 r_param[1] = 1.0;
593 }
594
595 int dime [3];
596 double grid[3], cglen[3], fglen[3], glen[3], center[3], ccenter[3], fcenter[3];
597 double maxx = 0, minx = 0, maxy = 0, miny = 0, maxz = 0, minz = 0;
598
599 for (int i=0; i<3; i++) {
600 dime[i] = params->dime[i];
601 grid[i] = params->grid[i];
602 cglen[i] = params->cglen[i];
603 fglen[i] = params->fglen[i];
604 glen[i] = params->glen[i];
605 center[i] = params->center[i];
606 ccenter[i] = params->ccenter[i];
607 fcenter[i] = params->fcenter[i];
608 }
609
610 for (int i=0; i<numAtoms; i++) {
611 if (maxx < positionx[i] + radii[i]) maxx = positionx[i] + radii[i];
612 if (minx > positionx[i] - radii[i]) minx = positionx[i] - radii[i];
613 if (maxy < positiony[i] + radii[i]) maxy = positiony[i] + radii[i];
614 if (miny > positiony[i] - radii[i]) miny = positiony[i] - radii[i];
615 if (maxz < positionz[i] + radii[i]) maxz = positionz[i] + radii[i];
616 if (minz > positionz[i] - radii[i]) minz = positionz[i] - radii[i];
617 }
618
619 if (params->verbose > 1) {
620 iout << "APBS: Molecular dimensions: " << maxx-minx << " "
621 << maxy-miny << " "
622 << maxz-minz << "\n" << endi;
623 }
624
625 // for mg-auto calculate missing grid parameters if dime = 0
626 // if ((params->i_param[0] == 0 || params->i_param[0] == 1) && dime[0] == 0) {
627 if (params->recalculateGrid) {
628 cglen[0] = 1.7 * (maxx-minx);
629 cglen[1] = 1.7 * (maxy-miny);
630 cglen[2] = 1.7 * (maxz-minz);
631 fglen[0] = 20.0 + (maxx-minx);
632 fglen[1] = 20.0 + (maxy-miny);
633 fglen[2] = 20.0 + (maxz-minz);
634
635 for (int i=0; i<3; i++) {
636 if (fglen[i] > cglen[i]) cglen[i] = fglen[i];
637 }
638
639 if (params->verbose > 1) {
640 iout << "APBS: Requesting dime re-calculation on the fly. \n" << endi;
641 }
642
643 for (int i=0; i<3; i++) {
644 dime[i] =
645 32*(int((int(fglen[i]/grid[i] + 0.5) - 1)/32.0 + 0.5)) + 1;
646 if (dime[i] < 33) dime[i] = 33;
647 }
648
649 // correction to get the exact grid spacing requested
650 for (int i=0; i<3; i++) {
651 fglen[i] = (dime[i] - 1) * grid[i];
652 if (fglen[i] > cglen[i]) cglen[i] = fglen[i];
653 }
654 }
655
656 if (params->verbose > 1) {
657 iout << "APBS: Grid values: \n" << endi;
658 if (params->i_param[0] == 1 || params->i_param[0] == 2){
659 iout << "APBS: Grid lengths (fglen): " << fglen[0] << " "
660 << fglen[1] << " " << fglen[2] << "\n" << endi;
661 iout << "APBS: Grid lenghts (cglen): " << cglen[0] << " "
662 << cglen[1] << " " << cglen[2] << "\n" << endi;
663 } else {
664 iout << "APBS: Grid lengths: " << dime[0]*grid[0]-grid[0] << " "
665 << dime[1]*grid[1]-grid[1] << " "
666 << dime[2]*grid[2]-grid[2] << "\n" << endi;
667 }
668 iout << "APBS: Grid dimensions: " << dime[0] << " "
669 << dime[1] << " " << dime[2] << "\n" << endi;
670 if (grid[0] > 0) {
671 iout << "APBS: Grid spacings (in A): " << grid[0] << " "
672 << grid[1] << " " << grid[2] << "\n" << endi;
673 } else {
674 iout << "APBS: Grid spacings (in A): " <<
675 fglen[0]/(dime[0]-1) << " " << fglen[1]/(dime[1]-1)
676 << " " << fglen[2]/(dime[2]-1) << "\n" << endi;
677 }
678 iout << "APBS: Required memory: " <<
679 dime[0]*dime[1]*dime[2]*200.0/1024/1024 << " MB\n" << endi;
680 }
681
682 if (params->verbose > 5) { iout << "APBS: Calling apbsdrv_ \n" << endi; }
683
684 if (params->verbose > 9) {
685 iout << "APBS: apbsdrv_ input params:\n" << endi;
686 iout << numAtoms << "\n" << endi;
687 for (int i=0; i<numAtoms; i++) {
688 iout << positionx[i] << " " << positiony[i] << " "
689 << positionz[i] << " " << radii[i] << " "
690 << charges[i] << "\n" << endi;
691 }
692 for (int i=0; i<3; i++) {
693 iout << grid[i] << " "
694 << dime[i] << " "
695 << glen[i] << " "
696 << cglen[i] << " "
697 << fglen[i] << " "
698 << center[i] << " "
699 << ccenter[i] << " "
700 << fcenter[i] << "\n" << endi;
701 }
702 for (int i=0; i<9; i++) {
703 iout << r_param[i] << " " << endi;
704 }
705 iout << "\n" << endi;
706 for (int i=0; i<25; i++) {
707 iout << params->i_param[i] << " " << endi;
708 }
709 iout << "\n" << endi;
710 for (int i=0; i<params->i_param[21]; i++) {
711 iout << params->ionq[i] << " "
712 << ionc[i] << " "
713 << params->ionq[i] << "\n" << endi;
714 }
715 iout << params->debug << "\n" << endi;
716
717 }
718
719 int result = apbsdrv_(
720 &numAtoms,
721 positionx,
722 positiony,
723 positionz,
724 radii,
725 charges,
726 r_param,
727 params->i_param,
728 //(double *)&params->grid,
729 grid,
730 //params->dime,
731 dime,
732 params->pdime,
733 //(double *)&params->glen,
734 glen,
735 //(double *)&params->center,
736 center,
737 //(double *)&params->cglen,
738 cglen,
739 //(double *)&params->fglen,
740 fglen,
741 //(double *)&params->ccenter,
742 ccenter,
743 //(double *)&params->fcenter,
744 fcenter,
745 &params->ofrac,
746 &params->debug,
747 params->ionq,
748 ionc,
749 params->ionr,
750 esEnergy,
751 npEnergy,
752 forcePtr[0], forcePtr[1], forcePtr[2],
753 qfForce[0], qfForce[1], qfForce[2],
754 ibForce[0], ibForce[1], ibForce[2],
755 npForce[0], npForce[1], npForce[2],
756 dbForce[0], dbForce[1], dbForce[2],
757 apbsgrid_meta, apbsgrid
758 );
759
760 if (in_vacuum) {
761 vacuum_elec_energy = esEnergy[0] * APBS_ENERGY_UNITS;
762 } else {
763 solvent_elec_energy = esEnergy[0] * APBS_ENERGY_UNITS;
764 solvent_np_energy = npEnergy[0] * APBS_ENERGY_UNITS;
765 }
766 }
767
768 static void parsePQRFile(const char *path, int numAtoms,
769 double *charges, double *radii) {
770
771 iout << "APBS: Reading PQR file " << path << "\n" << endi;
772 FILE *fd = fopen(path, "rt");
773 if (!fd) {
774 NAMD_die("Error opening PQR file.\n");
775 }
776 int index = 0;
777 char buf[200];
778 double x, y, z;
779 while (fgets(buf, 200, fd)) {
780 if (strncmp(buf, "ATOM", 4)) continue;
781 if (index >= numAtoms) break;
782 if (sscanf(buf+30, "%lf%lf%lf%lf%lf", &x, &y, &z, charges+index, radii+index) != 5) {
783 NAMD_die("Error parsing PQR file.\n");
784 }
785 index++;
786 }
787 if (index != numAtoms) {
788 NAMD_die("Incorrect atom count found in PQR file.\n");
789 }
790 fclose(fd);
791 }
792
793 #endif