| 1 | /* | 
|---|
| 2 | * World.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Feb 3, 2010 | 
|---|
| 5 | *      Author: crueger | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef WORLD_HPP_ | 
|---|
| 9 | #define WORLD_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 | /*********************************************** includes ***********************************/ | 
|---|
| 12 |  | 
|---|
| 13 | #include <string> | 
|---|
| 14 | #include <map> | 
|---|
| 15 | #include <vector> | 
|---|
| 16 | #include <set> | 
|---|
| 17 | #include <boost/thread.hpp> | 
|---|
| 18 | #include <boost/shared_ptr.hpp> | 
|---|
| 19 |  | 
|---|
| 20 | #include "types.hpp" | 
|---|
| 21 | #include "Descriptors/SelectiveIterator.hpp" | 
|---|
| 22 | #include "Patterns/Observer.hpp" | 
|---|
| 23 | #include "Patterns/Cacheable.hpp" | 
|---|
| 24 | #include "Patterns/Singleton.hpp" | 
|---|
| 25 | #include "Patterns/ObservedContainer.hpp" | 
|---|
| 26 | #include "Helpers/Range.hpp" | 
|---|
| 27 |  | 
|---|
| 28 | // include config.h | 
|---|
| 29 | #ifdef HAVE_CONFIG_H | 
|---|
| 30 | #include <config.h> | 
|---|
| 31 | #endif | 
|---|
| 32 |  | 
|---|
| 33 | // forward declarations | 
|---|
| 34 | class atom; | 
|---|
| 35 | class AtomDescriptor; | 
|---|
| 36 | class AtomDescriptor_impl; | 
|---|
| 37 | template<typename T> class AtomsCalculation; | 
|---|
| 38 | class Box; | 
|---|
| 39 | class config; | 
|---|
| 40 | class ManipulateAtomsProcess; | 
|---|
| 41 | class Matrix; | 
|---|
| 42 | class molecule; | 
|---|
| 43 | class MoleculeDescriptor; | 
|---|
| 44 | class MoleculeDescriptor_impl; | 
|---|
| 45 | class MoleculeListClass; | 
|---|
| 46 | class periodentafel; | 
|---|
| 47 | class ThermoStatContainer; | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | /****************************************** forward declarations *****************************/ | 
|---|
| 51 |  | 
|---|
| 52 | /********************************************** Class World *******************************/ | 
|---|
| 53 |  | 
|---|
| 54 | class World : public Singleton<World>, public Observable | 
|---|
| 55 | { | 
|---|
| 56 |  | 
|---|
| 57 | // Make access to constructor and destructor possible from inside the singleton | 
|---|
| 58 | friend class Singleton<World>; | 
|---|
| 59 |  | 
|---|
| 60 | // necessary for coupling with descriptors | 
|---|
| 61 | friend class AtomDescriptor_impl; | 
|---|
| 62 | friend class AtomDescriptor; | 
|---|
| 63 | friend class MoleculeDescriptor_impl; | 
|---|
| 64 | friend class MoleculeDescriptor; | 
|---|
| 65 | // coupling with descriptors over selection | 
|---|
| 66 | friend class AtomSelectionDescriptor_impl; | 
|---|
| 67 | friend class MoleculeSelectionDescriptor_impl; | 
|---|
| 68 |  | 
|---|
| 69 | // Actions, calculations etc associated with the World | 
|---|
| 70 | friend class ManipulateAtomsProcess; | 
|---|
| 71 | template<typename> friend class AtomsCalculation; | 
|---|
| 72 | public: | 
|---|
| 73 |  | 
|---|
| 74 | // Types for Atom and Molecule structures | 
|---|
| 75 | typedef ObservedContainer<std::map<atomId_t,atom*> > AtomSet; | 
|---|
| 76 | typedef ObservedContainer<std::map<moleculeId_t,molecule*> > MoleculeSet; | 
|---|
| 77 |  | 
|---|
| 78 | /***** getter and setter *****/ | 
|---|
| 79 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object | 
|---|
| 80 | /** | 
|---|
| 81 | * returns the periodentafel for the world. | 
|---|
| 82 | */ | 
|---|
| 83 | periodentafel *&getPeriode(); | 
|---|
| 84 |  | 
|---|
| 85 | /** | 
|---|
| 86 | * returns the configuration for the world. | 
|---|
| 87 | */ | 
|---|
| 88 | config *&getConfig(); | 
|---|
| 89 |  | 
|---|
| 90 | /** | 
|---|
| 91 | * returns the first atom that matches a given descriptor. | 
|---|
| 92 | * Do not rely on ordering for descriptors that match more than one atom. | 
|---|
| 93 | */ | 
|---|
| 94 | atom* getAtom(AtomDescriptor descriptor); | 
|---|
| 95 |  | 
|---|
| 96 | /** | 
|---|
| 97 | * returns a vector containing all atoms that match a given descriptor | 
|---|
| 98 | */ | 
|---|
| 99 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor); | 
|---|
| 100 | std::vector<atom*> getAllAtoms(); | 
|---|
| 101 |  | 
|---|
| 102 | /** | 
|---|
| 103 | * returns a calculation that calls a given function on all atoms matching a descriptor. | 
|---|
| 104 | * the calculation is not called at this point and can be used as an action, i.e. be stored in | 
|---|
| 105 | * menus, be kept around for later use etc. | 
|---|
| 106 | */ | 
|---|
| 107 | template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string,AtomDescriptor); | 
|---|
| 108 | template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string); | 
|---|
| 109 |  | 
|---|
| 110 | /** | 
|---|
| 111 | * get the number of atoms in the World | 
|---|
| 112 | */ | 
|---|
| 113 | int numAtoms(); | 
|---|
| 114 |  | 
|---|
| 115 | /** | 
|---|
| 116 | * returns the first molecule that matches a given descriptor. | 
|---|
| 117 | * Do not rely on ordering for descriptors that match more than one molecule. | 
|---|
| 118 | */ | 
|---|
| 119 | molecule *getMolecule(MoleculeDescriptor descriptor); | 
|---|
| 120 |  | 
|---|
| 121 | /** | 
|---|
| 122 | * returns a vector containing all molecules that match a given descriptor | 
|---|
| 123 | */ | 
|---|
| 124 | std::vector<molecule*> getAllMolecules(MoleculeDescriptor descriptor); | 
|---|
| 125 | std::vector<molecule*> getAllMolecules(); | 
|---|
| 126 |  | 
|---|
| 127 | /** | 
|---|
| 128 | * get the number of molecules in the World | 
|---|
| 129 | */ | 
|---|
| 130 | int numMolecules(); | 
|---|
| 131 |  | 
|---|
| 132 | /** | 
|---|
| 133 | * get the domain size as a symmetric matrix (6 components) | 
|---|
| 134 | */ | 
|---|
| 135 | Box& getDomain(); | 
|---|
| 136 |  | 
|---|
| 137 | /** | 
|---|
| 138 | * Set the domain size from a matrix object | 
|---|
| 139 | * | 
|---|
| 140 | * Matrix needs to be symmetric | 
|---|
| 141 | */ | 
|---|
| 142 | void setDomain(const Matrix &mat); | 
|---|
| 143 |  | 
|---|
| 144 | /** | 
|---|
| 145 | * set the domain size as a symmetric matrix (6 components) | 
|---|
| 146 | */ | 
|---|
| 147 | void setDomain(double * matrix); | 
|---|
| 148 |  | 
|---|
| 149 | /** | 
|---|
| 150 | * get the default name | 
|---|
| 151 | */ | 
|---|
| 152 | std::string getDefaultName(); | 
|---|
| 153 |  | 
|---|
| 154 | /** | 
|---|
| 155 | * set the default name | 
|---|
| 156 | */ | 
|---|
| 157 | void setDefaultName(std::string name); | 
|---|
| 158 |  | 
|---|
| 159 | /** | 
|---|
| 160 | * get pointer to World's ThermoStatContainer | 
|---|
| 161 | */ | 
|---|
| 162 | ThermoStatContainer * getThermostats(); | 
|---|
| 163 |  | 
|---|
| 164 | /* | 
|---|
| 165 | * get the ExitFlag | 
|---|
| 166 | */ | 
|---|
| 167 | int getExitFlag(); | 
|---|
| 168 |  | 
|---|
| 169 | /* | 
|---|
| 170 | * set the ExitFlag | 
|---|
| 171 | */ | 
|---|
| 172 | void setExitFlag(int flag); | 
|---|
| 173 |  | 
|---|
| 174 | /***** Methods to work with the World *****/ | 
|---|
| 175 |  | 
|---|
| 176 | /** | 
|---|
| 177 | * create a new molecule. This method should be used whenever any kind of molecule is needed. Assigns a unique | 
|---|
| 178 | * ID to the molecule and stores it in the World for later retrieval. Do not create molecules directly. | 
|---|
| 179 | */ | 
|---|
| 180 | molecule *createMolecule(); | 
|---|
| 181 |  | 
|---|
| 182 | void destroyMolecule(molecule*); | 
|---|
| 183 | void destroyMolecule(moleculeId_t); | 
|---|
| 184 |  | 
|---|
| 185 | /** | 
|---|
| 186 | * Create a new atom. This method should be used whenever any atom is needed. Assigns a unique ID and stores | 
|---|
| 187 | * the atom in the World. If the atom is not destroyed it will automatically be destroyed when the world ends. | 
|---|
| 188 | */ | 
|---|
| 189 | atom *createAtom(); | 
|---|
| 190 |  | 
|---|
| 191 | /** | 
|---|
| 192 | * Registers a Atom unknown to world. Needed in some rare cases, e.g. when cloning atoms, or in some unittests. | 
|---|
| 193 | * Do not re-register Atoms already known to the world since this will cause double-frees. | 
|---|
| 194 | */ | 
|---|
| 195 | int registerAtom(atom*); | 
|---|
| 196 |  | 
|---|
| 197 | /** | 
|---|
| 198 | * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on | 
|---|
| 199 | * atom directly since this will leave the pointer inside the world. | 
|---|
| 200 | */ | 
|---|
| 201 | void destroyAtom(atom*); | 
|---|
| 202 |  | 
|---|
| 203 | /** | 
|---|
| 204 | * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on | 
|---|
| 205 | * atom directly since this will leave the pointer inside the world. | 
|---|
| 206 | */ | 
|---|
| 207 | void destroyAtom(atomId_t); | 
|---|
| 208 |  | 
|---|
| 209 | /** | 
|---|
| 210 | * used when changing an atom Id. | 
|---|
| 211 | * Unless you are calling this method from inside an atom don't fiddle with the third parameter. | 
|---|
| 212 | * | 
|---|
| 213 | * Return value indicates wether the change could be done or not. | 
|---|
| 214 | */ | 
|---|
| 215 | bool changeAtomId(atomId_t oldId, atomId_t newId, atom* target=0); | 
|---|
| 216 |  | 
|---|
| 217 | /** | 
|---|
| 218 | * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not | 
|---|
| 219 | * called at this time, so it can be passed around, stored inside menuItems etc. | 
|---|
| 220 | */ | 
|---|
| 221 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor); | 
|---|
| 222 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string); | 
|---|
| 223 |  | 
|---|
| 224 | /**** | 
|---|
| 225 | * Iterators to use internal data structures | 
|---|
| 226 | * All these iterators are observed to track changes. | 
|---|
| 227 | * There is a corresponding protected section with unobserved iterators, | 
|---|
| 228 | * which can be used internally when the extra speed is needed | 
|---|
| 229 | */ | 
|---|
| 230 |  | 
|---|
| 231 | typedef SelectiveIterator<atom*,AtomSet,AtomDescriptor>       AtomIterator; | 
|---|
| 232 |  | 
|---|
| 233 | /** | 
|---|
| 234 | * returns an iterator over all Atoms matching a given descriptor. | 
|---|
| 235 | * This iterator is observed, so don't keep it around unnecessary to | 
|---|
| 236 | * avoid unintended blocking. | 
|---|
| 237 | */ | 
|---|
| 238 | AtomIterator getAtomIter(AtomDescriptor descr); | 
|---|
| 239 | AtomIterator getAtomIter(); | 
|---|
| 240 |  | 
|---|
| 241 | AtomIterator atomEnd(); | 
|---|
| 242 |  | 
|---|
| 243 | typedef SelectiveIterator<molecule*,MoleculeSet,MoleculeDescriptor>   MoleculeIterator; | 
|---|
| 244 |  | 
|---|
| 245 | /** | 
|---|
| 246 | * returns an iterator over all Molecules matching a given descriptor. | 
|---|
| 247 | * This iterator is observed, so don't keep it around unnecessary to | 
|---|
| 248 | * avoid unintended blocking. | 
|---|
| 249 | */ | 
|---|
| 250 | MoleculeIterator getMoleculeIter(MoleculeDescriptor descr); | 
|---|
| 251 | MoleculeIterator getMoleculeIter(); | 
|---|
| 252 |  | 
|---|
| 253 | MoleculeIterator moleculeEnd(); | 
|---|
| 254 |  | 
|---|
| 255 | /******** Selections of molecules and Atoms *************/ | 
|---|
| 256 | void clearAtomSelection(); | 
|---|
| 257 | void selectAtom(atom*); | 
|---|
| 258 | void selectAtom(atomId_t); | 
|---|
| 259 | void selectAllAtoms(AtomDescriptor); | 
|---|
| 260 | void selectAtomsOfMolecule(molecule*); | 
|---|
| 261 | void selectAtomsOfMolecule(moleculeId_t); | 
|---|
| 262 | void unselectAtom(atom*); | 
|---|
| 263 | void unselectAtom(atomId_t); | 
|---|
| 264 | void unselectAllAtoms(AtomDescriptor); | 
|---|
| 265 | void unselectAtomsOfMolecule(molecule*); | 
|---|
| 266 | void unselectAtomsOfMolecule(moleculeId_t); | 
|---|
| 267 | size_t countSelectedAtoms() const; | 
|---|
| 268 | bool isSelected(atom *_atom) const; | 
|---|
| 269 | const std::vector<atom *> getSelectedAtoms() const; | 
|---|
| 270 |  | 
|---|
| 271 | void clearMoleculeSelection(); | 
|---|
| 272 | void selectMolecule(molecule*); | 
|---|
| 273 | void selectMolecule(moleculeId_t); | 
|---|
| 274 | void selectAllMolecules(MoleculeDescriptor); | 
|---|
| 275 | void selectMoleculeOfAtom(atom*); | 
|---|
| 276 | void selectMoleculeOfAtom(atomId_t); | 
|---|
| 277 | void unselectMolecule(molecule*); | 
|---|
| 278 | void unselectMolecule(moleculeId_t); | 
|---|
| 279 | void unselectAllMolecules(MoleculeDescriptor); | 
|---|
| 280 | void unselectMoleculeOfAtom(atom*); | 
|---|
| 281 | void unselectMoleculeOfAtom(atomId_t); | 
|---|
| 282 | size_t countSelectedMolecules() const; | 
|---|
| 283 | bool isSelected(molecule *_mol) const; | 
|---|
| 284 | const std::vector<molecule *> getSelectedMolecules() const; | 
|---|
| 285 |  | 
|---|
| 286 | /******************** Iterators to selections *****************/ | 
|---|
| 287 | typedef AtomSet::iterator AtomSelectionIterator; | 
|---|
| 288 | AtomSelectionIterator beginAtomSelection(); | 
|---|
| 289 | AtomSelectionIterator endAtomSelection(); | 
|---|
| 290 |  | 
|---|
| 291 | typedef MoleculeSet::iterator MoleculeSelectionIterator; | 
|---|
| 292 | MoleculeSelectionIterator beginMoleculeSelection(); | 
|---|
| 293 | MoleculeSelectionIterator endMoleculeSelection(); | 
|---|
| 294 |  | 
|---|
| 295 | protected: | 
|---|
| 296 | /**** | 
|---|
| 297 | * Iterators to use internal data structures | 
|---|
| 298 | * All these iterators are unobserved for speed reasons. | 
|---|
| 299 | * There is a corresponding public section to these methods, | 
|---|
| 300 | * which produce observed iterators.*/ | 
|---|
| 301 |  | 
|---|
| 302 | // Atoms | 
|---|
| 303 | typedef SelectiveIterator<atom*,AtomSet::set_t,AtomDescriptor>        internal_AtomIterator; | 
|---|
| 304 |  | 
|---|
| 305 | /** | 
|---|
| 306 | * returns an iterator over all Atoms matching a given descriptor. | 
|---|
| 307 | * used for internal purposes, like AtomProcesses and AtomCalculations. | 
|---|
| 308 | */ | 
|---|
| 309 | internal_AtomIterator getAtomIter_internal(AtomDescriptor descr); | 
|---|
| 310 |  | 
|---|
| 311 | /** | 
|---|
| 312 | * returns an iterator to the end of the AtomSet. Due to overloading this iterator | 
|---|
| 313 | * can be compared to iterators produced by getAtomIter (see the mis-matching types). | 
|---|
| 314 | * Thus it can be used to detect when such an iterator is at the end of the list. | 
|---|
| 315 | * used for internal purposes, like AtomProcesses and AtomCalculations. | 
|---|
| 316 | */ | 
|---|
| 317 | internal_AtomIterator atomEnd_internal(); | 
|---|
| 318 |  | 
|---|
| 319 | // Molecules | 
|---|
| 320 | typedef SelectiveIterator<molecule*,MoleculeSet::set_t,MoleculeDescriptor>   internal_MoleculeIterator; | 
|---|
| 321 |  | 
|---|
| 322 |  | 
|---|
| 323 | /** | 
|---|
| 324 | * returns an iterator over all Molecules matching a given descriptor. | 
|---|
| 325 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations. | 
|---|
| 326 | */ | 
|---|
| 327 | internal_MoleculeIterator getMoleculeIter_internal(MoleculeDescriptor descr); | 
|---|
| 328 |  | 
|---|
| 329 | /** | 
|---|
| 330 | * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator | 
|---|
| 331 | * can be compared to iterators produced by getMoleculeIter (see the mis-matching types). | 
|---|
| 332 | * Thus it can be used to detect when such an iterator is at the end of the list. | 
|---|
| 333 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations. | 
|---|
| 334 | */ | 
|---|
| 335 | internal_MoleculeIterator moleculeEnd_internal(); | 
|---|
| 336 |  | 
|---|
| 337 |  | 
|---|
| 338 | /******* Internal manipulation routines for double callback and Observer mechanism ******/ | 
|---|
| 339 | void doManipulate(ManipulateAtomsProcess *); | 
|---|
| 340 |  | 
|---|
| 341 | private: | 
|---|
| 342 |  | 
|---|
| 343 | atomId_t getNextAtomId(); | 
|---|
| 344 | void releaseAtomId(atomId_t); | 
|---|
| 345 | bool reserveAtomId(atomId_t); | 
|---|
| 346 | void defragAtomIdPool(); | 
|---|
| 347 |  | 
|---|
| 348 | moleculeId_t getNextMoleculeId(); | 
|---|
| 349 | void releaseMoleculeId(moleculeId_t); | 
|---|
| 350 | bool reserveMoleculeId(moleculeId_t); | 
|---|
| 351 | void defragMoleculeIdPool(); | 
|---|
| 352 |  | 
|---|
| 353 | periodentafel *periode; | 
|---|
| 354 | config *configuration; | 
|---|
| 355 | Box *cell_size; | 
|---|
| 356 | std::string defaultName; | 
|---|
| 357 | class ThermoStatContainer *Thermostats; | 
|---|
| 358 | int ExitFlag; | 
|---|
| 359 | private: | 
|---|
| 360 |  | 
|---|
| 361 | AtomSet atoms; | 
|---|
| 362 | AtomSet selectedAtoms; | 
|---|
| 363 | typedef std::set<range<atomId_t> > atomIdPool_t; | 
|---|
| 364 | /** | 
|---|
| 365 | * stores the pool for all available AtomIds below currAtomId | 
|---|
| 366 | * | 
|---|
| 367 | * The pool contains ranges of free ids in the form [bottom,top). | 
|---|
| 368 | */ | 
|---|
| 369 | atomIdPool_t atomIdPool; | 
|---|
| 370 | atomId_t currAtomId; //!< stores the next available Id for atoms | 
|---|
| 371 | size_t lastAtomPoolSize; //!< size of the pool after last defrag, to skip some defrags | 
|---|
| 372 | unsigned int numAtomDefragSkips; | 
|---|
| 373 |  | 
|---|
| 374 | MoleculeSet molecules; | 
|---|
| 375 | MoleculeSet selectedMolecules; | 
|---|
| 376 | typedef std::set<range<atomId_t> > moleculeIdPool_t; | 
|---|
| 377 | /** | 
|---|
| 378 | * stores the pool for all available AtomIds below currAtomId | 
|---|
| 379 | * | 
|---|
| 380 | * The pool contains ranges of free ids in the form [bottom,top). | 
|---|
| 381 | */ | 
|---|
| 382 | moleculeIdPool_t moleculeIdPool; | 
|---|
| 383 | moleculeId_t currMoleculeId; | 
|---|
| 384 | size_t lastMoleculePoolSize; //!< size of the pool after last defrag, to skip some defrags | 
|---|
| 385 | unsigned int numMoleculeDefragSkips; | 
|---|
| 386 | private: | 
|---|
| 387 | /** | 
|---|
| 388 | * private constructor to ensure creation of the world using | 
|---|
| 389 | * the singleton pattern. | 
|---|
| 390 | */ | 
|---|
| 391 | World(); | 
|---|
| 392 |  | 
|---|
| 393 | /** | 
|---|
| 394 | * private destructor to ensure destruction of the world using the | 
|---|
| 395 | * singleton pattern. | 
|---|
| 396 | */ | 
|---|
| 397 | virtual ~World(); | 
|---|
| 398 |  | 
|---|
| 399 | /***** | 
|---|
| 400 | * some legacy stuff that is include for now but will be removed later | 
|---|
| 401 | *****/ | 
|---|
| 402 | public: | 
|---|
| 403 | MoleculeListClass *&getMolecules(); | 
|---|
| 404 |  | 
|---|
| 405 | private: | 
|---|
| 406 | MoleculeListClass *molecules_deprecated; | 
|---|
| 407 | }; | 
|---|
| 408 |  | 
|---|
| 409 | #endif /* WORLD_HPP_ */ | 
|---|