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