| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2012 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_Controller.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 <set> | 
|---|
| 23 |  | 
|---|
| 24 | #include "Box.hpp" | 
|---|
| 25 | #include "CodePatterns/Assert.hpp" | 
|---|
| 26 | #include "CodePatterns/Log.hpp" | 
|---|
| 27 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| 28 | #include "CodePatterns/Range.hpp" | 
|---|
| 29 | #include "LinkedCell_Controller.hpp" | 
|---|
| 30 | #include "LinkedCell_Model.hpp" | 
|---|
| 31 | #include "LinkedCell_View.hpp" | 
|---|
| 32 | #include "LinkedCell_View_ModelWrapper.hpp" | 
|---|
| 33 | #include "IPointCloud.hpp" | 
|---|
| 34 | #include "WorldTime.hpp" | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | using namespace LinkedCell; | 
|---|
| 38 |  | 
|---|
| 39 | double LinkedCell_Controller::lower_threshold = 1.; | 
|---|
| 40 | double LinkedCell_Controller::upper_threshold = 20.; | 
|---|
| 41 |  | 
|---|
| 42 | /** Constructor of class LinkedCell_Controller. | 
|---|
| 43 | * | 
|---|
| 44 | */ | 
|---|
| 45 | LinkedCell_Controller::LinkedCell_Controller(const Box &_domain) : | 
|---|
| 46 | Observer("LinkedCell_Controller"), | 
|---|
| 47 | domain(_domain) | 
|---|
| 48 | { | 
|---|
| 49 | // sign on to specific notifications | 
|---|
| 50 | domain.signOn(this, Box::MatrixChanged); | 
|---|
| 51 | WorldTime::getInstance().signOn(this, WorldTime::TimeChanged); | 
|---|
| 52 |  | 
|---|
| 53 | /// Check that upper_threshold fits within half the box. | 
|---|
| 54 | Vector diagonal(1.,1.,1.); | 
|---|
| 55 | diagonal.Scale(upper_threshold); | 
|---|
| 56 | Vector diagonal_transformed = domain.getMinv() * diagonal; | 
|---|
| 57 | double max_factor = 1.; | 
|---|
| 58 | for (size_t i=0; i<NDIM; ++i) | 
|---|
| 59 | if (diagonal_transformed.at(i) > 1./max_factor) | 
|---|
| 60 | max_factor = 1./diagonal_transformed.at(i); | 
|---|
| 61 | upper_threshold *= max_factor; | 
|---|
| 62 |  | 
|---|
| 63 | /// Check that lower_threshold is still lower, if not set to half times upper_threshold. | 
|---|
| 64 | if (lower_threshold > upper_threshold) | 
|---|
| 65 | lower_threshold = 0.5*upper_threshold; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | /** Destructor of class LinkedCell_Controller. | 
|---|
| 69 | * | 
|---|
| 70 | * Here, we free all LinkedCell_Model instances again. | 
|---|
| 71 | * | 
|---|
| 72 | */ | 
|---|
| 73 | LinkedCell_Controller::~LinkedCell_Controller() | 
|---|
| 74 | { | 
|---|
| 75 | // sign off | 
|---|
| 76 | domain.signOff(this, Box::MatrixChanged); | 
|---|
| 77 | WorldTime::getInstance().signOff(this, WorldTime::TimeChanged); | 
|---|
| 78 |  | 
|---|
| 79 | /// we free all LinkedCell_Model instances again. | 
|---|
| 80 | for(MapEdgelengthModel::iterator iter = ModelsMap.begin(); | 
|---|
| 81 | !ModelsMap.empty(); iter = ModelsMap.begin()) { | 
|---|
| 82 | delete iter->second; | 
|---|
| 83 | ModelsMap.erase(iter); | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** Internal function to obtain the range within which an model is suitable. | 
|---|
| 88 | * | 
|---|
| 89 | * \note We use statics lower_threshold and upper_threshold as min and max | 
|---|
| 90 | * boundaries. | 
|---|
| 91 | * | 
|---|
| 92 | * @param distance desired egde length | 
|---|
| 93 | * @return range within which model edge length is acceptable | 
|---|
| 94 | */ | 
|---|
| 95 | const range<double> LinkedCell_Controller::getHeuristicRange(const double distance) const | 
|---|
| 96 | { | 
|---|
| 97 | const double lower = 0.5*distance < lower_threshold ? lower_threshold : 0.5*distance; | 
|---|
| 98 | const double upper = 2.*distance > upper_threshold ? upper_threshold : 2.*distance; | 
|---|
| 99 | range<double> HeuristicInterval(lower, upper); | 
|---|
| 100 | return HeuristicInterval; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | /** Internal function to decide whether a suitable model is present or not. | 
|---|
| 104 | * | 
|---|
| 105 | * Here, the heuristic for deciding whether a new linked cell structure has to | 
|---|
| 106 | * be constructed or not is implemented. The current heuristic is as follows: | 
|---|
| 107 | * -# the best model should have at least half the desired length (such | 
|---|
| 108 | *    that most we have to look two neighbor shells wide and not one). | 
|---|
| 109 | * -# the best model should have at most twice the desired length but | 
|---|
| 110 | *    no less than 1 angstroem. | 
|---|
| 111 | * | 
|---|
| 112 | * \note Dealing out a pointer is here (hopefully) safe because the function is | 
|---|
| 113 | * internal and we - inside this class - know what we are doing. | 
|---|
| 114 | * | 
|---|
| 115 | * @param distance edge length of the requested linked cell structure | 
|---|
| 116 | * @return NULL - there is no fitting LinkedCell_Model, else - pointer to instance | 
|---|
| 117 | */ | 
|---|
| 118 | const LinkedCell_Model *LinkedCell_Controller::getBestModel(double distance) const | 
|---|
| 119 | { | 
|---|
| 120 | /// Bound distance to be within [lower_threshold, upper_threshold). | 
|---|
| 121 | /// Note that we need to stay away from upper boundary a bit, | 
|---|
| 122 | /// otherwise the distance will end up outside of the interval. | 
|---|
| 123 | if (distance < lower_threshold) | 
|---|
| 124 | distance = lower_threshold; | 
|---|
| 125 | if (distance > upper_threshold) | 
|---|
| 126 | distance = upper_threshold - std::numeric_limits<double>::round_error(); | 
|---|
| 127 |  | 
|---|
| 128 | /// Look for all models within [0.5 distance, 2. distance). | 
|---|
| 129 | MapEdgelengthModel::const_iterator bestmatch = ModelsMap.end(); | 
|---|
| 130 | if (!ModelsMap.empty()) { | 
|---|
| 131 | for(MapEdgelengthModel::const_iterator iter = ModelsMap.begin(); | 
|---|
| 132 | iter != ModelsMap.end(); ++iter) { | 
|---|
| 133 | // check that we are truely within range | 
|---|
| 134 | range<double> HeuristicInterval(getHeuristicRange(iter->first)); | 
|---|
| 135 | if (HeuristicInterval.isInRange(distance)) { | 
|---|
| 136 | // if it's the first match or a closer one, pick | 
|---|
| 137 | if ((bestmatch == ModelsMap.end()) | 
|---|
| 138 | || (fabs(bestmatch->first - distance) > fabs(iter->first - distance))) | 
|---|
| 139 | bestmatch = iter; | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | /// Return best match or NULL if none found. | 
|---|
| 145 | if (bestmatch != ModelsMap.end()) | 
|---|
| 146 | return bestmatch->second; | 
|---|
| 147 | else | 
|---|
| 148 | return NULL; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | /** Internal function to insert a new model and check for valid insertion. | 
|---|
| 152 | * | 
|---|
| 153 | * @param distance edge length of new model | 
|---|
| 154 | * @param instance pointer to model | 
|---|
| 155 | */ | 
|---|
| 156 | void LinkedCell_Controller::insertNewModel(const double edgelength, const LinkedCell_Model* instance) | 
|---|
| 157 | { | 
|---|
| 158 | std::pair< MapEdgelengthModel::iterator, bool> inserter = | 
|---|
| 159 | ModelsMap.insert( std::make_pair(edgelength, instance) ); | 
|---|
| 160 | ASSERT(inserter.second, | 
|---|
| 161 | "LinkedCell_Controller::getView() - LinkedCell_Model instance with distance " | 
|---|
| 162 | +toString(edgelength)+" already present."); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | /** Returns the a suitable LinkedCell_Model contained in a LinkedCell_View | 
|---|
| 166 | *  for the requested \a distance. | 
|---|
| 167 | * | 
|---|
| 168 | * \sa getBestModel() | 
|---|
| 169 | * | 
|---|
| 170 | * @param distance edge length of the requested linked cell structure | 
|---|
| 171 | * @param set of initial points to insert when new model is created (not always), should be World's | 
|---|
| 172 | * @return LinkedCell_View wrapping the best LinkedCell_Model | 
|---|
| 173 | */ | 
|---|
| 174 | LinkedCell_View LinkedCell_Controller::getView(const double distance, IPointCloud &set) | 
|---|
| 175 | { | 
|---|
| 176 | /// Look for best instance. | 
|---|
| 177 | const LinkedCell_Model * const LCModel_best = getBestModel(distance); | 
|---|
| 178 |  | 
|---|
| 179 | /// Construct new instance if none found, | 
|---|
| 180 | if (LCModel_best == NULL) { | 
|---|
| 181 | LinkedCell_Model * const LCModel_new = new LinkedCell_Model(distance, domain); | 
|---|
| 182 | LCModel_new->insertPointCloud(set); | 
|---|
| 183 | insertNewModel(distance, LCModel_new); | 
|---|
| 184 | LinkedCell_View view(*LCModel_new); | 
|---|
| 185 | return view; | 
|---|
| 186 | } else { | 
|---|
| 187 | /// else construct interface and return. | 
|---|
| 188 | LinkedCell_View view(*LCModel_best); | 
|---|
| 189 | return view; | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | /** Internal function to re-create all present and used models for the new Box. | 
|---|
| 194 | * | 
|---|
| 195 | * This is necessary in the following cases: | 
|---|
| 196 | * -# the Box is changed | 
|---|
| 197 | * -# we step on to a different time step, i.e. all atomic positions change | 
|---|
| 198 | * | 
|---|
| 199 | * The main problem are the views currently in use. | 
|---|
| 200 | * | 
|---|
| 201 | * We make use of LinkedCell:LinkedCell_View::RAIIMap as there all present are | 
|---|
| 202 | * listed. We go through the list, create a map with old model ref as keys to | 
|---|
| 203 | * just newly created ones, and finally go again through each view and exchange | 
|---|
| 204 | * the model against the new ones via a simple map lookup. | 
|---|
| 205 | * | 
|---|
| 206 | */ | 
|---|
| 207 | void LinkedCell_Controller::updateModels() | 
|---|
| 208 | { | 
|---|
| 209 | LOG(1, "INFO: Updating all models."); | 
|---|
| 210 |  | 
|---|
| 211 | typedef std::map<const LinkedCell_Model *, LinkedCell_Model *>  ModelLookup; | 
|---|
| 212 | ModelLookup models; | 
|---|
| 213 |  | 
|---|
| 214 | // set up map, for now with NULL pointers | 
|---|
| 215 | for (LinkedCell_View::ModelInstanceMap::const_iterator iter = LinkedCell_View::RAIIMap.begin(); | 
|---|
| 216 | iter != LinkedCell_View::RAIIMap.end(); ++iter) { | 
|---|
| 217 | #ifndef NDEBUG | 
|---|
| 218 | std::pair< ModelLookup::iterator, bool > inserter = | 
|---|
| 219 | #endif | 
|---|
| 220 | models.insert( std::pair<const LinkedCell_Model *, LinkedCell_Model *>( (*iter)->LC->getModel(), NULL) ); | 
|---|
| 221 | LOG(2, "INFO: Added " << (*iter)->LC->getModel() << " to list of models to replace."); | 
|---|
| 222 | ASSERT( inserter.second, | 
|---|
| 223 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - failed to insert old model " | 
|---|
| 224 | +toString( (*iter)->LC->getModel() )+","+toString(NULL)+" into models, is already present"); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | // invert MapEdgelengthModel | 
|---|
| 228 | LOG(2, "INFO: ModelsMap is " << ModelsMap << "."); | 
|---|
| 229 | typedef std::map<const LinkedCell_Model*, double > MapEdgelengthModel_inverted; | 
|---|
| 230 | MapEdgelengthModel_inverted ModelsMap_inverted; | 
|---|
| 231 | for (MapEdgelengthModel::const_iterator iter = ModelsMap.begin(); | 
|---|
| 232 | iter != ModelsMap.end(); ++iter) { | 
|---|
| 233 | #ifndef NDEBUG | 
|---|
| 234 | MapEdgelengthModel_inverted::const_iterator assertiter = ModelsMap_inverted.find(iter->second); | 
|---|
| 235 | ASSERT( assertiter == ModelsMap_inverted.end(), | 
|---|
| 236 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - ModelsMap is not invertible, value " | 
|---|
| 237 | +toString(iter->second)+" is already present."); | 
|---|
| 238 | #endif | 
|---|
| 239 | ModelsMap_inverted.insert( std::make_pair(iter->second, iter->first) ); | 
|---|
| 240 | } | 
|---|
| 241 | LOG(2, "INFO: Inverted ModelsMap is " << ModelsMap_inverted << "."); | 
|---|
| 242 |  | 
|---|
| 243 | // go through map and re-create models | 
|---|
| 244 | for (ModelLookup::iterator iter = models.begin(); iter != models.end(); ++iter) { | 
|---|
| 245 | // delete old model | 
|---|
| 246 | const LinkedCell_Model * const oldref = iter->first; | 
|---|
| 247 | #ifndef NDEBUG | 
|---|
| 248 | MapEdgelengthModel_inverted::const_iterator assertiter = ModelsMap_inverted.find(oldref); | 
|---|
| 249 | ASSERT( assertiter != ModelsMap_inverted.end(), | 
|---|
| 250 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - ModelsMap_inverted does not contain old model " | 
|---|
| 251 | +toString(oldref)+"."); | 
|---|
| 252 | #endif | 
|---|
| 253 | const double distance = ModelsMap_inverted[oldref]; | 
|---|
| 254 | // create new one, afterwards erase old model (this is for unit test to get different memory addresses) | 
|---|
| 255 | LinkedCell_Model * const newref = new LinkedCell_Model(distance, domain); | 
|---|
| 256 | MapEdgelengthModel::iterator oldmodeliter = ModelsMap.find(distance); | 
|---|
| 257 | delete oldmodeliter->second; | 
|---|
| 258 | ModelsMap.erase(oldmodeliter); | 
|---|
| 259 | LOG(2, "INFO: oldref is " << oldref << ", newref is " << newref << "."); | 
|---|
| 260 | iter->second = newref; | 
|---|
| 261 | // replace in ModelsMap | 
|---|
| 262 | #ifndef NDEBUG | 
|---|
| 263 | std::pair< MapEdgelengthModel::iterator, bool > inserter = | 
|---|
| 264 | #endif | 
|---|
| 265 | ModelsMap.insert( std::make_pair(distance, newref) ); | 
|---|
| 266 | ASSERT( inserter.second, | 
|---|
| 267 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - failed to insert new model " | 
|---|
| 268 | +toString(distance)+","+toString(newref)+" into ModelsMap, is already present"); | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | // remove all remaining active models (also those that don't have an active View on them) | 
|---|
| 272 | for (MapEdgelengthModel::iterator iter = ModelsMap.begin(); | 
|---|
| 273 | !ModelsMap.empty(); | 
|---|
| 274 | iter = ModelsMap.begin()) { | 
|---|
| 275 | delete iter->second; | 
|---|
| 276 | ModelsMap.erase(iter); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 |  | 
|---|
| 280 | // delete inverted map for safety (values are gone) | 
|---|
| 281 | ModelsMap_inverted.clear(); | 
|---|
| 282 |  | 
|---|
| 283 | // go through views and exchange the models | 
|---|
| 284 | for (LinkedCell_View::ModelInstanceMap::const_iterator iter = LinkedCell_View::RAIIMap.begin(); | 
|---|
| 285 | iter != LinkedCell_View::RAIIMap.end(); ++iter) { | 
|---|
| 286 | ModelLookup::const_iterator modeliter = models.find((*iter)->LC->getModel()); | 
|---|
| 287 | ASSERT( modeliter != models.end(), | 
|---|
| 288 | "LinkedCell_Controller::updateModelsForNewBoxMatrix() - we miss a model " | 
|---|
| 289 | +toString((*iter)->LC->getModel())+" in ModelLookup."); | 
|---|
| 290 | // this is ugly but the only place where we have to set ourselves over the constness of the member variable | 
|---|
| 291 | if (modeliter != models.end()) { | 
|---|
| 292 | LOG(2, "INFO: Setting model to " << modeliter->second << " in view " << *iter << "."); | 
|---|
| 293 | (*iter)->LC->setModel(modeliter->second); | 
|---|
| 294 | } | 
|---|
| 295 | } | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | /** Callback function for Observer mechanism. | 
|---|
| 299 | * | 
|---|
| 300 | * @param publisher reference to the Observable that calls | 
|---|
| 301 | */ | 
|---|
| 302 | void LinkedCell_Controller::update(Observable *publisher) | 
|---|
| 303 | { | 
|---|
| 304 | ELOG(2, "LinkedCell_Model received inconclusive general update from " | 
|---|
| 305 | << publisher << "."); | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | /** Callback function for the Notifications mechanism. | 
|---|
| 309 | * | 
|---|
| 310 | * @param publisher reference to the Observable that calls | 
|---|
| 311 | * @param notification specific notification as cause of the call | 
|---|
| 312 | */ | 
|---|
| 313 | void LinkedCell_Controller::recieveNotification(Observable *publisher, Notification_ptr notification) | 
|---|
| 314 | { | 
|---|
| 315 | if (publisher == &domain) { | 
|---|
| 316 | switch(notification->getChannelNo()) { | 
|---|
| 317 | case Box::MatrixChanged: | 
|---|
| 318 | LOG(1, "INFO: LinkedCell_Controller got update from Box."); | 
|---|
| 319 | updateModels(); | 
|---|
| 320 | break; | 
|---|
| 321 | default: | 
|---|
| 322 | ASSERT(0, | 
|---|
| 323 | "LinkedCell_Controller::recieveNotification() - unwanted notification from Box " | 
|---|
| 324 | +toString(notification->getChannelNo())+" received."); | 
|---|
| 325 | break; | 
|---|
| 326 | } | 
|---|
| 327 | } else if (publisher == WorldTime::getPointer()) { | 
|---|
| 328 | switch(notification->getChannelNo()) { | 
|---|
| 329 | case WorldTime::TimeChanged: | 
|---|
| 330 | LOG(1, "INFO: LinkedCell_Controller got update from WorldTime."); | 
|---|
| 331 | updateModels(); | 
|---|
| 332 | break; | 
|---|
| 333 | default: | 
|---|
| 334 | ASSERT(0, | 
|---|
| 335 | "LinkedCell_Controller::recieveNotification() - unwanted notification from WorldTime " | 
|---|
| 336 | +toString(notification->getChannelNo())+" received."); | 
|---|
| 337 | break; | 
|---|
| 338 | } | 
|---|
| 339 | } else { | 
|---|
| 340 | ELOG(1, "Notification " << notification->getChannelNo() | 
|---|
| 341 | << " from unknown publisher " << publisher << "."); | 
|---|
| 342 | } | 
|---|
| 343 | } | 
|---|
| 344 |  | 
|---|
| 345 | /** Callback function when an Observer dies. | 
|---|
| 346 | * | 
|---|
| 347 | * @param publisher reference to the Observable that calls | 
|---|
| 348 | */ | 
|---|
| 349 | void LinkedCell_Controller::subjectKilled(Observable *publisher) | 
|---|
| 350 | {} | 
|---|
| 351 |  | 
|---|