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