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