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