[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 |
|
---|
[e776dc] | 110 | // delete change queue
|
---|
| 111 | delete Changes;
|
---|
| 112 |
|
---|
[402f2c] | 113 | // reset linked cell structure
|
---|
[d82961] | 114 | Reset();
|
---|
[e776dc] | 115 |
|
---|
[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!");
|
---|
[d82961] | 152 |
|
---|
| 153 | // allocate LinkedCell instances
|
---|
| 154 | for(index[0] = 0; index[0] != static_cast<LinkedCellArray::index>(Dimensions.at(0,0)); ++index[0]) {
|
---|
| 155 | for(index[1] = 0; index[1] != static_cast<LinkedCellArray::index>(Dimensions.at(1,1)); ++index[1]) {
|
---|
| 156 | for(index[2] = 0; index[2] != static_cast<LinkedCellArray::index>(Dimensions.at(2,2)); ++index[2]) {
|
---|
| 157 | LOG(5, "INFO: Creating cell at " << index[0] << " " << index[1] << " " << index[2] << ".");
|
---|
[8c31865] | 158 | N->setN()(index) = new LinkedCell(index);
|
---|
[d82961] | 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /** Frees all Linked Cell instances and sets array dimensions to (0,0,0).
|
---|
| 165 | *
|
---|
| 166 | */
|
---|
| 167 | void LinkedCell::LinkedCell_Model::Reset()
|
---|
| 168 | {
|
---|
| 169 | // free all LinkedCell instances
|
---|
[8c31865] | 170 | for(iterator3 iter3 = N->setN().begin(); iter3 != N->setN().end(); ++iter3) {
|
---|
[d82961] | 171 | for(iterator2 iter2 = (*iter3).begin(); iter2 != (*iter3).end(); ++iter2) {
|
---|
| 172 | for(iterator1 iter1 = (*iter2).begin(); iter1 != (*iter2).end(); ++iter1) {
|
---|
| 173 | delete *iter1;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | // set dimensions to zero
|
---|
[8c31865] | 178 | N->setN().resize(boost::extents[0][0][0]);
|
---|
[d82961] | 179 | }
|
---|
| 180 |
|
---|
| 181 | /** Inserts all points contained in \a set.
|
---|
| 182 | *
|
---|
| 183 | * @param set set with points to insert into linked cell structure
|
---|
| 184 | */
|
---|
| 185 | void LinkedCell::LinkedCell_Model::insertPointCloud(IPointCloud &set)
|
---|
| 186 | {
|
---|
| 187 | if (set.IsEmpty()) {
|
---|
| 188 | ELOG(1, "set is NULL or contains no linked cell nodes!");
|
---|
| 189 | return;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | // put each atom into its respective cell
|
---|
| 193 | set.GoToFirst();
|
---|
| 194 | while (!set.IsEnd()) {
|
---|
| 195 | TesselPoint *Walker = set.GetPoint();
|
---|
| 196 | addNode(Walker);
|
---|
| 197 | set.GoToNext();
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** Calculates the required edge length for the given desired distance.
|
---|
| 202 | *
|
---|
| 203 | * We need to make some matrix transformations in order to obtain the required
|
---|
| 204 | * edge lengths per axis. Goal is guarantee that whatever the shape of the
|
---|
| 205 | * domain that always all points at least up to \a distance away are contained
|
---|
| 206 | * in the nearest neighboring cells.
|
---|
| 207 | *
|
---|
| 208 | * @param distance distance of this linked cell array
|
---|
| 209 | */
|
---|
| 210 | void LinkedCell::LinkedCell_Model::setPartition(double distance)
|
---|
| 211 | {
|
---|
| 212 | // generate box matrix of desired edge length
|
---|
| 213 | RealSpaceMatrix neighborhood;
|
---|
| 214 | neighborhood.setIdentity();
|
---|
| 215 | neighborhood *= distance;
|
---|
| 216 |
|
---|
| 217 | // obtain refs to both domain matrix transformations
|
---|
| 218 | //const RealSpaceMatrix &M = domain.getM();
|
---|
| 219 | const RealSpaceMatrix &Minv = domain.getMinv();
|
---|
| 220 |
|
---|
| 221 | RealSpaceMatrix output = Minv * neighborhood;
|
---|
| 222 |
|
---|
| 223 | std::cout << Minv << " * " << neighborhood << " = " << output << std::endl;
|
---|
| 224 |
|
---|
| 225 | Dimensions = output.invert();
|
---|
| 226 | Partition = Minv*Dimensions; //
|
---|
| 227 |
|
---|
| 228 | std::cout << "Dimensions are then " << Dimensions << std::endl;
|
---|
| 229 | std::cout << "Partition matrix is then " << Partition << std::endl;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /** Returns the number of required neighbor-shells to get all neighboring points
|
---|
| 233 | * in the given \a distance.
|
---|
| 234 | *
|
---|
| 235 | * @param distance radius of neighborhood sphere
|
---|
| 236 | * @return number of LinkedCell's per dimension to get all neighbors
|
---|
| 237 | */
|
---|
| 238 | const LinkedCell::tripleIndex LinkedCell::LinkedCell_Model::getStep(const double distance) const
|
---|
| 239 | {
|
---|
| 240 | tripleIndex index;
|
---|
| 241 | index[0] = index[1] = index[2] = 0;
|
---|
| 242 |
|
---|
| 243 | if (fabs(distance) < std::numeric_limits<double>::min())
|
---|
| 244 | return index;
|
---|
| 245 | // generate box matrix of desired edge length
|
---|
| 246 | RealSpaceMatrix neighborhood;
|
---|
| 247 | neighborhood.setIdentity();
|
---|
| 248 | neighborhood *= distance;
|
---|
| 249 |
|
---|
| 250 | const RealSpaceMatrix output = Partition * neighborhood;
|
---|
| 251 |
|
---|
| 252 | //std::cout << "GetSteps: " << Partition << " * " << neighborhood << " = " << output << std::endl;
|
---|
| 253 |
|
---|
| 254 | const RealSpaceMatrix steps = output;
|
---|
| 255 | for (size_t i =0; i<NDIM; ++i)
|
---|
| 256 | index[i] = ceil(steps.at(i,i));
|
---|
| 257 | LOG(2, "INFO: number of shells are ("+toString(index[0])+","+toString(index[1])+","+toString(index[2])+").");
|
---|
| 258 |
|
---|
| 259 | return index;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /** Calculates the index of the cell \a position would belong to.
|
---|
| 263 | *
|
---|
| 264 | * @param position position whose associated cell to calculate
|
---|
| 265 | * @return index of the cell
|
---|
| 266 | */
|
---|
| 267 | const LinkedCell::tripleIndex LinkedCell::LinkedCell_Model::getIndexToVector(const Vector &position) const
|
---|
| 268 | {
|
---|
| 269 | tripleIndex index;
|
---|
| 270 | Vector x(Partition*position);
|
---|
| 271 | LOG(2, "INFO: Transformed position is " << x << ".");
|
---|
| 272 | for (int i=0;i<NDIM;i++) {
|
---|
| 273 | index[i] = static_cast<LinkedCellArray::index>(floor(x[i]));
|
---|
| 274 | }
|
---|
| 275 | return index;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[db8960] | 278 | /** Adds an update to the list of lazy changes to add a node.
|
---|
[d82961] | 279 | *
|
---|
| 280 | * @param Walker node to add
|
---|
| 281 | */
|
---|
[029870] | 282 | void LinkedCell::LinkedCell_Model::addNode(const TesselPoint *Walker)
|
---|
[db8960] | 283 | {
|
---|
[54f3d1] | 284 | LOG(2, "INFO: Requesting update to add node " << *Walker << ".");
|
---|
| 285 | Changes->addUpdate(Walker, 0, boost::bind(&LinkedCell_Model::addNode_internal, this, _1));
|
---|
[db8960] | 286 | }
|
---|
| 287 |
|
---|
| 288 | /** Adds an update to the list of lazy changes to add remove a node.
|
---|
| 289 | *
|
---|
| 290 | * We do nothing of Walker is not found
|
---|
| 291 | *
|
---|
| 292 | * @param Walker node to remove
|
---|
| 293 | */
|
---|
| 294 | void LinkedCell::LinkedCell_Model::deleteNode(const TesselPoint *Walker)
|
---|
| 295 | {
|
---|
[54f3d1] | 296 | LOG(2, "INFO: Requesting update to delete node " << *Walker << ".");
|
---|
| 297 | Changes->addUpdate(Walker, 0, boost::bind(&LinkedCell_Model::deleteNode_internal, this, _1));
|
---|
[db8960] | 298 | }
|
---|
| 299 |
|
---|
| 300 | /** Adds an update to the list of lazy changes to move a node.
|
---|
| 301 | *
|
---|
| 302 | * @param Walker node who has moved.
|
---|
| 303 | */
|
---|
| 304 | void LinkedCell::LinkedCell_Model::moveNode(const TesselPoint *Walker)
|
---|
| 305 | {
|
---|
[54f3d1] | 306 | LOG(2, "INFO: Requesting update to move node " << *Walker << " to position "
|
---|
| 307 | << Walker->getPosition() << ".");
|
---|
| 308 | Changes->addUpdate(Walker, 10, boost::bind(&LinkedCell_Model::moveNode_internal, this, _1));
|
---|
[db8960] | 309 | }
|
---|
| 310 |
|
---|
| 311 | /** Internal function to add a node to the linked cell structure
|
---|
| 312 | *
|
---|
| 313 | * @param Walker node to add
|
---|
| 314 | */
|
---|
| 315 | void LinkedCell::LinkedCell_Model::addNode_internal(const TesselPoint *Walker)
|
---|
[d82961] | 316 | {
|
---|
[a5b2b6b] | 317 | // find index
|
---|
[d82961] | 318 | tripleIndex index = getIndexToVector(Walker->getPosition());
|
---|
| 319 | LOG(2, "INFO: " << *Walker << " goes into cell " << index[0] << ", " << index[1] << ", " << index[2] << ".");
|
---|
| 320 | LOG(2, "INFO: Cell's indices are "
|
---|
[8c31865] | 321 | << (N->getN())(index)->getIndex(0) << " "
|
---|
| 322 | << (N->getN())(index)->getIndex(1) << " "
|
---|
| 323 | << (N->getN())(index)->getIndex(2) << ".");
|
---|
[a5b2b6b] | 324 | // add to cell
|
---|
[8c31865] | 325 | (N->setN())(index)->addPoint(Walker);
|
---|
[a5b2b6b] | 326 | // add to index with check for presence
|
---|
[8c31865] | 327 | std::pair<MapPointToCell::iterator, bool> inserter = CellLookup.insert( std::make_pair(Walker, (N->setN())(index)) );
|
---|
[d82961] | 328 | ASSERT( inserter.second,
|
---|
| 329 | "LinkedCell_Model::addNode() - Walker "
|
---|
| 330 | +toString(*Walker)+" is already present in cell "
|
---|
| 331 | +toString((inserter.first)->second->getIndex(0))+" "
|
---|
| 332 | +toString((inserter.first)->second->getIndex(1))+" "
|
---|
| 333 | +toString((inserter.first)->second->getIndex(2))+".");
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[db8960] | 336 | /** Internal function to remove a node to the linked cell structure
|
---|
[d82961] | 337 | *
|
---|
| 338 | * We do nothing of Walker is not found
|
---|
| 339 | *
|
---|
| 340 | * @param Walker node to remove
|
---|
| 341 | */
|
---|
[db8960] | 342 | void LinkedCell::LinkedCell_Model::deleteNode_internal(const TesselPoint *Walker)
|
---|
[d82961] | 343 | {
|
---|
[a5b2b6b] | 344 | MapPointToCell::iterator iter = CellLookup.find(Walker);
|
---|
[d82961] | 345 | ASSERT(iter != CellLookup.end(),
|
---|
| 346 | "LinkedCell_Model::deleteNode() - Walker not present in cell stored under CellLookup.");
|
---|
| 347 | if (iter != CellLookup.end()) {
|
---|
[8c31865] | 348 | // remove from lookup
|
---|
[d82961] | 349 | CellLookup.erase(iter);
|
---|
[8c31865] | 350 | // remove from cell
|
---|
[d82961] | 351 | iter->second->deletePoint(Walker);
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[db8960] | 355 | /** Internal function to move node from current cell to another on position change.
|
---|
[d82961] | 356 | *
|
---|
| 357 | * @param Walker node who has moved.
|
---|
| 358 | */
|
---|
[db8960] | 359 | void LinkedCell::LinkedCell_Model::moveNode_internal(const TesselPoint *Walker)
|
---|
[d82961] | 360 | {
|
---|
[cf6530] | 361 | MapPointToCell::iterator iter = CellLookup.find(Walker);
|
---|
| 362 | ASSERT(iter != CellLookup.end(),
|
---|
| 363 | "LinkedCell_Model::deleteNode() - Walker not present in cell stored under CellLookup.");
|
---|
| 364 | if (iter != CellLookup.end()) {
|
---|
| 365 | tripleIndex index = getIndexToVector(Walker->getPosition());
|
---|
| 366 | if (index != iter->second->getIndices()) {
|
---|
| 367 | // remove in old cell
|
---|
| 368 | iter->second->deletePoint(Walker);
|
---|
| 369 | // add to new cell
|
---|
[8c31865] | 370 | N->setN()(index)->addPoint(Walker);
|
---|
[cf6530] | 371 | // update lookup
|
---|
[8c31865] | 372 | iter->second = N->setN()(index);
|
---|
[cf6530] | 373 | }
|
---|
| 374 | }
|
---|
[d82961] | 375 | }
|
---|
| 376 |
|
---|
| 377 | /** Checks whether cell indicated by \a relative relative to LinkedCell_Model::internal_index
|
---|
| 378 | * is out of bounds.
|
---|
| 379 | *
|
---|
| 380 | * \note We do not check for boundary conditions of LinkedCell_Model::domain,
|
---|
| 381 | * we only look at the array sizes
|
---|
| 382 | *
|
---|
| 383 | * @param relative index relative to LinkedCell_Model::internal_index.
|
---|
| 384 | * @return true - relative index is still inside bounds, false - outside
|
---|
| 385 | */
|
---|
| 386 | bool LinkedCell::LinkedCell_Model::checkArrayBounds(const tripleIndex &index) const
|
---|
| 387 | {
|
---|
| 388 | bool status = true;
|
---|
| 389 | for (size_t i=0;i<NDIM;++i) {
|
---|
| 390 | status = status && (
|
---|
| 391 | (index[i] >= 0) &&
|
---|
| 392 | (index[i] < getSize(i))
|
---|
| 393 | );
|
---|
| 394 | }
|
---|
| 395 | return status;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | /** Corrects \a index according to boundary conditions of LinkedCell_Model::domain .
|
---|
| 399 | *
|
---|
| 400 | * @param index index to correct according to boundary conditions
|
---|
| 401 | */
|
---|
| 402 | void LinkedCell::LinkedCell_Model::applyBoundaryConditions(tripleIndex &index) const
|
---|
| 403 | {
|
---|
| 404 | for (size_t i=0;i<NDIM;++i) {
|
---|
| 405 | switch (domain.getConditions()[i]) {
|
---|
| 406 | case Box::Wrap:
|
---|
| 407 | if ((index[i] < 0) || (index[i] >= getSize(i)))
|
---|
| 408 | index[i] = (index[i] % getSize(i));
|
---|
| 409 | break;
|
---|
| 410 | case Box::Bounce:
|
---|
[53d894] | 411 | if (index[i] < 0)
|
---|
| 412 | index[i] = 0;
|
---|
| 413 | if (index[i] >= getSize(i))
|
---|
| 414 | index[i] = getSize(i)-1;
|
---|
[d82961] | 415 | break;
|
---|
| 416 | case Box::Ignore:
|
---|
[53d894] | 417 | if (index[i] < 0)
|
---|
| 418 | index[i] = 0;
|
---|
| 419 | if (index[i] >= getSize(i))
|
---|
| 420 | index[i] = getSize(i)-1;
|
---|
[d82961] | 421 | break;
|
---|
| 422 | default:
|
---|
| 423 | ASSERT(0, "LinkedCell_Model::checkBounds() - unknown boundary conditions.");
|
---|
| 424 | break;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | /** Calculates the interval bounds of the linked cell grid.
|
---|
[53d894] | 430 | *
|
---|
| 431 | * \note we assume for index to allows be valid, i.e. within the range of LinkedCell_Model::N.
|
---|
| 432 | *
|
---|
[d82961] | 433 | * \param index index to give relative bounds to
|
---|
| 434 | * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
|
---|
| 435 | * \return pair of tripleIndex indicating lower and upper bounds
|
---|
| 436 | */
|
---|
| 437 | const LinkedCell::LinkedCell_Model::LinkedCellNeighborhoodBounds LinkedCell::LinkedCell_Model::getNeighborhoodBounds(
|
---|
| 438 | const tripleIndex &index,
|
---|
| 439 | const tripleIndex &step
|
---|
| 440 | ) const
|
---|
| 441 | {
|
---|
| 442 | LinkedCellNeighborhoodBounds neighbors;
|
---|
| 443 |
|
---|
| 444 | // calculate bounds
|
---|
| 445 | for (size_t i=0;i<NDIM;++i) {
|
---|
[53d894] | 446 | ASSERT(index[i] >= 0,
|
---|
| 447 | "LinkedCell_Model::getNeighborhoodBounds() - index "+toString(index)+" out of lower bounds.");
|
---|
| 448 | ASSERT (index[i] < getSize(i),
|
---|
| 449 | "LinkedCell_Model::getNeighborhoodBounds() - index "+toString(index)+" out of upper bounds.");
|
---|
[d82961] | 450 | switch (domain.getConditions()[i]) {
|
---|
| 451 | case Box::Wrap:
|
---|
[53d894] | 452 | if ((index[i] - step[i]) < 0)
|
---|
| 453 | neighbors.first[i] = getSize(i) + (index[i] - step[i]);
|
---|
| 454 | else if ((index[i] - step[i]) >= getSize(i))
|
---|
| 455 | neighbors.first[i] = (index[i] - step[i]) - getSize(i);
|
---|
| 456 | else
|
---|
[d82961] | 457 | neighbors.first[i] = index[i] - step[i];
|
---|
[53d894] | 458 | neighbors.second[i] = 2*step[i]+1;
|
---|
[d82961] | 459 | break;
|
---|
| 460 | case Box::Bounce:
|
---|
[53d894] | 461 | neighbors.second[i] = 2*step[i]+1;
|
---|
[d82961] | 462 | if (index[i] - step[i] >= 0) {
|
---|
| 463 | neighbors.first[i] = index[i] - step[i];
|
---|
| 464 | } else {
|
---|
[53d894] | 465 | neighbors.first[i] = 0;
|
---|
| 466 | neighbors.second[i] = index[i] + step[i]+1;
|
---|
[d82961] | 467 | }
|
---|
[53d894] | 468 | if (index[i] + step[i] >= getSize(i)) {
|
---|
| 469 | neighbors.second[i] = getSize(i) - (index[i] - step[i]);
|
---|
[d82961] | 470 | }
|
---|
| 471 | break;
|
---|
| 472 | case Box::Ignore:
|
---|
[53d894] | 473 | if (index[i] - step[i] < 0)
|
---|
| 474 | neighbors.first[i] = 0;
|
---|
| 475 | else
|
---|
| 476 | neighbors.first[i] = index[i] - step[i];
|
---|
| 477 | if (index[i] + step[i] >= getSize(i))
|
---|
| 478 | neighbors.second[i] = getSize(i) - index[i];
|
---|
| 479 | else
|
---|
| 480 | neighbors.second[i] = 2*step[i]+1;
|
---|
[d82961] | 481 | break;
|
---|
| 482 | default:
|
---|
| 483 | ASSERT(0, "LinkedCell_Model::getNeighborhoodBounds() - unknown boundary conditions.");
|
---|
| 484 | break;
|
---|
| 485 | }
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | return neighbors;
|
---|
| 489 | }
|
---|
[2614e2a] | 490 |
|
---|
[8c31865] | 491 | /** Returns a reference to the cell indicated by LinkedCell_Model::internal_index.
|
---|
| 492 | *
|
---|
| 493 | * \return LinkedCell ref to current cell
|
---|
| 494 | */
|
---|
| 495 | const LinkedCell::LinkedCell& LinkedCell::LinkedCell_Model::getCell(const tripleIndex &index) const
|
---|
| 496 | {
|
---|
| 497 | return *(N->getN()(index));
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 |
|
---|
| 501 | /** Returns size of array for given \a dim.
|
---|
| 502 | *
|
---|
| 503 | * @param dim desired dimension
|
---|
| 504 | * @return size of array along dimension
|
---|
| 505 | */
|
---|
| 506 | LinkedCell::LinkedCellArray::index LinkedCell::LinkedCell_Model::getSize(const size_t dim) const
|
---|
| 507 | {
|
---|
| 508 | ASSERT((dim >= 0) && (dim < NDIM),
|
---|
| 509 | "LinkedCell_Model::getSize() - dimension "
|
---|
| 510 | +toString(dim)+" is out of bounds.");
|
---|
| 511 | return N->getN().shape()[dim];
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[2614e2a] | 514 | /** Callback function for Observer mechanism.
|
---|
| 515 | *
|
---|
| 516 | * @param publisher reference to the Observable that calls
|
---|
| 517 | */
|
---|
| 518 | void LinkedCell::LinkedCell_Model::update(Observable *publisher)
|
---|
[f55ae5] | 519 | {
|
---|
| 520 | ELOG(2, "LinkedCell_Model received inconclusive general update from "
|
---|
| 521 | << publisher << ".");
|
---|
| 522 | }
|
---|
[2614e2a] | 523 |
|
---|
| 524 | /** Callback function for the Notifications mechanism.
|
---|
| 525 | *
|
---|
| 526 | * @param publisher reference to the Observable that calls
|
---|
| 527 | * @param notification specific notification as cause of the call
|
---|
| 528 | */
|
---|
| 529 | void LinkedCell::LinkedCell_Model::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
[f55ae5] | 530 | {
|
---|
| 531 | // either it's the World or from the atom (through relay) itself
|
---|
| 532 | if (publisher == World::getPointer()) {
|
---|
| 533 | switch(notification->getChannelNo()) {
|
---|
| 534 | case World::AtomInserted:
|
---|
| 535 | addNode(World::getInstance().lastChanged<atom>());
|
---|
| 536 | break;
|
---|
| 537 | case World::AtomRemoved:
|
---|
| 538 | deleteNode(World::getInstance().lastChanged<atom>());
|
---|
| 539 | break;
|
---|
| 540 | }
|
---|
| 541 | } else {
|
---|
| 542 | switch(notification->getChannelNo()) {
|
---|
| 543 | case AtomObservable::PositionChanged:
|
---|
| 544 | {
|
---|
| 545 | moveNode(dynamic_cast<const TesselPoint *>(publisher));
|
---|
| 546 | break;
|
---|
| 547 | }
|
---|
| 548 | default:
|
---|
| 549 | LOG(2, "LinkedCell_Model received unwanted notification from AtomObserver's channel "
|
---|
| 550 | << notification->getChannelNo() << ".");
|
---|
| 551 | break;
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
[2614e2a] | 555 |
|
---|
| 556 | /** Callback function when an Observer dies.
|
---|
| 557 | *
|
---|
| 558 | * @param publisher reference to the Observable that calls
|
---|
| 559 | */
|
---|
| 560 | void LinkedCell::LinkedCell_Model::subjectKilled(Observable *publisher)
|
---|
| 561 | {}
|
---|
| 562 |
|
---|