1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * This file is part of MoleCuilder.
|
---|
9 | *
|
---|
10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * LinkedCell_Controller.cpp
|
---|
26 | *
|
---|
27 | * Created on: Nov 15, 2011
|
---|
28 | * Author: heber
|
---|
29 | */
|
---|
30 |
|
---|
31 | // include config.h
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include "CodePatterns/MemDebug.hpp"
|
---|
37 |
|
---|
38 | #include <set>
|
---|
39 |
|
---|
40 | #include "Box.hpp"
|
---|
41 | #include "CodePatterns/Assert.hpp"
|
---|
42 | #include "CodePatterns/Log.hpp"
|
---|
43 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
44 | #include "CodePatterns/Range.hpp"
|
---|
45 | #include "LinkedCell_Controller.hpp"
|
---|
46 | #include "LinkedCell_Model.hpp"
|
---|
47 | #include "LinkedCell_View.hpp"
|
---|
48 | #include "LinkedCell_View_ModelWrapper.hpp"
|
---|
49 | #include "IPointCloud.hpp"
|
---|
50 | #include "WorldTime.hpp"
|
---|
51 |
|
---|
52 |
|
---|
53 | using namespace LinkedCell;
|
---|
54 |
|
---|
55 | double LinkedCell_Controller::lower_threshold = 1.;
|
---|
56 | double LinkedCell_Controller::upper_threshold = 20.;
|
---|
57 |
|
---|
58 | /** Constructor of class LinkedCell_Controller.
|
---|
59 | *
|
---|
60 | */
|
---|
61 | LinkedCell_Controller::LinkedCell_Controller(const Box &_domain) :
|
---|
62 | Observer("LinkedCell_Controller"),
|
---|
63 | domain(_domain)
|
---|
64 | {
|
---|
65 | // sign on to specific notifications
|
---|
66 | domain.signOn(this, Box::MatrixChanged);
|
---|
67 | WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
|
---|
68 |
|
---|
69 | /// Check that upper_threshold fits within half the box.
|
---|
70 | Vector diagonal(1.,1.,1.);
|
---|
71 | diagonal.Scale(upper_threshold);
|
---|
72 | Vector diagonal_transformed = domain.getMinv() * diagonal;
|
---|
73 | double max_factor = 1.;
|
---|
74 | for (size_t i=0; i<NDIM; ++i)
|
---|
75 | if (diagonal_transformed.at(i) > 1./max_factor)
|
---|
76 | max_factor = 1./diagonal_transformed.at(i);
|
---|
77 | upper_threshold *= max_factor;
|
---|
78 |
|
---|
79 | /// Check that lower_threshold is still lower, if not set to half times upper_threshold.
|
---|
80 | if (lower_threshold > upper_threshold)
|
---|
81 | lower_threshold = 0.5*upper_threshold;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Destructor of class LinkedCell_Controller.
|
---|
85 | *
|
---|
86 | * Here, we free all LinkedCell_Model instances again.
|
---|
87 | *
|
---|
88 | */
|
---|
89 | LinkedCell_Controller::~LinkedCell_Controller()
|
---|
90 | {
|
---|
91 | // sign off
|
---|
92 | domain.signOff(this, Box::MatrixChanged);
|
---|
93 | WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
|
---|
94 |
|
---|
95 | /// we free all LinkedCell_Model instances again.
|
---|
96 | for(MapEdgelengthModel::iterator iter = ModelsMap.begin();
|
---|
97 | !ModelsMap.empty(); iter = ModelsMap.begin()) {
|
---|
98 | delete iter->second;
|
---|
99 | ModelsMap.erase(iter);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Internal function to obtain the range within which an model is suitable.
|
---|
104 | *
|
---|
105 | * \note We use statics lower_threshold and upper_threshold as min and max
|
---|
106 | * boundaries.
|
---|
107 | *
|
---|
108 | * @param distance desired egde length
|
---|
109 | * @return range within which model edge length is acceptable
|
---|
110 | */
|
---|
111 | const range<double> LinkedCell_Controller::getHeuristicRange(const double distance) const
|
---|
112 | {
|
---|
113 | const double lower = 0.5*distance < lower_threshold ? lower_threshold : 0.5*distance;
|
---|
114 | const double upper = 2.*distance > upper_threshold ? upper_threshold : 2.*distance;
|
---|
115 | range<double> HeuristicInterval(lower, upper);
|
---|
116 | return HeuristicInterval;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static size_t checkLengthsLess(
|
---|
120 | const Vector &Lengths,
|
---|
121 | const double min_distance)
|
---|
122 | {
|
---|
123 | size_t counter=0;
|
---|
124 | for (size_t i=0;i<NDIM;++i) {
|
---|
125 | if (Lengths[i] < min_distance)
|
---|
126 | ++counter;
|
---|
127 | }
|
---|
128 | return counter;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Given some given \a distance return it such that it is larger equal than some minimum
|
---|
132 | * sensible value with respect to the current domain.
|
---|
133 | *
|
---|
134 | * The sensible minimum distance \f$\Delta\f$ is computed as follows:
|
---|
135 | * \f$ \frac {l_1} \Delta \frac {l_2} \Delta \frac {l_3} \Delta \leq M \f$, where M is the
|
---|
136 | * maximum number of linked cells. Where some additional thought must be given
|
---|
137 | * to rounding issues. Hence, we calculate \f$ (\frac {l_i} d)^{\frac 1 d}+.5\f$,
|
---|
138 | * where d is the assumed dimensionality of the domain: d=3 for cubic, d=2 for
|
---|
139 | * a planar, d=1 for a bar-like system.
|
---|
140 | *
|
---|
141 | * \param distance distance compared with minimum value
|
---|
142 | * \return minimum sensible value or \a distance if larger
|
---|
143 | */
|
---|
144 | double LinkedCell_Controller::getMinimumSensibleLength(double distance) const
|
---|
145 | {
|
---|
146 | // first check the box with respect to the given distance
|
---|
147 | // as we must not generate more than 10^3 cells per axis due to
|
---|
148 | // memory constraints
|
---|
149 | const RealSpaceMatrix &M = domain.getM();
|
---|
150 | RealSpaceMatrix UnitMatrix;
|
---|
151 | UnitMatrix.setIdentity();
|
---|
152 | UnitMatrix *= M;
|
---|
153 | Vector Lengths = UnitMatrix.transformToEigenbasis();
|
---|
154 | double max_length = 0.;
|
---|
155 | double volume = 1.;
|
---|
156 | double min_distance = lower_threshold;
|
---|
157 | for (size_t dim=NDIM; dim>0; --dim) {
|
---|
158 | const double fraction = pow(MAX_LINKEDCELLNODES+1., 1./(double)dim);
|
---|
159 | for (size_t i=0;i<NDIM;++i) {
|
---|
160 | max_length = std::max(Lengths[i], max_length);
|
---|
161 | // the length dropping out for dimensionality less than 3 must appear
|
---|
162 | // just as factor of 1 not less.
|
---|
163 | if (Lengths[i] < min_distance)
|
---|
164 | volume *= pow(min_distance, 1./(double)dim)+.5;
|
---|
165 | else
|
---|
166 | volume *= pow(Lengths[i], 1./(double)dim)+.5; //.5 for rounding issues
|
---|
167 | }
|
---|
168 | min_distance = volume/fraction;
|
---|
169 | // assuming a (cubic-like) 3d system
|
---|
170 | if (checkLengthsLess(Lengths, min_distance) == 0)
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | if (distance < min_distance) {
|
---|
174 | LOG(1, "INFO: Setting LC distance from given " << distance
|
---|
175 | << " to " << min_distance << " due to box size.");
|
---|
176 | distance = min_distance;
|
---|
177 | }
|
---|
178 | return distance;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Internal function to decide whether a suitable model is present or not.
|
---|
182 | *
|
---|
183 | * Here, the heuristic for deciding whether a new linked cell structure has to
|
---|
184 | * be constructed or not is implemented. The current heuristic is as follows:
|
---|
185 | * -# the best model should have at least half the desired length (such
|
---|
186 | * that most we have to look two neighbor shells wide and not one).
|
---|
187 | * -# the best model should have at most twice the desired length but
|
---|
188 | * no less than 1 angstroem.
|
---|
189 | *
|
---|
190 | * \note Dealing out a pointer is here (hopefully) safe because the function is
|
---|
191 | * internal and we - inside this class - know what we are doing.
|
---|
192 | *
|
---|
193 | * @param distance edge length of the requested linked cell structure
|
---|
194 | * @return NULL - there is no fitting LinkedCell_Model, else - pointer to instance
|
---|
195 | */
|
---|
196 | const LinkedCell_Model *LinkedCell_Controller::getBestModel(double distance) const
|
---|
197 | {
|
---|
198 | /// Bound distance to be within [lower_threshold, upper_threshold).
|
---|
199 | /// Note that we need to stay away from upper boundary a bit,
|
---|
200 | /// otherwise the distance will end up outside of the interval.
|
---|
201 | if (distance < lower_threshold)
|
---|
202 | distance = lower_threshold;
|
---|
203 | if (distance > upper_threshold)
|
---|
204 | distance = upper_threshold - std::numeric_limits<double>::round_error();
|
---|
205 |
|
---|
206 | /// Look for all models within [0.5 distance, 2. distance).
|
---|
207 | MapEdgelengthModel::const_iterator bestmatch = ModelsMap.end();
|
---|
208 | if (!ModelsMap.empty()) {
|
---|
209 | for(MapEdgelengthModel::const_iterator iter = ModelsMap.begin();
|
---|
210 | iter != ModelsMap.end(); ++iter) {
|
---|
211 | // check that we are truely within range
|
---|
212 | range<double> HeuristicInterval(getHeuristicRange(iter->first));
|
---|
213 | if (HeuristicInterval.isInRange(distance)) {
|
---|
214 | // if it's the first match or a closer one, pick
|
---|
215 | if ((bestmatch == ModelsMap.end())
|
---|
216 | || (fabs(bestmatch->first - distance) > fabs(iter->first - distance)))
|
---|
217 | bestmatch = iter;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | /// Return best match or NULL if none found.
|
---|
223 | if (bestmatch != ModelsMap.end())
|
---|
224 | return bestmatch->second;
|
---|
225 | else
|
---|
226 | return NULL;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Internal function to insert a new model and check for valid insertion.
|
---|
230 | *
|
---|
231 | * @param distance edge length of new model
|
---|
232 | * @param instance pointer to model
|
---|
233 | */
|
---|
234 | void LinkedCell_Controller::insertNewModel(const double edgelength, const LinkedCell_Model* instance)
|
---|
235 | {
|
---|
236 | std::pair< MapEdgelengthModel::iterator, bool> inserter =
|
---|
237 | ModelsMap.insert( std::make_pair(edgelength, instance) );
|
---|
238 | ASSERT(inserter.second,
|
---|
239 | "LinkedCell_Controller::getView() - LinkedCell_Model instance with distance "
|
---|
240 | +toString(edgelength)+" already present.");
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** Returns the a suitable LinkedCell_Model contained in a LinkedCell_View
|
---|
244 | * for the requested \a distance.
|
---|
245 | *
|
---|
246 | * \sa getBestModel()
|
---|
247 | *
|
---|
248 | * @param distance edge length of the requested linked cell structure
|
---|
249 | * @param set of initial points to insert when new model is created (not always), should be World's
|
---|
250 | * @return LinkedCell_View wrapping the best LinkedCell_Model
|
---|
251 | */
|
---|
252 | LinkedCell_View LinkedCell_Controller::getView(const double distance, IPointCloud &set)
|
---|
253 | {
|
---|
254 | // distance should be a given minimum length dependent on domain at least
|
---|
255 | const double ConstraintDistance = getMinimumSensibleLength(distance);
|
---|
256 |
|
---|
257 | /// Look for best instance.
|
---|
258 | const LinkedCell_Model * const LCModel_best = getBestModel(ConstraintDistance);
|
---|
259 |
|
---|
260 | /// Construct new instance if none found,
|
---|
261 | if (LCModel_best == NULL) {
|
---|
262 | LinkedCell_Model * const LCModel_new = new LinkedCell_Model(ConstraintDistance, domain);
|
---|
263 | LCModel_new->insertPointCloud(set);
|
---|
264 | insertNewModel(ConstraintDistance, LCModel_new);
|
---|
265 | LinkedCell_View view(*LCModel_new);
|
---|
266 | return view;
|
---|
267 | } else {
|
---|
268 | /// else construct interface and return.
|
---|
269 | LinkedCell_View view(*LCModel_best);
|
---|
270 | return view;
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** Internal function to re-create all present and used models for the new Box.
|
---|
275 | *
|
---|
276 | * This is necessary in the following cases:
|
---|
277 | * -# the Box is changed
|
---|
278 | * -# we step on to a different time step, i.e. all atomic positions change
|
---|
279 | *
|
---|
280 | * The main problem are the views currently in use.
|
---|
281 | *
|
---|
282 | * We make use of LinkedCell:LinkedCell_View::RAIIMap as there all present are
|
---|
283 | * listed. We go through the list, create a map with old model ref as keys to
|
---|
284 | * just newly created ones, and finally go again through each view and exchange
|
---|
285 | * the model against the new ones via a simple map lookup.
|
---|
286 | *
|
---|
287 | */
|
---|
288 | void LinkedCell_Controller::updateModels()
|
---|
289 | {
|
---|
290 | LOG(1, "INFO: Updating all models.");
|
---|
291 |
|
---|
292 | typedef std::map<const LinkedCell_Model *, LinkedCell_Model *> ModelLookup;
|
---|
293 | ModelLookup models;
|
---|
294 |
|
---|
295 | // set up map, for now with NULL pointers
|
---|
296 | for (LinkedCell_View::ModelInstanceMap::const_iterator iter = LinkedCell_View::RAIIMap.begin();
|
---|
297 | iter != LinkedCell_View::RAIIMap.end(); ++iter) {
|
---|
298 | #ifndef NDEBUG
|
---|
299 | std::pair< ModelLookup::iterator, bool > inserter =
|
---|
300 | #endif
|
---|
301 | models.insert( std::pair<const LinkedCell_Model *, LinkedCell_Model *>( (*iter)->LC->getModel(), NULL) );
|
---|
302 | LOG(2, "INFO: Added " << (*iter)->LC->getModel() << " to list of models to replace.");
|
---|
303 | ASSERT( inserter.second,
|
---|
304 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - failed to insert old model "
|
---|
305 | +toString( (*iter)->LC->getModel() )+",NULL into models, is already present");
|
---|
306 | }
|
---|
307 |
|
---|
308 | // invert MapEdgelengthModel
|
---|
309 | LOG(2, "INFO: ModelsMap is " << ModelsMap << ".");
|
---|
310 | typedef std::map<const LinkedCell_Model*, double > MapEdgelengthModel_inverted;
|
---|
311 | MapEdgelengthModel_inverted ModelsMap_inverted;
|
---|
312 | for (MapEdgelengthModel::const_iterator iter = ModelsMap.begin();
|
---|
313 | iter != ModelsMap.end(); ++iter) {
|
---|
314 | #ifndef NDEBUG
|
---|
315 | MapEdgelengthModel_inverted::const_iterator assertiter = ModelsMap_inverted.find(iter->second);
|
---|
316 | ASSERT( assertiter == ModelsMap_inverted.end(),
|
---|
317 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - ModelsMap is not invertible, value "
|
---|
318 | +toString(iter->second)+" is already present.");
|
---|
319 | #endif
|
---|
320 | ModelsMap_inverted.insert( std::make_pair(iter->second, iter->first) );
|
---|
321 | }
|
---|
322 | LOG(2, "INFO: Inverted ModelsMap is " << ModelsMap_inverted << ".");
|
---|
323 |
|
---|
324 | // go through map and re-create models
|
---|
325 | for (ModelLookup::iterator iter = models.begin(); iter != models.end(); ++iter) {
|
---|
326 | // delete old model
|
---|
327 | const LinkedCell_Model * const oldref = iter->first;
|
---|
328 | #ifndef NDEBUG
|
---|
329 | MapEdgelengthModel_inverted::const_iterator assertiter = ModelsMap_inverted.find(oldref);
|
---|
330 | ASSERT( assertiter != ModelsMap_inverted.end(),
|
---|
331 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - ModelsMap_inverted does not contain old model "
|
---|
332 | +toString(oldref)+".");
|
---|
333 | #endif
|
---|
334 | const double distance = ModelsMap_inverted[oldref];
|
---|
335 | // create new one, afterwards erase old model (this is for unit test to get different memory addresses)
|
---|
336 | LinkedCell_Model * const newref = new LinkedCell_Model(distance, domain);
|
---|
337 | MapEdgelengthModel::iterator oldmodeliter = ModelsMap.find(distance);
|
---|
338 | delete oldmodeliter->second;
|
---|
339 | ModelsMap.erase(oldmodeliter);
|
---|
340 | LOG(2, "INFO: oldref is " << oldref << ", newref is " << newref << ".");
|
---|
341 | iter->second = newref;
|
---|
342 | // replace in ModelsMap
|
---|
343 | #ifndef NDEBUG
|
---|
344 | std::pair< MapEdgelengthModel::iterator, bool > inserter =
|
---|
345 | #endif
|
---|
346 | ModelsMap.insert( std::make_pair(distance, newref) );
|
---|
347 | ASSERT( inserter.second,
|
---|
348 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - failed to insert new model "
|
---|
349 | +toString(distance)+","+toString(newref)+" into ModelsMap, is already present");
|
---|
350 | }
|
---|
351 |
|
---|
352 | // remove all remaining active models (also those that don't have an active View on them)
|
---|
353 | for (MapEdgelengthModel::iterator iter = ModelsMap.begin();
|
---|
354 | !ModelsMap.empty();
|
---|
355 | iter = ModelsMap.begin()) {
|
---|
356 | delete iter->second;
|
---|
357 | ModelsMap.erase(iter);
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | // delete inverted map for safety (values are gone)
|
---|
362 | ModelsMap_inverted.clear();
|
---|
363 |
|
---|
364 | // go through views and exchange the models
|
---|
365 | for (LinkedCell_View::ModelInstanceMap::const_iterator iter = LinkedCell_View::RAIIMap.begin();
|
---|
366 | iter != LinkedCell_View::RAIIMap.end(); ++iter) {
|
---|
367 | ModelLookup::const_iterator modeliter = models.find((*iter)->LC->getModel());
|
---|
368 | ASSERT( modeliter != models.end(),
|
---|
369 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - we miss a model "
|
---|
370 | +toString((*iter)->LC->getModel())+" in ModelLookup.");
|
---|
371 | // this is ugly but the only place where we have to set ourselves over the constness of the member variable
|
---|
372 | if (modeliter != models.end()) {
|
---|
373 | LOG(2, "INFO: Setting model to " << modeliter->second << " in view " << *iter << ".");
|
---|
374 | (*iter)->LC->setModel(modeliter->second);
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | /** Callback function for Observer mechanism.
|
---|
380 | *
|
---|
381 | * @param publisher reference to the Observable that calls
|
---|
382 | */
|
---|
383 | void LinkedCell_Controller::update(Observable *publisher)
|
---|
384 | {
|
---|
385 | ELOG(2, "LinkedCell_Model received inconclusive general update from "
|
---|
386 | << publisher << ".");
|
---|
387 | }
|
---|
388 |
|
---|
389 | /** Callback function for the Notifications mechanism.
|
---|
390 | *
|
---|
391 | * @param publisher reference to the Observable that calls
|
---|
392 | * @param notification specific notification as cause of the call
|
---|
393 | */
|
---|
394 | void LinkedCell_Controller::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
395 | {
|
---|
396 | if (publisher == &domain) {
|
---|
397 | switch(notification->getChannelNo()) {
|
---|
398 | case Box::MatrixChanged:
|
---|
399 | LOG(1, "INFO: LinkedCell_Controller got update from Box.");
|
---|
400 | updateModels();
|
---|
401 | break;
|
---|
402 | default:
|
---|
403 | ASSERT(0,
|
---|
404 | "LinkedCell_Controller::recieveNotification() - unwanted notification from Box "
|
---|
405 | +toString(notification->getChannelNo())+" received.");
|
---|
406 | break;
|
---|
407 | }
|
---|
408 | } else if (publisher == WorldTime::getPointer()) {
|
---|
409 | switch(notification->getChannelNo()) {
|
---|
410 | case WorldTime::TimeChanged:
|
---|
411 | LOG(1, "INFO: LinkedCell_Controller got update from WorldTime.");
|
---|
412 | updateModels();
|
---|
413 | break;
|
---|
414 | default:
|
---|
415 | ASSERT(0,
|
---|
416 | "LinkedCell_Controller::recieveNotification() - unwanted notification from WorldTime "
|
---|
417 | +toString(notification->getChannelNo())+" received.");
|
---|
418 | break;
|
---|
419 | }
|
---|
420 | } else {
|
---|
421 | ELOG(1, "Notification " << notification->getChannelNo()
|
---|
422 | << " from unknown publisher " << publisher << ".");
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | /** Callback function when an Observer dies.
|
---|
427 | *
|
---|
428 | * @param publisher reference to the Observable that calls
|
---|
429 | */
|
---|
430 | void LinkedCell_Controller::subjectKilled(Observable *publisher)
|
---|
431 | {}
|
---|
432 |
|
---|