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