| [d82961] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2011 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | /*
 | 
|---|
 | 9 |  * LinkedCell_Model.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Nov 15, 2011
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include "LinkedCell_Model.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | #include <algorithm>
 | 
|---|
 | 25 | #include <boost/multi_array.hpp>
 | 
|---|
 | 26 | #include <limits>
 | 
|---|
 | 27 | 
 | 
|---|
| [402f2c] | 28 | #include "Atom/AtomObserver.hpp"
 | 
|---|
 | 29 | #include "Atom/TesselPoint.hpp"
 | 
|---|
| [d82961] | 30 | #include "Box.hpp"
 | 
|---|
 | 31 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 32 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 33 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [2614e2a] | 34 | #include "CodePatterns/Observer/Observer.hpp"
 | 
|---|
| [f55ae5] | 35 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| [2614e2a] | 36 | #include "CodePatterns/toString.hpp"
 | 
|---|
| [d82961] | 37 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
 | 38 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
 | 39 | #include "LinkedCell/IPointCloud.hpp"
 | 
|---|
 | 40 | #include "LinkedCell/LinkedCell.hpp"
 | 
|---|
| [402f2c] | 41 | #include "World.hpp"
 | 
|---|
| [d82961] | 42 | 
 | 
|---|
 | 43 | #include "LinkedCell_Model_inline.hpp"
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | // initialize static entities
 | 
|---|
 | 46 | LinkedCell::tripleIndex LinkedCell::LinkedCell_Model::NearestNeighbors;
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | /** Constructor of LinkedCell_Model.
 | 
|---|
 | 49 |  *
 | 
|---|
 | 50 |  * @param radius desired maximum neighborhood distance
 | 
|---|
 | 51 |  * @param _domain Box instance with domain size and boundary conditions
 | 
|---|
 | 52 |  */
 | 
|---|
 | 53 | LinkedCell::LinkedCell_Model::LinkedCell_Model(const double radius, const Box &_domain) :
 | 
|---|
| [2614e2a] | 54 |     ::Observer(std::string("LinkedCell_Model")+std::string("_")+toString(radius)),
 | 
|---|
| [d82961] | 55 |     internal_Sizes(NULL),
 | 
|---|
 | 56 |     domain(_domain)
 | 
|---|
 | 57 | {
 | 
|---|
 | 58 |   // set default argument
 | 
|---|
 | 59 |   NearestNeighbors[0] = NearestNeighbors[1] = NearestNeighbors[2] = 1;
 | 
|---|
 | 60 | 
 | 
|---|
| [2614e2a] | 61 |   // get the partition of the domain
 | 
|---|
| [d82961] | 62 |   setPartition(radius);
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |   // allocate linked cell structure
 | 
|---|
 | 65 |   AllocateCells();
 | 
|---|
| [402f2c] | 66 | 
 | 
|---|
 | 67 |   // sign in to AtomObserver
 | 
|---|
 | 68 |   startListening();
 | 
|---|
| [d82961] | 69 | }
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | /** Constructor of LinkedCell_Model.
 | 
|---|
 | 72 |  *
 | 
|---|
 | 73 |  * @oaram set set of points to place into the linked cell structure
 | 
|---|
 | 74 |  * @param radius desired maximum neighborhood distance
 | 
|---|
 | 75 |  * @param _domain Box instance with domain size and boundary conditions
 | 
|---|
 | 76 |  */
 | 
|---|
 | 77 | LinkedCell::LinkedCell_Model::LinkedCell_Model(IPointCloud &set, const double radius, const Box &_domain) :
 | 
|---|
| [2614e2a] | 78 |     ::Observer(std::string("LinkedCell_Model")+std::string("_")+toString(radius)),
 | 
|---|
| [d82961] | 79 |     internal_Sizes(NULL),
 | 
|---|
 | 80 |     domain(_domain)
 | 
