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