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