| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2011 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * World.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Feb 3, 2010
 | 
|---|
| 12 |  *      Author: crueger
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "World.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include <functional>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "Actions/ActionTrait.hpp"
 | 
|---|
| 27 | #include "Actions/ManipulateAtomsProcess.hpp"
 | 
|---|
| 28 | #include "Atom/atom.hpp"
 | 
|---|
| 29 | #include "Atom/AtomObserver.hpp"
 | 
|---|
| 30 | #include "Box.hpp"
 | 
|---|
| 31 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 32 | #include "config.hpp"
 | 
|---|
| 33 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 34 | #include "Descriptors/AtomDescriptor_impl.hpp"
 | 
|---|
| 35 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
| 36 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
 | 
|---|
| 37 | #include "Descriptors/SelectiveIterator_impl.hpp"
 | 
|---|
| 38 | #include "Element/periodentafel.hpp"
 | 
|---|
| 39 | #include "Graph/BondGraph.hpp"
 | 
|---|
| 40 | #include "Graph/DepthFirstSearchAnalysis.hpp"
 | 
|---|
| 41 | #include "Helpers/defs.hpp"
 | 
|---|
| 42 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 43 | #include "molecule.hpp"
 | 
|---|
| 44 | #include "MoleculeListClass.hpp"
 | 
|---|
| 45 | #include "Thermostats/ThermoStatContainer.hpp"
 | 
|---|
| 46 | #include "WorldTime.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "IdPool_impl.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| 51 | #include "CodePatterns/Observer/Channels.hpp"
 | 
|---|
| 52 | #include "CodePatterns/Observer/ObservedContainer_impl.hpp"
 | 
|---|
| 53 | 
 | 
|---|
| 54 | using namespace MoleCuilder;
 | 
|---|
| 55 | 
 | 
|---|
| 56 | /******************************* Notifications ************************/
 | 
|---|
| 57 | 
 | 
|---|
| 58 | 
 | 
|---|
| 59 | atom* World::_lastchangedatom = NULL;
 | 
|---|
| 60 | molecule* World::_lastchangedmol = NULL;
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /******************************* getter and setter ************************/
 | 
|---|
| 63 | periodentafel *&World::getPeriode()
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   return periode;
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | BondGraph *&World::getBondGraph()
 | 
