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 |
|
---|
28 | #include "Atom/AtomObserver.hpp"
|
---|
29 | #include "Atom/TesselPoint.hpp"
|
---|
30 | #include "Box.hpp"
|
---|
31 | #include "CodePatterns/Assert.hpp"
|
---|
32 | #include "CodePatterns/Info.hpp"
|
---|
33 | #include "CodePatterns/Log.hpp"
|
---|
34 | #include "CodePatterns/Observer/Observer.hpp"
|
---|
35 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
36 | #include "CodePatterns/toString.hpp"
|
---|
37 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
38 | #include "LinearAlgebra/Vector.hpp"
|
---|
39 | #include "LinkedCell/IPointCloud.hpp"
|
---|
40 | #include "LinkedCell/LinkedCell.hpp"
|
---|
41 | #include "World.hpp"
|
---|
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) :
|
---|
54 | ::Observer(std::string("LinkedCell_Model")+std::string("_")+toString(radius)),
|
---|
55 | internal_Sizes(NULL),
|
---|
56 | domain(_domain)
|
---|
57 | {
|
---|
58 | // set default argument
|
---|
59 | NearestNeighbors[0] = NearestNeighbors[1] = NearestNeighbors[2] = 1;
|
---|
60 |
|
---|
61 | // get the partition of the domain
|
---|
62 | setPartition(radius);
|
---|
63 |
|
---|
64 | // allocate linked cell structure
|
---|
65 | AllocateCells();
|
---|
66 |
|
---|
67 | // sign in to AtomObserver
|
---|
68 | startListening();
|
---|
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) :
|
---|
78 | ::Observer(std::string("LinkedCell_Model")+std::string("_")+toString(radius)),
|
---|
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);
|
---|
91 |
|
---|
92 | // sign in to AtomObserver
|
---|
93 | startListening();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Destructor of class LinkedCell_Model.
|
---|
97 | *
|
---|
98 | */
|
---|
99 | LinkedCell::LinkedCell_Model::~LinkedCell_Model()
|
---|
100 | {
|
---|
101 | // sign off from observables
|
---|
102 | stopListening();
|
---|
103 |
|
---|
104 | // reset linked cell structure
|
---|
105 | Reset();
|
---|
106 | }
|
---|
107 |
|
---|
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 |
|
---|
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,
|
---|
141 | "LinkedCell_Model::AllocateCells() - Number linked of linked cell nodes exceded hard-coded limit, use greater edge length!");
|
---|
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 |
|
---|
268 | /** Adds a node to the linked cell structure
|
---|
269 | *
|
---|
270 | * @param Walker node to add
|
---|
271 | */
|
---|
272 | void LinkedCell::LinkedCell_Model::addNode(const TesselPoint *Walker)
|
---|
273 | {
|
---|
274 | tripleIndex index = getIndexToVector(Walker->getPosition());
|
---|
275 | LOG(2, "INFO: " << *Walker << " goes into cell " << index[0] << ", " << index[1] << ", " << index[2] << ".");
|
---|
276 | LOG(2, "INFO: Cell's indices are "
|
---|
277 | << N(index)->getIndex(0) << " "
|
---|
278 | << N(index)->getIndex(1) << " "
|
---|
279 | << N(index)->getIndex(2) << ".");
|
---|
280 | N(index)->addPoint(Walker);
|
---|
281 | std::pair<MapPointToCell::iterator, bool> inserter = CellLookup.insert( std::make_pair(Walker, N(index)) );
|
---|
282 | ASSERT( inserter.second,
|
---|
283 | "LinkedCell_Model::addNode() - Walker "
|
---|
284 | +toString(*Walker)+" is already present in cell "
|
---|
285 | +toString((inserter.first)->second->getIndex(0))+" "
|
---|
286 | +toString((inserter.first)->second->getIndex(1))+" "
|
---|
287 | +toString((inserter.first)->second->getIndex(2))+".");
|
---|
288 | }
|
---|
289 |
|
---|
290 | /** Removes a node to the linked cell structure
|
---|
291 | *
|
---|
292 | * We do nothing of Walker is not found
|
---|
293 | *
|
---|
294 | * @param Walker node to remove
|
---|
295 | */
|
---|
296 | void LinkedCell::LinkedCell_Model::deleteNode(const TesselPoint *Walker)
|
---|
297 | {
|
---|
298 | MapPointToCell::iterator iter = CellLookup.begin();
|
---|
299 | for (; iter != CellLookup.end(); ++iter)
|
---|
300 | if (iter->first == Walker)
|
---|
301 | break;
|
---|
302 | ASSERT(iter != CellLookup.end(),
|
---|
303 | "LinkedCell_Model::deleteNode() - Walker not present in cell stored under CellLookup.");
|
---|
304 | if (iter != CellLookup.end()) {
|
---|
305 | CellLookup.erase(iter);
|
---|
306 | iter->second->deletePoint(Walker);
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** Move Walker from current cell to another on position change.
|
---|
311 | *
|
---|
312 | * @param Walker node who has moved.
|
---|
313 | */
|
---|
314 | void LinkedCell::LinkedCell_Model::moveNode(const TesselPoint *Walker)
|
---|
315 | {
|
---|
316 | ASSERT(0, "LinkedCell_Model::moveNode() - not implemented yet.");
|
---|
317 | }
|
---|
318 |
|
---|
319 | /** Checks whether cell indicated by \a relative relative to LinkedCell_Model::internal_index
|
---|
320 | * is out of bounds.
|
---|
321 | *
|
---|
322 | * \note We do not check for boundary conditions of LinkedCell_Model::domain,
|
---|
323 | * we only look at the array sizes
|
---|
324 | *
|
---|
325 | * @param relative index relative to LinkedCell_Model::internal_index.
|
---|
326 | * @return true - relative index is still inside bounds, false - outside
|
---|
327 | */
|
---|
328 | bool LinkedCell::LinkedCell_Model::checkArrayBounds(const tripleIndex &index) const
|
---|
329 | {
|
---|
330 | bool status = true;
|
---|
331 | for (size_t i=0;i<NDIM;++i) {
|
---|
332 | status = status && (
|
---|
333 | (index[i] >= 0) &&
|
---|
334 | (index[i] < getSize(i))
|
---|
335 | );
|
---|
336 | }
|
---|
337 | return status;
|
---|
338 | }
|
---|
339 |
|
---|
340 | /** Corrects \a index according to boundary conditions of LinkedCell_Model::domain .
|
---|
341 | *
|
---|
342 | * @param index index to correct according to boundary conditions
|
---|
343 | */
|
---|
344 | void LinkedCell::LinkedCell_Model::applyBoundaryConditions(tripleIndex &index) const
|
---|
345 | {
|
---|
346 | for (size_t i=0;i<NDIM;++i) {
|
---|
347 | switch (domain.getConditions()[i]) {
|
---|
348 | case Box::Wrap:
|
---|
349 | if ((index[i] < 0) || (index[i] >= getSize(i)))
|
---|
350 | index[i] = (index[i] % getSize(i));
|
---|
351 | break;
|
---|
352 | case Box::Bounce:
|
---|
353 | break;
|
---|
354 | case Box::Ignore:
|
---|
355 | break;
|
---|
356 | default:
|
---|
357 | ASSERT(0, "LinkedCell_Model::checkBounds() - unknown boundary conditions.");
|
---|
358 | break;
|
---|
359 | }
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** Calculates the interval bounds of the linked cell grid.
|
---|
364 | * \param index index to give relative bounds to
|
---|
365 | * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
|
---|
366 | * \return pair of tripleIndex indicating lower and upper bounds
|
---|
367 | */
|
---|
368 | const LinkedCell::LinkedCell_Model::LinkedCellNeighborhoodBounds LinkedCell::LinkedCell_Model::getNeighborhoodBounds(
|
---|
369 | const tripleIndex &index,
|
---|
370 | const tripleIndex &step
|
---|
371 | ) const
|
---|
372 | {
|
---|
373 | LinkedCellNeighborhoodBounds neighbors;
|
---|
374 |
|
---|
375 | // calculate bounds
|
---|
376 | for (size_t i=0;i<NDIM;++i) {
|
---|
377 | switch (domain.getConditions()[i]) {
|
---|
378 | case Box::Wrap:
|
---|
379 | if (index[i] - step[i] >= 0) {
|
---|
380 | neighbors.first[i] = index[i] - step[i];
|
---|
381 | } else {
|
---|
382 | neighbors.first[i] = getSize(i) - (index[i] - step[i]);
|
---|
383 | }
|
---|
384 | if (index[i] + step[i] < getSize(i)) {
|
---|
385 | neighbors.second[i] = index[i] + step[i];
|
---|
386 | } else {
|
---|
387 | neighbors.second[i] = index[i] + step[i] - getSize(i);
|
---|
388 | }
|
---|
389 | break;
|
---|
390 | case Box::Bounce:
|
---|
391 | if (index[i] - step[i] >= 0) {
|
---|
392 | neighbors.first[i] = index[i] - step[i];
|
---|
393 | } else {
|
---|
394 | neighbors.first[i] = - (index[i] - step[i]);
|
---|
395 | }
|
---|
396 | if (index[i] + step[i] < getSize(i)) {
|
---|
397 | neighbors.second[i] = index[i] + step[i];
|
---|
398 | } else {
|
---|
399 | const size_t shift = index[i] + step[i];
|
---|
400 | const size_t mod = shift / getSize(i);
|
---|
401 | if ((mod / 2)*2 == mod) // even -> come in from lower bound
|
---|
402 | neighbors.second[i] = (shift % getSize(i));
|
---|
403 | else // odd -> come in from upper bound
|
---|
404 | neighbors.second[i] = getSize(i) - (shift % getSize(i));
|
---|
405 | }
|
---|
406 | break;
|
---|
407 | case Box::Ignore:
|
---|
408 | ASSERT(index[i] - step[i] >= 0,
|
---|
409 | "LinkedCell_Model::getNeighborhoodBounds() - lower out of bounds on 'Ignore' boundary conditions");
|
---|
410 | neighbors.first[i] = index[i] - step[i];
|
---|
411 | ASSERT (index[i] + step[i] < getSize(i),
|
---|
412 | "LinkedCell_Model::getNeighborhoodBounds() - upper out of bounds on 'Ignore' boundary conditions");
|
---|
413 | neighbors.second[i] = index[i] + step[i];
|
---|
414 | ASSERT(neighbors.second[i] < neighbors.first[i],
|
---|
415 | "LinkedCell_Model::getNeighborhoodBounds() - ["
|
---|
416 | +toString(neighbors.first[i])+","+toString(neighbors.second[i])
|
---|
417 | +"] does not make sense as array bounds.");
|
---|
418 | break;
|
---|
419 | default:
|
---|
420 | ASSERT(0, "LinkedCell_Model::getNeighborhoodBounds() - unknown boundary conditions.");
|
---|
421 | break;
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | return neighbors;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /** Callback function for Observer mechanism.
|
---|
429 | *
|
---|
430 | * @param publisher reference to the Observable that calls
|
---|
431 | */
|
---|
432 | void LinkedCell::LinkedCell_Model::update(Observable *publisher)
|
---|
433 | {
|
---|
434 | ELOG(2, "LinkedCell_Model received inconclusive general update from "
|
---|
435 | << publisher << ".");
|
---|
436 | }
|
---|
437 |
|
---|
438 | /** Callback function for the Notifications mechanism.
|
---|
439 | *
|
---|
440 | * @param publisher reference to the Observable that calls
|
---|
441 | * @param notification specific notification as cause of the call
|
---|
442 | */
|
---|
443 | void LinkedCell::LinkedCell_Model::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
444 | {
|
---|
445 | // either it's the World or from the atom (through relay) itself
|
---|
446 | if (publisher == World::getPointer()) {
|
---|
447 | switch(notification->getChannelNo()) {
|
---|
448 | case World::AtomInserted:
|
---|
449 | addNode(World::getInstance().lastChanged<atom>());
|
---|
450 | break;
|
---|
451 | case World::AtomRemoved:
|
---|
452 | deleteNode(World::getInstance().lastChanged<atom>());
|
---|
453 | break;
|
---|
454 | }
|
---|
455 | } else {
|
---|
456 | switch(notification->getChannelNo()) {
|
---|
457 | case AtomObservable::PositionChanged:
|
---|
458 | {
|
---|
459 | moveNode(dynamic_cast<const TesselPoint *>(publisher));
|
---|
460 | break;
|
---|
461 | }
|
---|
462 | default:
|
---|
463 | LOG(2, "LinkedCell_Model received unwanted notification from AtomObserver's channel "
|
---|
464 | << notification->getChannelNo() << ".");
|
---|
465 | break;
|
---|
466 | }
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | /** Callback function when an Observer dies.
|
---|
471 | *
|
---|
472 | * @param publisher reference to the Observable that calls
|
---|
473 | */
|
---|
474 | void LinkedCell::LinkedCell_Model::subjectKilled(Observable *publisher)
|
---|
475 | {}
|
---|
476 |
|
---|