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