[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[5d1611] | 8 | /*
|
---|
| 9 | * World.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Feb 3, 2010
|
---|
| 12 | * Author: crueger
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 21 |
|
---|
[5d1611] | 22 | #include "World.hpp"
|
---|
| 23 |
|
---|
[90c4280] | 24 | #include <functional>
|
---|
[5d1611] | 25 |
|
---|
[d297a3] | 26 | #include "Actions/ActionTraits.hpp"
|
---|
[76163d] | 27 | //#include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
|
---|
[d297a3] | 28 | #include "Actions/ManipulateAtomsProcess.hpp"
|
---|
[d346b6] | 29 | #include "atom.hpp"
|
---|
[632508] | 30 | #include "Graph/BondGraph.hpp"
|
---|
[d297a3] | 31 | #include "Box.hpp"
|
---|
| 32 | #include "CodePatterns/Assert.hpp"
|
---|
[8e1f7af] | 33 | #include "config.hpp"
|
---|
[fc1b24] | 34 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
[865a945] | 35 | #include "Descriptors/AtomDescriptor_impl.hpp"
|
---|
[1c51c8] | 36 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 37 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
|
---|
[6e97e5] | 38 | #include "Descriptors/SelectiveIterator_impl.hpp"
|
---|
[e4fe8d] | 39 | #include "Helpers/defs.hpp"
|
---|
[d297a3] | 40 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
| 41 | #include "molecule.hpp"
|
---|
| 42 | #include "periodentafel.hpp"
|
---|
| 43 | #include "ThermoStatContainer.hpp"
|
---|
| 44 | #include "WorldTime.hpp"
|
---|
[d346b6] | 45 |
|
---|
[ad011c] | 46 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
| 47 | #include "CodePatterns/ObservedContainer_impl.hpp"
|
---|
[23b547] | 48 |
|
---|
[d346b6] | 49 | using namespace std;
|
---|
[4d9c01] | 50 |
|
---|
[11e206] | 51 | const unsigned int MAX_POOL_FRAGMENTATION=20;
|
---|
| 52 | const unsigned int MAX_FRAGMENTATION_SKIPS=100;
|
---|
| 53 |
|
---|
[5d1611] | 54 | /******************************* getter and setter ************************/
|
---|
[f71baf] | 55 | periodentafel *&World::getPeriode()
|
---|
| 56 | {
|
---|
[5d1611] | 57 | return periode;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[f71baf] | 60 | BondGraph *&World::getBondGraph()
|
---|
| 61 | {
|
---|
| 62 | return BG;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void World::setBondGraph(BondGraph *_BG){
|
---|
| 66 | delete (BG);
|
---|
| 67 | BG = _BG;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[8e1f7af] | 70 | config *&World::getConfig(){
|
---|
| 71 | return configuration;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[1c51c8] | 74 | // Atoms
|
---|
| 75 |
|
---|
[7a1ce5] | 76 | atom* World::getAtom(AtomDescriptor descriptor){
|
---|
[fc1b24] | 77 | return descriptor.find();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[4d72e4] | 80 | World::AtomComposite World::getAllAtoms(AtomDescriptor descriptor){
|
---|
[fc1b24] | 81 | return descriptor.findAll();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[4d72e4] | 84 | World::AtomComposite World::getAllAtoms(){
|
---|
[0e2a47] | 85 | return getAllAtoms(AllAtoms());
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[354859] | 88 | int World::numAtoms(){
|
---|
| 89 | return atoms.size();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[1c51c8] | 92 | // Molecules
|
---|
| 93 |
|
---|
| 94 | molecule *World::getMolecule(MoleculeDescriptor descriptor){
|
---|
| 95 | return descriptor.find();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
|
---|
| 99 | return descriptor.findAll();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[97ebf8] | 102 | std::vector<molecule*> World::getAllMolecules(){
|
---|
| 103 | return getAllMolecules(AllMolecules());
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[354859] | 106 | int World::numMolecules(){
|
---|
| 107 | return molecules_deprecated->ListOfMolecules.size();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[5f612ee] | 110 | // system
|
---|
| 111 |
|
---|
[84c494] | 112 | Box& World::getDomain() {
|
---|
| 113 | return *cell_size;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[cca9ef] | 116 | void World::setDomain(const RealSpaceMatrix &mat){
|
---|
[be97a8] | 117 | OBSERVE;
|
---|
[84c494] | 118 | *cell_size = mat;
|
---|
[5f612ee] | 119 | }
|
---|
| 120 |
|
---|
| 121 | void World::setDomain(double * matrix)
|
---|
| 122 | {
|
---|
[b9c847] | 123 | OBSERVE;
|
---|
[cca9ef] | 124 | RealSpaceMatrix M = ReturnFullMatrixforSymmetric(matrix);
|
---|
[84c494] | 125 | cell_size->setM(M);
|
---|
[5f612ee] | 126 | }
|
---|
| 127 |
|
---|
[d297a3] | 128 | void World::setTime(const unsigned int _step)
|
---|
| 129 | {
|
---|
[76163d] | 130 | if (_step != WorldTime::getTime()) {
|
---|
| 131 | // set new time
|
---|
| 132 | WorldTime::setTime(_step);
|
---|
| 133 | // re-instantiate bond structure
|
---|
| 134 | //FragmentationSubgraphDissection();
|
---|
| 135 | }
|
---|
[d297a3] | 136 | }
|
---|
| 137 |
|
---|
[387b36] | 138 | std::string World::getDefaultName() {
|
---|
[5f612ee] | 139 | return defaultName;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[387b36] | 142 | void World::setDefaultName(std::string name)
|
---|
[5f612ee] | 143 | {
|
---|
[be97a8] | 144 | OBSERVE;
|
---|
[387b36] | 145 | defaultName = name;
|
---|
[5f612ee] | 146 | };
|
---|
| 147 |
|
---|
[43dad6] | 148 | class ThermoStatContainer * World::getThermostats()
|
---|
| 149 | {
|
---|
| 150 | return Thermostats;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
[e4b5de] | 154 | int World::getExitFlag() {
|
---|
| 155 | return ExitFlag;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void World::setExitFlag(int flag) {
|
---|
| 159 | if (ExitFlag < flag)
|
---|
| 160 | ExitFlag = flag;
|
---|
| 161 | }
|
---|
[5f612ee] | 162 |
|
---|
[afb47f] | 163 | /******************** Methods to change World state *********************/
|
---|
| 164 |
|
---|
[354859] | 165 | molecule* World::createMolecule(){
|
---|
| 166 | OBSERVE;
|
---|
| 167 | molecule *mol = NULL;
|
---|
[cbc5fb] | 168 | mol = NewMolecule();
|
---|
[127a8e] | 169 | moleculeId_t id = getNextMoleculeId();
|
---|
| 170 | ASSERT(!molecules.count(id),"proposed id did not specify an unused ID");
|
---|
| 171 | mol->setId(id);
|
---|
[244d26] | 172 | // store the molecule by ID
|
---|
[cbc5fb] | 173 | molecules[mol->getId()] = mol;
|
---|
[354859] | 174 | mol->signOn(this);
|
---|
| 175 | return mol;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[cbc5fb] | 178 | void World::destroyMolecule(molecule* mol){
|
---|
| 179 | OBSERVE;
|
---|
[fa7989] | 180 | ASSERT(mol,"Molecule that was meant to be destroyed did not exist");
|
---|
[cbc5fb] | 181 | destroyMolecule(mol->getId());
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | void World::destroyMolecule(moleculeId_t id){
|
---|
| 185 | OBSERVE;
|
---|
| 186 | molecule *mol = molecules[id];
|
---|
[6d574a] | 187 | ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
|
---|
[cbc5fb] | 188 | DeleteMolecule(mol);
|
---|
| 189 | molecules.erase(id);
|
---|
[127a8e] | 190 | releaseMoleculeId(id);
|
---|
[cbc5fb] | 191 | }
|
---|
| 192 |
|
---|
[46d958] | 193 | atom *World::createAtom(){
|
---|
| 194 | OBSERVE;
|
---|
[88d586] | 195 | atomId_t id = getNextAtomId();
|
---|
[127a8e] | 196 | ASSERT(!atoms.count(id),"proposed id did not specify an unused ID");
|
---|
[88d586] | 197 | atom *res = NewAtom(id);
|
---|
[46d958] | 198 | res->setWorld(this);
|
---|
[244d26] | 199 | // store the atom by ID
|
---|
[46d958] | 200 | atoms[res->getId()] = res;
|
---|
| 201 | return res;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[5f612ee] | 204 |
|
---|
[46d958] | 205 | int World::registerAtom(atom *atom){
|
---|
| 206 | OBSERVE;
|
---|
[88d586] | 207 | atomId_t id = getNextAtomId();
|
---|
| 208 | atom->setId(id);
|
---|
[46d958] | 209 | atom->setWorld(this);
|
---|
| 210 | atoms[atom->getId()] = atom;
|
---|
| 211 | return atom->getId();
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | void World::destroyAtom(atom* atom){
|
---|
| 215 | OBSERVE;
|
---|
| 216 | int id = atom->getId();
|
---|
| 217 | destroyAtom(id);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[cbc5fb] | 220 | void World::destroyAtom(atomId_t id) {
|
---|
[46d958] | 221 | OBSERVE;
|
---|
| 222 | atom *atom = atoms[id];
|
---|
[6d574a] | 223 | ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
|
---|
[46d958] | 224 | DeleteAtom(atom);
|
---|
| 225 | atoms.erase(id);
|
---|
[88d586] | 226 | releaseAtomId(id);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
|
---|
| 230 | OBSERVE;
|
---|
| 231 | // in case this call did not originate from inside the atom, we redirect it,
|
---|
| 232 | // to also let it know that it has changed
|
---|
| 233 | if(!target){
|
---|
| 234 | target = atoms[oldId];
|
---|
[6d574a] | 235 | ASSERT(target,"Atom with that ID not found");
|
---|
[88d586] | 236 | return target->changeId(newId);
|
---|
| 237 | }
|
---|
| 238 | else{
|
---|
| 239 | if(reserveAtomId(newId)){
|
---|
| 240 | atoms.erase(oldId);
|
---|
| 241 | atoms.insert(pair<atomId_t,atom*>(newId,target));
|
---|
| 242 | return true;
|
---|
| 243 | }
|
---|
| 244 | else{
|
---|
| 245 | return false;
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
[46d958] | 248 | }
|
---|
| 249 |
|
---|
[a7a087] | 250 | bool World::changeMoleculeId(moleculeId_t oldId, moleculeId_t newId, molecule* target){
|
---|
| 251 | OBSERVE;
|
---|
| 252 | // in case this call did not originate from inside the atom, we redirect it,
|
---|
| 253 | // to also let it know that it has changed
|
---|
| 254 | if(!target){
|
---|
| 255 | target = molecules[oldId];
|
---|
| 256 | ASSERT(target,"Molecule with that ID not found");
|
---|
| 257 | return target->changeId(newId);
|
---|
| 258 | }
|
---|
| 259 | else{
|
---|
| 260 | if(reserveMoleculeId(newId)){
|
---|
| 261 | molecules.erase(oldId);
|
---|
| 262 | molecules.insert(pair<moleculeId_t,molecule*>(newId,target));
|
---|
| 263 | return true;
|
---|
| 264 | }
|
---|
| 265 | else{
|
---|
| 266 | return false;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[7c4e29] | 271 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
|
---|
[e4afb4] | 272 | ActionTraits manipulateTrait(name);
|
---|
| 273 | return new ManipulateAtomsProcess(op, descr,manipulateTrait,false);
|
---|
[7c4e29] | 274 | }
|
---|
| 275 |
|
---|
[0e2a47] | 276 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
|
---|
| 277 | return manipulateAtoms(op,name,AllAtoms());
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[afb47f] | 280 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
|
---|
| 281 |
|
---|
| 282 | void World::doManipulate(ManipulateAtomsProcess *proc){
|
---|
| 283 | proc->signOn(this);
|
---|
| 284 | {
|
---|
| 285 | OBSERVE;
|
---|
| 286 | proc->doManipulate(this);
|
---|
| 287 | }
|
---|
| 288 | proc->signOff(this);
|
---|
| 289 | }
|
---|
[88d586] | 290 | /******************************* IDManagement *****************************/
|
---|
| 291 |
|
---|
[57adc7] | 292 | // Atoms
|
---|
| 293 |
|
---|
[88d586] | 294 | atomId_t World::getNextAtomId(){
|
---|
[127a8e] | 295 | // try to find an Id in the pool;
|
---|
| 296 | if(!atomIdPool.empty()){
|
---|
| 297 | atomIdPool_t::iterator iter=atomIdPool.begin();
|
---|
| 298 | atomId_t id = iter->first;
|
---|
[dc11c9] | 299 | range<atomId_t> newRange = makeRange(id+1,iter->last);
|
---|
[127a8e] | 300 | // we wont use this iterator anymore, so we don't care about invalidating
|
---|
| 301 | atomIdPool.erase(iter);
|
---|
[dc11c9] | 302 | if(newRange.first<newRange.last){
|
---|
[127a8e] | 303 | atomIdPool.insert(newRange);
|
---|
| 304 | }
|
---|
[23b547] | 305 | return id;
|
---|
[88d586] | 306 | }
|
---|
[127a8e] | 307 | // Nothing in the pool... we are out of luck
|
---|
| 308 | return currAtomId++;
|
---|
[88d586] | 309 | }
|
---|
| 310 |
|
---|
| 311 | void World::releaseAtomId(atomId_t id){
|
---|
[dc11c9] | 312 | atomIdPool.insert(makeRange(id,id+1));
|
---|
[127a8e] | 313 | defragAtomIdPool();
|
---|
[88d586] | 314 | }
|
---|
[afb47f] | 315 |
|
---|
[88d586] | 316 | bool World::reserveAtomId(atomId_t id){
|
---|
| 317 | if(id>=currAtomId ){
|
---|
[dc11c9] | 318 | range<atomId_t> newRange = makeRange(currAtomId,id);
|
---|
| 319 | if(newRange.first<newRange.last){
|
---|
[127a8e] | 320 | atomIdPool.insert(newRange);
|
---|
[88d586] | 321 | }
|
---|
| 322 | currAtomId=id+1;
|
---|
[127a8e] | 323 | defragAtomIdPool();
|
---|
[88d586] | 324 | return true;
|
---|
| 325 | }
|
---|
[127a8e] | 326 | // look for a range that matches the request
|
---|
| 327 | for(atomIdPool_t::iterator iter=atomIdPool.begin();iter!=atomIdPool.end();++iter){
|
---|
[dc11c9] | 328 | if(iter->isBefore(id)){
|
---|
| 329 | // we have covered all available ranges... nothing to be found here
|
---|
[127a8e] | 330 | break;
|
---|
| 331 | }
|
---|
| 332 | // no need to check first, since it has to be <=id, since otherwise we would have broken out
|
---|
[dc11c9] | 333 | if(!iter->isBeyond(id)){
|
---|
[127a8e] | 334 | // we found a matching range... get the id from this range
|
---|
| 335 |
|
---|
| 336 | // split up this range at the point of id
|
---|
[dc11c9] | 337 | range<atomId_t> bottomRange = makeRange(iter->first,id);
|
---|
| 338 | range<atomId_t> topRange = makeRange(id+1,iter->last);
|
---|
[127a8e] | 339 | // remove this range
|
---|
| 340 | atomIdPool.erase(iter);
|
---|
[dc11c9] | 341 | if(bottomRange.first<bottomRange.last){
|
---|
[127a8e] | 342 | atomIdPool.insert(bottomRange);
|
---|
| 343 | }
|
---|
[dc11c9] | 344 | if(topRange.first<topRange.last){
|
---|
[127a8e] | 345 | atomIdPool.insert(topRange);
|
---|
| 346 | }
|
---|
| 347 | defragAtomIdPool();
|
---|
| 348 | return true;
|
---|
| 349 | }
|
---|
[88d586] | 350 | }
|
---|
[127a8e] | 351 | // this ID could not be reserved
|
---|
| 352 | return false;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | void World::defragAtomIdPool(){
|
---|
| 356 | // check if the situation is bad enough to make defragging neccessary
|
---|
| 357 | if((numAtomDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
|
---|
| 358 | (atomIdPool.size()<lastAtomPoolSize+MAX_POOL_FRAGMENTATION)){
|
---|
| 359 | ++numAtomDefragSkips;
|
---|
| 360 | return;
|
---|
| 361 | }
|
---|
| 362 | for(atomIdPool_t::iterator iter = atomIdPool.begin();iter!=atomIdPool.end();){
|
---|
| 363 | // see if this range is adjacent to the next one
|
---|
| 364 | atomIdPool_t::iterator next = iter;
|
---|
| 365 | next++;
|
---|
[dc11c9] | 366 | if(next!=atomIdPool.end() && (next->first==iter->last)){
|
---|
[127a8e] | 367 | // merge the two ranges
|
---|
[dc11c9] | 368 | range<atomId_t> newRange = makeRange(iter->first,next->last);
|
---|
[127a8e] | 369 | atomIdPool.erase(iter);
|
---|
| 370 | atomIdPool.erase(next);
|
---|
| 371 | pair<atomIdPool_t::iterator,bool> res = atomIdPool.insert(newRange);
|
---|
| 372 | ASSERT(res.second,"Id-Pool was confused");
|
---|
| 373 | iter=res.first;
|
---|
| 374 | continue;
|
---|
| 375 | }
|
---|
| 376 | ++iter;
|
---|
| 377 | }
|
---|
| 378 | if(!atomIdPool.empty()){
|
---|
| 379 | // check if the last range is at the border
|
---|
| 380 | atomIdPool_t::iterator iter = atomIdPool.end();
|
---|
| 381 | iter--;
|
---|
[dc11c9] | 382 | if(iter->last==currAtomId){
|
---|
[127a8e] | 383 | currAtomId=iter->first;
|
---|
| 384 | atomIdPool.erase(iter);
|
---|
| 385 | }
|
---|
[88d586] | 386 | }
|
---|
[127a8e] | 387 | lastAtomPoolSize=atomIdPool.size();
|
---|
| 388 | numAtomDefragSkips=0;
|
---|
[88d586] | 389 | }
|
---|
[57adc7] | 390 |
|
---|
| 391 | // Molecules
|
---|
| 392 |
|
---|
[127a8e] | 393 | moleculeId_t World::getNextMoleculeId(){
|
---|
| 394 | // try to find an Id in the pool;
|
---|
| 395 | if(!moleculeIdPool.empty()){
|
---|
| 396 | moleculeIdPool_t::iterator iter=moleculeIdPool.begin();
|
---|
| 397 | moleculeId_t id = iter->first;
|
---|
[dc11c9] | 398 | range<moleculeId_t> newRange = makeRange(id+1,iter->last);
|
---|
[127a8e] | 399 | // we wont use this iterator anymore, so we don't care about invalidating
|
---|
| 400 | moleculeIdPool.erase(iter);
|
---|
[dc11c9] | 401 | if(newRange.first<newRange.last){
|
---|
[127a8e] | 402 | moleculeIdPool.insert(newRange);
|
---|
| 403 | }
|
---|
| 404 | return id;
|
---|
| 405 | }
|
---|
| 406 | // Nothing in the pool... we are out of luck
|
---|
| 407 | return currMoleculeId++;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | void World::releaseMoleculeId(moleculeId_t id){
|
---|
[dc11c9] | 411 | moleculeIdPool.insert(makeRange(id,id+1));
|
---|
[127a8e] | 412 | defragMoleculeIdPool();
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | bool World::reserveMoleculeId(moleculeId_t id){
|
---|
| 416 | if(id>=currMoleculeId ){
|
---|
[dc11c9] | 417 | range<moleculeId_t> newRange = makeRange(currMoleculeId,id);
|
---|
| 418 | if(newRange.first<newRange.last){
|
---|
[127a8e] | 419 | moleculeIdPool.insert(newRange);
|
---|
| 420 | }
|
---|
| 421 | currMoleculeId=id+1;
|
---|
| 422 | defragMoleculeIdPool();
|
---|
| 423 | return true;
|
---|
| 424 | }
|
---|
| 425 | // look for a range that matches the request
|
---|
| 426 | for(moleculeIdPool_t::iterator iter=moleculeIdPool.begin();iter!=moleculeIdPool.end();++iter){
|
---|
[dc11c9] | 427 | if(iter->isBefore(id)){
|
---|
[127a8e] | 428 | // we have coverd all available ranges... nothing to be found here
|
---|
| 429 | break;
|
---|
| 430 | }
|
---|
| 431 | // no need to check first, since it has to be <=id, since otherwise we would have broken out
|
---|
[dc11c9] | 432 | if(!iter->isBeyond(id)){
|
---|
[127a8e] | 433 | // we found a matching range... get the id from this range
|
---|
| 434 |
|
---|
| 435 | // split up this range at the point of id
|
---|
[dc11c9] | 436 | range<moleculeId_t> bottomRange = makeRange(iter->first,id);
|
---|
| 437 | range<moleculeId_t> topRange = makeRange(id+1,iter->last);
|
---|
[127a8e] | 438 | // remove this range
|
---|
| 439 | moleculeIdPool.erase(iter);
|
---|
[dc11c9] | 440 | if(bottomRange.first<bottomRange.last){
|
---|
[127a8e] | 441 | moleculeIdPool.insert(bottomRange);
|
---|
| 442 | }
|
---|
[dc11c9] | 443 | if(topRange.first<topRange.last){
|
---|
[127a8e] | 444 | moleculeIdPool.insert(topRange);
|
---|
| 445 | }
|
---|
| 446 | defragMoleculeIdPool();
|
---|
| 447 | return true;
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
| 450 | // this ID could not be reserved
|
---|
| 451 | return false;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | void World::defragMoleculeIdPool(){
|
---|
| 455 | // check if the situation is bad enough to make defragging neccessary
|
---|
| 456 | if((numMoleculeDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
|
---|
| 457 | (moleculeIdPool.size()<lastMoleculePoolSize+MAX_POOL_FRAGMENTATION)){
|
---|
| 458 | ++numMoleculeDefragSkips;
|
---|
| 459 | return;
|
---|
| 460 | }
|
---|
| 461 | for(moleculeIdPool_t::iterator iter = moleculeIdPool.begin();iter!=moleculeIdPool.end();){
|
---|
| 462 | // see if this range is adjacent to the next one
|
---|
| 463 | moleculeIdPool_t::iterator next = iter;
|
---|
| 464 | next++;
|
---|
[dc11c9] | 465 | if(next!=moleculeIdPool.end() && (next->first==iter->last)){
|
---|
[127a8e] | 466 | // merge the two ranges
|
---|
[dc11c9] | 467 | range<moleculeId_t> newRange = makeRange(iter->first,next->last);
|
---|
[127a8e] | 468 | moleculeIdPool.erase(iter);
|
---|
| 469 | moleculeIdPool.erase(next);
|
---|
| 470 | pair<moleculeIdPool_t::iterator,bool> res = moleculeIdPool.insert(newRange);
|
---|
| 471 | ASSERT(res.second,"Id-Pool was confused");
|
---|
| 472 | iter=res.first;
|
---|
| 473 | continue;
|
---|
| 474 | }
|
---|
| 475 | ++iter;
|
---|
| 476 | }
|
---|
| 477 | if(!moleculeIdPool.empty()){
|
---|
| 478 | // check if the last range is at the border
|
---|
| 479 | moleculeIdPool_t::iterator iter = moleculeIdPool.end();
|
---|
| 480 | iter--;
|
---|
[dc11c9] | 481 | if(iter->last==currMoleculeId){
|
---|
[127a8e] | 482 | currMoleculeId=iter->first;
|
---|
| 483 | moleculeIdPool.erase(iter);
|
---|
| 484 | }
|
---|
| 485 | }
|
---|
| 486 | lastMoleculePoolSize=moleculeIdPool.size();
|
---|
| 487 | numMoleculeDefragSkips=0;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[865a945] | 490 | /******************************* Iterators ********************************/
|
---|
| 491 |
|
---|
[fa0b18] | 492 | // external parts with observers
|
---|
| 493 |
|
---|
[6e97e5] | 494 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
|
---|
| 495 |
|
---|
[fa0b18] | 496 | World::AtomIterator
|
---|
| 497 | World::getAtomIter(AtomDescriptor descr){
|
---|
| 498 | return AtomIterator(descr,atoms);
|
---|
| 499 | }
|
---|
[865a945] | 500 |
|
---|
[fa0b18] | 501 | World::AtomIterator
|
---|
| 502 | World::getAtomIter(){
|
---|
| 503 | return AtomIterator(AllAtoms(),atoms);
|
---|
[865a945] | 504 | }
|
---|
[354859] | 505 |
|
---|
[fa0b18] | 506 | World::AtomIterator
|
---|
| 507 | World::atomEnd(){
|
---|
[6e97e5] | 508 | return AtomIterator(AllAtoms(),atoms,atoms.end());
|
---|
[7c4e29] | 509 | }
|
---|
| 510 |
|
---|
[6e97e5] | 511 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
|
---|
| 512 |
|
---|
[5d880e] | 513 | World::MoleculeIterator
|
---|
| 514 | World::getMoleculeIter(MoleculeDescriptor descr){
|
---|
| 515 | return MoleculeIterator(descr,molecules);
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | World::MoleculeIterator
|
---|
| 519 | World::getMoleculeIter(){
|
---|
| 520 | return MoleculeIterator(AllMolecules(),molecules);
|
---|
[1c51c8] | 521 | }
|
---|
| 522 |
|
---|
[5d880e] | 523 | World::MoleculeIterator
|
---|
| 524 | World::moleculeEnd(){
|
---|
[6e97e5] | 525 | return MoleculeIterator(AllMolecules(),molecules,molecules.end());
|
---|
[1c51c8] | 526 | }
|
---|
| 527 |
|
---|
[fa0b18] | 528 | // Internal parts, without observers
|
---|
| 529 |
|
---|
| 530 | // Build the AtomIterator from template
|
---|
| 531 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet::set_t,AtomDescriptor);
|
---|
| 532 |
|
---|
| 533 |
|
---|
| 534 | World::internal_AtomIterator
|
---|
| 535 | World::getAtomIter_internal(AtomDescriptor descr){
|
---|
| 536 | return internal_AtomIterator(descr,atoms.getContent());
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | World::internal_AtomIterator
|
---|
| 540 | World::atomEnd_internal(){
|
---|
| 541 | return internal_AtomIterator(AllAtoms(),atoms.getContent(),atoms.end_internal());
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[6e97e5] | 544 | // build the MoleculeIterator from template
|
---|
[e3d865] | 545 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet::set_t,MoleculeDescriptor);
|
---|
[6e97e5] | 546 |
|
---|
[e3d865] | 547 | World::internal_MoleculeIterator World::getMoleculeIter_internal(MoleculeDescriptor descr){
|
---|
| 548 | return internal_MoleculeIterator(descr,molecules.getContent());
|
---|
[1c51c8] | 549 | }
|
---|
| 550 |
|
---|
[e3d865] | 551 | World::internal_MoleculeIterator World::moleculeEnd_internal(){
|
---|
| 552 | return internal_MoleculeIterator(AllMolecules(),molecules.getContent(),molecules.end_internal());
|
---|
[1c51c8] | 553 | }
|
---|
| 554 |
|
---|
[90c4280] | 555 | /************************** Selection of Atoms and molecules ******************/
|
---|
| 556 |
|
---|
| 557 | // Atoms
|
---|
| 558 |
|
---|
| 559 | void World::clearAtomSelection(){
|
---|
| 560 | selectedAtoms.clear();
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[e4afb4] | 563 | void World::selectAtom(const atom *_atom){
|
---|
| 564 | // atom * is unchanged in this function, but we do store entity as changeable
|
---|
| 565 | ASSERT(_atom,"Invalid pointer in selection of atom");
|
---|
| 566 | selectedAtoms[_atom->getId()]=const_cast<atom *>(_atom);
|
---|
[90c4280] | 567 | }
|
---|
| 568 |
|
---|
[e4afb4] | 569 | void World::selectAtom(const atomId_t id){
|
---|
[90c4280] | 570 | ASSERT(atoms.count(id),"Atom Id selected that was not in the world");
|
---|
| 571 | selectedAtoms[id]=atoms[id];
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | void World::selectAllAtoms(AtomDescriptor descr){
|
---|
| 575 | internal_AtomIterator begin = getAtomIter_internal(descr);
|
---|
| 576 | internal_AtomIterator end = atomEnd_internal();
|
---|
[e4afb4] | 577 | void (World::*func)(const atom*) = &World::selectAtom; // needed for type resolution of overloaded function
|
---|
[90c4280] | 578 | for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
|
---|
| 579 | }
|
---|
| 580 |
|
---|
[e4afb4] | 581 | void World::selectAtomsOfMolecule(const molecule *_mol){
|
---|
[90c4280] | 582 | ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
|
---|
| 583 | // need to make it const to get the fast iterators
|
---|
| 584 | const molecule *mol = _mol;
|
---|
[e4afb4] | 585 | void (World::*func)(const atom*) = &World::selectAtom; // needed for type resolution of overloaded function
|
---|
[90c4280] | 586 | for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is select... see above
|
---|
| 587 | }
|
---|
| 588 |
|
---|
[e4afb4] | 589 | void World::selectAtomsOfMolecule(const moleculeId_t id){
|
---|
[90c4280] | 590 | ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
|
---|
| 591 | selectAtomsOfMolecule(molecules[id]);
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[e4afb4] | 594 | void World::unselectAtom(const atom *_atom){
|
---|
| 595 | ASSERT(_atom,"Invalid pointer in unselection of atom");
|
---|
| 596 | unselectAtom(_atom->getId());
|
---|
[61d655e] | 597 | }
|
---|
| 598 |
|
---|
[e4afb4] | 599 | void World::unselectAtom(const atomId_t id){
|
---|
[61d655e] | 600 | ASSERT(atoms.count(id),"Atom Id unselected that was not in the world");
|
---|
| 601 | selectedAtoms.erase(id);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | void World::unselectAllAtoms(AtomDescriptor descr){
|
---|
| 605 | internal_AtomIterator begin = getAtomIter_internal(descr);
|
---|
| 606 | internal_AtomIterator end = atomEnd_internal();
|
---|
[e4afb4] | 607 | void (World::*func)(const atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
|
---|
[61d655e] | 608 | for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[e4afb4] | 611 | void World::unselectAtomsOfMolecule(const molecule *_mol){
|
---|
[61d655e] | 612 | ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
|
---|
| 613 | // need to make it const to get the fast iterators
|
---|
| 614 | const molecule *mol = _mol;
|
---|
[e4afb4] | 615 | void (World::*func)(const atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
|
---|
[61d655e] | 616 | for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is unsselect... see above
|
---|
| 617 | }
|
---|
| 618 |
|
---|
[e4afb4] | 619 | void World::unselectAtomsOfMolecule(const moleculeId_t id){
|
---|
[61d655e] | 620 | ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
|
---|
| 621 | unselectAtomsOfMolecule(molecules[id]);
|
---|
| 622 | }
|
---|
| 623 |
|
---|
[e472eab] | 624 | size_t World::countSelectedAtoms() const {
|
---|
[eacc3b] | 625 | size_t count = 0;
|
---|
[e472eab] | 626 | for (AtomSet::const_iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter)
|
---|
[eacc3b] | 627 | count++;
|
---|
| 628 | return count;
|
---|
| 629 | }
|
---|
| 630 |
|
---|
[e4afb4] | 631 | bool World::isSelected(const atom *_atom) const {
|
---|
| 632 | return selectedAtoms.find(_atom->getId()) != selectedAtoms.end();
|
---|
[e0e156] | 633 | }
|
---|
| 634 |
|
---|
[e472eab] | 635 | const std::vector<atom *> World::getSelectedAtoms() const {
|
---|
| 636 | std::vector<atom *> returnAtoms;
|
---|
| 637 | returnAtoms.resize(countSelectedAtoms());
|
---|
| 638 | int count = 0;
|
---|
| 639 | for (AtomSet::const_iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter)
|
---|
| 640 | returnAtoms[count++] = iter->second;
|
---|
| 641 | return returnAtoms;
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 |
|
---|
[90c4280] | 645 | // Molecules
|
---|
| 646 |
|
---|
| 647 | void World::clearMoleculeSelection(){
|
---|
| 648 | selectedMolecules.clear();
|
---|
| 649 | }
|
---|
| 650 |
|
---|
[e4afb4] | 651 | void World::selectMolecule(const molecule *_mol){
|
---|
| 652 | // molecule * is unchanged in this function, but we do store entity as changeable
|
---|
| 653 | ASSERT(_mol,"Invalid pointer to molecule in selection");
|
---|
| 654 | selectedMolecules[_mol->getId()]=const_cast<molecule *>(_mol);
|
---|
[90c4280] | 655 | }
|
---|
| 656 |
|
---|
[e4afb4] | 657 | void World::selectMolecule(const moleculeId_t id){
|
---|
[90c4280] | 658 | ASSERT(molecules.count(id),"Molecule Id selected that was not in the world");
|
---|
| 659 | selectedMolecules[id]=molecules[id];
|
---|
| 660 | }
|
---|
| 661 |
|
---|
[e472eab] | 662 | void World::selectAllMolecules(MoleculeDescriptor descr){
|
---|
[90c4280] | 663 | internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
|
---|
| 664 | internal_MoleculeIterator end = moleculeEnd_internal();
|
---|
[e4afb4] | 665 | void (World::*func)(const molecule*) = &World::selectMolecule; // needed for type resolution of overloaded function
|
---|
[90c4280] | 666 | for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
|
---|
| 667 | }
|
---|
| 668 |
|
---|
[e4afb4] | 669 | void World::selectMoleculeOfAtom(const atom *_atom){
|
---|
| 670 | ASSERT(_atom,"Invalid atom pointer in selection of MoleculeOfAtom");
|
---|
| 671 | molecule *mol=_atom->getMolecule();
|
---|
[90c4280] | 672 | // the atom might not be part of a molecule
|
---|
| 673 | if(mol){
|
---|
| 674 | selectMolecule(mol);
|
---|
| 675 | }
|
---|
| 676 | }
|
---|
| 677 |
|
---|
[e4afb4] | 678 | void World::selectMoleculeOfAtom(const atomId_t id){
|
---|
[90c4280] | 679 | ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
|
---|
| 680 | selectMoleculeOfAtom(atoms[id]);
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[e4afb4] | 683 | void World::unselectMolecule(const molecule *_mol){
|
---|
| 684 | ASSERT(_mol,"invalid pointer in unselection of molecule");
|
---|
| 685 | unselectMolecule(_mol->getId());
|
---|
[61d655e] | 686 | }
|
---|
| 687 |
|
---|
[e4afb4] | 688 | void World::unselectMolecule(const moleculeId_t id){
|
---|
[61d655e] | 689 | ASSERT(molecules.count(id),"No such molecule with ID in unselection");
|
---|
| 690 | selectedMolecules.erase(id);
|
---|
| 691 | }
|
---|
| 692 |
|
---|
[e472eab] | 693 | void World::unselectAllMolecules(MoleculeDescriptor descr){
|
---|
[61d655e] | 694 | internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
|
---|
| 695 | internal_MoleculeIterator end = moleculeEnd_internal();
|
---|
[e4afb4] | 696 | void (World::*func)(const molecule*) = &World::unselectMolecule; // needed for type resolution of overloaded function
|
---|
[61d655e] | 697 | for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
|
---|
| 698 | }
|
---|
| 699 |
|
---|
[e4afb4] | 700 | void World::unselectMoleculeOfAtom(const atom *_atom){
|
---|
| 701 | ASSERT(_atom,"Invalid atom pointer in selection of MoleculeOfAtom");
|
---|
| 702 | molecule *mol=_atom->getMolecule();
|
---|
[61d655e] | 703 | // the atom might not be part of a molecule
|
---|
| 704 | if(mol){
|
---|
| 705 | unselectMolecule(mol);
|
---|
| 706 | }
|
---|
| 707 | }
|
---|
| 708 |
|
---|
[e4afb4] | 709 | void World::unselectMoleculeOfAtom(const atomId_t id){
|
---|
[61d655e] | 710 | ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
|
---|
| 711 | unselectMoleculeOfAtom(atoms[id]);
|
---|
| 712 | }
|
---|
| 713 |
|
---|
[e472eab] | 714 | size_t World::countSelectedMolecules() const {
|
---|
[eacc3b] | 715 | size_t count = 0;
|
---|
[e472eab] | 716 | for (MoleculeSet::const_iterator iter = selectedMolecules.begin(); iter != selectedMolecules.end(); ++iter)
|
---|
[eacc3b] | 717 | count++;
|
---|
| 718 | return count;
|
---|
| 719 | }
|
---|
| 720 |
|
---|
[e4afb4] | 721 | bool World::isSelected(const molecule *_mol) const {
|
---|
| 722 | return selectedMolecules.find(_mol->getId()) != selectedMolecules.end();
|
---|
[e0e156] | 723 | }
|
---|
| 724 |
|
---|
[e472eab] | 725 | const std::vector<molecule *> World::getSelectedMolecules() const {
|
---|
| 726 | std::vector<molecule *> returnMolecules;
|
---|
| 727 | returnMolecules.resize(countSelectedMolecules());
|
---|
| 728 | int count = 0;
|
---|
| 729 | for (MoleculeSet::const_iterator iter = selectedMolecules.begin(); iter != selectedMolecules.end(); ++iter)
|
---|
| 730 | returnMolecules[count++] = iter->second;
|
---|
| 731 | return returnMolecules;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
[3839e5] | 734 | /******************* Iterators over Selection *****************************/
|
---|
| 735 | World::AtomSelectionIterator World::beginAtomSelection(){
|
---|
| 736 | return selectedAtoms.begin();
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | World::AtomSelectionIterator World::endAtomSelection(){
|
---|
| 740 | return selectedAtoms.end();
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 |
|
---|
| 744 | World::MoleculeSelectionIterator World::beginMoleculeSelection(){
|
---|
| 745 | return selectedMolecules.begin();
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | World::MoleculeSelectionIterator World::endMoleculeSelection(){
|
---|
| 749 | return selectedMolecules.end();
|
---|
| 750 | }
|
---|
| 751 |
|
---|
[5d1611] | 752 | /******************************* Singleton Stuff **************************/
|
---|
| 753 |
|
---|
[7a1ce5] | 754 | World::World() :
|
---|
[cd5047] | 755 | Observable("World"),
|
---|
[f71baf] | 756 | BG(new BondGraph(true)), // assume Angstroem for the moment
|
---|
[354859] | 757 | periode(new periodentafel),
|
---|
[8e1f7af] | 758 | configuration(new config),
|
---|
[43dad6] | 759 | Thermostats(new ThermoStatContainer),
|
---|
[e4b5de] | 760 | ExitFlag(0),
|
---|
[fa0b18] | 761 | atoms(this),
|
---|
[90c4280] | 762 | selectedAtoms(this),
|
---|
[24a5e0] | 763 | currAtomId(0),
|
---|
[127a8e] | 764 | lastAtomPoolSize(0),
|
---|
| 765 | numAtomDefragSkips(0),
|
---|
[51be2a] | 766 | molecules(this),
|
---|
[90c4280] | 767 | selectedMolecules(this),
|
---|
[24a5e0] | 768 | currMoleculeId(0),
|
---|
[654394] | 769 | lastMoleculePoolSize(0),
|
---|
| 770 | numMoleculeDefragSkips(0),
|
---|
[24a5e0] | 771 | molecules_deprecated(new MoleculeListClass(this))
|
---|
[7dad10] | 772 | {
|
---|
[84c494] | 773 | cell_size = new Box;
|
---|
[cca9ef] | 774 | RealSpaceMatrix domain;
|
---|
[84c494] | 775 | domain.at(0,0) = 20;
|
---|
| 776 | domain.at(1,1) = 20;
|
---|
| 777 | domain.at(2,2) = 20;
|
---|
| 778 | cell_size->setM(domain);
|
---|
[387b36] | 779 | defaultName = "none";
|
---|
[7dad10] | 780 | molecules_deprecated->signOn(this);
|
---|
| 781 | }
|
---|
[5d1611] | 782 |
|
---|
| 783 | World::~World()
|
---|
[354859] | 784 | {
|
---|
[028c2e] | 785 | molecules_deprecated->signOff(this);
|
---|
[84c494] | 786 | delete cell_size;
|
---|
[46d958] | 787 | delete molecules_deprecated;
|
---|
[cbc5fb] | 788 | MoleculeSet::iterator molIter;
|
---|
| 789 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
|
---|
| 790 | DeleteMolecule((*molIter).second);
|
---|
| 791 | }
|
---|
| 792 | molecules.clear();
|
---|
| 793 | AtomSet::iterator atIter;
|
---|
| 794 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
|
---|
| 795 | DeleteAtom((*atIter).second);
|
---|
[46d958] | 796 | }
|
---|
| 797 | atoms.clear();
|
---|
[f71baf] | 798 | delete BG;
|
---|
[6cb9c76] | 799 | delete periode;
|
---|
| 800 | delete configuration;
|
---|
| 801 | delete Thermostats;
|
---|
[354859] | 802 | }
|
---|
[5d1611] | 803 |
|
---|
[23b547] | 804 | // Explicit instantiation of the singleton mechanism at this point
|
---|
[5d1611] | 805 |
|
---|
[23b547] | 806 | CONSTRUCT_SINGLETON(World)
|
---|
[5d1611] | 807 |
|
---|
[5f1d5b8] | 808 | CONSTRUCT_OBSERVEDCONTAINER(World::AtomSTLSet)
|
---|
| 809 |
|
---|
| 810 | CONSTRUCT_OBSERVEDCONTAINER(World::MoleculeSTLSet)
|
---|
| 811 |
|
---|
[5d1611] | 812 | /******************************* deprecated Legacy Stuff ***********************/
|
---|
| 813 |
|
---|
[354859] | 814 | MoleculeListClass *&World::getMolecules() {
|
---|
| 815 | return molecules_deprecated;
|
---|
[5d1611] | 816 | }
|
---|