| [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"
 | 
|---|
| [84c494] | 24 | #include "Box.hpp"
 | 
|---|
 | 25 | #include "Matrix.hpp"
 | 
|---|
| [d346b6] | 26 | 
 | 
|---|
| [23b547] | 27 | #include "Patterns/Singleton_impl.hpp"
 | 
|---|
 | 28 | 
 | 
|---|
| [d346b6] | 29 | using namespace std;
 | 
|---|
| [4d9c01] | 30 | 
 | 
|---|
| [5d1611] | 31 | /******************************* getter and setter ************************/
 | 
|---|
| [354859] | 32 | periodentafel *&World::getPeriode(){
 | 
|---|
| [5d1611] | 33 |   return periode;
 | 
|---|
 | 34 | }
 | 
|---|
 | 35 | 
 | 
|---|
| [8e1f7af] | 36 | config *&World::getConfig(){
 | 
|---|
 | 37 |   return configuration;
 | 
|---|
 | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
| [1c51c8] | 40 | // Atoms
 | 
|---|
 | 41 | 
 | 
|---|
| [7a1ce5] | 42 | atom* World::getAtom(AtomDescriptor descriptor){
 | 
|---|
| [fc1b24] | 43 |   return descriptor.find();
 | 
|---|
 | 44 | }
 | 
|---|
 | 45 | 
 | 
|---|
| [7a1ce5] | 46 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
 | 
|---|
| [fc1b24] | 47 |   return descriptor.findAll();
 | 
|---|
 | 48 | }
 | 
|---|
 | 49 | 
 | 
|---|
| [0e2a47] | 50 | vector<atom*> World::getAllAtoms(){
 | 
|---|
 | 51 |   return getAllAtoms(AllAtoms());
 | 
|---|
 | 52 | }
 | 
|---|
 | 53 | 
 | 
|---|
| [354859] | 54 | int World::numAtoms(){
 | 
|---|
 | 55 |   return atoms.size();
 | 
|---|
 | 56 | }
 | 
|---|
 | 57 | 
 | 
|---|
| [1c51c8] | 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 | 
 | 
|---|
| [97ebf8] | 68 | std::vector<molecule*> World::getAllMolecules(){
 | 
|---|
 | 69 |   return getAllMolecules(AllMolecules());
 | 
|---|
 | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
| [354859] | 72 | int World::numMolecules(){
 | 
|---|
 | 73 |   return molecules_deprecated->ListOfMolecules.size();
 | 
|---|
 | 74 | }
 | 
|---|
 | 75 | 
 | 
|---|
| [5f612ee] | 76 | // system
 | 
|---|
 | 77 | 
 | 
|---|
| [84c494] | 78 | Box& World::getDomain() {
 | 
|---|
 | 79 |   return *cell_size;
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | void World::setDomain(const Matrix &mat){
 | 
|---|
 | 83 |   *cell_size = mat;
 | 
|---|
| [5f612ee] | 84 | }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | void World::setDomain(double * matrix)
 | 
|---|
 | 87 | {
 | 
|---|
| [84c494] | 88 |   Matrix M = ReturnFullMatrixforSymmetric(matrix);
 | 
|---|
 | 89 |   cell_size->setM(M);
 | 
|---|
| [5f612ee] | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
| [387b36] | 92 | std::string World::getDefaultName() {
 | 
|---|
| [5f612ee] | 93 |   return defaultName;
 | 
|---|
 | 94 | }
 | 
|---|
 | 95 | 
 | 
|---|
| [387b36] | 96 | void World::setDefaultName(std::string name)
 | 
|---|
| [5f612ee] | 97 | {
 | 
|---|
| [387b36] | 98 |   defaultName = name;
 | 
|---|
| [5f612ee] | 99 | };
 | 
|---|
 | 100 | 
 | 
|---|
| [43dad6] | 101 | class ThermoStatContainer * World::getThermostats()
 | 
|---|
 | 102 | {
 | 
|---|
 | 103 |   return Thermostats;
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | 
 | 
|---|
| [e4b5de] | 107 | int World::getExitFlag() {
 | 
|---|
 | 108 |   return ExitFlag;
 | 
|---|
 | 109 | }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | void World::setExitFlag(int flag) {
 | 
|---|
 | 112 |   if (ExitFlag < flag)
 | 
|---|
 | 113 |     ExitFlag = flag;
 | 
|---|
 | 114 | }
 | 
|---|
| [5f612ee] | 115 | 
 | 
|---|
| [afb47f] | 116 | /******************** Methods to change World state *********************/
 | 
|---|
 | 117 | 
 | 
|---|
| [354859] | 118 | molecule* World::createMolecule(){
 | 
|---|
 | 119 |   OBSERVE;
 | 
|---|
 | 120 |   molecule *mol = NULL;
 | 
|---|
| [cbc5fb] | 121 |   mol = NewMolecule();
 | 
|---|
| [6d574a] | 122 |   ASSERT(!molecules.count(currMoleculeId),"currMoleculeId did not specify an unused ID");
 | 
|---|
| [cbc5fb] | 123 |   mol->setId(currMoleculeId++);
 | 
|---|
| [244d26] | 124 |   // store the molecule by ID
 | 
|---|
| [cbc5fb] | 125 |   molecules[mol->getId()] = mol;
 | 
|---|
| [354859] | 126 |   mol->signOn(this);
 | 
|---|
 | 127 |   return mol;
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
| [cbc5fb] | 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];
 | 
|---|
| [6d574a] | 138 |   ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
 | 
|---|
| [cbc5fb] | 139 |   DeleteMolecule(mol);
 | 
|---|
 | 140 |   molecules.erase(id);
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | 
 | 
|---|
| [46d958] | 143 | atom *World::createAtom(){
 | 
|---|
 | 144 |   OBSERVE;
 | 
|---|
| [88d586] | 145 |   atomId_t id = getNextAtomId();
 | 
|---|
 | 146 |   atom *res = NewAtom(id);
 | 
|---|
| [46d958] | 147 |   res->setWorld(this);
 | 
|---|
| [244d26] | 148 |   // store the atom by ID
 | 
|---|
| [46d958] | 149 |   atoms[res->getId()] = res;
 | 
|---|
 | 150 |   return res;
 | 
|---|
 | 151 | }
 | 
|---|
 | 152 | 
 | 
|---|
| [5f612ee] | 153 | 
 | 
|---|
| [46d958] | 154 | int World::registerAtom(atom *atom){
 | 
|---|
 | 155 |   OBSERVE;
 | 
|---|
| [88d586] | 156 |   atomId_t id = getNextAtomId();
 | 
|---|
 | 157 |   atom->setId(id);
 | 
|---|
| [46d958] | 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 | 
 | 
|---|
| [cbc5fb] | 169 | void World::destroyAtom(atomId_t id) {
 | 
|---|
| [46d958] | 170 |   OBSERVE;
 | 
|---|
 | 171 |   atom *atom = atoms[id];
 | 
|---|
| [6d574a] | 172 |   ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
 | 
|---|
| [46d958] | 173 |   DeleteAtom(atom);
 | 
|---|
 | 174 |   atoms.erase(id);
 | 
|---|
| [88d586] | 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];
 | 
|---|
| [6d574a] | 184 |     ASSERT(target,"Atom with that ID not found");
 | 
|---|
| [88d586] | 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 |   }
 | 
|---|
| [46d958] | 197 | }
 | 
|---|
 | 198 | 
 | 
|---|
| [7c4e29] | 199 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
 | 
|---|
 | 200 |   return new ManipulateAtomsProcess(op, descr,name,true);
 | 
|---|
 | 201 | }
 | 
|---|
 | 202 | 
 | 
|---|
| [0e2a47] | 203 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
 | 
|---|
 | 204 |   return manipulateAtoms(op,name,AllAtoms());
 | 
|---|
 | 205 | }
 | 
|---|
 | 206 | 
 | 
|---|
| [afb47f] | 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 | }
 | 
|---|
| [88d586] | 217 | /******************************* IDManagement *****************************/
 | 
|---|
 | 218 | 
 | 
|---|
| [57adc7] | 219 | // Atoms
 | 
|---|
 | 220 | 
 | 
|---|
| [88d586] | 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);
 | 
