APBS 3.0.0
Loading...
Searching...
No Matches
vclist.c
Go to the documentation of this file.
1
57#include "vclist.h"
58
59VEMBED(rcsid="$Id$")
60
61#if !defined(VINLINE_VCLIST)
62
63VPUBLIC unsigned long int Vclist_memChk(Vclist *thee) {
64 if (thee == VNULL) return 0;
65 return Vmem_bytes(thee->vmem);
66}
67
68VPUBLIC double Vclist_maxRadius(Vclist *thee) {
69 VASSERT(thee != VNULL);
70 return thee->max_radius;
71}
72
73#endif /* if !defined(VINLINE_VCLIST) */
74
75VPUBLIC Vclist* Vclist_ctor(Valist *alist, double max_radius,
76 int npts[VAPBS_DIM], Vclist_DomainMode mode,
77 double lower_corner[VAPBS_DIM], double upper_corner[VAPBS_DIM]) {
78
79 Vclist *thee = VNULL;
80
81 /* Set up the structure */
82 thee = (Vclist*)Vmem_malloc(VNULL, 1, sizeof(Vclist) );
83 VASSERT( thee != VNULL);
84 VASSERT( Vclist_ctor2(thee, alist, max_radius, npts, mode, lower_corner,
85 upper_corner) == VRC_SUCCESS );
86 return thee;
87}
88
89/* Get the dimensions of the molecule stored in thee->alist */
90VPRIVATE void Vclist_getMolDims(
91 Vclist *thee,
92 double lower_corner[VAPBS_DIM], /* Set to lower corner of molecule */
93 double upper_corner[VAPBS_DIM], /* Set to lower corner of molecule */
94 double *r_max /* Set to max atom radius */
95 ) {
96
97 int i, j;
98 double pos;
99 Valist *alist;
100 Vatom *atom;
101
102 alist = thee->alist;
103
104 /* Initialize */
105 for (i=0; i<VAPBS_DIM; i++) {
106 lower_corner[i] = VLARGE;
107 upper_corner[i] = -VLARGE;
108 }
109 *r_max = -1.0;
110
111 /* Check each atom */
112 for (i=0; i<Valist_getNumberAtoms(alist); i++) {
113 atom = Valist_getAtom(alist, i);
114 for (j=0; j<VAPBS_DIM; j++) {
115 pos = (Vatom_getPosition(atom))[j];
116 if ( pos < lower_corner[j] ) lower_corner[j] = pos;
117 if ( pos > upper_corner[j] ) upper_corner[j] = pos;
118 }
119 if (Vatom_getRadius(atom) > *r_max) *r_max = Vatom_getRadius(atom);
120 }
121
122}
123
124/* Setup lookup grid */
125VPRIVATE Vrc_Codes Vclist_setupGrid(Vclist *thee) {
126
127 /* Inflation factor ~ sqrt(2)*/
128 #define VCLIST_INFLATE 1.42
129
130 int i;
131 double length[VAPBS_DIM], r_max;
132
133 /* Set up the grid corners */
134 switch (thee->mode) {
136 /* Get molecule dimensions */
137 Vclist_getMolDims(thee, thee->lower_corner, thee->upper_corner,
138 &r_max);
139 /* Set up grid spacings */
140 for (i=0; i<VAPBS_DIM; i++) {
141 thee->upper_corner[i] = thee->upper_corner[i]
142 + VCLIST_INFLATE*(r_max+thee->max_radius);
143 thee->lower_corner[i] = thee->lower_corner[i]
144 - VCLIST_INFLATE*(r_max+thee->max_radius);
145 }
146 break;
148 /* Grid corners established in constructor */
149 break;
150 default:
151 Vnm_print(2, "Vclist_setupGrid: invalid setup mode (%d)!\n",
152 thee->mode);
153 return VRC_FAILURE;
154 }
155
156 /* Set up the grid lengths and spacings */
157 for (i=0; i<VAPBS_DIM; i++) {
158 length[i] = thee->upper_corner[i] - thee->lower_corner[i];
159 thee->spacs[i] = length[i]/((double)(thee->npts[i] - 1));
160 }
161 Vnm_print(0, "Vclist_setupGrid: Grid lengths = (%g, %g, %g)\n",
162 length[0], length[1], length[2]);
163
164 Vnm_print(0, "Vclist_setupGrid: Grid lower corner = (%g, %g, %g)\n",
165 (thee->lower_corner)[0], (thee->lower_corner)[1],
166 (thee->lower_corner)[2]);
167
168 return VRC_SUCCESS;
169
170 #undef VCLIST_INFLATE
171}
172
173/* Check and store parameters passed to constructor */
174VPRIVATE Vrc_Codes Vclist_storeParms(Vclist *thee, Valist *alist,
175 double max_radius, int npts[VAPBS_DIM], Vclist_DomainMode mode,
176 double lower_corner[VAPBS_DIM], double upper_corner[VAPBS_DIM] ) {
177
178 int i = 0;
179
180 if (alist == VNULL) {
181 Vnm_print(2, "Vclist_ctor2: Got NULL Valist!\n");
182 return VRC_FAILURE;
183 } else thee->alist = alist;
184
185 thee->n = 1;
186 for (i=0; i<VAPBS_DIM; i++) {
187 if (npts[i] < 3) {
188 Vnm_print(2,
189 "Vclist_ctor2: n[%d] (%d) must be greater than 2!\n",
190 i, npts[i]);
191 return VRC_FAILURE;
192 }
193 thee->npts[i] = npts[i];
194 thee->n *= npts[i];
195 }
196 Vnm_print(0, "Vclist_ctor2: Using %d x %d x %d hash table\n",
197 npts[0], npts[1], npts[2]);
198
199 thee->mode = mode;
200 switch (thee->mode) {
202 Vnm_print(0, "Vclist_ctor2: automatic domain setup.\n");
203 break;
205 Vnm_print(0, "Vclist_ctor2: manual domain setup.\n");
206 Vnm_print(0, "Vclist_ctor2: lower corner = [ \n");
207 for (i=0; i<VAPBS_DIM; i++) {
208 thee->lower_corner[i] = lower_corner[i];
209 Vnm_print(0, "%g ", lower_corner[i]);
210 }
211 Vnm_print(0, "]\n");
212 Vnm_print(0, "Vclist_ctor2: upper corner = [ \n");
213 for (i=0; i<VAPBS_DIM; i++) {
214 thee->upper_corner[i] = upper_corner[i];
215 Vnm_print(0, "%g ", upper_corner[i]);
216 }
217 Vnm_print(0, "]\n");
218 break;
219 default:
220 Vnm_print(2, "Vclist_ctor2: invalid setup mode (%d)!\n", mode);
221 return VRC_FAILURE;
222 }
223
224 thee->max_radius = max_radius;
225 Vnm_print(0, "Vclist_ctor2: Using %g max radius\n", max_radius);
226
227 return VRC_SUCCESS;
228}
229
230/* Calculate the gridpoints an atom spans */
231VPRIVATE void Vclist_gridSpan(Vclist *thee,
232 Vatom *atom, /* Atom */
233 int imin[VAPBS_DIM], /* Set to min grid indices */
234 int imax[VAPBS_DIM] /* Set to max grid indices */
235 ) {
236
237 int i;
238 double *coord, dc, idc, rtot;
239
240 /* Get the position in the grid's frame of reference */
241 coord = Vatom_getPosition(atom);
242
243 /* Get the range the atom radius + probe radius spans */
244 rtot = Vatom_getRadius(atom) + thee->max_radius;
245
246 /* Calculate the range of grid points the inflated atom spans in the x
247 * direction. */
248 for (i=0; i<VAPBS_DIM; i++) {
249 dc = coord[i] - (thee->lower_corner)[i];
250 idc = (dc + rtot)/(thee->spacs[i]);
251 imax[i] = (int)(ceil(idc));
252 imax[i] = VMIN2(imax[i], thee->npts[i]-1);
253 idc = (dc - rtot)/(thee->spacs[i]);
254 imin[i] = (int)(floor(idc));
255 imin[i] = VMAX2(imin[i], 0);
256 }
257
258}
259
260/* Get the array index for a particular cell based on its i,j,k
261 * coordinates */
262VPRIVATE int Vclist_arrayIndex(Vclist *thee, int i, int j, int k) {
263
264 return (thee->npts[2])*(thee->npts[1])*i + (thee->npts[2])*j + k;
265
266}
267
268
269/* Assign atoms to cells */
270VPRIVATE Vrc_Codes Vclist_assignAtoms(Vclist *thee) {
271
272 int iatom, i, j, k, ui, inext;
273 int imax[VAPBS_DIM], imin[VAPBS_DIM];
274 int totatoms;
275 Vatom *atom;
276 VclistCell *cell;
277
278
279 /* Find out how many atoms are associated with each grid point */
280 totatoms = 0;
281 for (iatom=0; iatom<Valist_getNumberAtoms(thee->alist); iatom++) {
282
283 /* Get grid span for atom */
284 atom = Valist_getAtom(thee->alist, iatom);
285 Vclist_gridSpan(thee, atom, imin, imax);
286
287 /* Now find and assign the grid points */
288 VASSERT(VAPBS_DIM == 3);
289 for ( i = imin[0]; i <= imax[0]; i++) {
290 for ( j = imin[1]; j <= imax[1]; j++) {
291 for ( k = imin[2]; k <= imax[2]; k++) {
292 /* Get index to array */
293 ui = Vclist_arrayIndex(thee, i, j, k);
294 /* Increment number of atoms for this grid point */
295 cell = &(thee->cells[ui]);
296 (cell->natoms)++;
297 totatoms++;
298 }
299 }
300 }
301 }
302 Vnm_print(0, "Vclist_assignAtoms: Have %d atom entries\n", totatoms);
303
304 /* Allocate the space to store the pointers to the atoms */
305 for (ui=0; ui<thee->n; ui++) {
306 cell = &(thee->cells[ui]);
307 if ( VclistCell_ctor2(cell, cell->natoms) == VRC_FAILURE ) {
308 Vnm_print(2, "Vclist_assignAtoms: cell error!\n");
309 return VRC_FAILURE;
310 }
311 /* Clear the counter for later use */
312 cell->natoms = 0;
313 }
314
315 /* Assign the atoms to grid points */
316 for (iatom=0; iatom<Valist_getNumberAtoms(thee->alist); iatom++) {
317
318 /* Get grid span for atom */
319 atom = Valist_getAtom(thee->alist, iatom);
320 Vclist_gridSpan(thee, atom, imin, imax);
321
322 /* Now find and assign the grid points */
323 for (i = imin[0]; i <= imax[0]; i++) {
324 for (j = imin[1]; j <= imax[1]; j++) {
325 for (k = imin[2]; k <= imax[2]; k++) {
326 /* Get index to array */
327 ui = Vclist_arrayIndex(thee, i, j, k);
328 cell = &(thee->cells[ui]);
329 /* Index of next available array location */
330 inext = cell->natoms;
331 cell->atoms[inext] = atom;
332 /* Increment number of atoms */
333 (cell->natoms)++;
334 }
335 }
336 }
337 }
338
339 return VRC_SUCCESS;
340}
341
342/* Main (FORTRAN stub) constructor */
343VPUBLIC Vrc_Codes Vclist_ctor2(Vclist *thee, Valist *alist, double max_radius,
344 int npts[VAPBS_DIM], Vclist_DomainMode mode,
345 double lower_corner[VAPBS_DIM], double upper_corner[VAPBS_DIM]) {
346
347 int i;
348 VclistCell *cell;
349
350 /* Check and store parameters */
351 if ( Vclist_storeParms(thee, alist, max_radius, npts, mode, lower_corner,
352 upper_corner) == VRC_FAILURE ) {
353 Vnm_print(2, "Vclist_ctor2: parameter check failed!\n");
354 return VRC_FAILURE;
355 }
356
357 /* Set up memory */
358 thee->vmem = Vmem_ctor("APBS::VCLIST");
359 if (thee->vmem == VNULL) {
360 Vnm_print(2, "Vclist_ctor2: memory object setup failed!\n");
361 return VRC_FAILURE;
362 }
363
364 /* Set up cells */
365 thee->cells = (VclistCell*)Vmem_malloc( thee->vmem, thee->n, sizeof(VclistCell) );
366 if (thee->cells == VNULL) {
367 Vnm_print(2,
368 "Vclist_ctor2: Failed allocating %d VclistCell objects!\n",
369 thee->n);
370 return VRC_FAILURE;
371 }
372 for (i=0; i<thee->n; i++) {
373 cell = &(thee->cells[i]);
374 cell->natoms = 0;
375 }
376
377 /* Set up the grid */
378 if ( Vclist_setupGrid(thee) == VRC_FAILURE ) {
379 Vnm_print(2, "Vclist_ctor2: grid setup failed!\n");
380 return VRC_FAILURE;
381 }
382
383 /* Assign atoms to grid cells */
384 if (Vclist_assignAtoms(thee) == VRC_FAILURE) {
385 Vnm_print(2, "Vclist_ctor2: atom assignment failed!\n");
386 return VRC_FAILURE;
387 }
388
389
390
391
392
393 return VRC_SUCCESS;
394}
395
396/* Destructor */
397VPUBLIC void Vclist_dtor(Vclist **thee) {
398
399 if ((*thee) != VNULL) {
400 Vclist_dtor2(*thee);
401 Vmem_free(VNULL, 1, sizeof(Vclist), (void **)thee);
402 (*thee) = VNULL;
403 }
404
405}
406
407/* Main (stub) destructor */
408VPUBLIC void Vclist_dtor2(Vclist *thee) {
409
410 VclistCell *cell;
411 int i;
412
413 for (i=0; i<thee->n; i++) {
414 cell = &(thee->cells[i]);
415 VclistCell_dtor2(cell);
416 }
417 Vmem_free(thee->vmem, thee->n, sizeof(VclistCell),
418 (void **)&(thee->cells));
419 Vmem_dtor(&(thee->vmem));
420
421}
422
424 double pos[VAPBS_DIM]
425 ) {
426
427 int i,
428 ic[VAPBS_DIM],
429 ui;
430 double c[VAPBS_DIM];
431
432 /* Assert this before we do anything else, since its failure should fail the function */
433 VASSERT(VAPBS_DIM == 3);
434
435 /* Convert to grid based coordinates */
436 for (i=0; i<VAPBS_DIM; i++) {
437 c[i] = pos[i] - (thee->lower_corner)[i];
438 ic[i] = (int)(c[i]/thee->spacs[i]);
439
440 if (ic[i] < 0 || ic[i] >= thee->npts[i]) {
441 return VNULL;
442 }
443 }
444
445 /* Get the array index */
446 ui = Vclist_arrayIndex(thee, ic[0], ic[1], ic[2]);
447
448 return &(thee->cells[ui]);
449
450}
451
452VPUBLIC VclistCell* VclistCell_ctor(int natoms) {
453
454 VclistCell *thee = VNULL;
455
456 /* Set up the structure */
457 thee = (VclistCell*)Vmem_malloc(VNULL, 1, sizeof(VclistCell));
458 VASSERT( thee != VNULL);
459 VASSERT( VclistCell_ctor2(thee, natoms) == VRC_SUCCESS );
460
461 return thee;
462}
463
464VPUBLIC Vrc_Codes VclistCell_ctor2(VclistCell *thee, int natoms) {
465
466 if (thee == VNULL) {
467 Vnm_print(2, "VclistCell_ctor2: NULL thee!\n");
468 return VRC_FAILURE;
469 }
470
471 thee->natoms = natoms;
472 if (thee->natoms > 0) {
473 thee->atoms = (Vatom**)Vmem_malloc(VNULL, natoms, sizeof(Vatom *));
474 if (thee->atoms == VNULL) {
475 Vnm_print(2,
476 "VclistCell_ctor2: unable to allocate space for %d atom pointers!\n",
477 natoms);
478 return VRC_FAILURE;
479 }
480 }
481
482 return VRC_SUCCESS;
483
484}
485
486VPUBLIC void VclistCell_dtor(VclistCell **thee) {
487
488 if ((*thee) != VNULL) {
489 VclistCell_dtor2(*thee);
490 Vmem_free(VNULL, 1, sizeof(VclistCell), (void **)thee);
491 (*thee) = VNULL;
492 }
493
494}
495
496/* Main (stub) destructor */
497VPUBLIC void VclistCell_dtor2(VclistCell *thee) {
498
499 if (thee->natoms > 0) {
500 Vmem_free(VNULL, thee->natoms, sizeof(Vatom *),
501 (void **)&(thee->atoms));
502 }
503
504}
VPUBLIC Vatom * Valist_getAtom(Valist *thee, int i)
Get pointer to particular atom in list.
Definition valist.c:115
VPUBLIC int Valist_getNumberAtoms(Valist *thee)
Get number of atoms in the list.
Definition valist.c:105
VPUBLIC double Vatom_getRadius(Vatom *thee)
Get atomic position.
Definition vatom.c:105
VPUBLIC double * Vatom_getPosition(Vatom *thee)
Get atomic position.
Definition vatom.c:63
enum eVclist_DomainMode Vclist_DomainMode
Declaration of Vclist_DomainMode enumeration type.
Definition vclist.h:94
VPUBLIC void Vclist_dtor(Vclist **thee)
Destroy object.
Definition vclist.c:397
VPUBLIC unsigned long int Vclist_memChk(Vclist *thee)
Get number of bytes in this object and its members.
Definition vclist.c:63
VPUBLIC VclistCell * Vclist_getCell(Vclist *thee, double pos[VAPBS_DIM])
Return cell corresponding to specified position or return VNULL.
Definition vclist.c:423
VPUBLIC void Vclist_dtor2(Vclist *thee)
FORTRAN stub to destroy object.
Definition vclist.c:408
VPUBLIC Vclist * Vclist_ctor(Valist *alist, double max_radius, int npts[VAPBS_DIM], Vclist_DomainMode mode, double lower_corner[VAPBS_DIM], double upper_corner[VAPBS_DIM])
Construct the cell list object.
Definition vclist.c:75
VPUBLIC Vrc_Codes VclistCell_ctor2(VclistCell *thee, int natoms)
Construct a cell list object.
Definition vclist.c:464
VPUBLIC void VclistCell_dtor(VclistCell **thee)
Destroy object.
Definition vclist.c:486
VPUBLIC Vrc_Codes Vclist_ctor2(Vclist *thee, Valist *alist, double max_radius, int npts[VAPBS_DIM], Vclist_DomainMode mode, double lower_corner[VAPBS_DIM], double upper_corner[VAPBS_DIM])
FORTRAN stub to construct the cell list object.
Definition vclist.c:343
VPUBLIC double Vclist_maxRadius(Vclist *thee)
Get the max probe radius value (in A) the cell list was constructed with.
Definition vclist.c:68
VPUBLIC void VclistCell_dtor2(VclistCell *thee)
FORTRAN stub to destroy object.
Definition vclist.c:497
VPUBLIC VclistCell * VclistCell_ctor(int natoms)
Allocate and construct a cell list cell object.
Definition vclist.c:452
@ CLIST_AUTO_DOMAIN
Definition vclist.h:83
@ CLIST_MANUAL_DOMAIN
Definition vclist.h:85
#define VEMBED(rctag)
Allows embedding of RCS ID tags in object files.
Definition vhal.h:556
#define VAPBS_DIM
Our dimension.
Definition vhal.h:402
@ VRC_FAILURE
Definition vhal.h:69
@ VRC_SUCCESS
Definition vhal.h:70
Container class for list of atom objects.
Definition valist.h:78
Contains public data members for Vatom class/module.
Definition vatom.h:84
Atom cell list cell.
Definition vclist.h:101
Vatom ** atoms
Definition vclist.h:102
int natoms
Definition vclist.h:103
Atom cell list.
Definition vclist.h:117
int npts[VAPBS_DIM]
Definition vclist.h:122
double upper_corner[VAPBS_DIM]
Definition vclist.h:127
double lower_corner[VAPBS_DIM]
Definition vclist.h:126
int n
Definition vclist.h:123
VclistCell * cells
Definition vclist.h:125
double spacs[VAPBS_DIM]
Definition vclist.h:128
Vmem * vmem
Definition vclist.h:119
Valist * alist
Definition vclist.h:120
Vclist_DomainMode mode
Definition vclist.h:121
double max_radius
Definition vclist.h:124
Contains declarations for class Vclist.