File: box_system.cc

package info (click to toggle)
ergo 3.8.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,568 kB
  • sloc: cpp: 94,763; ansic: 17,785; sh: 10,701; makefile: 1,403; yacc: 127; lex: 116; awk: 23
file content (446 lines) | stat: -rw-r--r-- 15,007 bytes parent folder | download
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
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
 * and Anastasia Kruchinina.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Primary academic reference:
 * Ergo: An open-source program for linear-scaling electronic structure
 * calculations,
 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
 * Kruchinina,
 * SoftwareX 7, 107 (2018),
 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

/** @file box_system.cc

    @brief BoxSystem class representing a hierarchical data structure
    of boxes in 3D space (an oct-tree).

    The idea is that you have a list of items at different points in space,
    and you want a hierarchical system of boxes containing those items.

    You give a list of items, and the function create_box_system will
    create a system of boxes for you.

    @author: Elias Rudberg <em>responsible</em>
*/

#include <cmath>
#include <stdlib.h>
#include <vector>
#include "box_system.h"
#include "output.h"
#include "memorymanag.h"
#include "utilities.h"
#include "mat_gblas.h"


BoxSystem::BoxSystem()
{
  boxList = NULL;
}

BoxSystem::~BoxSystem()
{
  if(boxList)
    delete [] boxList;
}


/** Creates the box system.

    @param itemList list of items to create the box structure for.
    @param noOfItems their number.
    @param toplevelBoxSize
*/

