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