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