[5d1611] | 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 |
|
---|
[b34306] | 11 | /*********************************************** includes ***********************************/
|
---|
| 12 |
|
---|
[7c4e29] | 13 | #include <string>
|
---|
[d346b6] | 14 | #include <map>
|
---|
[fc1b24] | 15 | #include <vector>
|
---|
[354859] | 16 | #include <set>
|
---|
[7c4e29] | 17 | #include <boost/thread.hpp>
|
---|
[865a945] | 18 | #include <boost/shared_ptr.hpp>
|
---|
[5d1611] | 19 |
|
---|
[ead4e6] | 20 | #include "types.hpp"
|
---|
[6e97e5] | 21 | #include "Descriptors/SelectiveIterator.hpp"
|
---|
[5d1611] | 22 | #include "Patterns/Observer.hpp"
|
---|
| 23 | #include "Patterns/Cacheable.hpp"
|
---|
[23b547] | 24 | #include "Patterns/Singleton.hpp"
|
---|
[b99bf3] | 25 | #include "Patterns/ObservedContainer.hpp"
|
---|
[dc11c9] | 26 | #include "Helpers/Range.hpp"
|
---|
[23b547] | 27 |
|
---|
[b34306] | 28 | // include config.h
|
---|
| 29 | #ifdef HAVE_CONFIG_H
|
---|
| 30 | #include <config.h>
|
---|
| 31 | #endif
|
---|
[5d1611] | 32 |
|
---|
| 33 | // forward declarations
|
---|
[4d9c01] | 34 | class atom;
|
---|
[fc1b24] | 35 | class AtomDescriptor;
|
---|
[7a1ce5] | 36 | class AtomDescriptor_impl;
|
---|
[43dad6] | 37 | template<typename T> class AtomsCalculation;
|
---|
[84c494] | 38 | class Box;
|
---|
[43dad6] | 39 | class config;
|
---|
| 40 | class ManipulateAtomsProcess;
|
---|
[84c494] | 41 | class Matrix;
|
---|
[43dad6] | 42 | class molecule;
|
---|
[1c51c8] | 43 | class MoleculeDescriptor;
|
---|
| 44 | class MoleculeDescriptor_impl;
|
---|
[43dad6] | 45 | class MoleculeListClass;
|
---|
| 46 | class periodentafel;
|
---|
| 47 | class ThermoStatContainer;
|
---|
[5d1611] | 48 |
|
---|
[fa0b18] | 49 |
|
---|
[b34306] | 50 | /****************************************** forward declarations *****************************/
|
---|
[23b547] | 51 |
|
---|
[b34306] | 52 | /********************************************** Class World *******************************/
|
---|
[23b547] | 53 |
|
---|
| 54 | class World : public Singleton<World>, public Observable
|
---|
[5d1611] | 55 | {
|
---|
[23b547] | 56 |
|
---|
| 57 | // Make access to constructor and destructor possible from inside the singleton
|
---|
| 58 | friend class Singleton<World>;
|
---|
| 59 |
|
---|
[b54ac8] | 60 | // necessary for coupling with descriptors
|
---|
[7a1ce5] | 61 | friend class AtomDescriptor_impl;
|
---|
[865a945] | 62 | friend class AtomDescriptor;
|
---|
[1c51c8] | 63 | friend class MoleculeDescriptor_impl;
|
---|
| 64 | friend class MoleculeDescriptor;
|
---|
[41aa39] | 65 | // coupling with descriptors over selection
|
---|
| 66 | friend class AtomSelectionDescriptor_impl;
|
---|
[cf0ca1] | 67 | friend class MoleculeSelectionDescriptor_impl;
|
---|
[865a945] | 68 |
|
---|
[b54ac8] | 69 | // Actions, calculations etc associated with the World
|
---|
[7c4e29] | 70 | friend class ManipulateAtomsProcess;
|
---|
[b54ac8] | 71 | template<typename> friend class AtomsCalculation;
|
---|
[5d1611] | 72 | public:
|
---|
[23b547] | 73 |
|
---|
| 74 | // Types for Atom and Molecule structures
|
---|
[4ef9b7] | 75 | typedef ObservedContainer<std::map<atomId_t,atom*> > AtomSet;
|
---|
[51be2a] | 76 | typedef ObservedContainer<std::map<moleculeId_t,molecule*> > MoleculeSet;
|
---|
[5d1611] | 77 |
|
---|
| 78 | /***** getter and setter *****/
|
---|
[354859] | 79 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
|
---|
[02ee15] | 80 | /**
|
---|
| 81 | * returns the periodentafel for the world.
|
---|
| 82 | */
|
---|
[354859] | 83 | periodentafel *&getPeriode();
|
---|
[02ee15] | 84 |
|
---|
[8e1f7af] | 85 | /**
|
---|
| 86 | * returns the configuration for the world.
|
---|
| 87 | */
|
---|
| 88 | config *&getConfig();
|
---|
| 89 |
|
---|
[02ee15] | 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 | */
|
---|
[7a1ce5] | 94 | atom* getAtom(AtomDescriptor descriptor);
|
---|
[02ee15] | 95 |
|
---|
| 96 | /**
|
---|
| 97 | * returns a vector containing all atoms that match a given descriptor
|
---|
| 98 | */
|
---|
[7a1ce5] | 99 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
|
---|
[0e2a47] | 100 | std::vector<atom*> getAllAtoms();
|
---|
[b54ac8] | 101 |
|
---|
[02ee15] | 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 | */
|
---|
[0e2a47] | 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);
|
---|
[b54ac8] | 109 |
|
---|
[02ee15] | 110 | /**
|
---|
| 111 | * get the number of atoms in the World
|
---|
| 112 | */
|
---|
[354859] | 113 | int numAtoms();
|
---|
[02ee15] | 114 |
|
---|
[1c51c8] | 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);
|
---|
[97ebf8] | 125 | std::vector<molecule*> getAllMolecules();
|
---|
[1c51c8] | 126 |
|
---|
[02ee15] | 127 | /**
|
---|
| 128 | * get the number of molecules in the World
|
---|
| 129 | */
|
---|
[354859] | 130 | int numMolecules();
|
---|
| 131 |
|
---|
[5f612ee] | 132 | /**
|
---|
| 133 | * get the domain size as a symmetric matrix (6 components)
|
---|
| 134 | */
|
---|
[84c494] | 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);
|
---|
[5f612ee] | 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 | */
|
---|
[387b36] | 152 | std::string getDefaultName();
|
---|
[5f612ee] | 153 |
|
---|
| 154 | /**
|
---|
| 155 | * set the default name
|
---|
| 156 | */
|
---|
[387b36] | 157 | void setDefaultName(std::string name);
|
---|
[5f612ee] | 158 |
|
---|
[43dad6] | 159 | /**
|
---|
| 160 | * get pointer to World's ThermoStatContainer
|
---|
| 161 | */
|
---|
| 162 | ThermoStatContainer * getThermostats();
|
---|
| 163 |
|
---|
[e4b5de] | 164 | /*
|
---|
| 165 | * get the ExitFlag
|
---|
| 166 | */
|
---|
| 167 | int getExitFlag();
|
---|
| 168 |
|
---|
| 169 | /*
|
---|
| 170 | * set the ExitFlag
|
---|
| 171 | */
|
---|
| 172 | void setExitFlag(int flag);
|
---|
| 173 |
|
---|
[354859] | 174 | /***** Methods to work with the World *****/
|
---|
[02ee15] | 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 | */
|
---|
[354859] | 180 | molecule *createMolecule();
|
---|
[02ee15] | 181 |
|
---|
[cbc5fb] | 182 | void destroyMolecule(molecule*);
|
---|
| 183 | void destroyMolecule(moleculeId_t);
|
---|
| 184 |
|
---|
[02ee15] | 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 | */
|
---|
[46d958] | 189 | atom *createAtom();
|
---|
[02ee15] | 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 | */
|
---|
[46d958] | 195 | int registerAtom(atom*);
|
---|
[02ee15] | 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 | */
|
---|
[46d958] | 201 | void destroyAtom(atom*);
|
---|
[02ee15] | 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 | */
|
---|
[cbc5fb] | 207 | void destroyAtom(atomId_t);
|
---|
[865a945] | 208 |
|
---|
[88d586] | 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 |
|
---|
[02ee15] | 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 | */
|
---|
[7c4e29] | 221 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
|
---|
[0e2a47] | 222 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
|
---|
[7c4e29] | 223 |
|
---|
[fa0b18] | 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,
|
---|
[90c4280] | 228 | * which can be used internally when the extra speed is needed
|
---|
[fa0b18] | 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 |
|
---|
[e3d865] | 243 | typedef SelectiveIterator<molecule*,MoleculeSet,MoleculeDescriptor> MoleculeIterator;
|
---|
[51be2a] | 244 |
|
---|
[90c4280] | 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 | */
|
---|
[5d880e] | 250 | MoleculeIterator getMoleculeIter(MoleculeDescriptor descr);
|
---|
| 251 | MoleculeIterator getMoleculeIter();
|
---|
| 252 |
|
---|
| 253 | MoleculeIterator moleculeEnd();
|
---|
| 254 |
|
---|
[90c4280] | 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);
|
---|
[61d655e] | 262 | void unselectAtom(atom*);
|
---|
| 263 | void unselectAtom(atomId_t);
|
---|
| 264 | void unselectAllAtoms(AtomDescriptor);
|
---|
| 265 | void unselectAtomsOfMolecule(molecule*);
|
---|
| 266 | void unselectAtomsOfMolecule(moleculeId_t);
|
---|
[90c4280] | 267 |
|
---|
| 268 | void clearMoleculeSelection();
|
---|
| 269 | void selectMolecule(molecule*);
|
---|
| 270 | void selectMolecule(moleculeId_t);
|
---|
| 271 | void selectAllMoleculess(MoleculeDescriptor);
|
---|
| 272 | void selectMoleculeOfAtom(atom*);
|
---|
| 273 | void selectMoleculeOfAtom(atomId_t);
|
---|
[61d655e] | 274 | void unselectMolecule(molecule*);
|
---|
| 275 | void unselectMolecule(moleculeId_t);
|
---|
| 276 | void unselectAllMoleculess(MoleculeDescriptor);
|
---|
| 277 | void unselectMoleculeOfAtom(atom*);
|
---|
| 278 | void unselectMoleculeOfAtom(atomId_t);
|
---|
[90c4280] | 279 |
|
---|
[3839e5] | 280 | /******************** Iterators to selections *****************/
|
---|
| 281 | typedef AtomSet::iterator AtomSelectionIterator;
|
---|
| 282 | AtomSelectionIterator beginAtomSelection();
|
---|
| 283 | AtomSelectionIterator endAtomSelection();
|
---|
| 284 |
|
---|
| 285 | typedef MoleculeSet::iterator MoleculeSelectionIterator;
|
---|
| 286 | MoleculeSelectionIterator beginMoleculeSelection();
|
---|
| 287 | MoleculeSelectionIterator endMoleculeSelection();
|
---|
| 288 |
|
---|
[865a945] | 289 | protected:
|
---|
[fa0b18] | 290 | /****
|
---|
| 291 | * Iterators to use internal data structures
|
---|
| 292 | * All these iterators are unobserved for speed reasons.
|
---|
| 293 | * There is a corresponding public section to these methods,
|
---|
| 294 | * which produce observed iterators.*/
|
---|
[1c51c8] | 295 |
|
---|
| 296 | // Atoms
|
---|
[e3d865] | 297 | typedef SelectiveIterator<atom*,AtomSet::set_t,AtomDescriptor> internal_AtomIterator;
|
---|
[865a945] | 298 |
|
---|
[02ee15] | 299 | /**
|
---|
| 300 | * returns an iterator over all Atoms matching a given descriptor.
|
---|
| 301 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
| 302 | */
|
---|
[fa0b18] | 303 | internal_AtomIterator getAtomIter_internal(AtomDescriptor descr);
|
---|
[02ee15] | 304 |
|
---|
| 305 | /**
|
---|
[d2dbac0] | 306 | * returns an iterator to the end of the AtomSet. Due to overloading this iterator
|
---|
[02ee15] | 307 | * can be compared to iterators produced by getAtomIter (see the mis-matching types).
|
---|
| 308 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
| 309 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
| 310 | */
|
---|
[fa0b18] | 311 | internal_AtomIterator atomEnd_internal();
|
---|
[865a945] | 312 |
|
---|
[1c51c8] | 313 | // Molecules
|
---|
[e3d865] | 314 | typedef SelectiveIterator<molecule*,MoleculeSet::set_t,MoleculeDescriptor> internal_MoleculeIterator;
|
---|
[51be2a] | 315 |
|
---|
[1c51c8] | 316 |
|
---|
| 317 | /**
|
---|
| 318 | * returns an iterator over all Molecules matching a given descriptor.
|
---|
| 319 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
|
---|
| 320 | */
|
---|
[e3d865] | 321 | internal_MoleculeIterator getMoleculeIter_internal(MoleculeDescriptor descr);
|
---|
[1c51c8] | 322 |
|
---|
| 323 | /**
|
---|
| 324 | * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
|
---|
| 325 | * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
|
---|
| 326 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
| 327 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
|
---|
| 328 | */
|
---|
[e3d865] | 329 | internal_MoleculeIterator moleculeEnd_internal();
|
---|
[1c51c8] | 330 |
|
---|
| 331 |
|
---|
[afb47f] | 332 | /******* Internal manipulation routines for double callback and Observer mechanism ******/
|
---|
| 333 | void doManipulate(ManipulateAtomsProcess *);
|
---|
| 334 |
|
---|
[5d1611] | 335 | private:
|
---|
[88d586] | 336 |
|
---|
| 337 | atomId_t getNextAtomId();
|
---|
| 338 | void releaseAtomId(atomId_t);
|
---|
| 339 | bool reserveAtomId(atomId_t);
|
---|
[127a8e] | 340 | void defragAtomIdPool();
|
---|
| 341 |
|
---|
| 342 | moleculeId_t getNextMoleculeId();
|
---|
| 343 | void releaseMoleculeId(moleculeId_t);
|
---|
| 344 | bool reserveMoleculeId(moleculeId_t);
|
---|
| 345 | void defragMoleculeIdPool();
|
---|
[88d586] | 346 |
|
---|
[5d1611] | 347 | periodentafel *periode;
|
---|
[8e1f7af] | 348 | config *configuration;
|
---|
[84c494] | 349 | Box *cell_size;
|
---|
[387b36] | 350 | std::string defaultName;
|
---|
[43dad6] | 351 | class ThermoStatContainer *Thermostats;
|
---|
[e4b5de] | 352 | int ExitFlag;
|
---|
[6e97e5] | 353 | private:
|
---|
[127a8e] | 354 |
|
---|
[1a76a6] | 355 | AtomSet atoms;
|
---|
[90c4280] | 356 | AtomSet selectedAtoms;
|
---|
[dc11c9] | 357 | typedef std::set<range<atomId_t> > atomIdPool_t;
|
---|
[127a8e] | 358 | /**
|
---|
| 359 | * stores the pool for all available AtomIds below currAtomId
|
---|
| 360 | *
|
---|
| 361 | * The pool contains ranges of free ids in the form [bottom,top).
|
---|
| 362 | */
|
---|
| 363 | atomIdPool_t atomIdPool;
|
---|
[cbc5fb] | 364 | atomId_t currAtomId; //!< stores the next available Id for atoms
|
---|
[127a8e] | 365 | size_t lastAtomPoolSize; //!< size of the pool after last defrag, to skip some defrags
|
---|
| 366 | unsigned int numAtomDefragSkips;
|
---|
| 367 |
|
---|
[d2dbac0] | 368 | MoleculeSet molecules;
|
---|
[90c4280] | 369 | MoleculeSet selectedMolecules;
|
---|
[dc11c9] | 370 | typedef std::set<range<atomId_t> > moleculeIdPool_t;
|
---|
[1a76a6] | 371 | /**
|
---|
| 372 | * stores the pool for all available AtomIds below currAtomId
|
---|
| 373 | *
|
---|
| 374 | * The pool contains ranges of free ids in the form [bottom,top).
|
---|
| 375 | */
|
---|
[127a8e] | 376 | moleculeIdPool_t moleculeIdPool;
|
---|
[cbc5fb] | 377 | moleculeId_t currMoleculeId;
|
---|
[127a8e] | 378 | size_t lastMoleculePoolSize; //!< size of the pool after last defrag, to skip some defrags
|
---|
| 379 | unsigned int numMoleculeDefragSkips;
|
---|
[5d1611] | 380 | private:
|
---|
[02ee15] | 381 | /**
|
---|
| 382 | * private constructor to ensure creation of the world using
|
---|
| 383 | * the singleton pattern.
|
---|
| 384 | */
|
---|
[5d1611] | 385 | World();
|
---|
[02ee15] | 386 |
|
---|
| 387 | /**
|
---|
| 388 | * private destructor to ensure destruction of the world using the
|
---|
| 389 | * singleton pattern.
|
---|
| 390 | */
|
---|
[5d1611] | 391 | virtual ~World();
|
---|
| 392 |
|
---|
| 393 | /*****
|
---|
| 394 | * some legacy stuff that is include for now but will be removed later
|
---|
| 395 | *****/
|
---|
| 396 | public:
|
---|
[354859] | 397 | MoleculeListClass *&getMolecules();
|
---|
[4d9c01] | 398 |
|
---|
[5d1611] | 399 | private:
|
---|
[354859] | 400 | MoleculeListClass *molecules_deprecated;
|
---|
[5d1611] | 401 | };
|
---|
| 402 |
|
---|
| 403 | #endif /* WORLD_HPP_ */
|
---|