Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/linkedcell.cpp

    r0f4538 r042f82  
    66LinkedCell::LinkedCell()
    77{
    8         LC = NULL;
    9         for(int i=0;i<NDIM;i++)
    10                 N[i] = 0;
    11         index = -1;
    12         RADIUS = 0.;
    13         max.Zero();
    14         min.Zero();
     8  LC = NULL;
     9  for(int i=0;i<NDIM;i++)
     10    N[i] = 0;
     11  index = -1;
     12  RADIUS = 0.;
     13  max.Zero();
     14  min.Zero();
    1515};
    1616
     
    2121LinkedCell::LinkedCell(molecule *mol, double radius)
    2222{
    23         atom *Walker = NULL;
     23  atom *Walker = NULL;
    2424
    25         RADIUS = radius;
    26         LC = NULL;
    27         for(int i=0;i<NDIM;i++)
    28                 N[i] = 0;
    29         index = -1;
    30         max.Zero();
    31         min.Zero();
    32         cout << Verbose(1) << "Begin of LinkedCell" << endl;
    33         if (mol->start->next == mol->end) {
    34                 cerr << "ERROR: molecule contains no atoms!" << endl;
    35                 return;
    36         }
    37         // 1. find max and min per axis of atoms
    38         Walker = mol->start->next;
    39         for (int i=0;i<NDIM;i++) {
    40                 max.x[i] = Walker->x.x[i];
    41                 min.x[i] = Walker->x.x[i];
    42         }
    43         while (Walker != mol->end) {
    44                 for (int i=0;i<NDIM;i++) {
    45                         if (max.x[i] < Walker->x.x[i])
    46                                 max.x[i] = Walker->x.x[i];
    47                         if (min.x[i] > Walker->x.x[i])
    48                                 min.x[i] = Walker->x.x[i];
    49                 }
    50                 Walker = Walker->next;
    51         }
    52         cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl;
     25  RADIUS = radius;
     26  LC = NULL;
     27  for(int i=0;i<NDIM;i++)
     28    N[i] = 0;
     29  index = -1;
     30  max.Zero();
     31  min.Zero();
     32  cout << Verbose(1) << "Begin of LinkedCell" << endl;
     33  if (mol->start->next == mol->end) {
     34    cerr << "ERROR: molecule contains no atoms!" << endl;
     35    return;
     36  }
     37  // 1. find max and min per axis of atoms
     38  Walker = mol->start->next;
     39  for (int i=0;i<NDIM;i++) {
     40    max.x[i] = Walker->x.x[i];
     41    min.x[i] = Walker->x.x[i];
     42  }
     43  while (Walker != mol->end) {
     44    for (int i=0;i<NDIM;i++) {
     45      if (max.x[i] < Walker->x.x[i])
     46        max.x[i] = Walker->x.x[i];
     47      if (min.x[i] > Walker->x.x[i])
     48        min.x[i] = Walker->x.x[i];
     49    }
     50    Walker = Walker->next;
     51  }
     52  cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl;
    5353
    54         // 2. find then umber of cells per axis
    55         for (int i=0;i<NDIM;i++) {
    56                 N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1;
    57         }
    58         cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl;
     54  // 2. find then umber of cells per axis
     55  for (int i=0;i<NDIM;i++) {
     56    N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1;
     57  }
     58  cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl;
    5959
    60         // 3. allocate the lists
    61         cout << Verbose(2) << "Allocating cells ... ";
    62         if (LC != NULL) {
    63                 cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl;
    64                 return;
    65         }
    66         LC = new LinkedAtoms[N[0]*N[1]*N[2]];
    67         for (index=0;index<N[0]*N[1]*N[2];index++) {
    68                 LC [index].clear();
    69         }
    70         cout << "done."  << endl;
     60  // 3. allocate the lists
     61  cout << Verbose(2) << "Allocating cells ... ";
     62  if (LC != NULL) {
     63    cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl;
     64    return;
     65  }
     66  LC = new LinkedAtoms[N[0]*N[1]*N[2]];
     67  for (index=0;index<N[0]*N[1]*N[2];index++) {
     68    LC [index].clear();
     69  }
     70  cout << "done."  << endl;
    7171
    72         // 4. put each atom into its respective cell
     72  // 4. put each atom into its respective cell
    7373  cout << Verbose(2) << "Filling cells ... ";
    74         Walker = mol->start;
    75         while (Walker->next != mol->end) {
    76                 Walker = Walker->next;
    77                 for (int i=0;i<NDIM;i++) {
    78                         n[i] = (int)floor((Walker->x.x[i] - min.x[i])/RADIUS);
    79                 }
    80                 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
    81                 LC[index].push_back(Walker);
    82                 //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
    83         }
     74  Walker = mol->start;
     75  while (Walker->next != mol->end) {
     76    Walker = Walker->next;
     77    for (int i=0;i<NDIM;i++) {
     78      n[i] = (int)floor((Walker->x.x[i] - min.x[i])/RADIUS);
     79    }
     80    index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
     81    LC[index].push_back(Walker);
     82    //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
     83  }
    8484  cout << "done."  << endl;
    85         cout << Verbose(1) << "End of LinkedCell" << endl;
     85  cout << Verbose(1) << "End of LinkedCell" << endl;
    8686};
    8787
     
    9090LinkedCell::~LinkedCell()
    9191{
    92         if (LC != NULL)
    93         for (index=0;index<N[0]*N[1]*N[2];index++)
    94                 LC[index].clear();
    95         delete[](LC);
    96         for(int i=0;i<NDIM;i++)
    97                 N[i] = 0;
    98         index = -1;
    99         max.Zero();
    100         min.Zero();
     92  if (LC != NULL)
     93  for (index=0;index<N[0]*N[1]*N[2];index++)
     94    LC[index].clear();
     95  delete[](LC);
     96  for(int i=0;i<NDIM;i++)
     97    N[i] = 0;
     98  index = -1;
     99  max.Zero();
     100  min.Zero();
    101101};
    102102
     
    106106bool LinkedCell::CheckBounds()
    107107{
    108         bool status = true;
    109         for(int i=0;i<NDIM;i++)
    110                 status = status && ((n[i] >=0) && (n[i] < N[i]));
    111         if (!status)
    112         cerr << "ERROR: indices are out of bounds!" << endl;
    113         return status;
     108  bool status = true;
     109  for(int i=0;i<NDIM;i++)
     110    status = status && ((n[i] >=0) && (n[i] < N[i]));
     111  if (!status)
     112  cerr << "ERROR: indices are out of bounds!" << endl;
     113  return status;
    114114};
    115115
     
    120120LinkedAtoms* LinkedCell::GetCurrentCell()
    121121{
    122         if (CheckBounds()) {
    123                 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
    124                 return (&(LC[index]));
    125         } else {
    126                 return NULL;
    127         }
     122  if (CheckBounds()) {
     123    index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
     124    return (&(LC[index]));
     125  } else {
     126    return NULL;
     127  }
    128128};
    129129
    130130/** Calculates the index for a given atom *Walker.
    131  * \param Walker atom to set index to
     131 * \param *Walker atom to set index to
    132132 * \return if the atom is also found in this cell - true, else - false
    133133 */
    134 bool LinkedCell::SetIndexToAtom(const atom &Walker)
     134bool LinkedCell::SetIndexToAtom(atom *Walker)
    135135{
    136         bool status = false;
    137         for (int i=0;i<NDIM;i++) {
    138                 n[i] = (int)floor((Walker.x.x[i] - min.x[i])/RADIUS);
    139         }
    140 
    141         index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
    142         if (CheckBounds()) {
    143                 for (LinkedAtoms::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
    144                         status = status || ((*Runner) == &Walker);
    145                 return status;
    146         } else {
    147                 cerr << Verbose(1) << "WARN: Atom "<< Walker << " at " << Walker.x << " is out of bounds." << endl;
    148                 return false;
    149         }
    150 };
    151 
    152 /** Calculates the interval bounds of the linked cell grid.
    153  * \param *lower lower bounds
    154  * \param *upper upper bounds
    155  */
    156 void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM])
    157 {
     136  bool status = false;
    158137  for (int i=0;i<NDIM;i++) {
    159     lower[i] = ((n[i]-1) >= 0) ? n[i]-1 : 0;
    160     upper[i] = ((n[i]+1) < N[i]) ? n[i]+1 : N[i]-1;
    161     //cout << " [" << Nlower[i] << "," << Nupper[i] << "] ";
    162     // check for this axis whether the point is outside of our grid
    163     if (n[i] < 0)
    164       upper[i] = lower[i];
    165     if (n[i] > N[i])
    166       lower[i] = upper[i];
    167 
    168     //cout << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
     138    n[i] = (int)floor((Walker->x.x[i] - min.x[i])/RADIUS);
     139  }
     140  index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
     141  if (CheckBounds()) {
     142    for (LinkedAtoms::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
     143      status = status || ((*Runner) == Walker);
     144    return status;
     145  } else {
     146    cerr << Verbose(1) << "ERROR: Atom "<< *Walker << " at " << Walker->x << " is out of bounds." << endl;
     147    return false;
    169148  }
    170149};
     
    174153 * \return Vector is inside bounding box - true, else - false
    175154 */
    176 bool LinkedCell::SetIndexToVector(const Vector *x)
     155bool LinkedCell::SetIndexToVector(Vector *x)
    177156{
    178         bool status = true;
    179         for (int i=0;i<NDIM;i++) {
    180                 n[i] = (int)floor((x->x[i] - min.x[i])/RADIUS);
    181                 if (max.x[i] < x->x[i])
    182                         status = false;
    183                 if (min.x[i] > x->x[i])
    184                         status = false;
    185         }
    186         return status;
     157  bool status = true;
     158  for (int i=0;i<NDIM;i++) {
     159    n[i] = (int)floor((x->x[i] - min.x[i])/RADIUS);
     160    if (max.x[i] < x->x[i])
     161      status = false;
     162    if (min.x[i] > x->x[i])
     163      status = false;
     164  }
     165  return status;
    187166};
    188167
Note: See TracChangeset for help on using the changeset viewer.