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