| 1 | /* | 
|---|
| 2 | * World.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Feb 3, 2010 | 
|---|
| 5 | *      Author: crueger | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Helpers/MemDebug.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | #include "World.hpp" | 
|---|
| 11 |  | 
|---|
| 12 | #include "atom.hpp" | 
|---|
| 13 | #include "config.hpp" | 
|---|
| 14 | #include "molecule.hpp" | 
|---|
| 15 | #include "periodentafel.hpp" | 
|---|
| 16 | #include "ThermoStatContainer.hpp" | 
|---|
| 17 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
| 18 | #include "Descriptors/AtomDescriptor_impl.hpp" | 
|---|
| 19 | #include "Descriptors/MoleculeDescriptor.hpp" | 
|---|
| 20 | #include "Descriptors/MoleculeDescriptor_impl.hpp" | 
|---|
| 21 | #include "Descriptors/SelectiveIterator_impl.hpp" | 
|---|
| 22 | #include "Actions/ManipulateAtomsProcess.hpp" | 
|---|
| 23 | #include "Helpers/Assert.hpp" | 
|---|
| 24 | #include "Box.hpp" | 
|---|
| 25 | #include "Matrix.hpp" | 
|---|
| 26 |  | 
|---|
| 27 | #include "Patterns/Singleton_impl.hpp" | 
|---|
| 28 |  | 
|---|
| 29 | using namespace std; | 
|---|
| 30 |  | 
|---|
| 31 | /******************************* getter and setter ************************/ | 
|---|
| 32 | periodentafel *&World::getPeriode(){ | 
|---|
| 33 | return periode; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | config *&World::getConfig(){ | 
|---|
| 37 | return configuration; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | // Atoms | 
|---|
| 41 |  | 
|---|
| 42 | atom* World::getAtom(AtomDescriptor descriptor){ | 
|---|
| 43 | return descriptor.find(); | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){ | 
|---|
| 47 | return descriptor.findAll(); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | vector<atom*> World::getAllAtoms(){ | 
|---|
| 51 | return getAllAtoms(AllAtoms()); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | int World::numAtoms(){ | 
|---|
| 55 | return atoms.size(); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | // Molecules | 
|---|
| 59 |  | 
|---|
| 60 | molecule *World::getMolecule(MoleculeDescriptor descriptor){ | 
|---|
| 61 | return descriptor.find(); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){ | 
|---|
| 65 | return descriptor.findAll(); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | std::vector<molecule*> World::getAllMolecules(){ | 
|---|
| 69 | return getAllMolecules(AllMolecules()); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | int World::numMolecules(){ | 
|---|
| 73 | return molecules_deprecated->ListOfMolecules.size(); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | // system | 
|---|
| 77 |  | 
|---|
| 78 | Box& World::getDomain() { | 
|---|
| 79 | return *cell_size; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | void World::setDomain(const Matrix &mat){ | 
|---|
| 83 | *cell_size = mat; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | void World::setDomain(double * matrix) | 
|---|
| 87 | { | 
|---|
| 88 | Matrix M = ReturnFullMatrixforSymmetric(matrix); | 
|---|
| 89 | cell_size->setM(M); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | std::string World::getDefaultName() { | 
|---|
| 93 | return defaultName; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | void World::setDefaultName(std::string name) | 
|---|
| 97 | { | 
|---|
| 98 | defaultName = name; | 
|---|
| 99 | }; | 
|---|
| 100 |  | 
|---|
| 101 | class ThermoStatContainer * World::getThermostats() | 
|---|
| 102 | { | 
|---|
| 103 | return Thermostats; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 | int World::getExitFlag() { | 
|---|
| 108 | return ExitFlag; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | void World::setExitFlag(int flag) { | 
|---|
| 112 | if (ExitFlag < flag) | 
|---|
| 113 | ExitFlag = flag; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | /******************** Methods to change World state *********************/ | 
|---|
| 117 |  | 
|---|
| 118 | molecule* World::createMolecule(){ | 
|---|
| 119 | OBSERVE; | 
|---|
| 120 | molecule *mol = NULL; | 
|---|
| 121 | mol = NewMolecule(); | 
|---|
| 122 | ASSERT(!molecules.count(currMoleculeId),"currMoleculeId did not specify an unused ID"); | 
|---|
| 123 | mol->setId(currMoleculeId++); | 
|---|
| 124 | // store the molecule by ID | 
|---|
| 125 | molecules[mol->getId()] = mol; | 
|---|
| 126 | mol->signOn(this); | 
|---|
| 127 | return mol; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | void World::destroyMolecule(molecule* mol){ | 
|---|
| 131 | OBSERVE; | 
|---|
| 132 | destroyMolecule(mol->getId()); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | void World::destroyMolecule(moleculeId_t id){ | 
|---|
| 136 | OBSERVE; | 
|---|
| 137 | molecule *mol = molecules[id]; | 
|---|
| 138 | ASSERT(mol,"Molecule id that was meant to be destroyed did not exist"); | 
|---|
| 139 | DeleteMolecule(mol); | 
|---|
| 140 | molecules.erase(id); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | atom *World::createAtom(){ | 
|---|
| 144 | OBSERVE; | 
|---|
| 145 | atomId_t id = getNextAtomId(); | 
|---|
| 146 | atom *res = NewAtom(id); | 
|---|
| 147 | res->setWorld(this); | 
|---|
| 148 | // store the atom by ID | 
|---|
| 149 | atoms[res->getId()] = res; | 
|---|
| 150 | return res; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 |  | 
|---|
| 154 | int World::registerAtom(atom *atom){ | 
|---|
| 155 | OBSERVE; | 
|---|
| 156 | atomId_t id = getNextAtomId(); | 
|---|
| 157 | atom->setId(id); | 
|---|
| 158 | atom->setWorld(this); | 
|---|
| 159 | atoms[atom->getId()] = atom; | 
|---|
| 160 | return atom->getId(); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void World::destroyAtom(atom* atom){ | 
|---|
| 164 | OBSERVE; | 
|---|
| 165 | int id = atom->getId(); | 
|---|
| 166 | destroyAtom(id); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | void World::destroyAtom(atomId_t id) { | 
|---|
| 170 | OBSERVE; | 
|---|
| 171 | atom *atom = atoms[id]; | 
|---|
| 172 | ASSERT(atom,"Atom ID that was meant to be destroyed did not exist"); | 
|---|
| 173 | DeleteAtom(atom); | 
|---|
| 174 | atoms.erase(id); | 
|---|
| 175 | releaseAtomId(id); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){ | 
|---|
| 179 | OBSERVE; | 
|---|
| 180 | // in case this call did not originate from inside the atom, we redirect it, | 
|---|
| 181 | // to also let it know that it has changed | 
|---|
| 182 | if(!target){ | 
|---|
| 183 | target = atoms[oldId]; | 
|---|
| 184 | ASSERT(target,"Atom with that ID not found"); | 
|---|
| 185 | return target->changeId(newId); | 
|---|
| 186 | } | 
|---|
| 187 | else{ | 
|---|
| 188 | if(reserveAtomId(newId)){ | 
|---|
| 189 | atoms.erase(oldId); | 
|---|
| 190 | atoms.insert(pair<atomId_t,atom*>(newId,target)); | 
|---|
| 191 | return true; | 
|---|
| 192 | } | 
|---|
| 193 | else{ | 
|---|
| 194 | return false; | 
|---|
| 195 | } | 
|---|
| 196 | } | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){ | 
|---|
| 200 | return new ManipulateAtomsProcess(op, descr,name,true); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){ | 
|---|
| 204 | return manipulateAtoms(op,name,AllAtoms()); | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | /********************* Internal Change methods for double Callback and Observer mechanism ********/ | 
|---|
| 208 |  | 
|---|
| 209 | void World::doManipulate(ManipulateAtomsProcess *proc){ | 
|---|
| 210 | proc->signOn(this); | 
|---|
| 211 | { | 
|---|
| 212 | OBSERVE; | 
|---|
| 213 | proc->doManipulate(this); | 
|---|
| 214 | } | 
|---|
| 215 | proc->signOff(this); | 
|---|
| 216 | } | 
|---|
| 217 | /******************************* IDManagement *****************************/ | 
|---|
| 218 |  | 
|---|
| 219 | // Atoms | 
|---|
| 220 |  | 
|---|
| 221 | atomId_t World::getNextAtomId(){ | 
|---|
| 222 | // see if we can reuse some Id | 
|---|
| 223 | if(atomIdPool.empty()){ | 
|---|
| 224 | return currAtomId++; | 
|---|
| 225 | } | 
|---|
| 226 | else{ | 
|---|
| 227 | // we give out the first ID from the pool | 
|---|
| 228 | atomId_t id = *(atomIdPool.begin()); | 
|---|
| 229 | atomIdPool.erase(id); | 
|---|
| 230 | return id; | 
|---|
| 231 | } | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | void World::releaseAtomId(atomId_t id){ | 
|---|
| 235 | atomIdPool.insert(id); | 
|---|
| 236 | // defragmentation of the pool | 
|---|
| 237 | set<atomId_t>::reverse_iterator iter; | 
|---|
| 238 | // go through all Ids in the pool that lie immediately below the border | 
|---|
| 239 | while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){ | 
|---|
| 240 | atomIdPool.erase(--currAtomId); | 
|---|
| 241 | } | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | bool World::reserveAtomId(atomId_t id){ | 
|---|
| 245 | if(id>=currAtomId ){ | 
|---|
| 246 | // add all ids between the new one and current border as available | 
|---|
| 247 | for(atomId_t pos=currAtomId; pos<id; ++pos){ | 
|---|
| 248 | atomIdPool.insert(pos); | 
|---|
| 249 | } | 
|---|
| 250 | currAtomId=id+1; | 
|---|
| 251 | return true; | 
|---|
| 252 | } | 
|---|
| 253 | else if(atomIdPool.count(id)){ | 
|---|
| 254 | atomIdPool.erase(id); | 
|---|
| 255 | return true; | 
|---|
| 256 | } | 
|---|
| 257 | else{ | 
|---|
| 258 | // this ID could not be reserved | 
|---|
| 259 | return false; | 
|---|
| 260 | } | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | // Molecules | 
|---|
| 264 |  | 
|---|
| 265 | /******************************* Iterators ********************************/ | 
|---|
| 266 |  | 
|---|
| 267 | // Build the AtomIterator from template | 
|---|
| 268 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor); | 
|---|
| 269 |  | 
|---|
| 270 |  | 
|---|
| 271 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){ | 
|---|
| 272 | return AtomIterator(descr,atoms); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | World::AtomIterator World::atomEnd(){ | 
|---|
| 276 | return AtomIterator(AllAtoms(),atoms,atoms.end()); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | // build the MoleculeIterator from template | 
|---|
| 280 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor); | 
|---|
| 281 |  | 
|---|
| 282 | World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){ | 
|---|
| 283 | return MoleculeIterator(descr,molecules); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | World::MoleculeIterator World::moleculeEnd(){ | 
|---|
| 287 | return MoleculeIterator(AllMolecules(),molecules,molecules.end()); | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | /******************************* Singleton Stuff **************************/ | 
|---|
| 291 |  | 
|---|
| 292 | World::World() : | 
|---|
| 293 | Observable("World"), | 
|---|
| 294 | periode(new periodentafel), | 
|---|
| 295 | configuration(new config), | 
|---|
| 296 | Thermostats(new ThermoStatContainer), | 
|---|
| 297 | ExitFlag(0), | 
|---|
| 298 | atoms(), | 
|---|
| 299 | currAtomId(0), | 
|---|
| 300 | molecules(), | 
|---|
| 301 | currMoleculeId(0), | 
|---|
| 302 | molecules_deprecated(new MoleculeListClass(this)) | 
|---|
| 303 | { | 
|---|
| 304 | cell_size = new Box; | 
|---|
| 305 | Matrix domain; | 
|---|
| 306 | domain.at(0,0) = 20; | 
|---|
| 307 | domain.at(1,1) = 20; | 
|---|
| 308 | domain.at(2,2) = 20; | 
|---|
| 309 | cell_size->setM(domain); | 
|---|
| 310 | defaultName = "none"; | 
|---|
| 311 | molecules_deprecated->signOn(this); | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | World::~World() | 
|---|
| 315 | { | 
|---|
| 316 | molecules_deprecated->signOff(this); | 
|---|
| 317 | delete cell_size; | 
|---|
| 318 | delete molecules_deprecated; | 
|---|
| 319 | delete periode; | 
|---|
| 320 | delete configuration; | 
|---|
| 321 | delete Thermostats; | 
|---|
| 322 | MoleculeSet::iterator molIter; | 
|---|
| 323 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){ | 
|---|
| 324 | DeleteMolecule((*molIter).second); | 
|---|
| 325 | } | 
|---|
| 326 | molecules.clear(); | 
|---|
| 327 | AtomSet::iterator atIter; | 
|---|
| 328 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){ | 
|---|
| 329 | DeleteAtom((*atIter).second); | 
|---|
| 330 | } | 
|---|
| 331 | atoms.clear(); | 
|---|
| 332 | } | 
|---|
| 333 |  | 
|---|
| 334 | // Explicit instantiation of the singleton mechanism at this point | 
|---|
| 335 |  | 
|---|
| 336 | CONSTRUCT_SINGLETON(World) | 
|---|
| 337 |  | 
|---|
| 338 | /******************************* deprecated Legacy Stuff ***********************/ | 
|---|
| 339 |  | 
|---|
| 340 | MoleculeListClass *&World::getMolecules() { | 
|---|
| 341 | return molecules_deprecated; | 
|---|
| 342 | } | 
|---|