Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/molecule.cpp

    re39e7a r5be798  
    3636
    3737#include <algorithm>
    38 #include <boost/assign.hpp>
    3938#include <boost/bind.hpp>
    4039#include <boost/foreach.hpp>
     
    5150#include "CodePatterns/enumeration.hpp"
    5251#include "CodePatterns/Log.hpp"
    53 #include "CodePatterns/Observer/Observable.hpp"
    5452#include "CodePatterns/Observer/Notification.hpp"
    5553#include "config.hpp"
     
    6967#include "WorldTime.hpp"
    7068
    71 using namespace boost::assign;
    72 
    73 // static entities
    74 static Observable::channels_t getBoundingBoxChannels()
    75 {
    76   Observable::channels_t channels;
    77   channels += molecule::AtomInserted, molecule::AtomRemoved, molecule::AtomMoved;
    78   return channels;
    79 }
    8069
    8170/************************************* Functions for class molecule *********************************/
     
    9483  BondCount(this,boost::bind(&molecule::doCountBonds,this),"BondCount"),
    9584  atomIdPool(1, 20, 100),
    96   BoundingBoxSweepingAxis(std::vector<AtomDistanceMap_t>(NDIM)),
    9785  _lastchangedatomid(-1),
    9886  last_atom(0)
     
    10492    OurChannel->addChannel(type);
    10593
    106   // cannot initialize in initializer body as then channels have not been setup yet
    107   BoundingBox.reset(
    108       new Cacheable<BoundingBoxInfo>(
    109           this, boost::bind(&molecule::updateBoundingBox, boost::cref(this)), "molecule_BoundingBox", getBoundingBoxChannels()));
    110 
    11194  strcpy(name,World::getInstance().getDefaultName().c_str());
    112 }
     95};
    11396
    11497molecule *NewMolecule(){
     
    187170}
    188171
    189 bool molecule::changeAtomId(int oldId, int newId)
    190 {
    191   OBSERVE;
    192   if ((!atomIds.contains( oldId )) || (atomIds.contains( newId )))
    193     return false;
    194   atomIds.erase( oldId );
    195   atomIds.insert( newId );
    196   // also update BoundingBoxSweepingAxis
    197   for (int i=0;i<NDIM;++i) {
    198     AtomDistanceMap_t::left_iterator iter = BoundingBoxSweepingAxis[i].left.find(oldId);
    199     ASSERT(iter != BoundingBoxSweepingAxis[i].left.end(),
    200         "molecule::changeAtomId() - could not find atom "+toString(oldId)
    201         +" in BoundingBoxSweepingAxis.");
    202     const double component = iter->second;
    203     BoundingBoxSweepingAxis[i].left.erase(iter);
    204     BoundingBoxSweepingAxis[i].left.insert( std::make_pair(newId, component) );
    205   }
    206   return true;
    207 }
    208 
    209172bool molecule::changeId(moleculeId_t newId){
    210173  // first we move ourselves in the world
     
    264227  atomIds.erase( _atom->getId() );
    265228  {
    266     BoundingBoxInfo oldinfo = updateBoundingBox();
    267     for (int i=0;i<NDIM;++i)
    268       BoundingBoxSweepingAxis[i].left.erase( _atom->getId() );
    269     BoundingBoxInfo newinfo = updateBoundingBox();
    270     if (oldinfo != newinfo)
    271       NOTIFY(BoundingBoxChanged);
    272   }
    273   {
    274229    NOTIFY(AtomNrChanged);
    275230    atomIdPool.releaseId(_atom->getNr());
     
    285240molecule::const_iterator molecule::erase( atom * key )
    286241{
     242  OBSERVE;
     243  {
     244    _lastchangedatomid = key->getId();
     245    NOTIFY(AtomRemoved);
     246  }
    287247  const_iterator iter = const_cast<const molecule &>(*this).find(key);
    288   if (iter != const_cast<const molecule &>(*this).end())
    289     return erase(iter);
    290   else
    291     return iter;
     248  if (iter != const_cast<const molecule &>(*this).end()){
     249    ++iter;
     250    atomIds.erase( key->getId() );
     251    {
     252      NOTIFY(AtomNrChanged);
     253      atomIdPool.releaseId(key->getNr());
     254      LocalToGlobalId.erase(key->getNr());
     255      key->setNr(-1);
     256    }
     257    NOTIFY(FormulaChanged);
     258    formula-=key->getType();
     259    key->removeFromMolecule();
     260  }
     261  return iter;
    292262}
    293263
     
    299269  std::pair<iterator,bool> res = atomIds.insert(key->getId());
    300270  if (res.second) { // push atom if went well
    301     {
    302       BoundingBoxInfo oldinfo = updateBoundingBox();
    303       for (int i=0;i<NDIM;++i)
    304         BoundingBoxSweepingAxis[i].left.insert( std::make_pair(key->getId(), key->getPosition()[i]));
    305       BoundingBoxInfo newinfo = updateBoundingBox();
    306       if (oldinfo != newinfo)
    307         NOTIFY(BoundingBoxChanged);
    308     }
    309271    NOTIFY(AtomNrChanged);
    310272    key->setNr(atomIdPool.getNextId());
     
    10761038}
    10771039
    1078 molecule::BoundingBoxInfo molecule::updateBoundingBox() const
    1079 {
    1080   BoundingBoxInfo info;
    1081   Vector min = zeroVec;
    1082   Vector max = zeroVec;
    1083   for (int i=0;i<NDIM;++i) {
    1084     if (!BoundingBoxSweepingAxis[i].right.empty()) {
    1085       min[i] = BoundingBoxSweepingAxis[i].right.begin()->first;
    1086       max[i] = BoundingBoxSweepingAxis[i].right.rbegin()->first;
    1087     }
    1088   }
    1089   info.radius = (.5*(max-min)).Norm();
    1090   info.position = .5*(max+min);
    1091   return info;
    1092 }
    1093 
    1094 molecule::BoundingBoxInfo molecule::getBoundingBox() const
    1095 {
    1096   return **BoundingBox;
    1097 }
    1098 
    10991040void molecule::update(Observable *publisher)
    11001041{
     
    11161057        // emit others about one of our atoms moved
    11171058        _lastchangedatomid = _atom->getId();
    1118         // update entry in map
    1119         BoundingBoxInfo oldinfo = updateBoundingBox();
    1120         for (int i=0;i<NDIM;++i) {
    1121           AtomDistanceMap_t::left_iterator iter = BoundingBoxSweepingAxis[i].left.find(_atom->getId());
    1122           ASSERT(iter != BoundingBoxSweepingAxis[i].left.end(),
    1123               "molecule::recieveNotification() - could not find atom "+toString(_atom->getId())
    1124               +" in BoundingBoxSweepingAxis.");
    1125           BoundingBoxSweepingAxis[i].left.erase(iter);
    1126           BoundingBoxSweepingAxis[i].left.insert(
    1127               std::make_pair(_atom->getId(), _atom->getPosition()[i]) );
    1128         }
    1129         BoundingBoxInfo newinfo = updateBoundingBox();
    11301059        OBSERVE;
    11311060        NOTIFY(AtomMoved);
    1132         if (oldinfo != newinfo)
    1133           NOTIFY(BoundingBoxChanged);
    11341061        break;
    11351062      }
Note: See TracChangeset for help on using the changeset viewer.