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