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