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