|---|
 | 81 | {
 | 
|---|
 | 82 |   Info info(__func__);
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 |   // get the partition of the domain
 | 
|---|
 | 85 |   setPartition(radius);
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 |   // allocate linked cell structure
 | 
|---|
 | 88 |   AllocateCells();
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 |   insertPointCloud(set);
 | 
|---|
| [402f2c] | 91 | 
 | 
|---|
 | 92 |   // sign in to AtomObserver
 | 
|---|
 | 93 |   startListening();
 | 
|---|
| [d82961] | 94 | }
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | /** Destructor of class LinkedCell_Model.
 | 
|---|
 | 97 |  *
 | 
|---|
 | 98 |  */
 | 
|---|
 | 99 | LinkedCell::LinkedCell_Model::~LinkedCell_Model()
 | 
|---|
 | 100 | {
 | 
|---|
| [402f2c] | 101 |   // sign off from observables
 | 
|---|
 | 102 |   stopListening();
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |   // reset linked cell structure
 | 
|---|
| [d82961] | 105 |   Reset();
 | 
|---|
 | 106 | }
 | 
|---|
 | 107 | 
 | 
|---|
| [402f2c] | 108 | /** Signs in to AtomObserver and World to known about all changes.
 | 
|---|
 | 109 |  *
 | 
|---|
 | 110 |  */
 | 
|---|
 | 111 | void LinkedCell::LinkedCell_Model::startListening()
 | 
