[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 |
|
---|
[d346b6] | 12 | #include "atom.hpp"
|
---|
[8e1f7af] | 13 | #include "config.hpp"
|
---|
[354859] | 14 | #include "molecule.hpp"
|
---|
| 15 | #include "periodentafel.hpp"
|
---|
[fc1b24] | 16 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
[865a945] | 17 | #include "Descriptors/AtomDescriptor_impl.hpp"
|
---|
[1c51c8] | 18 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 19 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
|
---|
[6e97e5] | 20 | #include "Descriptors/SelectiveIterator_impl.hpp"
|
---|
[7c4e29] | 21 | #include "Actions/ManipulateAtomsProcess.hpp"
|
---|
[6d574a] | 22 | #include "Helpers/Assert.hpp"
|
---|
[d346b6] | 23 |
|
---|
[23b547] | 24 | #include "Patterns/Singleton_impl.hpp"
|
---|
| 25 |
|
---|
[d346b6] | 26 | using namespace std;
|
---|
[4d9c01] | 27 |
|
---|
[5d1611] | 28 | /******************************* getter and setter ************************/
|
---|
[354859] | 29 | periodentafel *&World::getPeriode(){
|
---|
[5d1611] | 30 | return periode;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[8e1f7af] | 33 | config *&World::getConfig(){
|
---|
| 34 | return configuration;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[1c51c8] | 37 | // Atoms
|
---|
| 38 |
|
---|
[7a1ce5] | 39 | atom* World::getAtom(AtomDescriptor descriptor){
|
---|
[fc1b24] | 40 | return descriptor.find();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[7a1ce5] | 43 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
|
---|
[fc1b24] | 44 | return descriptor.findAll();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[0e2a47] | 47 | vector<atom*> World::getAllAtoms(){
|
---|
| 48 | return getAllAtoms(AllAtoms());
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[354859] | 51 | int World::numAtoms(){
|
---|
| 52 | return atoms.size();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[1c51c8] | 55 | // Molecules
|
---|
| 56 |
|
---|
| 57 | molecule *World::getMolecule(MoleculeDescriptor descriptor){
|
---|
| 58 | return descriptor.find();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
|
---|
| 62 | return descriptor.findAll();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[97ebf8] | 65 | std::vector<molecule*> World::getAllMolecules(){
|
---|
| 66 | return getAllMolecules(AllMolecules());
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[354859] | 69 | int World::numMolecules(){
|
---|
| 70 | return molecules_deprecated->ListOfMolecules.size();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[5f612ee] | 73 | // system
|
---|
| 74 |
|
---|
| 75 | double * World::getDomain() {
|
---|
| 76 | return cell_size;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void World::setDomain(double * matrix)
|
---|
| 80 | {
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[387b36] | 84 | std::string World::getDefaultName() {
|
---|
[5f612ee] | 85 | return defaultName;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[387b36] | 88 | void World::setDefaultName(std::string name)
|
---|
[5f612ee] | 89 | {
|
---|
[387b36] | 90 | defaultName = name;
|
---|
[5f612ee] | 91 | };
|
---|
| 92 |
|
---|
[e4b5de] | 93 | int World::getExitFlag() {
|
---|
| 94 | return ExitFlag;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void World::setExitFlag(int flag) {
|
---|
| 98 | if (ExitFlag < flag)
|
---|
| 99 | ExitFlag = flag;
|
---|
| 100 | }
|
---|
[5f612ee] | 101 |
|
---|
[afb47f] | 102 | /******************** Methods to change World state *********************/
|
---|
| 103 |
|
---|
[354859] | 104 | molecule* World::createMolecule(){
|
---|
| 105 | OBSERVE;
|
---|
| 106 | molecule *mol = NULL;
|
---|
[cbc5fb] | 107 | mol = NewMolecule();
|
---|
[6d574a] | 108 | ASSERT(!molecules.count(currMoleculeId),"currMoleculeId did not specify an unused ID");
|
---|
[cbc5fb] | 109 | mol->setId(currMoleculeId++);
|
---|
[244d26] | 110 | // store the molecule by ID
|
---|
[cbc5fb] | 111 | molecules[mol->getId()] = mol;
|
---|
[354859] | 112 | mol->signOn(this);
|
---|
| 113 | return mol;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[cbc5fb] | 116 | void World::destroyMolecule(molecule* mol){
|
---|
| 117 | OBSERVE;
|
---|
| 118 | destroyMolecule(mol->getId());
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void World::destroyMolecule(moleculeId_t id){
|
---|
| 122 | OBSERVE;
|
---|
| 123 | molecule *mol = molecules[id];
|
---|
[6d574a] | 124 | ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
|
---|
[cbc5fb] | 125 | DeleteMolecule(mol);
|
---|
| 126 | molecules.erase(id);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[5f612ee] | 129 | double *World::cell_size = NULL;
|
---|
[7c4e29] | 130 |
|
---|
[46d958] | 131 | atom *World::createAtom(){
|
---|
| 132 | OBSERVE;
|
---|
[88d586] | 133 | atomId_t id = getNextAtomId();
|
---|
| 134 | atom *res = NewAtom(id);
|
---|
[46d958] | 135 | res->setWorld(this);
|
---|
[244d26] | 136 | // store the atom by ID
|
---|
[46d958] | 137 | atoms[res->getId()] = res;
|
---|
| 138 | return res;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[5f612ee] | 141 |
|
---|
[46d958] | 142 | int World::registerAtom(atom *atom){
|
---|
| 143 | OBSERVE;
|
---|
[88d586] | 144 | atomId_t id = getNextAtomId();
|
---|
| 145 | atom->setId(id);
|
---|
[46d958] | 146 | atom->setWorld(this);
|
---|
| 147 | atoms[atom->getId()] = atom;
|
---|
| 148 | return atom->getId();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | void World::destroyAtom(atom* atom){
|
---|
| 152 | OBSERVE;
|
---|
| 153 | int id = atom->getId();
|
---|
| 154 | destroyAtom(id);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[cbc5fb] | 157 | void World::destroyAtom(atomId_t id) {
|
---|
[46d958] | 158 | OBSERVE;
|
---|
| 159 | atom *atom = atoms[id];
|
---|
[6d574a] | 160 | ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
|
---|
[46d958] | 161 | DeleteAtom(atom);
|
---|
| 162 | atoms.erase(id);
|
---|
[88d586] | 163 | releaseAtomId(id);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
|
---|
| 167 | OBSERVE;
|
---|
| 168 | // in case this call did not originate from inside the atom, we redirect it,
|
---|
| 169 | // to also let it know that it has changed
|
---|
| 170 | if(!target){
|
---|
| 171 | target = atoms[oldId];
|
---|
[6d574a] | 172 | ASSERT(target,"Atom with that ID not found");
|
---|
[88d586] | 173 | return target->changeId(newId);
|
---|
| 174 | }
|
---|
| 175 | else{
|
---|
| 176 | if(reserveAtomId(newId)){
|
---|
| 177 | atoms.erase(oldId);
|
---|
| 178 | atoms.insert(pair<atomId_t,atom*>(newId,target));
|
---|
| 179 | return true;
|
---|
| 180 | }
|
---|
| 181 | else{
|
---|
| 182 | return false;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
[46d958] | 185 | }
|
---|
| 186 |
|
---|
[7c4e29] | 187 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
|
---|
| 188 | return new ManipulateAtomsProcess(op, descr,name,true);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[0e2a47] | 191 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
|
---|
| 192 | return manipulateAtoms(op,name,AllAtoms());
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[afb47f] | 195 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
|
---|
| 196 |
|
---|
| 197 | void World::doManipulate(ManipulateAtomsProcess *proc){
|
---|
| 198 | proc->signOn(this);
|
---|
| 199 | {
|
---|
| 200 | OBSERVE;
|
---|
| 201 | proc->doManipulate(this);
|
---|
| 202 | }
|
---|
| 203 | proc->signOff(this);
|
---|
| 204 | }
|
---|
[88d586] | 205 | /******************************* IDManagement *****************************/
|
---|
| 206 |
|
---|
[57adc7] | 207 | // Atoms
|
---|
| 208 |
|
---|
[88d586] | 209 | atomId_t World::getNextAtomId(){
|
---|
| 210 | // see if we can reuse some Id
|
---|
| 211 | if(atomIdPool.empty()){
|
---|
| 212 | return currAtomId++;
|
---|
| 213 | }
|
---|
| 214 | else{
|
---|
| 215 | // we give out the first ID from the pool
|
---|
| 216 | atomId_t id = *(atomIdPool.begin());
|
---|
| 217 | atomIdPool.erase(id);
|
---|
[23b547] | 218 | return id;
|
---|
[88d586] | 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | void World::releaseAtomId(atomId_t id){
|
---|
| 223 | atomIdPool.insert(id);
|
---|
| 224 | // defragmentation of the pool
|
---|
| 225 | set<atomId_t>::reverse_iterator iter;
|
---|
| 226 | // go through all Ids in the pool that lie immediately below the border
|
---|
| 227 | while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
|
---|
| 228 | atomIdPool.erase(--currAtomId);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
[afb47f] | 231 |
|
---|
[88d586] | 232 | bool World::reserveAtomId(atomId_t id){
|
---|
| 233 | if(id>=currAtomId ){
|
---|
| 234 | // add all ids between the new one and current border as available
|
---|
| 235 | for(atomId_t pos=currAtomId; pos<id; ++pos){
|
---|
| 236 | atomIdPool.insert(pos);
|
---|
| 237 | }
|
---|
| 238 | currAtomId=id+1;
|
---|
| 239 | return true;
|
---|
| 240 | }
|
---|
| 241 | else if(atomIdPool.count(id)){
|
---|
| 242 | atomIdPool.erase(id);
|
---|
| 243 | return true;
|
---|
| 244 | }
|
---|
| 245 | else{
|
---|
| 246 | // this ID could not be reserved
|
---|
| 247 | return false;
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
[57adc7] | 250 |
|
---|
| 251 | // Molecules
|
---|
| 252 |
|
---|
[865a945] | 253 | /******************************* Iterators ********************************/
|
---|
| 254 |
|
---|
[6e97e5] | 255 | // Build the AtomIterator from template
|
---|
| 256 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
|
---|
| 257 |
|
---|
[865a945] | 258 |
|
---|
| 259 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){
|
---|
[6e97e5] | 260 | return AtomIterator(descr,atoms);
|
---|
[865a945] | 261 | }
|
---|
[354859] | 262 |
|
---|
[6e97e5] | 263 | World::AtomIterator World::atomEnd(){
|
---|
| 264 | return AtomIterator(AllAtoms(),atoms,atoms.end());
|
---|
[7c4e29] | 265 | }
|
---|
| 266 |
|
---|
[6e97e5] | 267 | // build the MoleculeIterator from template
|
---|
| 268 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
|
---|
| 269 |
|
---|
[1c51c8] | 270 | World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
|
---|
[6e97e5] | 271 | return MoleculeIterator(descr,molecules);
|
---|
[1c51c8] | 272 | }
|
---|
| 273 |
|
---|
[6e97e5] | 274 | World::MoleculeIterator World::moleculeEnd(){
|
---|
| 275 | return MoleculeIterator(AllMolecules(),molecules,molecules.end());
|
---|
[1c51c8] | 276 | }
|
---|
| 277 |
|
---|
[5d1611] | 278 | /******************************* Singleton Stuff **************************/
|
---|
| 279 |
|
---|
[7a1ce5] | 280 | World::World() :
|
---|
[cd5047] | 281 | Observable("World"),
|
---|
[354859] | 282 | periode(new periodentafel),
|
---|
[8e1f7af] | 283 | configuration(new config),
|
---|
[e4b5de] | 284 | ExitFlag(0),
|
---|
[d2dbac0] | 285 | atoms(),
|
---|
[24a5e0] | 286 | currAtomId(0),
|
---|
| 287 | molecules(),
|
---|
| 288 | currMoleculeId(0),
|
---|
| 289 | molecules_deprecated(new MoleculeListClass(this))
|
---|
[7dad10] | 290 | {
|
---|
[b34306] | 291 | cell_size = new double[6];
|
---|
[fd179f] | 292 | cell_size[0] = 20.;
|
---|
| 293 | cell_size[1] = 0.;
|
---|
| 294 | cell_size[2] = 20.;
|
---|
| 295 | cell_size[3] = 0.;
|
---|
| 296 | cell_size[4] = 0.;
|
---|
| 297 | cell_size[5] = 20.;
|
---|
[387b36] | 298 | defaultName = "none";
|
---|
[7dad10] | 299 | molecules_deprecated->signOn(this);
|
---|
| 300 | }
|
---|
[5d1611] | 301 |
|
---|
| 302 | World::~World()
|
---|
[354859] | 303 | {
|
---|
[028c2e] | 304 | molecules_deprecated->signOff(this);
|
---|
[5f612ee] | 305 | delete[] cell_size;
|
---|
[46d958] | 306 | delete molecules_deprecated;
|
---|
[354859] | 307 | delete periode;
|
---|
[8e1f7af] | 308 | delete configuration;
|
---|
[cbc5fb] | 309 | MoleculeSet::iterator molIter;
|
---|
| 310 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
|
---|
| 311 | DeleteMolecule((*molIter).second);
|
---|
| 312 | }
|
---|
| 313 | molecules.clear();
|
---|
| 314 | AtomSet::iterator atIter;
|
---|
| 315 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
|
---|
| 316 | DeleteAtom((*atIter).second);
|
---|
[46d958] | 317 | }
|
---|
| 318 | atoms.clear();
|
---|
[354859] | 319 | }
|
---|
[5d1611] | 320 |
|
---|
[23b547] | 321 | // Explicit instantiation of the singleton mechanism at this point
|
---|
[5d1611] | 322 |
|
---|
[23b547] | 323 | CONSTRUCT_SINGLETON(World)
|
---|
[5d1611] | 324 |
|
---|
| 325 | /******************************* deprecated Legacy Stuff ***********************/
|
---|
| 326 |
|
---|
[354859] | 327 | MoleculeListClass *&World::getMolecules() {
|
---|
| 328 | return molecules_deprecated;
|
---|
[5d1611] | 329 | }
|
---|