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