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