| [fea945] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [94d5ac6] | 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/>.
 | 
|---|
| [fea945] | 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 | 
 | 
|---|
| [9eb71b3] | 36 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [fea945] | 37 | 
 | 
|---|
| [4a8169] | 38 | #include <set>
 | 
|---|
 | 39 | 
 | 
|---|
| [fea945] | 40 | #include "Box.hpp"
 | 
|---|
| [fe8253] | 41 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [4a8169] | 42 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 43 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| [fe8253] | 44 | #include "CodePatterns/Range.hpp"
 | 
|---|
| [fea945] | 45 | #include "LinkedCell_Controller.hpp"
 | 
|---|
 | 46 | #include "LinkedCell_Model.hpp"
 | 
|---|
 | 47 | #include "LinkedCell_View.hpp"
 | 
|---|
| [cbdcb1] | 48 | #include "LinkedCell_View_ModelWrapper.hpp"
 | 
|---|
| [c80643] | 49 | #include "IPointCloud.hpp"
 | 
|---|
| [ce81e76] | 50 | #include "WorldTime.hpp"
 | 
|---|
| [fea945] | 51 | 
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | using namespace LinkedCell;
 | 
|---|
 | 54 | 
 | 
|---|
| [fe8253] | 55 | double LinkedCell_Controller::lower_threshold = 1.;
 | 
|---|
 | 56 | double LinkedCell_Controller::upper_threshold = 20.;
 | 
|---|
 | 57 | 
 | 
|---|
| [fea945] | 58 | /** Constructor of class LinkedCell_Controller.
 | 
|---|
 | 59 |  *
 | 
|---|
 | 60 |  */
 | 
|---|
 | 61 | LinkedCell_Controller::LinkedCell_Controller(const Box &_domain) :
 | 
|---|
| [4a8169] | 62 |     Observer("LinkedCell_Controller"),
 | 
|---|
| [fea945] | 63 |     domain(_domain)
 | 
|---|
| [fe8253] | 64 | {
 | 
|---|
| [5e2864] | 65 |   // sign on to specific notifications
 | 
|---|
 | 66 |   domain.signOn(this, Box::MatrixChanged);
 | 
|---|
| [ce81e76] | 67 |   WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
 | 
|---|
| [5e2864] | 68 | 
 | 
|---|
| [fe8253] | 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 | }
 | 
|---|
| [fea945] | 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 | {
 | 
|---|
| [5e2864] | 91 |   // sign off
 | 
|---|
 | 92 |   domain.signOff(this, Box::MatrixChanged);
 | 
|---|
| [ce81e76] | 93 |   WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
 | 
|---|
| [5e2864] | 94 | 
 | 
|---|
| [fe8253] | 95 |   /// we free all LinkedCell_Model instances again.
 | 
|---|
| [fea945] | 96 |   for(MapEdgelengthModel::iterator iter = ModelsMap.begin();
 | 
|---|
 | 97 |       !ModelsMap.empty(); iter = ModelsMap.begin()) {
 | 
|---|
 | 98 |     delete iter->second;
 | 
|---|
 | 99 |     ModelsMap.erase(iter);
 | 
|---|
 | 100 |   }
 | 
|---|
 | 101 | }
 | 
|---|
 | 102 | 
 | 
|---|
| [fe8253] | 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 | 
 | 
|---|
| [1d2d01e] | 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.
 | 
|---|
| [fea945] | 133 |  *
 | 
|---|
| [1d2d01e] | 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.
 | 
|---|
| [fea945] | 140 |  *
 | 
|---|
| [1d2d01e] | 141 |  * \param distance distance compared with minimum value
 | 
|---|
 | 142 |  * \return minimum sensible value or \a distance if larger
 | 
|---|
| [fea945] | 143 |  */
 | 
|---|
| [1d2d01e] | 144 | double LinkedCell_Controller::getMinimumSensibleLength(double distance) const
 | 
|---|
| [fea945] | 145 | {
 | 
|---|
| [15463a] | 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.;
 | 
|---|
| [1d2d01e] | 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;
 | 
|---|
| [15463a] | 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 |   }
 | 
|---|
| [1d2d01e] | 178 |   return distance;
 | 
|---|
 | 179 | }
 | 
|---|
| [15463a] | 180 | 
 | 
|---|
| [1d2d01e] | 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 | {
 | 
|---|
| [fe8253] | 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;
 | 
|---|
| [1d2d01e] | 203 |   if (distance > upper_threshold)
 | 
|---|
 | 204 |     distance = upper_threshold - std::numeric_limits<double>::round_error();
 | 
|---|
| [fe8253] | 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 |       }
 | 
|---|
| [fea945] | 219 |     }
 | 
|---|
 | 220 |   }
 | 
|---|
| [fe8253] | 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 |  */
 | 
|---|
| [455043] | 234 | void LinkedCell_Controller::insertNewModel(const double edgelength, const LinkedCell_Model* instance)
 | 
|---|
| [fe8253] | 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.");
 | 
|---|
| [fea945] | 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
 | 
|---|
| [c80643] | 249 |  * @param set of initial points to insert when new model is created (not always), should be World's
 | 
|---|
| [fea945] | 250 |  * @return LinkedCell_View wrapping the best LinkedCell_Model
 | 
|---|
 | 251 |  */
 | 
|---|
| [c80643] | 252 | LinkedCell_View LinkedCell_Controller::getView(const double distance, IPointCloud &set)
 | 
