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