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