|---|
| [fea945] | 253 | {
 | 
|---|
| [1d2d01e] | 254 |   // distance should be a given minimum length dependent on domain at least
 | 
|---|
 | 255 |   const double ConstraintDistance = getMinimumSensibleLength(distance);
 | 
|---|
 | 256 | 
 | 
|---|
| [fe8253] | 257 |   /// Look for best instance.
 | 
|---|
| [1d2d01e] | 258 |   const LinkedCell_Model * const LCModel_best = getBestModel(ConstraintDistance);
 | 
|---|
| [fea945] | 259 | 
 | 
|---|
| [fe8253] | 260 |   /// Construct new instance if none found,
 | 
|---|
| [fea945] | 261 |   if (LCModel_best == NULL) {
 | 
|---|
| [1d2d01e] | 262 |     LinkedCell_Model * const LCModel_new = new LinkedCell_Model(ConstraintDistance, domain);
 | 
|---|
| [c80643] | 263 |     LCModel_new->insertPointCloud(set);
 | 
|---|
| [1d2d01e] | 264 |     insertNewModel(ConstraintDistance, LCModel_new);
 | 
|---|
| [455043] | 265 |     LinkedCell_View view(*LCModel_new);
 | 
|---|
| [fe8253] | 266 |     return view;
 | 
|---|
| [fea945] | 267 |   } else {
 | 
|---|
| [fe8253] | 268 |     /// else construct interface and return.
 | 
|---|
 | 269 |     LinkedCell_View view(*LCModel_best);
 | 
|---|
 | 270 |     return view;
 | 
|---|
| [fea945] | 271 |   }
 | 
|---|
 | 272 | }
 | 
|---|
| [4a8169] | 273 | 
 | 
|---|
 | 274 | /** Internal function to re-create all present and used models for the new Box.
 | 
|---|
| [ce81e76] | 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
 | 
|---|
| [4a8169] | 279 |  *
 | 
|---|
 | 280 |  * The main problem are the views currently in use.
 | 
|---|
| [cbdcb1] | 281 |  *
 | 
|---|
| [4a8169] | 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 |  */
 | 
|---|
| [ce81e76] | 288 | void LinkedCell_Controller::updateModels()
 | 
|---|
| [4a8169] | 289 | {
 | 
|---|
| [cbdcb1] | 290 |   LOG(1, "INFO: Updating all models.");
 | 
|---|
 | 291 | 
 | 
|---|
| [4a8169] | 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
 | 
|---|
| [cbdcb1] | 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.");
 | 
|---|
| [4a8169] | 303 |     ASSERT( inserter.second,
 | 
|---|
 | 304 |         "LinkedCell_Controller::updateModelsForNewBoxMatrix() - failed to insert old model "
 | 
|---|
| [cf1183] | 305 |         +toString( (*iter)->LC->getModel() )+",NULL into models, is already present");
 | 
|---|
| [4a8169] | 306 |   }
 | 
|---|
 | 307 | 
 | 
|---|
 | 308 |   // invert MapEdgelengthModel
 | 
|---|
| [cbdcb1] | 309 |   LOG(2, "INFO: ModelsMap is " << ModelsMap << ".");
 | 
|---|
| [4a8169] | 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);
 | 
|---|
| [cbdcb1] | 316 |     ASSERT( assertiter == ModelsMap_inverted.end(),
 | 
|---|
| [4a8169] | 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 |   }
 | 
|---|
| [cbdcb1] | 322 |   LOG(2, "INFO: Inverted ModelsMap is " << ModelsMap_inverted << ".");
 | 
|---|
| [4a8169] | 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];
 | 
|---|
| [429069] | 335 |     // create new one, afterwards erase old model (this is for unit test to get different memory addresses)
 | 
|---|
| [4a8169] | 336 |     LinkedCell_Model * const newref = new LinkedCell_Model(distance, domain);
 | 
|---|
| [429069] | 337 |     MapEdgelengthModel::iterator oldmodeliter = ModelsMap.find(distance);
 | 
|---|
 | 338 |     delete oldmodeliter->second;
 | 
|---|
 | 339 |     ModelsMap.erase(oldmodeliter);
 | 
|---|
| [cbdcb1] | 340 |     LOG(2, "INFO: oldref is " << oldref << ", newref is " << newref << ".");
 | 
|---|
| [4a8169] | 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 |   }
 | 
|---|
| [429069] | 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 | 
 | 
|---|
| [4a8169] | 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) {
 | 
|---|
| [cbdcb1] | 367 |     ModelLookup::const_iterator modeliter = models.find((*iter)->LC->getModel());
 | 
|---|
| [4a8169] | 368 |     ASSERT( modeliter != models.end(),
 | 
|---|
 | 369 |         "LinkedCell_Controller::updateModelsForNewBoxMatrix() - we miss a model "
 | 
|---|
| [cbdcb1] | 370 |         +toString((*iter)->LC->getModel())+" in ModelLookup.");
 | 
|---|
| [4a8169] | 371 |     // this is ugly but the only place where we have to set ourselves over the constness of the member variable
 | 
|---|
| [cbdcb1] | 372 |     if (modeliter != models.end()) {
 | 
|---|
 | 373 |       LOG(2, "INFO: Setting model to " << modeliter->second << " in view " << *iter << ".");
 | 
|---|
 | 374 |       (*iter)->LC->setModel(modeliter->second);
 | 
|---|
 | 375 |     }
 | 
|---|
| [4a8169] | 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 | {
 | 
|---|
| [ce81e76] | 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 << ".");
 | 
|---|
| [4a8169] | 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 | 
 | 
|---|