int
BoxSystem::create_box_system(box_item_struct* itemList,
			     int noOfItems,
			     ergo_real toplevelBoxSize)
{
  // Allocate resultBoxList with just one item to begin with,
  // It will be expanded later as needed.
  int maxNoOfBoxes = 1;
  boxList = new box_struct_basic[maxNoOfBoxes];

  // create "mother box" containing all distrs.
  int currBoxIndex = 0;
  if(currBoxIndex >= maxNoOfBoxes)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in create_box_system: (currBoxIndex >= maxNoOfBoxes)");
      return -1;
    }
  box_struct_basic* motherBox = &boxList[currBoxIndex];
  currBoxIndex++;
  
  // first get dimensions of box
  const ergo_real HUGE_NUMBER = 888888888;
  ergo_real xminList[3];
  ergo_real xmaxList[3];
  ergo_real xdiffList[3];
  for(int kk = 0; kk < 3; kk++)
    {
      xminList[kk] =  HUGE_NUMBER;
      xmaxList[kk] = -HUGE_NUMBER;
    }
  for(int i = 0; i < noOfItems; i++)
    {
      for(int kk = 0; kk < 3; kk++)
	{
	  ergo_real x = itemList[i].centerCoords[kk];
	  if(x < xminList[kk])
	    xminList[kk] = x;
	  if(x > xmaxList[kk])
	    xmaxList[kk] = x;
	}
    } // END FOR i
  int bestCoordIndex = 0;
  for(int kk = 0; kk < 3; kk++)
    {
      xdiffList[kk] = xmaxList[kk] - xminList[kk];
      if(xdiffList[kk] > xdiffList[bestCoordIndex])
	bestCoordIndex = kk;
    }
  for(int kk = 0; kk < 3; kk++) {
    if(xdiffList[kk] < 0) {
      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in create_box_system: (xdiffList[kk] < 0).");
      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in create_box_system: kk = %d, xdiffList[kk] = %9.4g < 0.\n", kk, xdiffList[kk]);
      return -1;
    }
  }

  ergo_real largestDiff = xdiffList[bestCoordIndex];

  // compute number of levels and size of mother box
  int numberOfLevels = 1;
  ergo_real width = toplevelBoxSize;
  while(width < largestDiff)
    {
      width *= 2;
      numberOfLevels++;
    }

  if(numberOfLevels >= MAX_NO_OF_BOX_LEVELS)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in create_box_system: (numberOfLevels >= MAX_NO_OF_BOX_LEVELS)");
      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in create_box_system: numberOfLevels = %d, MAX_NO_OF_BOX_LEVELS = %d.", numberOfLevels, (int)MAX_NO_OF_BOX_LEVELS);
      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in create_box_system: toplevelBoxSize = %7.3g, largestDiff = %7.3g.", (double)toplevelBoxSize, (double)largestDiff);
      return -1;
    }

  motherBox->width = width;
  // Set center of mother box.
  for(int kk = 0; kk < 3; kk++)
    motherBox->centerCoords[kk] = xminList[kk] + motherBox->width / 2;
  motherBox->firstItemIndex = 0;
  motherBox->noOfItems = noOfItems;
  
  // OK, mother box done.
  // Now create boxes on other levels, one level at a time.
  
  this->levelList[0].noOfBoxes = 1;
  this->levelList[0].startIndexInBoxList = 0;

  int* itemIndexBucketList[2][2][2];
  for(int ix = 0; ix < 2; ix++)
    for(int iy = 0; iy < 2; iy++)
      for(int iz = 0; iz < 2; iz++)
	itemIndexBucketList[ix][iy][iz] = new int[noOfItems];
  int itemCounterList[2][2][2];
  // Allocate temporary itemList for use when reordering items.
  std::vector<box_item_struct> itemListTemp(noOfItems);

  for(int levelNumber = 1; levelNumber < numberOfLevels; levelNumber++)
    {
      levelList[levelNumber].startIndexInBoxList = currBoxIndex;
      int currLevelNoOfBoxes_1 = 0;
      // go through boxes of previous level, and create new boxes at this level if needed.
      // We go through boxes of previous level twice, the first time to check how many
      // new boxes must be allocated.
      int startIndex = levelList[levelNumber-1].startIndexInBoxList;
      for(int i = startIndex; i < startIndex + levelList[levelNumber-1].noOfBoxes; i++)
	{
	  // now resultBoxList[i] is the box whose children we are creating.
	  boxList[i].firstChildBoxIndex = currBoxIndex;
	  int noOfChildren = 0;
	  if(boxList[i].noOfItems == 0)
	    {
	      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "ERROR: (boxList[i].noOfItems == 0)");
	      return -1;
	    }
	  // create 2*2*2 = 8 new boxes
	  for(int ix = 0; ix < 2; ix++)
	    for(int iy = 0; iy < 2; iy++)
	      for(int iz = 0; iz < 2; iz++)
		{
		  itemCounterList[ix][iy][iz] = 0;
		} // END FOR ix iy iz
	  // now go through the points of the parent box to decide which of the 8 children each point belongs to.
	  for(int j = boxList[i].firstItemIndex; j < boxList[i].firstItemIndex + boxList[i].noOfItems; j++)
	    {
	      int ix = 0;
	      int iy = 0;
	      int iz = 0;
	      if(itemList[j].centerCoords[0] > boxList[i].centerCoords[0])
		ix = 1;
	      if(itemList[j].centerCoords[1] > boxList[i].centerCoords[1])
		iy = 1;
	      if(itemList[j].centerCoords[2] > boxList[i].centerCoords[2])
		iz = 1;
	      itemCounterList[ix][iy][iz]++;
	    } // END FOR j
	  // OK, all items counted, counters updated to indicate how many items 
	  // belong to each of the 8 candidate child boxes.
	  // Now count new boxes.
	  for(int ix = 0; ix < 2; ix++)
	    for(int iy = 0; iy < 2; iy++)
	      for(int iz = 0; iz < 2; iz++)
		{
		  if(itemCounterList[ix][iy][iz] > 0)
		    {
		      currLevelNoOfBoxes_1++;
		      noOfChildren++;
		    } // END IF (itemCounterList[ix][iy][iz] > 0)
		} // END FOR ix iy iz
	} // END FOR i
      
      // OK, now we know how many new boxes are needed.
      int maxNoOfBoxesNew = maxNoOfBoxes + currLevelNoOfBoxes_1;
      box_struct_basic* boxListNew = new box_struct_basic[maxNoOfBoxesNew];
      // copy previous contents of resultBoxList
      memcpy(boxListNew, boxList, maxNoOfBoxes*sizeof(box_struct_basic));
      // free old list, and set pointer to new list instead.
      delete [] boxList;
      boxList = boxListNew;
      maxNoOfBoxes = maxNoOfBoxesNew;

      // Now go through level again, this time creating new boxes and reordering items.
      int currLevelNoOfBoxes_2 = 0;
      for(int i = startIndex; i < startIndex + levelList[levelNumber-1].noOfBoxes; i++)
	{
	  // now resultBoxList[i] is the box whose children we are creating.
	  boxList[i].firstChildBoxIndex = currBoxIndex;
	  int noOfChildren = 0;
	  if(boxList[i].noOfItems == 0)
	    {
	      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "ERROR: (resultBoxList[i].noOfItems == 0)");
	      return -1;
	    }
	  // create 2*2*2 = 8 new boxes
	  box_struct_basic tempBoxList[2][2][2];
	  for(int ix = 0; ix < 2; ix++)
	    for(int iy = 0; iy < 2; iy++)
	      for(int iz = 0; iz < 2; iz++)
		{
		  ergo_real newWidth = boxList[i].width / 2;
		  ergo_real x = boxList[i].centerCoords[0] + (ix*2-1)*0.5*newWidth;
		  ergo_real y = boxList[i].centerCoords[1] + (iy*2-1)*0.5*newWidth;
		  ergo_real z = boxList[i].centerCoords[2] + (iz*2-1)*0.5*newWidth;
		  tempBoxList[ix][iy][iz].centerCoords[0] = x;
		  tempBoxList[ix][iy][iz].centerCoords[1] = y;
		  tempBoxList[ix][iy][iz].centerCoords[2] = z;
		  tempBoxList[ix][iy][iz].width = newWidth;
		  itemCounterList[ix][iy][iz] = 0;
		} // END FOR ix iy iz
	  // now go through the points of the parent box to decide which of the 8 children each point belongs to.
	  for(int j = boxList[i].firstItemIndex; j < boxList[i].firstItemIndex + boxList[i].noOfItems; j++)
	    {
	      int ix = 0;
	      int iy = 0;
	      int iz = 0;
	      if(itemList[j].centerCoords[0] > boxList[i].centerCoords[0])
		ix = 1;
	      if(itemList[j].centerCoords[1] > boxList[i].centerCoords[1])
		iy = 1;
	      if(itemList[j].centerCoords[2] > boxList[i].centerCoords[2])
		iz = 1;
	      itemIndexBucketList[ix][iy][iz][itemCounterList[ix][iy][iz]] = j;
	      itemCounterList[ix][iy][iz]++;
	    } // END FOR j
	  // OK, all items copied to itemBucketList.
	  // Now add new boxes, and order the items accordingly.
	  int currStartIndex = boxList[i].firstItemIndex;
	  for(int ix = 0; ix < 2; ix++)
	    for(int iy = 0; iy < 2; iy++)
	      for(int iz = 0; iz < 2; iz++)
		{
		  if(itemCounterList[ix][iy][iz] > 0)
		    {
		      if(currBoxIndex >= maxNoOfBoxes)
			{
			  do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in create_box_system: (currBoxIndex >= maxNoOfBoxes)");
			  return -1;
			}
		      boxList[currBoxIndex] = tempBoxList[ix][iy][iz];
		      boxList[currBoxIndex].firstItemIndex = currStartIndex;
		      boxList[currBoxIndex].noOfItems = itemCounterList[ix][iy][iz];
		      for(int k = 0; k < itemCounterList[ix][iy][iz]; k++)
			itemListTemp[currStartIndex+k] = itemList[itemIndexBucketList[ix][iy][iz][k]];
		      currStartIndex += itemCounterList[ix][iy][iz];
		      currBoxIndex++;
		      currLevelNoOfBoxes_2++;
		      noOfChildren++;
		    } // END IF (itemCounterList[ix][iy][iz] > 0)
		} // END FOR ix iy iz
	  boxList[i].noOfChildBoxes = noOfChildren;
	} // END FOR i
      if(currLevelNoOfBoxes_2 != currLevelNoOfBoxes_1)
	{
	  do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in create_box_system: (currLevelNoOfBoxes_2 != currLevelNoOfBoxes_1)");
	  return -1;
	}
      levelList[levelNumber].noOfBoxes = currLevelNoOfBoxes_2;
      memcpy(itemList, &itemListTemp[0], noOfItems*sizeof(box_item_struct));
    } // END FOR levelNumber
  
  // OK, boxes created.

  totNoOfBoxes = currBoxIndex;
  noOfLevels = numberOfLevels;

  for(int ix = 0; ix < 2; ix++)
    for(int iy = 0; iy < 2; iy++)
      for(int iz = 0; iz < 2; iz++)
	delete [] itemIndexBucketList[ix][iy][iz];

  do_output(LOG_CAT_INFO, LOG_AREA_INTEGRALS, 
	    "create_box_system end OK, toplevelBoxSize: %5.1f, #levels: %2i, #boxes at top level: %6i", 
	    (double)toplevelBoxSize, numberOfLevels, levelList[noOfLevels-1].noOfBoxes);
  
  return 0;
}