|---|
| 69 | {
 | 
|---|
| 70 |   return BG;
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | void World::setBondGraph(BondGraph *_BG){
 | 
|---|
| 74 |   delete (BG);
 | 
|---|
| 75 |   BG = _BG;
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | config *&World::getConfig(){
 | 
|---|
| 79 |   return configuration;
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | // Atoms
 | 
|---|
| 83 | 
 | 
|---|
| 84 | atom* World::getAtom(AtomDescriptor descriptor){
 | 
|---|
| 85 |   return descriptor.find();
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | World::AtomComposite World::getAllAtoms(AtomDescriptor descriptor){
 | 
|---|
| 89 |   return descriptor.findAll();
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | World::AtomComposite World::getAllAtoms(){
 | 
|---|
| 93 |   return getAllAtoms(AllAtoms());
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | int World::numAtoms(){
 | 
|---|
| 97 |   return atoms.size();
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | // Molecules
 | 
|---|
| 101 | 
 | 
|---|
| 102 | molecule *World::getMolecule(MoleculeDescriptor descriptor){
 | 
|---|
| 103 |   return descriptor.find();
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
 | 
|---|
| 107 |   return descriptor.findAll();
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | std::vector<molecule*> World::getAllMolecules(){
 | 
|---|
| 111 |   return getAllMolecules(AllMolecules());
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | int World::numMolecules(){
 | 
|---|
| 115 |   return molecules_deprecated->ListOfMolecules.size();
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | // system
 | 
|---|
| 119 | 
 | 
|---|
| 120 | Box& World::getDomain() {
 | 
|---|
| 121 |   return *cell_size;
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | void World::setDomain(const RealSpaceMatrix &mat){
 | 
|---|
| 125 |   OBSERVE;
 | 
|---|
| 126 |   *cell_size = mat;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | void World::setDomain(double * matrix)
 | 
|---|
| 130 | {
 | 
|---|
| 131 |   OBSERVE;
 | 
|---|
| 132 |   RealSpaceMatrix M = ReturnFullMatrixforSymmetric(matrix);
 | 
|---|
| 133 |   cell_size->setM(M);
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | void World::setTime(const unsigned int _step)
 | 
|---|
| 137 | {
 | 
|---|
| 138 |   if (_step != WorldTime::getTime()) {
 | 
|---|
| 139 |     // set new time
 | 
|---|
| 140 |     WorldTime::setTime(_step);
 | 
|---|
| 141 |     // TODO: removed when BondGraph creates the adjacency
 | 
|---|
| 142 |     // 1. remove all of World's molecules
 | 
|---|
| 143 |     for (MoleculeIterator iter = getMoleculeIter();
 | 
|---|
| 144 |         getMoleculeIter() != moleculeEnd();
 | 
|---|
| 145 |         iter = getMoleculeIter()) {
 | 
|---|
| 146 |       getMolecules()->erase(*iter);
 | 
|---|
| 147 |       destroyMolecule(*iter);
 | 
|---|
| 148 |     }
 | 
|---|
| 149 |     // 2. (re-)create bondgraph
 | 
|---|
| 150 |     AtomComposite Set = getAllAtoms();
 | 
|---|
| 151 |     BG->CreateAdjacency(Set);
 | 
|---|
| 152 | 
 | 
|---|
| 153 |     // 3. scan for connected subgraphs => molecules
 | 
|---|
| 154 |     DepthFirstSearchAnalysis DFS;
 | 
|---|
| 155 |     DFS();
 | 
|---|
| 156 |     DFS.UpdateMoleculeStructure();
 | 
|---|
| 157 |   }
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | std::string World::getDefaultName() {
 | 
|---|
| 161 |   return defaultName;
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | void World::setDefaultName(std::string name)
 | 
|---|
| 165 | {
 | 
|---|
| 166 |   OBSERVE;
 | 
|---|
| 167 |   defaultName = name;
 | 
|---|
| 168 | };
 | 
|---|
| 169 | 
 | 
|---|
| 170 | class ThermoStatContainer * World::getThermostats()
 | 
|---|
| 171 | {
 | 
|---|
| 172 |   return Thermostats;
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | 
 | 
|---|
| 176 | int World::getExitFlag() {
 | 
|---|
| 177 |   return ExitFlag;
 | 
|---|
| 178 | }
 | 
|---|
| 179 | 
 | 
|---|
| 180 | void World::setExitFlag(int flag) {
 | 
|---|
| 181 |   if (ExitFlag < flag)
 | 
|---|
| 182 |     ExitFlag = flag;
 | 
|---|
| 183 | }
 | 
|---|
| 184 | 
 | 
|---|
| 185 | /******************** Methods to change World state *********************/
 | 
|---|
| 186 | 
 | 
|---|
| 187 | molecule* World::createMolecule(){
 | 
|---|
| 188 |   OBSERVE;
 | 
|---|
| 189 |   molecule *mol = NULL;
 | 
|---|
| 190 |   mol = NewMolecule();
 | 
|---|
| 191 |   moleculeId_t id = moleculeIdPool.getNextId();
 | 
|---|
| 192 |   ASSERT(!molecules.count(id),"proposed id did not specify an unused ID");
 | 
|---|
| 193 |   mol->setId(id);
 | 
|---|
| 194 |   // store the molecule by ID
 | 
|---|
| 195 |   molecules[mol->getId()] = mol;
 | 
|---|
| 196 |   mol->signOn(this);
 | 
|---|
| 197 |   _lastchangedmol = mol;
 | 
|---|
| 198 |   NOTIFY(MoleculeInserted);
 | 
|---|
| 199 |   return mol;
 | 
|---|
| 200 | }
 | 
|---|
| 201 | 
 | 
|---|
| 202 | void World::destroyMolecule(molecule* mol){
 | 
|---|
| 203 |   OBSERVE;
 | 
|---|
| 204 |   ASSERT(mol,"Molecule that was meant to be destroyed did not exist");
 | 
|---|
| 205 |   destroyMolecule(mol->getId());
 | 
|---|
| 206 | }
 | 
|---|
| 207 | 
 | 
|---|
| 208 | void World::destroyMolecule(moleculeId_t id){
 | 
|---|
| 209 |   molecule *mol = molecules[id];
 | 
|---|
| 210 |   ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
 | 
|---|
| 211 |   // give notice about immediate removal
 | 
|---|
| 212 |   {
 | 
|---|
| 213 |     OBSERVE;
 | 
|---|
| 214 |     _lastchangedmol = mol;
 | 
|---|
| 215 |     NOTIFY(MoleculeRemoved);
 | 
|---|
| 216 |   }
 | 
|---|
| 217 |   DeleteMolecule(mol);
 | 
|---|
| 218 |   if (isMoleculeSelected(id))
 | 
|---|
| 219 |     selectedMolecules.erase(id);
 | 
|---|
| 220 |   molecules.erase(id);
 | 
|---|
| 221 |   moleculeIdPool.releaseId(id);
 | 
|---|
| 222 | }
 | 
|---|
| 223 | 
 | 
|---|
| 224 | atom *World::createAtom(){
 | 
|---|
| 225 |   OBSERVE;
 | 
|---|
| 226 |   atomId_t id = atomIdPool.getNextId();
 | 
|---|
| 227 |   ASSERT(!atoms.count(id),"proposed id did not specify an unused ID");
 | 
|---|
| 228 |   atom *res = NewAtom(id);
 | 
|---|
| 229 |   res->setWorld(this);
 | 
|---|
| 230 |   // sign on to global atom change tracker
 | 
|---|
| 231 |   AtomObserver::getInstance().AtomInserted(res);
 | 
|---|
| 232 |   // store the atom by ID
 | 
|---|
| 233 |   atoms[res->getId()] = res;
 | 
|---|
| 234 |   _lastchangedatom = res;
 | 
|---|
| 235 |   NOTIFY(AtomInserted);
 | 
|---|
| 236 |   return res;
 | 
|---|
| 237 | }
 | 
|---|
| 238 | 
 | 
|---|
| 239 | 
 | 
|---|
| 240 | int World::registerAtom(atom *atom){
 | 
|---|
| 241 |   OBSERVE;
 | 
|---|
| 242 |   atomId_t id = atomIdPool.getNextId();
 | 
|---|
| 243 |   atom->setId(id);
 | 
|---|
| 244 |   atom->setWorld(this);
 | 
|---|
| 245 |   atoms[atom->getId()] = atom;
 | 
|---|
| 246 |   return atom->getId();
 | 
|---|
| 247 | }
 | 
|---|
| 248 | 
 | 
|---|
| 249 | void World::destroyAtom(atom* atom){
 | 
|---|
| 250 |   int id = atom->getId();
 | 
|---|
| 251 |   destroyAtom(id);
 | 
|---|
| 252 | }
 | 
|---|
| 253 | 
 | 
|---|
| 254 | void World::destroyAtom(atomId_t id) {
 | 
|---|
| 255 |   atom *atom = atoms[id];
 | 
|---|
| 256 |   ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
 | 
|---|
| 257 |   // give notice about immediate removal
 | 
|---|
| 258 |   {
 | 
|---|
| 259 |     OBSERVE;
 | 
|---|
| 260 |     _lastchangedatom = atom;
 | 
|---|
| 261 |     NOTIFY(AtomRemoved);
 | 
|---|
| 262 |   }
 | 
|---|
| 263 |   DeleteAtom(atom);
 | 
|---|
| 264 |   if (isAtomSelected(id))
 | 
|---|
| 265 |     selectedAtoms.erase(id);
 | 
|---|
| 266 |   atoms.erase(id);
 | 
|---|
| 267 |   atomIdPool.releaseId(id);
 | 
|---|
| 268 | }
 | 
|---|
| 269 | 
 | 
|---|
| 270 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
 | 
|---|
| 271 |   OBSERVE;
 | 
|---|
| 272 |   // in case this call did not originate from inside the atom, we redirect it,
 | 
|---|
| 273 |   // to also let it know that it has changed
 | 
|---|
| 274 |   if(!target){
 | 
|---|
| 275 |     target = atoms[oldId];
 | 
|---|
| 276 |     ASSERT(target,"Atom with that ID not found");
 | 
|---|
| 277 |     return target->changeId(newId);
 | 
|---|
| 278 |   }
 | 
|---|
| 279 |   else{
 | 
|---|
| 280 |     if(atomIdPool.reserveId(newId)){
 | 
|---|
| 281 |       atoms.erase(oldId);
 | 
|---|
| 282 |       atoms.insert(pair<atomId_t,atom*>(newId,target));
 | 
|---|
| 283 |       return true;
 | 
|---|
| 284 |     }
 | 
|---|
| 285 |     else{
 | 
|---|
| 286 |       return false;
 | 
|---|
| 287 |     }
 | 
|---|
| 288 |   }
 | 
|---|
| 289 | }
 | 
|---|
| 290 | 
 | 
|---|
| 291 | bool World::changeMoleculeId(moleculeId_t oldId, moleculeId_t newId, molecule* target){
 | 
|---|
| 292 |   OBSERVE;
 | 
|---|
| 293 |   // in case this call did not originate from inside the atom, we redirect it,
 | 
|---|
| 294 |   // to also let it know that it has changed
 | 
|---|
| 295 |   if(!target){
 | 
|---|
| 296 |     target = molecules[oldId];
 | 
|---|
| 297 |     ASSERT(target,"Molecule with that ID not found");
 | 
|---|
| 298 |     return target->changeId(newId);
 | 
|---|
| 299 |   }
 | 
|---|
| 300 |   else{
 | 
|---|
| 301 |     if(moleculeIdPool.reserveId(newId)){
 | 
|---|
| 302 |       molecules.erase(oldId);
 | 
|---|
| 303 |       molecules.insert(pair<moleculeId_t,molecule*>(newId,target));
 | 
|---|
| 304 |       return true;
 | 
|---|
| 305 |     }
 | 
|---|
| 306 |     else{
 | 
|---|
| 307 |       return false;
 | 
|---|
| 308 |     }
 | 
|---|
| 309 |   }
 | 
|---|
| 310 | }
 | 
|---|
| 311 | 
 | 
|---|
| 312 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
 | 
|---|
| 313 |   ActionTrait manipulateTrait(name);
 | 
|---|
| 314 |   return new ManipulateAtomsProcess(op, descr,manipulateTrait,false);
 | 
|---|
| 315 | }
 | 
|---|
| 316 | 
 | 
|---|
| 317 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
 | 
|---|
| 318 |   return manipulateAtoms(op,name,AllAtoms());
 | 
|---|
| 319 | }
 | 
|---|
| 320 | 
 | 
|---|
| 321 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
 | 
|---|
| 322 | 
 | 
|---|
| 323 | void World::doManipulate(ManipulateAtomsProcess *proc){
 | 
|---|
| 324 |   proc->signOn(this);
 | 
|---|
| 325 |   {
 | 
|---|
| 326 |     OBSERVE;
 | 
|---|
| 327 |     proc->doManipulate(this);
 | 
|---|
| 328 |   }
 | 
|---|
| 329 |   proc->signOff(this);
 | 
|---|
| 330 | }
 | 
|---|
| 331 | /******************************* Iterators ********************************/
 | 
|---|
| 332 | 
 | 
|---|
| 333 | // external parts with observers
 | 
|---|
| 334 | 
 | 
|---|
| 335 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
 | 
|---|
| 336 | 
 | 
|---|
| 337 | World::AtomIterator
 | 
|---|
| 338 | World::getAtomIter(AtomDescriptor descr){
 | 
|---|
| 339 |     return AtomIterator(descr,atoms);
 | 
|---|
| 340 | }
 | 
|---|
| 341 | 
 | 
|---|
| 342 | World::AtomIterator
 | 
|---|
| 343 | World::getAtomIter(){
 | 
|---|
| 344 |     return AtomIterator(AllAtoms(),atoms);
 | 
|---|
| 345 | }
 | 
|---|
| 346 | 
 | 
|---|
| 347 | World::AtomIterator
 | 
|---|
| 348 | World::atomEnd(){
 | 
|---|
| 349 |   return AtomIterator(AllAtoms(),atoms,atoms.end());
 | 
|---|
| 350 | }
 | 
|---|
| 351 | 
 | 
|---|
| 352 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
 | 
|---|
| 353 | 
 | 
|---|
| 354 | World::MoleculeIterator
 | 
|---|
| 355 | World::getMoleculeIter(MoleculeDescriptor descr){
 | 
|---|
| 356 |     return MoleculeIterator(descr,molecules);
 | 
|---|
| 357 | }
 | 
|---|
| 358 | 
 | 
|---|
| 359 | World::MoleculeIterator
 | 
|---|
| 360 | World::getMoleculeIter(){
 | 
|---|
| 361 |     return MoleculeIterator(AllMolecules(),molecules);
 | 
|---|
| 362 | }
 | 
|---|
| 363 | 
 | 
|---|
| 364 | World::MoleculeIterator
 | 
|---|
| 365 | World::moleculeEnd(){
 | 
|---|
| 366 |   return MoleculeIterator(AllMolecules(),molecules,molecules.end());
 | 
|---|
| 367 | }
 | 
|---|
| 368 | 
 | 
|---|
| 369 | // Internal parts, without observers
 | 
|---|
| 370 | 
 | 
|---|
| 371 | // Build the AtomIterator from template
 | 
|---|
| 372 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet::set_t,AtomDescriptor);
 | 
|---|
| 373 | 
 | 
|---|
| 374 | 
 | 
|---|
| 375 | World::internal_AtomIterator
 | 
|---|
| 376 | World::getAtomIter_internal(AtomDescriptor descr){
 | 
|---|
| 377 |   return internal_AtomIterator(descr,atoms.getContent());
 | 
|---|
| 378 | }
 | 
|---|
| 379 | 
 | 
|---|
| 380 | World::internal_AtomIterator
 | 
|---|
| 381 | World::atomEnd_internal(){
 | 
|---|
| 382 |   return internal_AtomIterator(AllAtoms(),atoms.getContent(),atoms.end_internal());
 | 
|---|
| 383 | }
 | 
|---|
| 384 | 
 | 
|---|
| 385 | // build the MoleculeIterator from template
 | 
|---|
| 386 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet::set_t,MoleculeDescriptor);
 | 
|---|
| 387 | 
 | 
|---|
| 388 | World::internal_MoleculeIterator World::getMoleculeIter_internal(MoleculeDescriptor descr){
 | 
|---|
| 389 |   return internal_MoleculeIterator(descr,molecules.getContent());
 | 
|---|
| 390 | }
 | 
|---|
| 391 | 
 | 
|---|
| 392 | World::internal_MoleculeIterator World::moleculeEnd_internal(){
 | 
|---|
| 393 |   return internal_MoleculeIterator(AllMolecules(),molecules.getContent(),molecules.end_internal());
 | 
|---|
| 394 | }
 | 
|---|
| 395 | 
 | 
|---|
| 396 | /************************** Selection of Atoms and molecules ******************/
 | 
|---|
| 397 | 
 | 
|---|
| 398 | // Atoms
 | 
|---|
| 399 | 
 | 
|---|
| 400 | void World::clearAtomSelection(){
 | 
|---|
| 401 |   selectedAtoms.clear();
 | 
|---|
| 402 | }
 | 
|---|
| 403 | 
 | 
|---|
| 404 | void World::selectAtom(const atom *_atom){
 | 
|---|
| 405 |   // atom * is unchanged in this function, but we do store entity as changeable
 | 
|---|
| 406 |   ASSERT(_atom,"Invalid pointer in selection of atom");
 | 
|---|
| 407 |   selectedAtoms[_atom->getId()]=const_cast<atom *>(_atom);
 | 
|---|
| 408 | }
 | 
|---|
| 409 | 
 | 
|---|
| 410 | void World::selectAtom(const atomId_t id){
 | 
|---|
| 411 |   ASSERT(atoms.count(id),"Atom Id selected that was not in the world");
 | 
|---|
| 412 |   selectedAtoms[id]=atoms[id];
 | 
|---|
| 413 | }
 | 
|---|
| 414 | 
 | 
|---|
| 415 | void World::selectAllAtoms(AtomDescriptor descr){
 | 
|---|
| 416 |   internal_AtomIterator begin = getAtomIter_internal(descr);
 | 
|---|
| 417 |   internal_AtomIterator end = atomEnd_internal();
 | 
|---|
| 418 |   void (World::*func)(const atom*) = &World::selectAtom; // needed for type resolution of overloaded function
 | 
|---|
| 419 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
| 420 | }
 | 
|---|
| 421 | 
 | 
|---|
| 422 | void World::selectAtomsOfMolecule(const molecule *_mol){
 | 
|---|
| 423 |   ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
 | 
|---|
| 424 |   // need to make it const to get the fast iterators
 | 
|---|
| 425 |   const molecule *mol = _mol;
 | 
|---|
| 426 |   void (World::*func)(const atom*) = &World::selectAtom; // needed for type resolution of overloaded function
 | 
|---|
| 427 |   for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
| 428 | }
 | 
|---|
| 429 | 
 | 
|---|
| 430 | void World::selectAtomsOfMolecule(const moleculeId_t id){
 | 
|---|
| 431 |   ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
 | 
|---|
| 432 |   selectAtomsOfMolecule(molecules[id]);
 | 
|---|
| 433 | }
 | 
|---|
| 434 | 
 | 
|---|
| 435 | void World::unselectAtom(const atom *_atom){
 | 
|---|
| 436 |   ASSERT(_atom,"Invalid pointer in unselection of atom");
 | 
|---|
| 437 |   unselectAtom(_atom->getId());
 | 
|---|
| 438 | }
 | 
|---|
| 439 | 
 | 
|---|
| 440 | void World::unselectAtom(const atomId_t id){
 | 
|---|
| 441 |   ASSERT(atoms.count(id),"Atom Id unselected that was not in the world");
 | 
|---|
| 442 |   selectedAtoms.erase(id);
 | 
|---|
| 443 | }
 | 
|---|
| 444 | 
 | 
|---|
| 445 | void World::unselectAllAtoms(AtomDescriptor descr){
 | 
|---|
| 446 |   internal_AtomIterator begin = getAtomIter_internal(descr);
 | 
|---|
| 447 |   internal_AtomIterator end = atomEnd_internal();
 | 
|---|
| 448 |   void (World::*func)(const atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
 | 
|---|
| 449 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
 | 
|---|
| 450 | }
 | 
|---|
| 451 | 
 | 
|---|
| 452 | void World::unselectAtomsOfMolecule(const molecule *_mol){
 | 
|---|
| 453 |   ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
 | 
|---|
| 454 |   // need to make it const to get the fast iterators
 | 
|---|
| 455 |   const molecule *mol = _mol;
 | 
|---|
| 456 |   void (World::*func)(const atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
 | 
|---|
| 457 |   for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is unsselect... see above
 | 
|---|
| 458 | }
 | 
|---|
| 459 | 
 | 
|---|
| 460 | void World::unselectAtomsOfMolecule(const moleculeId_t id){
 | 
|---|
| 461 |   ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
 | 
|---|
| 462 |   unselectAtomsOfMolecule(molecules[id]);
 | 
|---|
| 463 | }
 | 
|---|
| 464 | 
 | 
|---|
| 465 | size_t World::countSelectedAtoms() const {
 | 
|---|
| 466 |   size_t count = 0;
 | 
|---|
| 467 |   for (AtomSet::const_iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter)
 | 
|---|
| 468 |     count++;
 | 
|---|
| 469 |   return count;
 | 
|---|
| 470 | }
 | 
|---|
| 471 | 
 | 
|---|
| 472 | bool World::isSelected(const atom *_atom) const {
 | 
|---|
| 473 |   return isAtomSelected(_atom->getId());
 | 
|---|
| 474 | }
 | 
|---|
| 475 | 
 | 
|---|
| 476 | bool World::isAtomSelected(const atomId_t no) const {
 | 
|---|
| 477 |   return selectedAtoms.find(no) != selectedAtoms.end();
 | 
|---|
| 478 | }
 | 
|---|
| 479 | 
 | 
|---|
| 480 | const std::vector<atom *> World::getSelectedAtoms() const {
 | 
|---|
| 481 |   std::vector<atom *> returnAtoms;
 | 
|---|
| 482 |   returnAtoms.resize(countSelectedAtoms());
 | 
|---|
| 483 |   int count = 0;
 | 
|---|
| 484 |   for (AtomSet::const_iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter)
 | 
|---|
| 485 |     returnAtoms[count++] = iter->second;
 | 
|---|
| 486 |   return returnAtoms;
 | 
|---|
| 487 | }
 | 
|---|
| 488 | 
 | 
|---|
| 489 | 
 | 
|---|
| 490 | // Molecules
 | 
|---|
| 491 | 
 | 
|---|
| 492 | void World::clearMoleculeSelection(){
 | 
|---|
| 493 |   selectedMolecules.clear();
 | 
|---|
| 494 | }
 | 
|---|
| 495 | 
 | 
|---|
| 496 | void World::selectMolecule(const molecule *_mol){
 | 
|---|
| 497 |   // molecule * is unchanged in this function, but we do store entity as changeable
 | 
|---|
| 498 |   ASSERT(_mol,"Invalid pointer to molecule in selection");
 | 
|---|
| 499 |   selectedMolecules[_mol->getId()]=const_cast<molecule *>(_mol);
 | 
|---|
| 500 | }
 | 
|---|
| 501 | 
 | 
|---|
| 502 | void World::selectMolecule(const moleculeId_t id){
 | 
|---|
| 503 |   ASSERT(molecules.count(id),"Molecule Id selected that was not in the world");
 | 
|---|
| 504 |   selectedMolecules[id]=molecules[id];
 | 
|---|
| 505 | }
 | 
|---|
| 506 | 
 | 
|---|
| 507 | void World::selectAllMolecules(MoleculeDescriptor descr){
 | 
|---|
| 508 |   internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
 | 
|---|
| 509 |   internal_MoleculeIterator end = moleculeEnd_internal();
 | 
|---|
| 510 |   void (World::*func)(const molecule*) = &World::selectMolecule; // needed for type resolution of overloaded function
 | 
|---|
| 511 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
| 512 | }
 | 
|---|
| 513 | 
 | 
|---|
| 514 | void World::selectMoleculeOfAtom(const atom *_atom){
 | 
|---|
| 515 |   ASSERT(_atom,"Invalid atom pointer in selection of MoleculeOfAtom");
 | 
|---|
| 516 |   molecule *mol=_atom->getMolecule();
 | 
|---|
| 517 |   // the atom might not be part of a molecule
 | 
|---|
| 518 |   if(mol){
 | 
|---|
| 519 |     selectMolecule(mol);
 | 
|---|
| 520 |   }
 | 
|---|
| 521 | }
 | 
|---|
| 522 | 
 | 
|---|
| 523 | void World::selectMoleculeOfAtom(const atomId_t id){
 | 
|---|
| 524 |   ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
 | 
|---|
| 525 |   selectMoleculeOfAtom(atoms[id]);
 | 
|---|
| 526 | }
 | 
|---|
| 527 | 
 | 
|---|
| 528 | void World::unselectMolecule(const molecule *_mol){
 | 
|---|
| 529 |   ASSERT(_mol,"invalid pointer in unselection of molecule");
 | 
|---|
| 530 |   unselectMolecule(_mol->getId());
 | 
|---|
| 531 | }
 | 
|---|
| 532 | 
 | 
|---|
| 533 | void World::unselectMolecule(const moleculeId_t id){
 | 
|---|
| 534 |   ASSERT(molecules.count(id),"No such molecule with ID in unselection");
 | 
|---|
| 535 |   selectedMolecules.erase(id);
 | 
|---|
| 536 | }
 | 
|---|
| 537 | 
 | 
|---|
| 538 | void World::unselectAllMolecules(MoleculeDescriptor descr){
 | 
|---|
| 539 |   internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
 | 
|---|
| 540 |   internal_MoleculeIterator end = moleculeEnd_internal();
 | 
|---|
| 541 |   void (World::*func)(const molecule*) = &World::unselectMolecule; // needed for type resolution of overloaded function
 | 
|---|
| 542 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
 | 
|---|
| 543 | }
 | 
|---|
| 544 | 
 | 
|---|
| 545 | void World::unselectMoleculeOfAtom(const atom *_atom){
 | 
|---|
| 546 |   ASSERT(_atom,"Invalid atom pointer in selection of MoleculeOfAtom");
 | 
|---|
| 547 |   molecule *mol=_atom->getMolecule();
 | 
|---|
| 548 |   // the atom might not be part of a molecule
 | 
|---|
| 549 |   if(mol){
 | 
|---|
| 550 |     unselectMolecule(mol);
 | 
|---|
| 551 |   }
 | 
|---|
| 552 | }
 | 
|---|
| 553 | 
 | 
|---|
| 554 | void World::unselectMoleculeOfAtom(const atomId_t id){
 | 
|---|
| 555 |   ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
 | 
|---|
| 556 |   unselectMoleculeOfAtom(atoms[id]);
 | 
|---|
| 557 | }
 | 
|---|
| 558 | 
 | 
|---|
| 559 | size_t World::countSelectedMolecules() const {
 | 
|---|
| 560 |   size_t count = 0;
 | 
|---|
| 561 |   for (MoleculeSet::const_iterator iter = selectedMolecules.begin(); iter != selectedMolecules.end(); ++iter)
 | 
|---|
| 562 |     count++;
 | 
|---|
| 563 |   return count;
 | 
|---|
| 564 | }
 | 
|---|
| 565 | 
 | 
|---|
| 566 | bool World::isSelected(const molecule *_mol) const {
 | 
|---|
| 567 |   return isMoleculeSelected(_mol->getId());
 | 
|---|
| 568 | }
 | 
|---|
| 569 | 
 | 
|---|
| 570 | bool World::isMoleculeSelected(const moleculeId_t no) const {
 | 
|---|
| 571 |   return selectedMolecules.find(no) != selectedMolecules.end();
 | 
|---|
| 572 | }
 | 
|---|
| 573 | 
 | 
|---|
| 574 | const std::vector<molecule *> World::getSelectedMolecules() const {
 | 
|---|
| 575 |   std::vector<molecule *> returnMolecules;
 | 
|---|
| 576 |   returnMolecules.resize(countSelectedMolecules());
 | 
|---|
| 577 |   int count = 0;
 | 
|---|
| 578 |   for (MoleculeSet::const_iterator iter = selectedMolecules.begin(); iter != selectedMolecules.end(); ++iter)
 | 
|---|
| 579 |     returnMolecules[count++] = iter->second;
 | 
|---|
| 580 |   return returnMolecules;
 | 
|---|
| 581 | }
 | 
|---|
| 582 | 
 | 
|---|
| 583 | /******************* Iterators over Selection *****************************/
 | 
|---|
| 584 | World::AtomSelectionIterator World::beginAtomSelection(){
 | 
|---|
| 585 |   return selectedAtoms.begin();
 | 
|---|
| 586 | }
 | 
|---|
| 587 | 
 | 
|---|
| 588 | World::AtomSelectionIterator World::endAtomSelection(){
 | 
|---|
| 589 |   return selectedAtoms.end();
 | 
|---|
| 590 | }
 | 
|---|
| 591 | 
 | 
|---|
| 592 | World::AtomSelectionConstIterator World::beginAtomSelection() const{
 | 
|---|
| 593 |   return selectedAtoms.begin();
 | 
|---|
| 594 | }
 | 
|---|
| 595 | 
 | 
|---|
| 596 | World::AtomSelectionConstIterator World::endAtomSelection() const{
 | 
|---|
| 597 |   return selectedAtoms.end();
 | 
|---|
| 598 | }
 | 
|---|
| 599 | 
 | 
|---|
| 600 | 
 | 
|---|
| 601 | World::MoleculeSelectionIterator World::beginMoleculeSelection(){
 | 
|---|
| 602 |   return selectedMolecules.begin();
 | 
|---|
| 603 | }
 | 
|---|
| 604 | 
 | 
|---|
| 605 | World::MoleculeSelectionIterator World::endMoleculeSelection(){
 | 
|---|
| 606 |   return selectedMolecules.end();
 | 
|---|
| 607 | }
 | 
|---|
| 608 | 
 | 
|---|
| 609 | World::MoleculeSelectionConstIterator World::beginMoleculeSelection() const{
 | 
|---|
| 610 |   return selectedMolecules.begin();
 | 
|---|
| 611 | }
 | 
|---|
| 612 | 
 | 
|---|
| 613 | World::MoleculeSelectionConstIterator World::endMoleculeSelection() const{
 | 
|---|
| 614 |   return selectedMolecules.end();
 | 
|---|
| 615 | }
 | 
|---|
| 616 | 
 | 
|---|
| 617 | /******************************* Singleton Stuff **************************/
 | 
|---|
| 618 | 
 | 
|---|
| 619 | World::World() :
 | 
|---|
| 620 |     Observable("World"),
 | 
|---|
| 621 |     BG(new BondGraph(true)),  // assume Angstroem for the moment
 | 
|---|
| 622 |     periode(new periodentafel(true)),
 | 
|---|
| 623 |     configuration(new config),
 | 
|---|
| 624 |     Thermostats(new ThermoStatContainer),
 | 
|---|
| 625 |     ExitFlag(0),
 | 
|---|
| 626 |     atoms(this),
 | 
|---|
| 627 |     selectedAtoms(this),
 | 
|---|
| 628 |     atomIdPool(0, 20, 100),
 | 
|---|
| 629 |     molecules(this),
 | 
|---|
| 630 |     selectedMolecules(this),
 | 
|---|
| 631 |     moleculeIdPool(0, 20,100),
 | 
|---|
| 632 |     molecules_deprecated(new MoleculeListClass(this))
 | 
|---|
| 633 | {
 | 
|---|
| 634 |   cell_size = new Box;
 | 
|---|
| 635 |   RealSpaceMatrix domain;
 | 
|---|
| 636 |   domain.at(0,0) = 20;
 | 
|---|
| 637 |   domain.at(1,1) = 20;
 | 
|---|
| 638 |   domain.at(2,2) = 20;
 | 
|---|
| 639 |   cell_size->setM(domain);
 | 
|---|
| 640 |   defaultName = "none";
 | 
|---|
| 641 |   Channels *OurChannel = new Channels;
 | 
|---|
| 642 |   NotificationChannels.insert( std::make_pair( this, OurChannel) );
 | 
|---|
| 643 |   for (size_t type = 0; type < (size_t)NotificationType_MAX; ++type)
 | 
|---|
| 644 |     OurChannel->addChannel(type);
 | 
|---|
| 645 |   molecules_deprecated->signOn(this);
 | 
|---|
| 646 | }
 | 
|---|
| 647 | 
 | 
|---|
| 648 | World::~World()
 | 
|---|
| 649 | {
 | 
|---|
| 650 |   molecules_deprecated->signOff(this);
 | 
|---|
| 651 |   delete cell_size;
 | 
|---|
| 652 |   delete molecules_deprecated;
 | 
|---|
| 653 |   MoleculeSet::iterator molIter;
 | 
|---|
| 654 |   for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
 | 
|---|
| 655 |     DeleteMolecule((*molIter).second);
 | 
|---|
| 656 |   }
 | 
|---|
| 657 |   molecules.clear();
 | 
|---|
| 658 |   AtomSet::iterator atIter;
 | 
|---|
| 659 |   for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
 | 
|---|
| 660 |     DeleteAtom((*atIter).second);
 | 
|---|
| 661 |   }
 | 
|---|
| 662 |   atoms.clear();
 | 
|---|
| 663 | 
 | 
|---|
| 664 |   // empty notifications
 | 
|---|
| 665 |   std::map<Observable *, Channels*>::iterator iter = NotificationChannels.find(this);
 | 
|---|
| 666 |   ASSERT(iter != NotificationChannels.end(),
 | 
|---|
| 667 |       "World::~World() - cannot find our Channels in NotificationChannels.");
 | 
|---|
| 668 |   delete iter->second;
 | 
|---|
| 669 |   NotificationChannels.erase(iter);
 | 
|---|
| 670 | 
 | 
|---|
| 671 |   delete BG;
 | 
|---|
| 672 |   delete periode;
 | 
|---|
| 673 |   delete configuration;
 | 
|---|
| 674 |   delete Thermostats;
 | 
|---|
| 675 | }
 | 
|---|
| 676 | 
 | 
|---|
| 677 | // Explicit instantiation of the singleton mechanism at this point
 | 
|---|
| 678 | 
 | 
|---|
| 679 | // moleculeId_t und atomId_t sind gleicher Basistyp, deswegen nur einen von beiden konstruieren
 | 
|---|
| 680 | CONSTRUCT_IDPOOL(moleculeId_t)
 | 
|---|
| 681 | 
 | 
|---|
| 682 | CONSTRUCT_SINGLETON(World)
 | 
|---|
| 683 | 
 | 
|---|
| 684 | CONSTRUCT_OBSERVEDCONTAINER(World::AtomSTLSet)
 | 
|---|
| 685 | 
 | 
|---|
| 686 | CONSTRUCT_OBSERVEDCONTAINER(World::MoleculeSTLSet)
 | 
|---|
| 687 | 
 | 
|---|
| 688 | /******************************* deprecated Legacy Stuff ***********************/
 | 
|---|
| 689 | 
 | 
|---|
| 690 | MoleculeListClass *&World::getMolecules() {
 | 
|---|
| 691 |   return molecules_deprecated;
 | 
|---|
| 692 | }
 | 
|---|