| [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"
 | 
|---|
 | 11 | 
 | 
|---|
| [90c4280] | 12 | #include <functional>
 | 
|---|
| [5d1611] | 13 | 
 | 
|---|
| [d346b6] | 14 | #include "atom.hpp"
 | 
|---|
| [8e1f7af] | 15 | #include "config.hpp"
 | 
|---|
| [354859] | 16 | #include "molecule.hpp"
 | 
|---|
 | 17 | #include "periodentafel.hpp"
 | 
|---|
| [43dad6] | 18 | #include "ThermoStatContainer.hpp"
 | 
|---|
| [fc1b24] | 19 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| [865a945] | 20 | #include "Descriptors/AtomDescriptor_impl.hpp"
 | 
|---|
| [1c51c8] | 21 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
 | 22 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
 | 
|---|
| [6e97e5] | 23 | #include "Descriptors/SelectiveIterator_impl.hpp"
 | 
|---|
| [7c4e29] | 24 | #include "Actions/ManipulateAtomsProcess.hpp"
 | 
|---|
| [6d574a] | 25 | #include "Helpers/Assert.hpp"
 | 
|---|
| [84c494] | 26 | #include "Box.hpp"
 | 
|---|
 | 27 | #include "Matrix.hpp"
 | 
|---|
| [127a8e] | 28 | #include "defs.hpp"
 | 
|---|
| [d346b6] | 29 | 
 | 
|---|
| [23b547] | 30 | #include "Patterns/Singleton_impl.hpp"
 | 
|---|
| [90c4280] | 31 | #include "Patterns/ObservedContainer_impl.hpp"
 | 
|---|
| [23b547] | 32 | 
 | 
|---|
| [d346b6] | 33 | using namespace std;
 | 
|---|
| [4d9c01] | 34 | 
 | 
|---|
| [5d1611] | 35 | /******************************* getter and setter ************************/
 | 
|---|
| [354859] | 36 | periodentafel *&World::getPeriode(){
 | 
|---|
| [5d1611] | 37 |   return periode;
 | 
|---|
 | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
| [8e1f7af] | 40 | config *&World::getConfig(){
 | 
|---|
 | 41 |   return configuration;
 | 
|---|
 | 42 | }
 | 
|---|
 | 43 | 
 | 
|---|
| [1c51c8] | 44 | // Atoms
 | 
|---|
 | 45 | 
 | 
|---|
| [7a1ce5] | 46 | atom* World::getAtom(AtomDescriptor descriptor){
 | 
|---|
| [fc1b24] | 47 |   return descriptor.find();
 | 
|---|
 | 48 | }
 | 
|---|
 | 49 | 
 | 
|---|
| [7a1ce5] | 50 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
 | 
|---|
| [fc1b24] | 51 |   return descriptor.findAll();
 | 
|---|
 | 52 | }
 | 
|---|
 | 53 | 
 | 
|---|
| [0e2a47] | 54 | vector<atom*> World::getAllAtoms(){
 | 
|---|
 | 55 |   return getAllAtoms(AllAtoms());
 | 
|---|
 | 56 | }
 | 
|---|
 | 57 | 
 | 
|---|
| [354859] | 58 | int World::numAtoms(){
 | 
|---|
 | 59 |   return atoms.size();
 | 
|---|
 | 60 | }
 | 
|---|
 | 61 | 
 | 
|---|
| [1c51c8] | 62 | // Molecules
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 | molecule *World::getMolecule(MoleculeDescriptor descriptor){
 | 
|---|
 | 65 |   return descriptor.find();
 | 
|---|
 | 66 | }
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
 | 
|---|
 | 69 |   return descriptor.findAll();
 | 
|---|
 | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
| [97ebf8] | 72 | std::vector<molecule*> World::getAllMolecules(){
 | 
|---|
 | 73 |   return getAllMolecules(AllMolecules());
 | 
|---|
 | 74 | }
 | 
|---|
 | 75 | 
 | 
|---|
| [354859] | 76 | int World::numMolecules(){
 | 
|---|
 | 77 |   return molecules_deprecated->ListOfMolecules.size();
 | 
|---|
 | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
| [5f612ee] | 80 | // system
 | 
|---|
 | 81 | 
 | 
|---|
| [84c494] | 82 | Box& World::getDomain() {
 | 
|---|
 | 83 |   return *cell_size;
 | 
|---|
 | 84 | }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | void World::setDomain(const Matrix &mat){
 | 
|---|
| [be97a8] | 87 |   OBSERVE;
 | 
|---|
| [84c494] | 88 |   *cell_size = mat;
 | 
|---|
| [5f612ee] | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 | void World::setDomain(double * matrix)
 | 
|---|
 | 92 | {
 | 
|---|
| [b9c847] | 93 |   OBSERVE;
 | 
|---|
| [84c494] | 94 |   Matrix M = ReturnFullMatrixforSymmetric(matrix);
 | 
|---|
 | 95 |   cell_size->setM(M);
 | 
|---|
| [5f612ee] | 96 | }
 | 
|---|
 | 97 | 
 | 
|---|
| [387b36] | 98 | std::string World::getDefaultName() {
 | 
|---|
| [5f612ee] | 99 |   return defaultName;
 | 
|---|
 | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
| [387b36] | 102 | void World::setDefaultName(std::string name)
 | 
|---|
| [5f612ee] | 103 | {
 | 
|---|
| [be97a8] | 104 |   OBSERVE;
 | 
|---|
| [387b36] | 105 |   defaultName = name;
 | 
|---|
| [5f612ee] | 106 | };
 | 
|---|
 | 107 | 
 | 
|---|
| [43dad6] | 108 | class ThermoStatContainer * World::getThermostats()
 | 
|---|
 | 109 | {
 | 
|---|
 | 110 |   return Thermostats;
 | 
|---|
 | 111 | }
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 | 
 | 
|---|
| [e4b5de] | 114 | int World::getExitFlag() {
 | 
|---|
 | 115 |   return ExitFlag;
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | void World::setExitFlag(int flag) {
 | 
|---|
 | 119 |   if (ExitFlag < flag)
 | 
|---|
 | 120 |     ExitFlag = flag;
 | 
|---|
 | 121 | }
 | 
|---|
| [5f612ee] | 122 | 
 | 
|---|
| [afb47f] | 123 | /******************** Methods to change World state *********************/
 | 
|---|
 | 124 | 
 | 
|---|
| [354859] | 125 | molecule* World::createMolecule(){
 | 
|---|
 | 126 |   OBSERVE;
 | 
|---|
 | 127 |   molecule *mol = NULL;
 | 
|---|
| [cbc5fb] | 128 |   mol = NewMolecule();
 | 
|---|
| [127a8e] | 129 |   moleculeId_t id = getNextMoleculeId();
 | 
|---|
 | 130 |   ASSERT(!molecules.count(id),"proposed id did not specify an unused ID");
 | 
|---|
 | 131 |   mol->setId(id);
 | 
|---|
| [244d26] | 132 |   // store the molecule by ID
 | 
|---|
| [cbc5fb] | 133 |   molecules[mol->getId()] = mol;
 | 
|---|
| [354859] | 134 |   mol->signOn(this);
 | 
|---|
 | 135 |   return mol;
 | 
|---|
 | 136 | }
 | 
|---|
 | 137 | 
 | 
|---|
| [cbc5fb] | 138 | void World::destroyMolecule(molecule* mol){
 | 
|---|
 | 139 |   OBSERVE;
 | 
|---|
 | 140 |   destroyMolecule(mol->getId());
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | void World::destroyMolecule(moleculeId_t id){
 | 
|---|
 | 144 |   OBSERVE;
 | 
|---|
 | 145 |   molecule *mol = molecules[id];
 | 
|---|
| [6d574a] | 146 |   ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
 | 
|---|
| [cbc5fb] | 147 |   DeleteMolecule(mol);
 | 
|---|
 | 148 |   molecules.erase(id);
 | 
|---|
| [127a8e] | 149 |   releaseMoleculeId(id);
 | 
|---|
| [cbc5fb] | 150 | }
 | 
|---|
 | 151 | 
 | 
|---|
| [46d958] | 152 | atom *World::createAtom(){
 | 
|---|
 | 153 |   OBSERVE;
 | 
|---|
| [88d586] | 154 |   atomId_t id = getNextAtomId();
 | 
|---|
| [127a8e] | 155 |   ASSERT(!atoms.count(id),"proposed id did not specify an unused ID");
 | 
|---|
| [88d586] | 156 |   atom *res = NewAtom(id);
 | 
|---|
| [46d958] | 157 |   res->setWorld(this);
 | 
|---|
| [244d26] | 158 |   // store the atom by ID
 | 
|---|
| [46d958] | 159 |   atoms[res->getId()] = res;
 | 
|---|
 | 160 |   return res;
 | 
|---|
 | 161 | }
 | 
|---|
 | 162 | 
 | 
|---|
| [5f612ee] | 163 | 
 | 
|---|
| [46d958] | 164 | int World::registerAtom(atom *atom){
 | 
|---|
 | 165 |   OBSERVE;
 | 
|---|
| [88d586] | 166 |   atomId_t id = getNextAtomId();
 | 
|---|
 | 167 |   atom->setId(id);
 | 
|---|
| [46d958] | 168 |   atom->setWorld(this);
 | 
|---|
 | 169 |   atoms[atom->getId()] = atom;
 | 
|---|
 | 170 |   return atom->getId();
 | 
|---|
 | 171 | }
 | 
|---|
 | 172 | 
 | 
|---|
 | 173 | void World::destroyAtom(atom* atom){
 | 
|---|
 | 174 |   OBSERVE;
 | 
|---|
 | 175 |   int id = atom->getId();
 | 
|---|
 | 176 |   destroyAtom(id);
 | 
|---|
 | 177 | }
 | 
|---|
 | 178 | 
 | 
|---|
| [cbc5fb] | 179 | void World::destroyAtom(atomId_t id) {
 | 
|---|
| [46d958] | 180 |   OBSERVE;
 | 
|---|
 | 181 |   atom *atom = atoms[id];
 | 
|---|
| [6d574a] | 182 |   ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
 | 
|---|
| [46d958] | 183 |   DeleteAtom(atom);
 | 
|---|
 | 184 |   atoms.erase(id);
 | 
|---|
| [88d586] | 185 |   releaseAtomId(id);
 | 
|---|
 | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|
 | 188 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
 | 
|---|
 | 189 |   OBSERVE;
 | 
|---|
 | 190 |   // in case this call did not originate from inside the atom, we redirect it,
 | 
|---|
 | 191 |   // to also let it know that it has changed
 | 
|---|
 | 192 |   if(!target){
 | 
|---|
 | 193 |     target = atoms[oldId];
 | 
|---|
| [6d574a] | 194 |     ASSERT(target,"Atom with that ID not found");
 | 
|---|
| [88d586] | 195 |     return target->changeId(newId);
 | 
|---|
 | 196 |   }
 | 
|---|
 | 197 |   else{
 | 
|---|
 | 198 |     if(reserveAtomId(newId)){
 | 
|---|
 | 199 |       atoms.erase(oldId);
 | 
|---|
 | 200 |       atoms.insert(pair<atomId_t,atom*>(newId,target));
 | 
|---|
 | 201 |       return true;
 | 
|---|
 | 202 |     }
 | 
|---|
 | 203 |     else{
 | 
|---|
 | 204 |       return false;
 | 
|---|
 | 205 |     }
 | 
|---|
 | 206 |   }
 | 
|---|
| [46d958] | 207 | }
 | 
|---|
 | 208 | 
 | 
|---|
| [7c4e29] | 209 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
 | 
|---|
 | 210 |   return new ManipulateAtomsProcess(op, descr,name,true);
 | 
|---|
 | 211 | }
 | 
|---|
 | 212 | 
 | 
|---|
| [0e2a47] | 213 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
 | 
|---|
 | 214 |   return manipulateAtoms(op,name,AllAtoms());
 | 
|---|
 | 215 | }
 | 
|---|
 | 216 | 
 | 
|---|
| [afb47f] | 217 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
 | 
|---|
 | 218 | 
 | 
|---|
 | 219 | void World::doManipulate(ManipulateAtomsProcess *proc){
 | 
|---|
 | 220 |   proc->signOn(this);
 | 
|---|
 | 221 |   {
 | 
|---|
 | 222 |     OBSERVE;
 | 
|---|
 | 223 |     proc->doManipulate(this);
 | 
|---|
 | 224 |   }
 | 
|---|
 | 225 |   proc->signOff(this);
 | 
|---|
 | 226 | }
 | 
|---|
| [88d586] | 227 | /******************************* IDManagement *****************************/
 | 
|---|
 | 228 | 
 | 
|---|
| [57adc7] | 229 | // Atoms
 | 
|---|
 | 230 | 
 | 
|---|
| [88d586] | 231 | atomId_t World::getNextAtomId(){
 | 
|---|
| [127a8e] | 232 |   // try to find an Id in the pool;
 | 
|---|
 | 233 |   if(!atomIdPool.empty()){
 | 
|---|
 | 234 |     atomIdPool_t::iterator iter=atomIdPool.begin();
 | 
|---|
 | 235 |     atomId_t id = iter->first;
 | 
|---|
| [dc11c9] | 236 |     range<atomId_t> newRange = makeRange(id+1,iter->last);
 | 
|---|
| [127a8e] | 237 |     // we wont use this iterator anymore, so we don't care about invalidating
 | 
|---|
 | 238 |     atomIdPool.erase(iter);
 | 
|---|
| [dc11c9] | 239 |     if(newRange.first<newRange.last){
 | 
|---|
| [127a8e] | 240 |       atomIdPool.insert(newRange);
 | 
|---|
 | 241 |     }
 | 
|---|
| [23b547] | 242 |     return id;
 | 
|---|
| [88d586] | 243 |   }
 | 
|---|
| [127a8e] | 244 |   // Nothing in the pool... we are out of luck
 | 
|---|
 | 245 |   return currAtomId++;
 | 
|---|
| [88d586] | 246 | }
 | 
|---|
 | 247 | 
 | 
|---|
 | 248 | void World::releaseAtomId(atomId_t id){
 | 
|---|
| [dc11c9] | 249 |   atomIdPool.insert(makeRange(id,id+1));
 | 
|---|
| [127a8e] | 250 |   defragAtomIdPool();
 | 
|---|
| [88d586] | 251 | }
 | 
|---|
| [afb47f] | 252 | 
 | 
|---|
| [88d586] | 253 | bool World::reserveAtomId(atomId_t id){
 | 
|---|
 | 254 |   if(id>=currAtomId ){
 | 
|---|
| [dc11c9] | 255 |     range<atomId_t> newRange = makeRange(currAtomId,id);
 | 
|---|
 | 256 |     if(newRange.first<newRange.last){
 | 
|---|
| [127a8e] | 257 |       atomIdPool.insert(newRange);
 | 
|---|
| [88d586] | 258 |     }
 | 
|---|
 | 259 |     currAtomId=id+1;
 | 
|---|
| [127a8e] | 260 |     defragAtomIdPool();
 | 
|---|
| [88d586] | 261 |     return true;
 | 
|---|
 | 262 |   }
 | 
|---|
| [127a8e] | 263 |   // look for a range that matches the request
 | 
|---|
 | 264 |   for(atomIdPool_t::iterator iter=atomIdPool.begin();iter!=atomIdPool.end();++iter){
 | 
|---|
| [dc11c9] | 265 |     if(iter->isBefore(id)){
 | 
|---|
 | 266 |       // we have covered all available ranges... nothing to be found here
 | 
|---|
| [127a8e] | 267 |       break;
 | 
|---|
 | 268 |     }
 | 
|---|
 | 269 |     // no need to check first, since it has to be <=id, since otherwise we would have broken out
 | 
|---|
| [dc11c9] | 270 |     if(!iter->isBeyond(id)){
 | 
|---|
| [127a8e] | 271 |       // we found a matching range... get the id from this range
 | 
|---|
 | 272 | 
 | 
|---|
 | 273 |       // split up this range at the point of id
 | 
|---|
| [dc11c9] | 274 |       range<atomId_t> bottomRange = makeRange(iter->first,id);
 | 
|---|
 | 275 |       range<atomId_t> topRange = makeRange(id+1,iter->last);
 | 
|---|
| [127a8e] | 276 |       // remove this range
 | 
|---|
 | 277 |       atomIdPool.erase(iter);
 | 
|---|
| [dc11c9] | 278 |       if(bottomRange.first<bottomRange.last){
 | 
|---|
| [127a8e] | 279 |         atomIdPool.insert(bottomRange);
 | 
|---|
 | 280 |       }
 | 
|---|
| [dc11c9] | 281 |       if(topRange.first<topRange.last){
 | 
|---|
| [127a8e] | 282 |         atomIdPool.insert(topRange);
 | 
|---|
 | 283 |       }
 | 
|---|
 | 284 |       defragAtomIdPool();
 | 
|---|
 | 285 |       return true;
 | 
|---|
 | 286 |     }
 | 
|---|
| [88d586] | 287 |   }
 | 
|---|
| [127a8e] | 288 |   // this ID could not be reserved
 | 
|---|
 | 289 |   return false;
 | 
|---|
 | 290 | }
 | 
|---|
 | 291 | 
 | 
|---|
 | 292 | void World::defragAtomIdPool(){
 | 
|---|
 | 293 |   // check if the situation is bad enough to make defragging neccessary
 | 
|---|
 | 294 |   if((numAtomDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
 | 
|---|
 | 295 |      (atomIdPool.size()<lastAtomPoolSize+MAX_POOL_FRAGMENTATION)){
 | 
|---|
 | 296 |     ++numAtomDefragSkips;
 | 
|---|
 | 297 |     return;
 | 
|---|
 | 298 |   }
 | 
|---|
 | 299 |   for(atomIdPool_t::iterator iter = atomIdPool.begin();iter!=atomIdPool.end();){
 | 
|---|
 | 300 |     // see if this range is adjacent to the next one
 | 
|---|
 | 301 |     atomIdPool_t::iterator next = iter;
 | 
|---|
 | 302 |     next++;
 | 
|---|
| [dc11c9] | 303 |     if(next!=atomIdPool.end() && (next->first==iter->last)){
 | 
|---|
| [127a8e] | 304 |       // merge the two ranges
 | 
|---|
| [dc11c9] | 305 |       range<atomId_t> newRange = makeRange(iter->first,next->last);
 | 
|---|
| [127a8e] | 306 |       atomIdPool.erase(iter);
 | 
|---|
 | 307 |       atomIdPool.erase(next);
 | 
|---|
 | 308 |       pair<atomIdPool_t::iterator,bool> res = atomIdPool.insert(newRange);
 | 
|---|
 | 309 |       ASSERT(res.second,"Id-Pool was confused");
 | 
|---|
 | 310 |       iter=res.first;
 | 
|---|
 | 311 |       continue;
 | 
|---|
 | 312 |     }
 | 
|---|
 | 313 |     ++iter;
 | 
|---|
 | 314 |   }
 | 
|---|
 | 315 |   if(!atomIdPool.empty()){
 | 
|---|
 | 316 |     // check if the last range is at the border
 | 
|---|
 | 317 |     atomIdPool_t::iterator iter = atomIdPool.end();
 | 
|---|
 | 318 |     iter--;
 | 
|---|
| [dc11c9] | 319 |     if(iter->last==currAtomId){
 | 
|---|
| [127a8e] | 320 |       currAtomId=iter->first;
 | 
|---|
 | 321 |       atomIdPool.erase(iter);
 | 
|---|
 | 322 |     }
 | 
|---|
| [88d586] | 323 |   }
 | 
|---|
| [127a8e] | 324 |   lastAtomPoolSize=atomIdPool.size();
 | 
|---|
 | 325 |   numAtomDefragSkips=0;
 | 
|---|
| [88d586] | 326 | }
 | 
|---|
| [57adc7] | 327 | 
 | 
|---|
 | 328 | // Molecules
 | 
|---|
 | 329 | 
 | 
|---|
| [127a8e] | 330 | moleculeId_t World::getNextMoleculeId(){
 | 
|---|
 | 331 |   // try to find an Id in the pool;
 | 
|---|
 | 332 |   if(!moleculeIdPool.empty()){
 | 
|---|
 | 333 |     moleculeIdPool_t::iterator iter=moleculeIdPool.begin();
 | 
|---|
 | 334 |     moleculeId_t id = iter->first;
 | 
|---|
| [dc11c9] | 335 |     range<moleculeId_t> newRange = makeRange(id+1,iter->last);
 | 
|---|
| [127a8e] | 336 |     // we wont use this iterator anymore, so we don't care about invalidating
 | 
|---|
 | 337 |     moleculeIdPool.erase(iter);
 | 
|---|
| [dc11c9] | 338 |     if(newRange.first<newRange.last){
 | 
|---|
| [127a8e] | 339 |       moleculeIdPool.insert(newRange);
 | 
|---|
 | 340 |     }
 | 
|---|
 | 341 |     return id;
 | 
|---|
 | 342 |   }
 | 
|---|
 | 343 |   // Nothing in the pool... we are out of luck
 | 
|---|
 | 344 |   return currMoleculeId++;
 | 
|---|
 | 345 | }
 | 
|---|
 | 346 | 
 | 
|---|
 | 347 | void World::releaseMoleculeId(moleculeId_t id){
 | 
|---|
| [dc11c9] | 348 |   moleculeIdPool.insert(makeRange(id,id+1));
 | 
|---|
| [127a8e] | 349 |   defragMoleculeIdPool();
 | 
|---|
 | 350 | }
 | 
|---|
 | 351 | 
 | 
|---|
 | 352 | bool World::reserveMoleculeId(moleculeId_t id){
 | 
|---|
 | 353 |   if(id>=currMoleculeId ){
 | 
|---|
| [dc11c9] | 354 |     range<moleculeId_t> newRange = makeRange(currMoleculeId,id);
 | 
|---|
 | 355 |     if(newRange.first<newRange.last){
 | 
|---|
| [127a8e] | 356 |       moleculeIdPool.insert(newRange);
 | 
|---|
 | 357 |     }
 | 
|---|
 | 358 |     currMoleculeId=id+1;
 | 
|---|
 | 359 |     defragMoleculeIdPool();
 | 
|---|
 | 360 |     return true;
 | 
|---|
 | 361 |   }
 | 
|---|
 | 362 |   // look for a range that matches the request
 | 
|---|
 | 363 |   for(moleculeIdPool_t::iterator iter=moleculeIdPool.begin();iter!=moleculeIdPool.end();++iter){
 | 
|---|
| [dc11c9] | 364 |     if(iter->isBefore(id)){
 | 
|---|
| [127a8e] | 365 |       // we have coverd all available ranges... nothing to be found here
 | 
|---|
 | 366 |       break;
 | 
|---|
 | 367 |     }
 | 
|---|
 | 368 |     // no need to check first, since it has to be <=id, since otherwise we would have broken out
 | 
|---|
| [dc11c9] | 369 |     if(!iter->isBeyond(id)){
 | 
|---|
| [127a8e] | 370 |       // we found a matching range... get the id from this range
 | 
|---|
 | 371 | 
 | 
|---|
 | 372 |       // split up this range at the point of id
 | 
|---|
| [dc11c9] | 373 |       range<moleculeId_t> bottomRange = makeRange(iter->first,id);
 | 
|---|
 | 374 |       range<moleculeId_t> topRange = makeRange(id+1,iter->last);
 | 
|---|
| [127a8e] | 375 |       // remove this range
 | 
|---|
 | 376 |       moleculeIdPool.erase(iter);
 | 
|---|
| [dc11c9] | 377 |       if(bottomRange.first<bottomRange.last){
 | 
|---|
| [127a8e] | 378 |         moleculeIdPool.insert(bottomRange);
 | 
|---|
 | 379 |       }
 | 
|---|
| [dc11c9] | 380 |       if(topRange.first<topRange.last){
 | 
|---|
| [127a8e] | 381 |         moleculeIdPool.insert(topRange);
 | 
|---|
 | 382 |       }
 | 
|---|
 | 383 |       defragMoleculeIdPool();
 | 
|---|
 | 384 |       return true;
 | 
|---|
 | 385 |     }
 | 
|---|
 | 386 |   }
 | 
|---|
 | 387 |   // this ID could not be reserved
 | 
|---|
 | 388 |   return false;
 | 
|---|
 | 389 | }
 | 
|---|
 | 390 | 
 | 
|---|
 | 391 | void World::defragMoleculeIdPool(){
 | 
|---|
 | 392 |   // check if the situation is bad enough to make defragging neccessary
 | 
|---|
 | 393 |   if((numMoleculeDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
 | 
|---|
 | 394 |      (moleculeIdPool.size()<lastMoleculePoolSize+MAX_POOL_FRAGMENTATION)){
 | 
|---|
 | 395 |     ++numMoleculeDefragSkips;
 | 
|---|
 | 396 |     return;
 | 
|---|
 | 397 |   }
 | 
|---|
 | 398 |   for(moleculeIdPool_t::iterator iter = moleculeIdPool.begin();iter!=moleculeIdPool.end();){
 | 
|---|
 | 399 |     // see if this range is adjacent to the next one
 | 
|---|
 | 400 |     moleculeIdPool_t::iterator next = iter;
 | 
|---|
 | 401 |     next++;
 | 
|---|
| [dc11c9] | 402 |     if(next!=moleculeIdPool.end() && (next->first==iter->last)){
 | 
|---|
| [127a8e] | 403 |       // merge the two ranges
 | 
|---|
| [dc11c9] | 404 |       range<moleculeId_t> newRange = makeRange(iter->first,next->last);
 | 
|---|
| [127a8e] | 405 |       moleculeIdPool.erase(iter);
 | 
|---|
 | 406 |       moleculeIdPool.erase(next);
 | 
|---|
 | 407 |       pair<moleculeIdPool_t::iterator,bool> res = moleculeIdPool.insert(newRange);
 | 
|---|
 | 408 |       ASSERT(res.second,"Id-Pool was confused");
 | 
|---|
 | 409 |       iter=res.first;
 | 
|---|
 | 410 |       continue;
 | 
|---|
 | 411 |     }
 | 
|---|
 | 412 |     ++iter;
 | 
|---|
 | 413 |   }
 | 
|---|
 | 414 |   if(!moleculeIdPool.empty()){
 | 
|---|
 | 415 |     // check if the last range is at the border
 | 
|---|
 | 416 |     moleculeIdPool_t::iterator iter = moleculeIdPool.end();
 | 
|---|
 | 417 |     iter--;
 | 
|---|
| [dc11c9] | 418 |     if(iter->last==currMoleculeId){
 | 
|---|
| [127a8e] | 419 |       currMoleculeId=iter->first;
 | 
|---|
 | 420 |       moleculeIdPool.erase(iter);
 | 
|---|
 | 421 |     }
 | 
|---|
 | 422 |   }
 | 
|---|
 | 423 |   lastMoleculePoolSize=moleculeIdPool.size();
 | 
|---|
 | 424 |   numMoleculeDefragSkips=0;
 | 
|---|
 | 425 | }
 | 
|---|
 | 426 | 
 | 
|---|
| [865a945] | 427 | /******************************* Iterators ********************************/
 | 
|---|
 | 428 | 
 | 
|---|
| [fa0b18] | 429 | // external parts with observers
 | 
|---|
 | 430 | 
 | 
|---|
| [6e97e5] | 431 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
 | 
|---|
 | 432 | 
 | 
|---|
| [fa0b18] | 433 | World::AtomIterator
 | 
|---|
 | 434 | World::getAtomIter(AtomDescriptor descr){
 | 
|---|
 | 435 |     return AtomIterator(descr,atoms);
 | 
|---|
 | 436 | }
 | 
|---|
| [865a945] | 437 | 
 | 
|---|
| [fa0b18] | 438 | World::AtomIterator
 | 
|---|
 | 439 | World::getAtomIter(){
 | 
|---|
 | 440 |     return AtomIterator(AllAtoms(),atoms);
 | 
|---|
| [865a945] | 441 | }
 | 
|---|
| [354859] | 442 | 
 | 
|---|
| [fa0b18] | 443 | World::AtomIterator
 | 
|---|
 | 444 | World::atomEnd(){
 | 
|---|
| [6e97e5] | 445 |   return AtomIterator(AllAtoms(),atoms,atoms.end());
 | 
|---|
| [7c4e29] | 446 | }
 | 
|---|
 | 447 | 
 | 
|---|
| [6e97e5] | 448 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
 | 
|---|
 | 449 | 
 | 
|---|
| [5d880e] | 450 | World::MoleculeIterator
 | 
|---|
 | 451 | World::getMoleculeIter(MoleculeDescriptor descr){
 | 
|---|
 | 452 |     return MoleculeIterator(descr,molecules);
 | 
|---|
 | 453 | }
 | 
|---|
 | 454 | 
 | 
|---|
 | 455 | World::MoleculeIterator
 | 
|---|
 | 456 | World::getMoleculeIter(){
 | 
|---|
 | 457 |     return MoleculeIterator(AllMolecules(),molecules);
 | 
|---|
| [1c51c8] | 458 | }
 | 
|---|
 | 459 | 
 | 
|---|
| [5d880e] | 460 | World::MoleculeIterator
 | 
|---|
 | 461 | World::moleculeEnd(){
 | 
|---|
| [6e97e5] | 462 |   return MoleculeIterator(AllMolecules(),molecules,molecules.end());
 | 
|---|
| [1c51c8] | 463 | }
 | 
|---|
 | 464 | 
 | 
|---|
| [fa0b18] | 465 | // Internal parts, without observers
 | 
|---|
 | 466 | 
 | 
|---|
 | 467 | // Build the AtomIterator from template
 | 
|---|
 | 468 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet::set_t,AtomDescriptor);
 | 
|---|
 | 469 | 
 | 
|---|
 | 470 | 
 | 
|---|
 | 471 | World::internal_AtomIterator
 | 
|---|
 | 472 | World::getAtomIter_internal(AtomDescriptor descr){
 | 
|---|
 | 473 |   return internal_AtomIterator(descr,atoms.getContent());
 | 
|---|
 | 474 | }
 | 
|---|
 | 475 | 
 | 
|---|
 | 476 | World::internal_AtomIterator
 | 
|---|
 | 477 | World::atomEnd_internal(){
 | 
|---|
 | 478 |   return internal_AtomIterator(AllAtoms(),atoms.getContent(),atoms.end_internal());
 | 
|---|
 | 479 | }
 | 
|---|
 | 480 | 
 | 
|---|
| [6e97e5] | 481 | // build the MoleculeIterator from template
 | 
|---|
| [e3d865] | 482 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet::set_t,MoleculeDescriptor);
 | 
|---|
| [6e97e5] | 483 | 
 | 
|---|
| [e3d865] | 484 | World::internal_MoleculeIterator World::getMoleculeIter_internal(MoleculeDescriptor descr){
 | 
|---|
 | 485 |   return internal_MoleculeIterator(descr,molecules.getContent());
 | 
|---|
| [1c51c8] | 486 | }
 | 
|---|
 | 487 | 
 | 
|---|
| [e3d865] | 488 | World::internal_MoleculeIterator World::moleculeEnd_internal(){
 | 
|---|
 | 489 |   return internal_MoleculeIterator(AllMolecules(),molecules.getContent(),molecules.end_internal());
 | 
|---|
| [1c51c8] | 490 | }
 | 
|---|
 | 491 | 
 | 
|---|
| [90c4280] | 492 | /************************** Selection of Atoms and molecules ******************/
 | 
|---|
 | 493 | 
 | 
|---|
 | 494 | // Atoms
 | 
|---|
 | 495 | 
 | 
|---|
 | 496 | void World::clearAtomSelection(){
 | 
|---|
 | 497 |   selectedAtoms.clear();
 | 
|---|
 | 498 | }
 | 
|---|
 | 499 | 
 | 
|---|
 | 500 | void World::selectAtom(atom *atom){
 | 
|---|
 | 501 |   ASSERT(atom,"Invalid pointer in selection of atom");
 | 
|---|
 | 502 |   selectedAtoms[atom->getId()]=atom;
 | 
|---|
 | 503 | }
 | 
|---|
 | 504 | 
 | 
|---|
 | 505 | void World::selectAtom(atomId_t id){
 | 
|---|
 | 506 |   ASSERT(atoms.count(id),"Atom Id selected that was not in the world");
 | 
|---|
 | 507 |   selectedAtoms[id]=atoms[id];
 | 
|---|
 | 508 | }
 | 
|---|
 | 509 | 
 | 
|---|
 | 510 | void World::selectAllAtoms(AtomDescriptor descr){
 | 
|---|
 | 511 |   internal_AtomIterator begin = getAtomIter_internal(descr);
 | 
|---|
 | 512 |   internal_AtomIterator end = atomEnd_internal();
 | 
|---|
 | 513 |   void (World::*func)(atom*) = &World::selectAtom; // needed for type resolution of overloaded function
 | 
|---|
 | 514 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
 | 515 | }
 | 
|---|
 | 516 | 
 | 
|---|
 | 517 | void World::selectAtomsOfMolecule(molecule *_mol){
 | 
|---|
 | 518 |   ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
 | 
|---|
 | 519 |   // need to make it const to get the fast iterators
 | 
|---|
 | 520 |   const molecule *mol = _mol;
 | 
|---|
 | 521 |   void (World::*func)(atom*) = &World::selectAtom; // needed for type resolution of overloaded function
 | 
|---|
 | 522 |   for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
 | 523 | }
 | 
|---|
 | 524 | 
 | 
|---|
 | 525 | void World::selectAtomsOfMolecule(moleculeId_t id){
 | 
|---|
 | 526 |   ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
 | 
|---|
 | 527 |   selectAtomsOfMolecule(molecules[id]);
 | 
|---|
 | 528 | }
 | 
|---|
 | 529 | 
 | 
|---|
| [61d655e] | 530 | void World::unselectAtom(atom *atom){
 | 
|---|
 | 531 |   ASSERT(atom,"Invalid pointer in unselection of atom");
 | 
|---|
 | 532 |   unselectAtom(atom->getId());
 | 
|---|
 | 533 | }
 | 
|---|
 | 534 | 
 | 
|---|
 | 535 | void World::unselectAtom(atomId_t id){
 | 
|---|
 | 536 |   ASSERT(atoms.count(id),"Atom Id unselected that was not in the world");
 | 
|---|
 | 537 |   selectedAtoms.erase(id);
 | 
|---|
 | 538 | }
 | 
|---|
 | 539 | 
 | 
|---|
 | 540 | void World::unselectAllAtoms(AtomDescriptor descr){
 | 
|---|
 | 541 |   internal_AtomIterator begin = getAtomIter_internal(descr);
 | 
|---|
 | 542 |   internal_AtomIterator end = atomEnd_internal();
 | 
|---|
 | 543 |   void (World::*func)(atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
 | 
|---|
 | 544 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
 | 
|---|
 | 545 | }
 | 
|---|
 | 546 | 
 | 
|---|
 | 547 | void World::unselectAtomsOfMolecule(molecule *_mol){
 | 
|---|
 | 548 |   ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
 | 
|---|
 | 549 |   // need to make it const to get the fast iterators
 | 
|---|
 | 550 |   const molecule *mol = _mol;
 | 
|---|
 | 551 |   void (World::*func)(atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
 | 
|---|
 | 552 |   for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is unsselect... see above
 | 
|---|
 | 553 | }
 | 
|---|
 | 554 | 
 | 
|---|
 | 555 | void World::unselectAtomsOfMolecule(moleculeId_t id){
 | 
|---|
 | 556 |   ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
 | 
|---|
 | 557 |   unselectAtomsOfMolecule(molecules[id]);
 | 
|---|
 | 558 | }
 | 
|---|
 | 559 | 
 | 
|---|
| [90c4280] | 560 | // Molecules
 | 
|---|
 | 561 | 
 | 
|---|
 | 562 | void World::clearMoleculeSelection(){
 | 
|---|
 | 563 |   selectedMolecules.clear();
 | 
|---|
 | 564 | }
 | 
|---|
 | 565 | 
 | 
|---|
 | 566 | void World::selectMolecule(molecule *mol){
 | 
|---|
 | 567 |   ASSERT(mol,"Invalid pointer to molecule in selection");
 | 
|---|
 | 568 |   selectedMolecules[mol->getId()]=mol;
 | 
|---|
 | 569 | }
 | 
|---|
 | 570 | 
 | 
|---|
 | 571 | void World::selectMolecule(moleculeId_t id){
 | 
|---|
 | 572 |   ASSERT(molecules.count(id),"Molecule Id selected that was not in the world");
 | 
|---|
 | 573 |   selectedMolecules[id]=molecules[id];
 | 
|---|
 | 574 | }
 | 
|---|
 | 575 | 
 | 
|---|
 | 576 | void World::selectAllMoleculess(MoleculeDescriptor descr){
 | 
|---|
 | 577 |   internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
 | 
|---|
 | 578 |   internal_MoleculeIterator end = moleculeEnd_internal();
 | 
|---|
 | 579 |   void (World::*func)(molecule*) = &World::selectMolecule; // needed for type resolution of overloaded function
 | 
|---|
 | 580 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
 | 581 | }
 | 
|---|
 | 582 | 
 | 
|---|
 | 583 | void World::selectMoleculeOfAtom(atom *atom){
 | 
|---|
 | 584 |   ASSERT(atom,"Invalid atom pointer in selection of MoleculeOfAtom");
 | 
|---|
 | 585 |   molecule *mol=atom->getMolecule();
 | 
|---|
 | 586 |   // the atom might not be part of a molecule
 | 
|---|
 | 587 |   if(mol){
 | 
|---|
 | 588 |     selectMolecule(mol);
 | 
|---|
 | 589 |   }
 | 
|---|
 | 590 | }
 | 
|---|
 | 591 | 
 | 
|---|
 | 592 | void World::selectMoleculeOfAtom(atomId_t id){
 | 
|---|
 | 593 |   ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
 | 
|---|
 | 594 |   selectMoleculeOfAtom(atoms[id]);
 | 
|---|
 | 595 | }
 | 
|---|
 | 596 | 
 | 
|---|
| [61d655e] | 597 | void World::unselectMolecule(molecule *mol){
 | 
|---|
 | 598 |   ASSERT(mol,"invalid pointer in unselection of molecule");
 | 
|---|
 | 599 |   unselectMolecule(mol->getId());
 | 
|---|
 | 600 | }
 | 
|---|
 | 601 | 
 | 
|---|
 | 602 | void World::unselectMolecule(moleculeId_t id){
 | 
|---|
 | 603 |   ASSERT(molecules.count(id),"No such molecule with ID in unselection");
 | 
|---|
 | 604 |   selectedMolecules.erase(id);
 | 
|---|
 | 605 | }
 | 
|---|
 | 606 | 
 | 
|---|
 | 607 | void World::unselectAllMoleculess(MoleculeDescriptor descr){
 | 
|---|
 | 608 |   internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
 | 
|---|
 | 609 |   internal_MoleculeIterator end = moleculeEnd_internal();
 | 
|---|
 | 610 |   void (World::*func)(molecule*) = &World::unselectMolecule; // needed for type resolution of overloaded function
 | 
|---|
 | 611 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
 | 
|---|
 | 612 | }
 | 
|---|
 | 613 | 
 | 
|---|
 | 614 | void World::unselectMoleculeOfAtom(atom *atom){
 | 
|---|
 | 615 |   ASSERT(atom,"Invalid atom pointer in selection of MoleculeOfAtom");
 | 
|---|
 | 616 |   molecule *mol=atom->getMolecule();
 | 
|---|
 | 617 |   // the atom might not be part of a molecule
 | 
|---|
 | 618 |   if(mol){
 | 
|---|
 | 619 |     unselectMolecule(mol);
 | 
|---|
 | 620 |   }
 | 
|---|
 | 621 | }
 | 
|---|
 | 622 | 
 | 
|---|
 | 623 | void World::unselectMoleculeOfAtom(atomId_t id){
 | 
|---|
 | 624 |   ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
 | 
|---|
 | 625 |   unselectMoleculeOfAtom(atoms[id]);
 | 
|---|
 | 626 | }
 | 
|---|
 | 627 | 
 | 
|---|
| [3839e5] | 628 | /******************* Iterators over Selection *****************************/
 | 
|---|
 | 629 | World::AtomSelectionIterator World::beginAtomSelection(){
 | 
|---|
 | 630 |   return selectedAtoms.begin();
 | 
|---|
 | 631 | }
 | 
|---|
 | 632 | 
 | 
|---|
 | 633 | World::AtomSelectionIterator World::endAtomSelection(){
 | 
|---|
 | 634 |   return selectedAtoms.end();
 | 
|---|
 | 635 | }
 | 
|---|
 | 636 | 
 | 
|---|
 | 637 | 
 | 
|---|
 | 638 | World::MoleculeSelectionIterator World::beginMoleculeSelection(){
 | 
|---|
 | 639 |   return selectedMolecules.begin();
 | 
|---|
 | 640 | }
 | 
|---|
 | 641 | 
 | 
|---|
 | 642 | World::MoleculeSelectionIterator World::endMoleculeSelection(){
 | 
|---|
 | 643 |   return selectedMolecules.end();
 | 
|---|
 | 644 | }
 | 
|---|
 | 645 | 
 | 
|---|
| [5d1611] | 646 | /******************************* Singleton Stuff **************************/
 | 
|---|
 | 647 | 
 | 
|---|
| [7a1ce5] | 648 | World::World() :
 | 
|---|
| [cd5047] | 649 |     Observable("World"),
 | 
|---|
| [354859] | 650 |     periode(new periodentafel),
 | 
|---|
| [8e1f7af] | 651 |     configuration(new config),
 | 
|---|
| [43dad6] | 652 |     Thermostats(new ThermoStatContainer),
 | 
|---|
| [e4b5de] | 653 |     ExitFlag(0),
 | 
|---|
| [fa0b18] | 654 |     atoms(this),
 | 
|---|
| [90c4280] | 655 |     selectedAtoms(this),
 | 
|---|
| [24a5e0] | 656 |     currAtomId(0),
 | 
|---|
| [127a8e] | 657 |     lastAtomPoolSize(0),
 | 
|---|
 | 658 |     numAtomDefragSkips(0),
 | 
|---|
| [51be2a] | 659 |     molecules(this),
 | 
|---|
| [90c4280] | 660 |     selectedMolecules(this),
 | 
|---|
| [24a5e0] | 661 |     currMoleculeId(0),
 | 
|---|
 | 662 |     molecules_deprecated(new MoleculeListClass(this))
 | 
|---|
| [7dad10] | 663 | {
 | 
|---|
| [84c494] | 664 |   cell_size = new Box;
 | 
|---|
 | 665 |   Matrix domain;
 | 
|---|
 | 666 |   domain.at(0,0) = 20;
 | 
|---|
 | 667 |   domain.at(1,1) = 20;
 | 
|---|
 | 668 |   domain.at(2,2) = 20;
 | 
|---|
 | 669 |   cell_size->setM(domain);
 | 
|---|
| [387b36] | 670 |   defaultName = "none";
 | 
|---|
| [7dad10] | 671 |   molecules_deprecated->signOn(this);
 | 
|---|
 | 672 | }
 | 
|---|
| [5d1611] | 673 | 
 | 
|---|
 | 674 | World::~World()
 | 
|---|
| [354859] | 675 | {
 | 
|---|
| [028c2e] | 676 |   molecules_deprecated->signOff(this);
 | 
|---|
| [84c494] | 677 |   delete cell_size;
 | 
|---|
| [46d958] | 678 |   delete molecules_deprecated;
 | 
|---|
| [354859] | 679 |   delete periode;
 | 
|---|
| [8e1f7af] | 680 |   delete configuration;
 | 
|---|
| [43dad6] | 681 |   delete Thermostats;
 | 
|---|
| [cbc5fb] | 682 |   MoleculeSet::iterator molIter;
 | 
|---|
 | 683 |   for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
 | 
|---|
 | 684 |     DeleteMolecule((*molIter).second);
 | 
|---|
 | 685 |   }
 | 
|---|
 | 686 |   molecules.clear();
 | 
|---|
 | 687 |   AtomSet::iterator atIter;
 | 
|---|
 | 688 |   for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
 | 
|---|
 | 689 |     DeleteAtom((*atIter).second);
 | 
|---|
| [46d958] | 690 |   }
 | 
|---|
 | 691 |   atoms.clear();
 | 
|---|
| [354859] | 692 | }
 | 
|---|
| [5d1611] | 693 | 
 | 
|---|
| [23b547] | 694 | // Explicit instantiation of the singleton mechanism at this point
 | 
|---|
| [5d1611] | 695 | 
 | 
|---|
| [23b547] | 696 | CONSTRUCT_SINGLETON(World)
 | 
|---|
| [5d1611] | 697 | 
 | 
|---|
 | 698 | /******************************* deprecated Legacy Stuff ***********************/
 | 
|---|
 | 699 | 
 | 
|---|
| [354859] | 700 | MoleculeListClass *&World::getMolecules() {
 | 
|---|
 | 701 |   return molecules_deprecated;
 | 
|---|
| [5d1611] | 702 | }
 | 
|---|