|---|
| [23b547] | 230 |     return id;
 | 
|---|
| [88d586] | 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 | }
 | 
|---|
| [afb47f] | 243 | 
 | 
|---|
| [88d586] | 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 | }
 | 
|---|
| [57adc7] | 262 | 
 | 
|---|
 | 263 | // Molecules
 | 
|---|
 | 264 | 
 | 
|---|
| [865a945] | 265 | /******************************* Iterators ********************************/
 | 
|---|
 | 266 | 
 | 
|---|
| [6e97e5] | 267 | // Build the AtomIterator from template
 | 
|---|
 | 268 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
 | 
|---|
 | 269 | 
 | 
|---|
| [865a945] | 270 | 
 | 
|---|
 | 271 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){
 | 
|---|
| [6e97e5] | 272 |   return AtomIterator(descr,atoms);
 | 
|---|
| [865a945] | 273 | }
 | 
|---|
| [354859] | 274 | 
 | 
|---|
| [6e97e5] | 275 | World::AtomIterator World::atomEnd(){
 | 
|---|
 | 276 |   return AtomIterator(AllAtoms(),atoms,atoms.end());
 | 
|---|
| [7c4e29] | 277 | }
 | 
|---|
 | 278 | 
 | 
|---|
| [6e97e5] | 279 | // build the MoleculeIterator from template
 | 