ergo_real
get_min_distance_from_point_to_box(const ergo_real* boxCenterCoords,
				   ergo_real halfwidth,
				   const ergo_real* point)
{
  ergo_real dxList[3];
  for(int k = 0; k < 3; k++)
    {
      ergo_real dx = template_blas_fabs(boxCenterCoords[k] - point[k]);
      if(dx > halfwidth)
	dxList[k] = dx - halfwidth;
      else
	dxList[k] = 0;
    }
  ergo_real sum = 0;
  for(int k = 0; k < 3; k++)
    sum += dxList[k] * dxList[k];
  return template_blas_sqrt(sum);
}

int BoxSystem::get_items_near_point_recursive(const box_item_struct* itemList,
					      const ergo_real* coords, 
					      ergo_real distance, 
					      int* resultOrgIndexList,
					      int level,
					      int boxIndex) const
{
  const box_struct_basic* box = &boxList[boxIndex];
  // Check if this entire box is so far away that it can be skipped.
  ergo_real min_distance_from_point_to_box = get_min_distance_from_point_to_box(box->centerCoords, box->width/2, coords);
  if(min_distance_from_point_to_box > distance)
    return 0;
  // No, we could not skip. Take care of box contents.
  if(level == noOfLevels-1)
    {
      // We are at top level. 
      // Go through all points in box and add the relevant ones to result list.
      int count = 0;
      for(int i = 0; i < box->noOfItems; i++)
	{
	  const box_item_struct* currItem = &itemList[box->firstItemIndex+i];
	  ergo_real sum = 0;
	  for(int coordNo = 0; coordNo < 3; coordNo++)
	    {
	      ergo_real d = coords[coordNo] - currItem->centerCoords[coordNo];
	      sum += d*d;
	    }
	  ergo_real currDist = template_blas_sqrt(sum);
	  if(currDist < distance)
	    {
	      // Add to result.
	      resultOrgIndexList[count] = currItem->originalIndex;
	      count++;
	    }
	} // END FOR i
      return count;
    }
  // Not top level. Go to next level.
  int count = 0;
  for(int i = 0; i < box->noOfChildBoxes; i++)
    {
      int childBoxIndex = box->firstChildBoxIndex + i;
      int nNew = get_items_near_point_recursive(itemList,
						coords, 
						distance, 
						&resultOrgIndexList[count],
						level+1,
						childBoxIndex);
      count += nNew;
    }
  return count;
}



static int
compare_ints(const void* p1, const void* p2)
{
  int i1 = *((int*)p1);
  int i2 = *((int*)p2);
  if(i1 > i2)
    return 1;
  if(i1 < i2)
    return -1;
  return 0;
}



/** Goes through existning box system to find all items within 
    specified distance from given reference point.

    @param itemList the list of items for which the box system was created.
    @param coords list of 3 coordinates for reference point.
    @param distance the distance to find items within.
    @param resultOrgIndexList preallocated list of resulting org indexes.
*/

int BoxSystem::get_items_near_point(const box_item_struct* itemList,
				    const ergo_real* coords, 
				    ergo_real distance, 
				    int* resultOrgIndexList) const
{
  if(!boxList)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_INTEGRALS, "error in BoxSystem::get_items_near_point: (!boxList). Must create box system first!");
      return -1;
    }
  int noOfItems = get_items_near_point_recursive(itemList, coords, distance, resultOrgIndexList, 0, levelList[0].startIndexInBoxList);
  // sort resultOrgIndexList
  qsort(resultOrgIndexList, noOfItems, sizeof(int), compare_ints);
  return noOfItems;
}