|---|
 | 112 | {
 | 
|---|
 | 113 |   World::getInstance().signOn(this, World::AtomInserted);
 | 
|---|
 | 114 |   World::getInstance().signOn(this, World::AtomRemoved);
 | 
|---|
 | 115 |   AtomObserver::getInstance().signOn(this, AtomObservable::PositionChanged);
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | /** Signs off from AtomObserver and World.
 | 
|---|
 | 119 |  *
 | 
|---|
 | 120 |  */
 | 
|---|
 | 121 | void LinkedCell::LinkedCell_Model::stopListening()
 | 
|---|
 | 122 | {
 | 
|---|
 | 123 |   World::getInstance().signOff(this, World::AtomInserted);
 | 
|---|
 | 124 |   World::getInstance().signOff(this, World::AtomRemoved);
 | 
|---|
 | 125 |   AtomObserver::getInstance().signOff(this, AtomObservable::PositionChanged);
 | 
|---|
 | 126 | }
 | 
|---|
 | 127 | 
 | 
|---|
| [d82961] | 128 | 
 | 
|---|
 | 129 | /** Allocates as much cells per axis as required by
 | 
|---|
 | 130 |  * LinkedCell_Model::BoxPartition.
 | 
|---|
 | 131 |  *
 | 
|---|
 | 132 |  */
 | 
|---|
 | 133 | void LinkedCell::LinkedCell_Model::AllocateCells()
 | 
|---|
 | 134 | {
 | 
|---|
 | 135 |   // resize array
 | 
|---|
 | 136 |   tripleIndex index;
 | 
|---|
 | 137 |   for (int i=0;i<NDIM;i++)
 | 
|---|
 | 138 |     index[i] = static_cast<LinkedCellArray::index>(Dimensions.at(i,i));
 | 
|---|
 | 139 |   N.resize(index);
 | 
|---|
 | 140 |   ASSERT(getSize(0)*getSize(1)*getSize(2) < MAX_LINKEDCELLNODES,
 | 
|---|
| [c80643] | 141 |       "LinkedCell_Model::AllocateCells() - Number linked of linked cell nodes exceeded hard-coded limit, use greater edge length!");
 | 
|---|
| [d82961] | 142 | 
 | 
|---|
 | 143 |   // allocate LinkedCell instances
 | 
|---|
 | 144 |   for(index[0] = 0; index[0] != static_cast<LinkedCellArray::index>(Dimensions.at(0,0)); ++index[0]) {
 | 
|---|
 | 145 |     for(index[1] = 0; index[1] != static_cast<LinkedCellArray::index>(Dimensions.at(1,1)); ++index[1]) {
 | 
|---|
 | 146 |       for(index[2] = 0; index[2] != static_cast<LinkedCellArray::index>(Dimensions.at(2,2)); ++index[2]) {
 | 
|---|
 | 147 |         LOG(5, "INFO: Creating cell at " << index[0] << " " << index[1] << " " << index[2] << ".");
 | 
|---|
 | 148 |         N(index) = new LinkedCell(index);
 | 
|---|
 | 149 |       }
 | 
|---|
 | 150 |     }
 | 
|---|
 | 151 |   }
 | 
|---|
 | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 | /** Frees all Linked Cell instances and sets array dimensions to (0,0,0).
 | 
|---|
 | 155 |  *
 | 
|---|
 | 156 |  */
 | 
|---|
 | 157 | void LinkedCell::LinkedCell_Model::Reset()
 | 
|---|
 | 158 | {
 | 
|---|
 | 159 |   // free all LinkedCell instances
 | 
|---|
 | 160 |   for(iterator3 iter3 = N.begin(); iter3 != N.end(); ++iter3) {
 | 
|---|
 | 161 |     for(iterator2 iter2 = (*iter3).begin(); iter2 != (*iter3).end(); ++iter2) {
 | 
|---|
 | 162 |       for(iterator1 iter1 = (*iter2).begin(); iter1 != (*iter2).end(); ++iter1) {
 | 
|---|
 | 163 |         delete *iter1;
 | 
|---|
 | 164 |       }
 | 
|---|
 | 165 |     }
 | 
|---|
 | 166 |   }
 | 
|---|
 | 167 |   // set dimensions to zero
 | 
|---|
 | 168 |   N.resize(boost::extents[0][0][0]);
 | 
|---|
 | 169 | }
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 | /** Inserts all points contained in \a set.
 | 
|---|
 | 172 |  *
 | 
|---|
 | 173 |  * @param set set with points to insert into linked cell structure
 | 
|---|
 | 174 |  */
 | 
|---|
 | 175 | void LinkedCell::LinkedCell_Model::insertPointCloud(IPointCloud &set)
 | 
|---|
 | 176 | {
 | 
|---|
 | 177 |   if (set.IsEmpty()) {
 | 
|---|
 | 178 |     ELOG(1, "set is NULL or contains no linked cell nodes!");
 | 
|---|
 | 179 |     return;
 | 
|---|
 | 180 |   }
 | 
|---|
 | 181 | 
 | 
|---|
 | 182 |   // put each atom into its respective cell
 | 
|---|
 | 183 |   set.GoToFirst();
 | 
|---|
 | 184 |   while (!set.IsEnd()) {
 | 
|---|
 | 185 |     TesselPoint *Walker = set.GetPoint();
 | 
|---|
 | 186 |     addNode(Walker);
 | 
|---|
 | 187 |     set.GoToNext();
 | 
|---|
 | 188 |   }
 | 
|---|
 | 189 | }
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 | /** Calculates the required edge length for the given desired distance.
 | 
|---|
 | 192 |  *
 | 
|---|
 | 193 |  * We need to make some matrix transformations in order to obtain the required
 | 
|---|
 | 194 |  * edge lengths per axis. Goal is guarantee that whatever the shape of the
 | 
|---|
 | 195 |  * domain that always all points at least up to \a distance away are contained
 | 
|---|
 | 196 |  * in the nearest neighboring cells.
 | 
|---|
 | 197 |  *
 | 
|---|
 | 198 |  * @param distance distance of this linked cell array
 | 
|---|
 | 199 |  */
 | 
|---|
 | 200 | void LinkedCell::LinkedCell_Model::setPartition(double distance)
 | 
|---|
 | 201 | {
 | 
|---|
 | 202 |   // generate box matrix of desired edge length
 | 
|---|
 | 203 |   RealSpaceMatrix neighborhood;
 | 
|---|
 | 204 |   neighborhood.setIdentity();
 | 
|---|
 | 205 |   neighborhood *= distance;
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 |   // obtain refs to both domain matrix transformations
 | 
|---|
 | 208 |   //const RealSpaceMatrix &M = domain.getM();
 | 
|---|
 | 209 |   const RealSpaceMatrix &Minv = domain.getMinv();
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 |   RealSpaceMatrix output = Minv * neighborhood;
 | 
|---|
 | 212 | 
 | 
|---|
 | 213 |   std::cout << Minv << " * " << neighborhood << " = " << output << std::endl;
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 |   Dimensions = output.invert();
 | 
|---|
 | 216 |   Partition = Minv*Dimensions; //
 | 
|---|
 | 217 | 
 | 
|---|
 | 218 |   std::cout << "Dimensions are then " << Dimensions << std::endl;
 | 
|---|
 | 219 |   std::cout << "Partition matrix is then " << Partition << std::endl;
 | 
|---|
 | 220 | }
 | 
|---|
 | 221 | 
 | 
|---|
 | 222 | /** Returns the number of required neighbor-shells to get all neighboring points
 | 
|---|
 | 223 |  * in the given \a distance.
 | 
|---|
 | 224 |  *
 | 
|---|
 | 225 |  * @param distance radius of neighborhood sphere
 | 
|---|
 | 226 |  * @return number of LinkedCell's per dimension to get all neighbors
 | 
|---|
 | 227 |  */
 | 
|---|
 | 228 | const LinkedCell::tripleIndex LinkedCell::LinkedCell_Model::getStep(const double distance) const
 | 
|---|
 | 229 | {
 | 
|---|
 | 230 |   tripleIndex index;
 | 
|---|
 | 231 |   index[0] = index[1] = index[2] = 0;
 | 
|---|
 | 232 | 
 | 
|---|
 | 233 |   if (fabs(distance) < std::numeric_limits<double>::min())
 | 
|---|
 | 234 |     return index;
 | 
|---|
 | 235 |   // generate box matrix of desired edge length
 | 
|---|
 | 236 |   RealSpaceMatrix neighborhood;
 | 
|---|
 | 237 |   neighborhood.setIdentity();
 | 
|---|
 | 238 |   neighborhood *= distance;
 | 
|---|
 | 239 | 
 | 
|---|
 | 240 |   const RealSpaceMatrix output = Partition * neighborhood;
 | 
|---|
 | 241 | 
 | 
|---|
 | 242 |   //std::cout << "GetSteps: " << Partition << " * " << neighborhood << " = " << output << std::endl;
 | 
|---|
 | 243 | 
 | 
|---|
 | 244 |   const RealSpaceMatrix steps = output;
 | 
|---|
 | 245 |   for (size_t i =0; i<NDIM; ++i)
 | 
|---|
 | 246 |     index[i] = ceil(steps.at(i,i));
 | 
|---|
 | 247 |   LOG(2, "INFO: number of shells are ("+toString(index[0])+","+toString(index[1])+","+toString(index[2])+").");
 | 
|---|
 | 248 | 
 | 
|---|
 | 249 |   return index;
 | 
|---|
 | 250 | }
 | 
|---|
 | 251 | 
 | 
|---|
 | 252 | /** Calculates the index of the cell \a position would belong to.
 | 
|---|
 | 253 |  *
 | 
|---|
 | 254 |  * @param position position whose associated cell to calculate
 | 
|---|
 | 255 |  * @return index of the cell
 | 
|---|
 | 256 |  */
 | 
|---|
 | 257 | const LinkedCell::tripleIndex LinkedCell::LinkedCell_Model::getIndexToVector(const Vector &position) const
 | 
|---|
 | 258 | {
 | 
|---|
 | 259 |   tripleIndex index;
 | 
|---|
 | 260 |   Vector x(Partition*position);
 | 
|---|
 | 261 |   LOG(2, "INFO: Transformed position is " << x << ".");
 | 
|---|
 | 262 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 263 |     index[i] = static_cast<LinkedCellArray::index>(floor(x[i]));
 | 
|---|
 | 264 |   }
 | 
|---|
 | 265 |   return index;
 | 
|---|
 | 266 | }
 | 
|---|
 | 267 | 
 | 
|---|
| [db8960] | 268 | /** Adds an update to the list of lazy changes to add a node.
 | 
|---|
| [d82961] | 269 |  *
 | 
|---|
 | 270 |  * @param Walker node to add
 | 
|---|
 | 271 |  */
 | 
|---|
| [029870] | 272 | void LinkedCell::LinkedCell_Model::addNode(const TesselPoint *Walker)
 | 
|---|
| [db8960] | 273 | {
 | 
|---|
 | 274 |   addNode_internal(Walker);
 | 
|---|
 | 275 | }
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 | /** Adds an update to the list of lazy changes to add remove a node.
 | 
|---|
 | 278 |  *
 | 
|---|
 | 279 |  * We do nothing of Walker is not found
 | 
|---|
 | 280 |  *
 | 
|---|
 | 281 |  * @param Walker node to remove
 | 
|---|
 | 282 |  */
 | 
|---|
 | 283 | void LinkedCell::LinkedCell_Model::deleteNode(const TesselPoint *Walker)
 | 
|---|
 | 284 | {
 | 
|---|
 | 285 |   deleteNode_internal(Walker);
 | 
|---|
 | 286 | }
 | 
|---|
 | 287 | 
 | 
|---|
 | 288 | /** Adds an update to the list of lazy changes to move a node.
 | 
|---|
 | 289 |  *
 | 
|---|
 | 290 |  * @param Walker node who has moved.
 | 
|---|
 | 291 |  */
 | 
|---|
 | 292 | void LinkedCell::LinkedCell_Model::moveNode(const TesselPoint *Walker)
 | 
|---|
 | 293 | {
 | 
|---|
 | 294 |   moveNode_internal(Walker);
 | 
|---|
 | 295 | }
 | 
|---|
 | 296 | 
 | 
|---|
 | 297 | /** Internal function to add a node to the linked cell structure
 | 
|---|
 | 298 |  *
 | 
|---|
 | 299 |  * @param Walker node to add
 | 
|---|
 | 300 |  */
 | 
|---|
 | 301 | void LinkedCell::LinkedCell_Model::addNode_internal(const TesselPoint *Walker)
 | 
|---|
| [d82961] | 302 | {
 | 
|---|
 | 303 |   tripleIndex index = getIndexToVector(Walker->getPosition());
 | 
|---|
 | 304 |   LOG(2, "INFO: " << *Walker << " goes into cell " << index[0] << ", " << index[1] << ", " << index[2] << ".");
 | 
|---|
 | 305 |   LOG(2, "INFO: Cell's indices are "
 | 
|---|
 | 306 |       << N(index)->getIndex(0) << " "
 | 
|---|
 | 307 |       << N(index)->getIndex(1) << " "
 | 
|---|
 | 308 |       << N(index)->getIndex(2) << ".");
 | 
|---|
 | 309 |   N(index)->addPoint(Walker);
 | 
|---|
 | 310 |   std::pair<MapPointToCell::iterator, bool> inserter = CellLookup.insert( std::make_pair(Walker, N(index)) );
 | 
|---|
 | 311 |   ASSERT( inserter.second,
 | 
|---|
 | 312 |       "LinkedCell_Model::addNode() - Walker "
 | 
|---|
 | 313 |       +toString(*Walker)+" is already present in cell "
 | 
|---|
 | 314 |       +toString((inserter.first)->second->getIndex(0))+" "
 | 
|---|
 | 315 |       +toString((inserter.first)->second->getIndex(1))+" "
 | 
|---|
 | 316 |       +toString((inserter.first)->second->getIndex(2))+".");
 | 
|---|
 | 317 | }
 | 
|---|
 | 318 | 
 | 
|---|
| [db8960] | 319 | /** Internal function to remove a node to the linked cell structure
 | 
|---|
| [d82961] | 320 |  *
 | 
|---|
 | 321 |  * We do nothing of Walker is not found
 | 
|---|
 | 322 |  *
 | 
|---|
 | 323 |  * @param Walker node to remove
 | 
|---|
 | 324 |  */
 | 
|---|
| [db8960] | 325 | void LinkedCell::LinkedCell_Model::deleteNode_internal(const TesselPoint *Walker)
 | 
|---|
| [d82961] | 326 | {
 | 
|---|
 | 327 |   MapPointToCell::iterator iter = CellLookup.begin();
 | 
|---|
 | 328 |   for (; iter != CellLookup.end(); ++iter)
 | 
|---|
 | 329 |     if (iter->first == Walker)
 | 
|---|
 | 330 |       break;
 | 
|---|
 | 331 |   ASSERT(iter != CellLookup.end(),
 | 
|---|
 | 332 |       "LinkedCell_Model::deleteNode() - Walker not present in cell stored under CellLookup.");
 | 
|---|
 | 333 |   if (iter != CellLookup.end()) {
 | 
|---|
 | 334 |     CellLookup.erase(iter);
 | 
|---|
 | 335 |     iter->second->deletePoint(Walker);
 | 
|---|
 | 336 |   }
 | 
|---|
 | 337 | }
 | 
|---|
 | 338 | 
 | 
|---|
| [db8960] | 339 | /** Internal function to move node from current cell to another on position change.
 | 
|---|
| [d82961] | 340 |  *
 | 
|---|
 | 341 |  * @param Walker node who has moved.
 | 
|---|
 | 342 |  */
 | 
|---|
| [db8960] | 343 | void LinkedCell::LinkedCell_Model::moveNode_internal(const TesselPoint *Walker)
 | 
|---|
| [d82961] | 344 | {
 | 
|---|
 | 345 |   ASSERT(0, "LinkedCell_Model::moveNode() - not implemented yet.");
 | 
|---|
 | 346 | }
 | 
|---|
 | 347 | 
 | 
|---|
 | 348 | /** Checks whether cell indicated by \a relative relative to LinkedCell_Model::internal_index
 | 
|---|
 | 349 |  * is out of bounds.
 | 
|---|
 | 350 |  *
 | 
|---|
 | 351 |  * \note We do not check for boundary conditions of LinkedCell_Model::domain,
 | 
|---|
 | 352 |  * we only look at the array sizes
 | 
|---|
 | 353 |  *
 | 
|---|
 | 354 |  * @param relative index relative to LinkedCell_Model::internal_index.
 | 
|---|
 | 355 |  * @return true - relative index is still inside bounds, false - outside
 | 
|---|
 | 356 |  */
 | 
|---|
 | 357 | bool LinkedCell::LinkedCell_Model::checkArrayBounds(const tripleIndex &index) const
 | 
|---|
 | 358 | {
 | 
|---|
 | 359 |   bool status = true;
 | 
|---|
 | 360 |   for (size_t i=0;i<NDIM;++i) {
 | 
|---|
 | 361 |     status = status && (
 | 
|---|
 | 362 |         (index[i] >= 0) &&
 | 
|---|
 | 363 |         (index[i] < getSize(i))
 | 
|---|
 | 364 |         );
 | 
|---|
 | 365 |   }
 | 
|---|
 | 366 |   return status;
 | 
|---|
 | 367 | }
 | 
|---|
 | 368 | 
 | 
|---|
 | 369 | /** Corrects \a index according to boundary conditions of LinkedCell_Model::domain .
 | 
|---|
 | 370 |  *
 | 
|---|
 | 371 |  * @param index index to correct according to boundary conditions
 | 
|---|
 | 372 |  */
 | 
|---|
 | 373 | void LinkedCell::LinkedCell_Model::applyBoundaryConditions(tripleIndex &index) const
 | 
|---|
 | 374 | {
 | 
|---|
 | 375 |   for (size_t i=0;i<NDIM;++i) {
 | 
|---|
 | 376 |     switch (domain.getConditions()[i]) {
 | 
|---|
 | 377 |     case Box::Wrap:
 | 
|---|
 | 378 |       if ((index[i] < 0) || (index[i] >= getSize(i)))
 | 
|---|
 | 379 |         index[i] = (index[i] % getSize(i));
 | 
|---|
 | 380 |       break;
 | 
|---|
 | 381 |     case Box::Bounce:
 | 
|---|
| [53d894] | 382 |       if (index[i] < 0)
 | 
|---|
 | 383 |         index[i] = 0;
 | 
|---|
 | 384 |       if (index[i] >= getSize(i))
 | 
|---|
 | 385 |         index[i] = getSize(i)-1;
 | 
|---|
| [d82961] | 386 |       break;
 | 
|---|
 | 387 |     case Box::Ignore:
 | 
|---|
| [53d894] | 388 |       if (index[i] < 0)
 | 
|---|
 | 389 |         index[i] = 0;
 | 
|---|
 | 390 |       if (index[i] >= getSize(i))
 | 
|---|
 | 391 |         index[i] = getSize(i)-1;
 | 
|---|
| [d82961] | 392 |       break;
 | 
|---|
 | 393 |     default:
 | 
|---|
 | 394 |       ASSERT(0, "LinkedCell_Model::checkBounds() - unknown boundary conditions.");
 | 
|---|
 | 395 |       break;
 | 
|---|
 | 396 |     }
 | 
|---|
 | 397 |   }
 | 
|---|
 | 398 | }
 | 
|---|
 | 399 | 
 | 
|---|
 | 400 | /** Calculates the interval bounds of the linked cell grid.
 | 
|---|
| [53d894] | 401 |  *
 | 
|---|
 | 402 |  * \note we assume for index to allows be valid, i.e. within the range of LinkedCell_Model::N.
 | 
|---|
 | 403 |  *
 | 
|---|
| [d82961] | 404 |  * \param index index to give relative bounds to
 | 
|---|
 | 405 |  * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
 | 
|---|
 | 406 |  * \return pair of tripleIndex indicating lower and upper bounds
 | 
|---|
 | 407 |  */
 | 
|---|
 | 408 | const LinkedCell::LinkedCell_Model::LinkedCellNeighborhoodBounds LinkedCell::LinkedCell_Model::getNeighborhoodBounds(
 | 
|---|
 | 409 |     const tripleIndex &index,
 | 
|---|
 | 410 |     const tripleIndex &step
 | 
|---|
 | 411 |     ) const
 | 
|---|
 | 412 | {
 | 
|---|
 | 413 |   LinkedCellNeighborhoodBounds neighbors;
 | 
|---|
 | 414 | 
 | 
|---|
 | 415 |   // calculate bounds
 | 
|---|
 | 416 |   for (size_t i=0;i<NDIM;++i) {
 | 
|---|
| [53d894] | 417 |     ASSERT(index[i] >= 0,
 | 
|---|
 | 418 |         "LinkedCell_Model::getNeighborhoodBounds() - index "+toString(index)+" out of lower bounds.");
 | 
|---|
 | 419 |     ASSERT (index[i] < getSize(i),
 | 
|---|
 | 420 |         "LinkedCell_Model::getNeighborhoodBounds() - index "+toString(index)+" out of upper bounds.");
 | 
|---|
| [d82961] | 421 |     switch (domain.getConditions()[i]) {
 | 
|---|
 | 422 |       case Box::Wrap:
 | 
|---|
| [53d894] | 423 |         if ((index[i] - step[i]) < 0)
 | 
|---|
 | 424 |           neighbors.first[i] = getSize(i) + (index[i] - step[i]);
 | 
|---|
 | 425 |         else if ((index[i] - step[i]) >= getSize(i))
 | 
|---|
 | 426 |           neighbors.first[i] = (index[i] - step[i]) - getSize(i);
 | 
|---|
 | 427 |         else
 | 
|---|
| [d82961] | 428 |           neighbors.first[i] = index[i] - step[i];
 | 
|---|
| [53d894] | 429 |         neighbors.second[i] = 2*step[i]+1;
 | 
|---|
| [d82961] | 430 |         break;
 | 
|---|
 | 431 |       case Box::Bounce:
 | 
|---|
| [53d894] | 432 |         neighbors.second[i] = 2*step[i]+1;
 | 
|---|
| [d82961] | 433 |         if (index[i] - step[i] >= 0) {
 | 
|---|
 | 434 |           neighbors.first[i] = index[i] - step[i];
 | 
|---|
 | 435 |         } else {
 | 
|---|
| [53d894] | 436 |           neighbors.first[i] = 0;
 | 
|---|
 | 437 |           neighbors.second[i] = index[i] + step[i]+1;
 | 
|---|
| [d82961] | 438 |         }
 | 
|---|
| [53d894] | 439 |         if (index[i] + step[i] >= getSize(i)) {
 | 
|---|
 | 440 |           neighbors.second[i] = getSize(i) - (index[i] - step[i]);
 | 
|---|
| [d82961] | 441 |         }
 | 
|---|
 | 442 |         break;
 | 
|---|
 | 443 |       case Box::Ignore:
 | 
|---|
| [53d894] | 444 |         if (index[i] - step[i] < 0)
 | 
|---|
 | 445 |           neighbors.first[i] = 0;
 | 
|---|
 | 446 |         else
 | 
|---|
 | 447 |           neighbors.first[i] = index[i] - step[i];
 | 
|---|
 | 448 |         if (index[i] + step[i] >= getSize(i))
 | 
|---|
 | 449 |           neighbors.second[i] = getSize(i) - index[i];
 | 
|---|
 | 450 |         else
 | 
|---|
 | 451 |           neighbors.second[i] = 2*step[i]+1;
 | 
|---|
| [d82961] | 452 |         break;
 | 
|---|
 | 453 |       default:
 | 
|---|
 | 454 |         ASSERT(0, "LinkedCell_Model::getNeighborhoodBounds() - unknown boundary conditions.");
 | 
|---|
 | 455 |         break;
 | 
|---|
 | 456 |     }
 | 
|---|
 | 457 |   }
 | 
|---|
 | 458 | 
 | 
|---|
 | 459 |   return neighbors;
 | 
|---|
 | 460 | }
 | 
|---|
| [2614e2a] | 461 | 
 | 
|---|
 | 462 | /** Callback function for Observer mechanism.
 | 
|---|
 | 463 |  *
 | 
|---|
 | 464 |  * @param publisher reference to the Observable that calls
 | 
|---|
 | 465 |  */
 | 
|---|
 | 466 | void LinkedCell::LinkedCell_Model::update(Observable *publisher)
 | 
|---|
| [f55ae5] | 467 | {
 | 
|---|
 | 468 |   ELOG(2, "LinkedCell_Model received inconclusive general update from "
 | 
|---|
 | 469 |       << publisher << ".");
 | 
|---|
 | 470 | }
 | 
|---|
| [2614e2a] | 471 | 
 | 
|---|
 | 472 | /** Callback function for the Notifications mechanism.
 | 
|---|
 | 473 |  *
 | 
|---|
 | 474 |  * @param publisher reference to the Observable that calls
 | 
|---|
 | 475 |  * @param notification specific notification as cause of the call
 | 
|---|
 | 476 |  */
 | 
|---|
 | 477 | void LinkedCell::LinkedCell_Model::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
| [f55ae5] | 478 | {
 | 
|---|
 | 479 |   // either it's the World or from the atom (through relay) itself
 | 
|---|
 | 480 |   if (publisher == World::getPointer()) {
 | 
|---|
 | 481 |     switch(notification->getChannelNo()) {
 | 
|---|
 | 482 |       case World::AtomInserted:
 | 
|---|
 | 483 |         addNode(World::getInstance().lastChanged<atom>());
 | 
|---|
 | 484 |         break;
 | 
|---|
 | 485 |       case World::AtomRemoved:
 | 
|---|
 | 486 |         deleteNode(World::getInstance().lastChanged<atom>());
 | 
|---|
 | 487 |         break;
 | 
|---|
 | 488 |     }
 | 
|---|
 | 489 |   } else {
 | 
|---|
 | 490 |     switch(notification->getChannelNo()) {
 | 
|---|
 | 491 |       case AtomObservable::PositionChanged:
 | 
|---|
 | 492 |       {
 | 
|---|
 | 493 |         moveNode(dynamic_cast<const TesselPoint *>(publisher));
 | 
|---|
 | 494 |         break;
 | 
|---|
 | 495 |       }
 | 
|---|
 | 496 |       default:
 | 
|---|
 | 497 |         LOG(2, "LinkedCell_Model received unwanted notification from AtomObserver's channel "
 | 
|---|
 | 498 |             << notification->getChannelNo() << ".");
 | 
|---|
 | 499 |         break;
 | 
|---|
 | 500 |     }
 | 
|---|
 | 501 |   }
 | 
|---|
 | 502 | }
 | 
|---|
| [2614e2a] | 503 | 
 | 
|---|
 | 504 | /** Callback function when an Observer dies.
 | 
|---|
 | 505 |  *
 | 
|---|
 | 506 |  * @param publisher reference to the Observable that calls
 | 
|---|
 | 507 |  */
 | 
|---|
 | 508 | void LinkedCell::LinkedCell_Model::subjectKilled(Observable *publisher)
 | 
|---|
 | 509 | {}
 | 
|---|
 | 510 | 
 | 
|---|