|---|
 | 280 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
 | 
|---|
 | 281 | 
 | 
|---|
| [1c51c8] | 282 | World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
 | 
|---|
| [6e97e5] | 283 |   return MoleculeIterator(descr,molecules);
 | 
|---|
| [1c51c8] | 284 | }
 | 
|---|
 | 285 | 
 | 
|---|
| [6e97e5] | 286 | World::MoleculeIterator World::moleculeEnd(){
 | 
|---|
 | 287 |   return MoleculeIterator(AllMolecules(),molecules,molecules.end());
 | 
|---|
| [1c51c8] | 288 | }
 | 
|---|
 | 289 | 
 | 
|---|
| [5d1611] | 290 | /******************************* Singleton Stuff **************************/
 | 
|---|
 | 291 | 
 | 
|---|
| [7a1ce5] | 292 | World::World() :
 | 
|---|
| [cd5047] | 293 |     Observable("World"),
 | 
|---|
| [354859] | 294 |     periode(new periodentafel),
 | 
|---|
| [8e1f7af] | 295 |     configuration(new config),
 | 
|---|
| [43dad6] | 296 |     Thermostats(new ThermoStatContainer),
 | 
|---|
| [e4b5de] | 297 |     ExitFlag(0),
 | 
|---|
| [d2dbac0] | 298 |     atoms(),
 | 
|---|
| [24a5e0] | 299 |     currAtomId(0),
 | 
|---|
 | 300 |     molecules(),
 | 
|---|
 | 301 |     currMoleculeId(0),
 | 
|---|
 | 302 |     molecules_deprecated(new MoleculeListClass(this))
 | 
|---|
| [7dad10] | 303 | {
 | 
|---|
| [84c494] | 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);
 | 
|---|
| [387b36] | 310 |   defaultName = "none";
 | 
|---|
| [7dad10] | 311 |   molecules_deprecated->signOn(this);
 | 
|---|
 | 312 | }
 | 
|---|
| [5d1611] | 313 | 
 | 
|---|
 | 314 | World::~World()
 | 
|---|
| [354859] | 315 | {
 | 
|---|
| [028c2e] | 316 |   molecules_deprecated->signOff(this);
 | 
|---|
| [84c494] | 317 |   delete cell_size;
 | 
|---|
| [46d958] | 318 |   delete molecules_deprecated;
 | 
|---|
| [354859] | 319 |   delete periode;
 | 
|---|
| [8e1f7af] | 320 |   delete configuration;
 | 
|---|
| [43dad6] | 321 |   delete Thermostats;
 | 
|---|
| [cbc5fb] | 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);
 | 
|---|
| [46d958] | 330 |   }
 | 
|---|
 | 331 |   atoms.clear();
 | 
|---|
| [354859] | 332 | }
 | 
|---|
| [5d1611] | 333 | 
 | 
|---|
| [23b547] | 334 | // Explicit instantiation of the singleton mechanism at this point
 | 
|---|
| [5d1611] | 335 | 
 | 
|---|
| [23b547] | 336 | CONSTRUCT_SINGLETON(World)
 | 
|---|
| [5d1611] | 337 | 
 | 
|---|
 | 338 | /******************************* deprecated Legacy Stuff ***********************/
 | 
|---|
 | 339 | 
 | 
|---|
| [354859] | 340 | MoleculeListClass *&World::getMolecules() {
 | 
|---|
 | 341 |   return molecules_deprecated;
 | 
|---|
| [5d1611] | 342 | }
 | 
|---|