| [5d1611] | 1 | /* | 
|---|
|  | 2 | * World.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Feb 3, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [112b09] | 8 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 9 |  | 
|---|
| [5d1611] | 10 | #include "World.hpp" | 
|---|
| [b99bf3] | 11 | #include "Patterns/ObservedContainer_impl.hpp" | 
|---|
| [5d1611] | 12 |  | 
|---|
| [d346b6] | 13 | #include "atom.hpp" | 
|---|
| [8e1f7af] | 14 | #include "config.hpp" | 
|---|
| [354859] | 15 | #include "molecule.hpp" | 
|---|
|  | 16 | #include "periodentafel.hpp" | 
|---|
| [43dad6] | 17 | #include "ThermoStatContainer.hpp" | 
|---|
| [fc1b24] | 18 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
| [865a945] | 19 | #include "Descriptors/AtomDescriptor_impl.hpp" | 
|---|
| [1c51c8] | 20 | #include "Descriptors/MoleculeDescriptor.hpp" | 
|---|
|  | 21 | #include "Descriptors/MoleculeDescriptor_impl.hpp" | 
|---|
| [6e97e5] | 22 | #include "Descriptors/SelectiveIterator_impl.hpp" | 
|---|
| [7c4e29] | 23 | #include "Actions/ManipulateAtomsProcess.hpp" | 
|---|
| [6d574a] | 24 | #include "Helpers/Assert.hpp" | 
|---|
| [84c494] | 25 | #include "Box.hpp" | 
|---|
|  | 26 | #include "Matrix.hpp" | 
|---|
| [127a8e] | 27 | #include "defs.hpp" | 
|---|
| [d346b6] | 28 |  | 
|---|
| [23b547] | 29 | #include "Patterns/Singleton_impl.hpp" | 
|---|
|  | 30 |  | 
|---|
| [d346b6] | 31 | using namespace std; | 
|---|
| [4d9c01] | 32 |  | 
|---|
| [5d1611] | 33 | /******************************* getter and setter ************************/ | 
|---|
| [354859] | 34 | periodentafel *&World::getPeriode(){ | 
|---|
| [5d1611] | 35 | return periode; | 
|---|
|  | 36 | } | 
|---|
|  | 37 |  | 
|---|
| [8e1f7af] | 38 | config *&World::getConfig(){ | 
|---|
|  | 39 | return configuration; | 
|---|
|  | 40 | } | 
|---|
|  | 41 |  | 
|---|
| [1c51c8] | 42 | // Atoms | 
|---|
|  | 43 |  | 
|---|
| [7a1ce5] | 44 | atom* World::getAtom(AtomDescriptor descriptor){ | 
|---|
| [fc1b24] | 45 | return descriptor.find(); | 
|---|
|  | 46 | } | 
|---|
|  | 47 |  | 
|---|
| [7a1ce5] | 48 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){ | 
|---|
| [fc1b24] | 49 | return descriptor.findAll(); | 
|---|
|  | 50 | } | 
|---|
|  | 51 |  | 
|---|
| [0e2a47] | 52 | vector<atom*> World::getAllAtoms(){ | 
|---|
|  | 53 | return getAllAtoms(AllAtoms()); | 
|---|
|  | 54 | } | 
|---|
|  | 55 |  | 
|---|
| [354859] | 56 | int World::numAtoms(){ | 
|---|
|  | 57 | return atoms.size(); | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
| [1c51c8] | 60 | // Molecules | 
|---|
|  | 61 |  | 
|---|
|  | 62 | molecule *World::getMolecule(MoleculeDescriptor descriptor){ | 
|---|
|  | 63 | return descriptor.find(); | 
|---|
|  | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){ | 
|---|
|  | 67 | return descriptor.findAll(); | 
|---|
|  | 68 | } | 
|---|
|  | 69 |  | 
|---|
| [97ebf8] | 70 | std::vector<molecule*> World::getAllMolecules(){ | 
|---|
|  | 71 | return getAllMolecules(AllMolecules()); | 
|---|
|  | 72 | } | 
|---|
|  | 73 |  | 
|---|
| [354859] | 74 | int World::numMolecules(){ | 
|---|
|  | 75 | return molecules_deprecated->ListOfMolecules.size(); | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
| [5f612ee] | 78 | // system | 
|---|
|  | 79 |  | 
|---|
| [84c494] | 80 | Box& World::getDomain() { | 
|---|
|  | 81 | return *cell_size; | 
|---|
|  | 82 | } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | void World::setDomain(const Matrix &mat){ | 
|---|
|  | 85 | *cell_size = mat; | 
|---|
| [5f612ee] | 86 | } | 
|---|
|  | 87 |  | 
|---|
|  | 88 | void World::setDomain(double * matrix) | 
|---|
|  | 89 | { | 
|---|
| [84c494] | 90 | Matrix M = ReturnFullMatrixforSymmetric(matrix); | 
|---|
|  | 91 | cell_size->setM(M); | 
|---|
| [5f612ee] | 92 | } | 
|---|
|  | 93 |  | 
|---|
| [387b36] | 94 | std::string World::getDefaultName() { | 
|---|
| [5f612ee] | 95 | return defaultName; | 
|---|
|  | 96 | } | 
|---|
|  | 97 |  | 
|---|
| [387b36] | 98 | void World::setDefaultName(std::string name) | 
|---|
| [5f612ee] | 99 | { | 
|---|
| [387b36] | 100 | defaultName = name; | 
|---|
| [5f612ee] | 101 | }; | 
|---|
|  | 102 |  | 
|---|
| [43dad6] | 103 | class ThermoStatContainer * World::getThermostats() | 
|---|
|  | 104 | { | 
|---|
|  | 105 | return Thermostats; | 
|---|
|  | 106 | } | 
|---|
|  | 107 |  | 
|---|
|  | 108 |  | 
|---|
| [e4b5de] | 109 | int World::getExitFlag() { | 
|---|
|  | 110 | return ExitFlag; | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 | void World::setExitFlag(int flag) { | 
|---|
|  | 114 | if (ExitFlag < flag) | 
|---|
|  | 115 | ExitFlag = flag; | 
|---|
|  | 116 | } | 
|---|
| [5f612ee] | 117 |  | 
|---|
| [afb47f] | 118 | /******************** Methods to change World state *********************/ | 
|---|
|  | 119 |  | 
|---|
| [354859] | 120 | molecule* World::createMolecule(){ | 
|---|
|  | 121 | OBSERVE; | 
|---|
|  | 122 | molecule *mol = NULL; | 
|---|
| [cbc5fb] | 123 | mol = NewMolecule(); | 
|---|
| [127a8e] | 124 | moleculeId_t id = getNextMoleculeId(); | 
|---|
|  | 125 | ASSERT(!molecules.count(id),"proposed id did not specify an unused ID"); | 
|---|
|  | 126 | mol->setId(id); | 
|---|
| [244d26] | 127 | // store the molecule by ID | 
|---|
| [cbc5fb] | 128 | molecules[mol->getId()] = mol; | 
|---|
| [354859] | 129 | mol->signOn(this); | 
|---|
|  | 130 | return mol; | 
|---|
|  | 131 | } | 
|---|
|  | 132 |  | 
|---|
| [cbc5fb] | 133 | void World::destroyMolecule(molecule* mol){ | 
|---|
|  | 134 | OBSERVE; | 
|---|
|  | 135 | destroyMolecule(mol->getId()); | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
|  | 138 | void World::destroyMolecule(moleculeId_t id){ | 
|---|
|  | 139 | OBSERVE; | 
|---|
|  | 140 | molecule *mol = molecules[id]; | 
|---|
| [6d574a] | 141 | ASSERT(mol,"Molecule id that was meant to be destroyed did not exist"); | 
|---|
| [cbc5fb] | 142 | DeleteMolecule(mol); | 
|---|
|  | 143 | molecules.erase(id); | 
|---|
| [127a8e] | 144 | releaseMoleculeId(id); | 
|---|
| [cbc5fb] | 145 | } | 
|---|
|  | 146 |  | 
|---|
| [46d958] | 147 | atom *World::createAtom(){ | 
|---|
|  | 148 | OBSERVE; | 
|---|
| [88d586] | 149 | atomId_t id = getNextAtomId(); | 
|---|
| [127a8e] | 150 | ASSERT(!atoms.count(id),"proposed id did not specify an unused ID"); | 
|---|
| [88d586] | 151 | atom *res = NewAtom(id); | 
|---|
| [46d958] | 152 | res->setWorld(this); | 
|---|
| [244d26] | 153 | // store the atom by ID | 
|---|
| [46d958] | 154 | atoms[res->getId()] = res; | 
|---|
|  | 155 | return res; | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
| [5f612ee] | 158 |  | 
|---|
| [46d958] | 159 | int World::registerAtom(atom *atom){ | 
|---|
|  | 160 | OBSERVE; | 
|---|
| [88d586] | 161 | atomId_t id = getNextAtomId(); | 
|---|
|  | 162 | atom->setId(id); | 
|---|
| [46d958] | 163 | atom->setWorld(this); | 
|---|
|  | 164 | atoms[atom->getId()] = atom; | 
|---|
|  | 165 | return atom->getId(); | 
|---|
|  | 166 | } | 
|---|
|  | 167 |  | 
|---|
|  | 168 | void World::destroyAtom(atom* atom){ | 
|---|
|  | 169 | OBSERVE; | 
|---|
|  | 170 | int id = atom->getId(); | 
|---|
|  | 171 | destroyAtom(id); | 
|---|
|  | 172 | } | 
|---|
|  | 173 |  | 
|---|
| [cbc5fb] | 174 | void World::destroyAtom(atomId_t id) { | 
|---|
| [46d958] | 175 | OBSERVE; | 
|---|
|  | 176 | atom *atom = atoms[id]; | 
|---|
| [6d574a] | 177 | ASSERT(atom,"Atom ID that was meant to be destroyed did not exist"); | 
|---|
| [46d958] | 178 | DeleteAtom(atom); | 
|---|
|  | 179 | atoms.erase(id); | 
|---|
| [88d586] | 180 | releaseAtomId(id); | 
|---|
|  | 181 | } | 
|---|
|  | 182 |  | 
|---|
|  | 183 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){ | 
|---|
|  | 184 | OBSERVE; | 
|---|
|  | 185 | // in case this call did not originate from inside the atom, we redirect it, | 
|---|
|  | 186 | // to also let it know that it has changed | 
|---|
|  | 187 | if(!target){ | 
|---|
|  | 188 | target = atoms[oldId]; | 
|---|
| [6d574a] | 189 | ASSERT(target,"Atom with that ID not found"); | 
|---|
| [88d586] | 190 | return target->changeId(newId); | 
|---|
|  | 191 | } | 
|---|
|  | 192 | else{ | 
|---|
|  | 193 | if(reserveAtomId(newId)){ | 
|---|
|  | 194 | atoms.erase(oldId); | 
|---|
|  | 195 | atoms.insert(pair<atomId_t,atom*>(newId,target)); | 
|---|
|  | 196 | return true; | 
|---|
|  | 197 | } | 
|---|
|  | 198 | else{ | 
|---|
|  | 199 | return false; | 
|---|
|  | 200 | } | 
|---|
|  | 201 | } | 
|---|
| [46d958] | 202 | } | 
|---|
|  | 203 |  | 
|---|
| [7c4e29] | 204 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){ | 
|---|
|  | 205 | return new ManipulateAtomsProcess(op, descr,name,true); | 
|---|
|  | 206 | } | 
|---|
|  | 207 |  | 
|---|
| [0e2a47] | 208 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){ | 
|---|
|  | 209 | return manipulateAtoms(op,name,AllAtoms()); | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
| [afb47f] | 212 | /********************* Internal Change methods for double Callback and Observer mechanism ********/ | 
|---|
|  | 213 |  | 
|---|
|  | 214 | void World::doManipulate(ManipulateAtomsProcess *proc){ | 
|---|
|  | 215 | proc->signOn(this); | 
|---|
|  | 216 | { | 
|---|
|  | 217 | OBSERVE; | 
|---|
|  | 218 | proc->doManipulate(this); | 
|---|
|  | 219 | } | 
|---|
|  | 220 | proc->signOff(this); | 
|---|
|  | 221 | } | 
|---|
| [88d586] | 222 | /******************************* IDManagement *****************************/ | 
|---|
|  | 223 |  | 
|---|
| [57adc7] | 224 | // Atoms | 
|---|
|  | 225 |  | 
|---|
| [88d586] | 226 | atomId_t World::getNextAtomId(){ | 
|---|
| [127a8e] | 227 | // try to find an Id in the pool; | 
|---|
|  | 228 | if(!atomIdPool.empty()){ | 
|---|
|  | 229 | atomIdPool_t::iterator iter=atomIdPool.begin(); | 
|---|
|  | 230 | atomId_t id = iter->first; | 
|---|
|  | 231 | pair<atomId_t,atomId_t> newRange = make_pair(id+1,iter->second); | 
|---|
|  | 232 | // we wont use this iterator anymore, so we don't care about invalidating | 
|---|
|  | 233 | atomIdPool.erase(iter); | 
|---|
|  | 234 | if(newRange.first<newRange.second){ | 
|---|
|  | 235 | atomIdPool.insert(newRange); | 
|---|
|  | 236 | } | 
|---|
| [23b547] | 237 | return id; | 
|---|
| [88d586] | 238 | } | 
|---|
| [127a8e] | 239 | // Nothing in the pool... we are out of luck | 
|---|
|  | 240 | return currAtomId++; | 
|---|
| [88d586] | 241 | } | 
|---|
|  | 242 |  | 
|---|
|  | 243 | void World::releaseAtomId(atomId_t id){ | 
|---|
| [127a8e] | 244 | atomIdPool.insert(make_pair(id,id+1)); | 
|---|
|  | 245 | defragAtomIdPool(); | 
|---|
| [88d586] | 246 | } | 
|---|
| [afb47f] | 247 |  | 
|---|
| [88d586] | 248 | bool World::reserveAtomId(atomId_t id){ | 
|---|
|  | 249 | if(id>=currAtomId ){ | 
|---|
| [127a8e] | 250 | pair<atomId_t,atomId_t> newRange = make_pair(currAtomId,id); | 
|---|
|  | 251 | if(newRange.first<newRange.second){ | 
|---|
|  | 252 | atomIdPool.insert(newRange); | 
|---|
| [88d586] | 253 | } | 
|---|
|  | 254 | currAtomId=id+1; | 
|---|
| [127a8e] | 255 | defragAtomIdPool(); | 
|---|
| [88d586] | 256 | return true; | 
|---|
|  | 257 | } | 
|---|
| [127a8e] | 258 | // look for a range that matches the request | 
|---|
|  | 259 | for(atomIdPool_t::iterator iter=atomIdPool.begin();iter!=atomIdPool.end();++iter){ | 
|---|
|  | 260 | if(iter->first>id){ | 
|---|
|  | 261 | // we have coverd all available ranges... nothing to be found here | 
|---|
|  | 262 | break; | 
|---|
|  | 263 | } | 
|---|
|  | 264 | // no need to check first, since it has to be <=id, since otherwise we would have broken out | 
|---|
|  | 265 | if(iter->second > id){ | 
|---|
|  | 266 | // we found a matching range... get the id from this range | 
|---|
|  | 267 |  | 
|---|
|  | 268 | // split up this range at the point of id | 
|---|
|  | 269 | pair<atomId_t,atomId_t> bottomRange = make_pair(iter->first,id); | 
|---|
|  | 270 | pair<atomId_t,atomId_t> topRange = make_pair(id+1,iter->second); | 
|---|
|  | 271 | // remove this range | 
|---|
|  | 272 | atomIdPool.erase(iter); | 
|---|
|  | 273 | if(bottomRange.first<bottomRange.second){ | 
|---|
|  | 274 | atomIdPool.insert(bottomRange); | 
|---|
|  | 275 | } | 
|---|
|  | 276 | if(topRange.first<topRange.second){ | 
|---|
|  | 277 | atomIdPool.insert(topRange); | 
|---|
|  | 278 | } | 
|---|
|  | 279 | defragAtomIdPool(); | 
|---|
|  | 280 | return true; | 
|---|
|  | 281 | } | 
|---|
| [88d586] | 282 | } | 
|---|
| [127a8e] | 283 | // this ID could not be reserved | 
|---|
|  | 284 | return false; | 
|---|
|  | 285 | } | 
|---|
|  | 286 |  | 
|---|
|  | 287 | void World::defragAtomIdPool(){ | 
|---|
|  | 288 | // check if the situation is bad enough to make defragging neccessary | 
|---|
|  | 289 | if((numAtomDefragSkips<MAX_FRAGMENTATION_SKIPS) && | 
|---|
|  | 290 | (atomIdPool.size()<lastAtomPoolSize+MAX_POOL_FRAGMENTATION)){ | 
|---|
|  | 291 | ++numAtomDefragSkips; | 
|---|
|  | 292 | return; | 
|---|
|  | 293 | } | 
|---|
|  | 294 | for(atomIdPool_t::iterator iter = atomIdPool.begin();iter!=atomIdPool.end();){ | 
|---|
|  | 295 | // see if this range is adjacent to the next one | 
|---|
|  | 296 | atomIdPool_t::iterator next = iter; | 
|---|
|  | 297 | next++; | 
|---|
|  | 298 | if(next!=atomIdPool.end() && (next->first==iter->second)){ | 
|---|
|  | 299 | // merge the two ranges | 
|---|
|  | 300 | pair<atomId_t,atomId_t> newRange = make_pair(iter->first,next->second); | 
|---|
|  | 301 | atomIdPool.erase(iter); | 
|---|
|  | 302 | atomIdPool.erase(next); | 
|---|
|  | 303 | pair<atomIdPool_t::iterator,bool> res = atomIdPool.insert(newRange); | 
|---|
|  | 304 | ASSERT(res.second,"Id-Pool was confused"); | 
|---|
|  | 305 | iter=res.first; | 
|---|
|  | 306 | continue; | 
|---|
|  | 307 | } | 
|---|
|  | 308 | ++iter; | 
|---|
|  | 309 | } | 
|---|
|  | 310 | if(!atomIdPool.empty()){ | 
|---|
|  | 311 | // check if the last range is at the border | 
|---|
|  | 312 | atomIdPool_t::iterator iter = atomIdPool.end(); | 
|---|
|  | 313 | iter--; | 
|---|
|  | 314 | if(iter->second==currAtomId){ | 
|---|
|  | 315 | currAtomId=iter->first; | 
|---|
|  | 316 | atomIdPool.erase(iter); | 
|---|
|  | 317 | } | 
|---|
| [88d586] | 318 | } | 
|---|
| [127a8e] | 319 | lastAtomPoolSize=atomIdPool.size(); | 
|---|
|  | 320 | numAtomDefragSkips=0; | 
|---|
| [88d586] | 321 | } | 
|---|
| [57adc7] | 322 |  | 
|---|
|  | 323 | // Molecules | 
|---|
|  | 324 |  | 
|---|
| [127a8e] | 325 | moleculeId_t World::getNextMoleculeId(){ | 
|---|
|  | 326 | // try to find an Id in the pool; | 
|---|
|  | 327 | if(!moleculeIdPool.empty()){ | 
|---|
|  | 328 | moleculeIdPool_t::iterator iter=moleculeIdPool.begin(); | 
|---|
|  | 329 | moleculeId_t id = iter->first; | 
|---|
|  | 330 | pair<moleculeId_t,moleculeId_t> newRange = make_pair(id+1,iter->second); | 
|---|
|  | 331 | // we wont use this iterator anymore, so we don't care about invalidating | 
|---|
|  | 332 | moleculeIdPool.erase(iter); | 
|---|
|  | 333 | if(newRange.first<newRange.second){ | 
|---|
|  | 334 | moleculeIdPool.insert(newRange); | 
|---|
|  | 335 | } | 
|---|
|  | 336 | return id; | 
|---|
|  | 337 | } | 
|---|
|  | 338 | // Nothing in the pool... we are out of luck | 
|---|
|  | 339 | return currMoleculeId++; | 
|---|
|  | 340 | } | 
|---|
|  | 341 |  | 
|---|
|  | 342 | void World::releaseMoleculeId(moleculeId_t id){ | 
|---|
|  | 343 | moleculeIdPool.insert(make_pair(id,id+1)); | 
|---|
|  | 344 | defragMoleculeIdPool(); | 
|---|
|  | 345 | } | 
|---|
|  | 346 |  | 
|---|
|  | 347 | bool World::reserveMoleculeId(moleculeId_t id){ | 
|---|
|  | 348 | if(id>=currMoleculeId ){ | 
|---|
|  | 349 | pair<moleculeId_t,moleculeId_t> newRange = make_pair(currMoleculeId,id); | 
|---|
|  | 350 | if(newRange.first<newRange.second){ | 
|---|
|  | 351 | moleculeIdPool.insert(newRange); | 
|---|
|  | 352 | } | 
|---|
|  | 353 | currMoleculeId=id+1; | 
|---|
|  | 354 | defragMoleculeIdPool(); | 
|---|
|  | 355 | return true; | 
|---|
|  | 356 | } | 
|---|
|  | 357 | // look for a range that matches the request | 
|---|
|  | 358 | for(moleculeIdPool_t::iterator iter=moleculeIdPool.begin();iter!=moleculeIdPool.end();++iter){ | 
|---|
|  | 359 | if(iter->first>id){ | 
|---|
|  | 360 | // we have coverd all available ranges... nothing to be found here | 
|---|
|  | 361 | break; | 
|---|
|  | 362 | } | 
|---|
|  | 363 | // no need to check first, since it has to be <=id, since otherwise we would have broken out | 
|---|
|  | 364 | if(iter->second > id){ | 
|---|
|  | 365 | // we found a matching range... get the id from this range | 
|---|
|  | 366 |  | 
|---|
|  | 367 | // split up this range at the point of id | 
|---|
|  | 368 | pair<moleculeId_t,moleculeId_t> bottomRange = make_pair(iter->first,id); | 
|---|
|  | 369 | pair<moleculeId_t,moleculeId_t> topRange = make_pair(id+1,iter->second); | 
|---|
|  | 370 | // remove this range | 
|---|
|  | 371 | moleculeIdPool.erase(iter); | 
|---|
|  | 372 | if(bottomRange.first<bottomRange.second){ | 
|---|
|  | 373 | moleculeIdPool.insert(bottomRange); | 
|---|
|  | 374 | } | 
|---|
|  | 375 | if(topRange.first<topRange.second){ | 
|---|
|  | 376 | moleculeIdPool.insert(topRange); | 
|---|
|  | 377 | } | 
|---|
|  | 378 | defragMoleculeIdPool(); | 
|---|
|  | 379 | return true; | 
|---|
|  | 380 | } | 
|---|
|  | 381 | } | 
|---|
|  | 382 | // this ID could not be reserved | 
|---|
|  | 383 | return false; | 
|---|
|  | 384 | } | 
|---|
|  | 385 |  | 
|---|
|  | 386 | void World::defragMoleculeIdPool(){ | 
|---|
|  | 387 | // check if the situation is bad enough to make defragging neccessary | 
|---|
|  | 388 | if((numMoleculeDefragSkips<MAX_FRAGMENTATION_SKIPS) && | 
|---|
|  | 389 | (moleculeIdPool.size()<lastMoleculePoolSize+MAX_POOL_FRAGMENTATION)){ | 
|---|
|  | 390 | ++numMoleculeDefragSkips; | 
|---|
|  | 391 | return; | 
|---|
|  | 392 | } | 
|---|
|  | 393 | for(moleculeIdPool_t::iterator iter = moleculeIdPool.begin();iter!=moleculeIdPool.end();){ | 
|---|
|  | 394 | // see if this range is adjacent to the next one | 
|---|
|  | 395 | moleculeIdPool_t::iterator next = iter; | 
|---|
|  | 396 | next++; | 
|---|
|  | 397 | if(next!=moleculeIdPool.end() && (next->first==iter->second)){ | 
|---|
|  | 398 | // merge the two ranges | 
|---|
|  | 399 | pair<moleculeId_t,moleculeId_t> newRange = make_pair(iter->first,next->second); | 
|---|
|  | 400 | moleculeIdPool.erase(iter); | 
|---|
|  | 401 | moleculeIdPool.erase(next); | 
|---|
|  | 402 | pair<moleculeIdPool_t::iterator,bool> res = moleculeIdPool.insert(newRange); | 
|---|
|  | 403 | ASSERT(res.second,"Id-Pool was confused"); | 
|---|
|  | 404 | iter=res.first; | 
|---|
|  | 405 | continue; | 
|---|
|  | 406 | } | 
|---|
|  | 407 | ++iter; | 
|---|
|  | 408 | } | 
|---|
|  | 409 | if(!moleculeIdPool.empty()){ | 
|---|
|  | 410 | // check if the last range is at the border | 
|---|
|  | 411 | moleculeIdPool_t::iterator iter = moleculeIdPool.end(); | 
|---|
|  | 412 | iter--; | 
|---|
|  | 413 | if(iter->second==currMoleculeId){ | 
|---|
|  | 414 | currMoleculeId=iter->first; | 
|---|
|  | 415 | moleculeIdPool.erase(iter); | 
|---|
|  | 416 | } | 
|---|
|  | 417 | } | 
|---|
|  | 418 | lastMoleculePoolSize=moleculeIdPool.size(); | 
|---|
|  | 419 | numMoleculeDefragSkips=0; | 
|---|
|  | 420 | } | 
|---|
|  | 421 |  | 
|---|
| [865a945] | 422 | /******************************* Iterators ********************************/ | 
|---|
|  | 423 |  | 
|---|
| [fa0b18] | 424 | // external parts with observers | 
|---|
|  | 425 |  | 
|---|
| [6e97e5] | 426 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor); | 
|---|
|  | 427 |  | 
|---|
| [fa0b18] | 428 | World::AtomIterator | 
|---|
|  | 429 | World::getAtomIter(AtomDescriptor descr){ | 
|---|
|  | 430 | return AtomIterator(descr,atoms); | 
|---|
|  | 431 | } | 
|---|
| [865a945] | 432 |  | 
|---|
| [fa0b18] | 433 | World::AtomIterator | 
|---|
|  | 434 | World::getAtomIter(){ | 
|---|
|  | 435 | return AtomIterator(AllAtoms(),atoms); | 
|---|
| [865a945] | 436 | } | 
|---|
| [354859] | 437 |  | 
|---|
| [fa0b18] | 438 | World::AtomIterator | 
|---|
|  | 439 | World::atomEnd(){ | 
|---|
| [6e97e5] | 440 | return AtomIterator(AllAtoms(),atoms,atoms.end()); | 
|---|
| [7c4e29] | 441 | } | 
|---|
|  | 442 |  | 
|---|
| [5d880e] | 443 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor); | 
|---|
|  | 444 |  | 
|---|
|  | 445 | World::MoleculeIterator | 
|---|
|  | 446 | World::getMoleculeIter(MoleculeDescriptor descr){ | 
|---|
|  | 447 | return MoleculeIterator(descr,molecules); | 
|---|
|  | 448 | } | 
|---|
|  | 449 |  | 
|---|
|  | 450 | World::MoleculeIterator | 
|---|
|  | 451 | World::getMoleculeIter(){ | 
|---|
|  | 452 | return MoleculeIterator(AllMolecules(),molecules); | 
|---|
|  | 453 | } | 
|---|
|  | 454 |  | 
|---|
|  | 455 | World::MoleculeIterator | 
|---|
|  | 456 | World::moleculeEnd(){ | 
|---|
|  | 457 | return MoleculeIterator(AllMolecules(),molecules,molecules.end()); | 
|---|
|  | 458 | } | 
|---|
|  | 459 |  | 
|---|
| [fa0b18] | 460 | // Internal parts, without observers | 
|---|
|  | 461 |  | 
|---|
|  | 462 | // Build the AtomIterator from template | 
|---|
|  | 463 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet::set_t,AtomDescriptor); | 
|---|
|  | 464 |  | 
|---|
|  | 465 |  | 
|---|
|  | 466 | World::internal_AtomIterator | 
|---|
|  | 467 | World::getAtomIter_internal(AtomDescriptor descr){ | 
|---|
|  | 468 | return internal_AtomIterator(descr,atoms.getContent()); | 
|---|
|  | 469 | } | 
|---|
|  | 470 |  | 
|---|
|  | 471 | World::internal_AtomIterator | 
|---|
|  | 472 | World::atomEnd_internal(){ | 
|---|
|  | 473 | return internal_AtomIterator(AllAtoms(),atoms.getContent(),atoms.end_internal()); | 
|---|
|  | 474 | } | 
|---|
|  | 475 |  | 
|---|
| [6e97e5] | 476 | // build the MoleculeIterator from template | 
|---|
| [e3d865] | 477 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet::set_t,MoleculeDescriptor); | 
|---|
| [6e97e5] | 478 |  | 
|---|
| [e3d865] | 479 | World::internal_MoleculeIterator World::getMoleculeIter_internal(MoleculeDescriptor descr){ | 
|---|
|  | 480 | return internal_MoleculeIterator(descr,molecules.getContent()); | 
|---|
| [1c51c8] | 481 | } | 
|---|
|  | 482 |  | 
|---|
| [e3d865] | 483 | World::internal_MoleculeIterator World::moleculeEnd_internal(){ | 
|---|
|  | 484 | return internal_MoleculeIterator(AllMolecules(),molecules.getContent(),molecules.end_internal()); | 
|---|
| [1c51c8] | 485 | } | 
|---|
|  | 486 |  | 
|---|
| [5d1611] | 487 | /******************************* Singleton Stuff **************************/ | 
|---|
|  | 488 |  | 
|---|
| [7a1ce5] | 489 | World::World() : | 
|---|
| [cd5047] | 490 | Observable("World"), | 
|---|
| [354859] | 491 | periode(new periodentafel), | 
|---|
| [8e1f7af] | 492 | configuration(new config), | 
|---|
| [43dad6] | 493 | Thermostats(new ThermoStatContainer), | 
|---|
| [e4b5de] | 494 | ExitFlag(0), | 
|---|
| [fa0b18] | 495 | atoms(this), | 
|---|
| [24a5e0] | 496 | currAtomId(0), | 
|---|
| [127a8e] | 497 | lastAtomPoolSize(0), | 
|---|
|  | 498 | numAtomDefragSkips(0), | 
|---|
| [51be2a] | 499 | molecules(this), | 
|---|
| [24a5e0] | 500 | currMoleculeId(0), | 
|---|
|  | 501 | molecules_deprecated(new MoleculeListClass(this)) | 
|---|
| [7dad10] | 502 | { | 
|---|
| [84c494] | 503 | cell_size = new Box; | 
|---|
|  | 504 | Matrix domain; | 
|---|
|  | 505 | domain.at(0,0) = 20; | 
|---|
|  | 506 | domain.at(1,1) = 20; | 
|---|
|  | 507 | domain.at(2,2) = 20; | 
|---|
|  | 508 | cell_size->setM(domain); | 
|---|
| [387b36] | 509 | defaultName = "none"; | 
|---|
| [7dad10] | 510 | molecules_deprecated->signOn(this); | 
|---|
|  | 511 | } | 
|---|
| [5d1611] | 512 |  | 
|---|
|  | 513 | World::~World() | 
|---|
| [354859] | 514 | { | 
|---|
| [028c2e] | 515 | molecules_deprecated->signOff(this); | 
|---|
| [84c494] | 516 | delete cell_size; | 
|---|
| [46d958] | 517 | delete molecules_deprecated; | 
|---|
| [354859] | 518 | delete periode; | 
|---|
| [8e1f7af] | 519 | delete configuration; | 
|---|
| [43dad6] | 520 | delete Thermostats; | 
|---|
| [cbc5fb] | 521 | MoleculeSet::iterator molIter; | 
|---|
|  | 522 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){ | 
|---|
|  | 523 | DeleteMolecule((*molIter).second); | 
|---|
|  | 524 | } | 
|---|
|  | 525 | molecules.clear(); | 
|---|
|  | 526 | AtomSet::iterator atIter; | 
|---|
|  | 527 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){ | 
|---|
|  | 528 | DeleteAtom((*atIter).second); | 
|---|
| [46d958] | 529 | } | 
|---|
|  | 530 | atoms.clear(); | 
|---|
| [354859] | 531 | } | 
|---|
| [5d1611] | 532 |  | 
|---|
| [23b547] | 533 | // Explicit instantiation of the singleton mechanism at this point | 
|---|
| [5d1611] | 534 |  | 
|---|
| [23b547] | 535 | CONSTRUCT_SINGLETON(World) | 
|---|
| [5d1611] | 536 |  | 
|---|
|  | 537 | /******************************* deprecated Legacy Stuff ***********************/ | 
|---|
|  | 538 |  | 
|---|
| [354859] | 539 | MoleculeListClass *&World::getMolecules() { | 
|---|
|  | 540 | return molecules_deprecated; | 
|---|
| [5d1611] | 541 | } | 
|---|