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 |
|
---|
27 | // include config.h
|
---|
28 | #ifdef HAVE_CONFIG_H
|
---|
29 | #include <config.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | // forward declarations
|
---|
33 | class atom;
|
---|
34 | class AtomDescriptor;
|
---|
35 | class AtomDescriptor_impl;
|
---|
36 | template<typename T> class AtomsCalculation;
|
---|
37 | class Box;
|
---|
38 | class config;
|
---|
39 | class ManipulateAtomsProcess;
|
---|
40 | class Matrix;
|
---|
41 | class molecule;
|
---|
42 | class MoleculeDescriptor;
|
---|
43 | class MoleculeDescriptor_impl;
|
---|
44 | class MoleculeListClass;
|
---|
45 | class periodentafel;
|
---|
46 | class ThermoStatContainer;
|
---|
47 |
|
---|
48 |
|
---|
49 | /****************************************** forward declarations *****************************/
|
---|
50 |
|
---|
51 | /********************************************** Class World *******************************/
|
---|
52 |
|
---|
53 | class World : public Singleton<World>, public Observable
|
---|
54 | {
|
---|
55 |
|
---|
56 | // Make access to constructor and destructor possible from inside the singleton
|
---|
57 | friend class Singleton<World>;
|
---|
58 |
|
---|
59 | // necessary for coupling with descriptors
|
---|
60 | friend class AtomDescriptor_impl;
|
---|
61 | friend class AtomDescriptor;
|
---|
62 | friend class MoleculeDescriptor_impl;
|
---|
63 | friend class MoleculeDescriptor;
|
---|
64 |
|
---|
65 | // Actions, calculations etc associated with the World
|
---|
66 | friend class ManipulateAtomsProcess;
|
---|
67 | template<typename> friend class AtomsCalculation;
|
---|
68 | public:
|
---|
69 |
|
---|
70 | // Types for Atom and Molecule structures
|
---|
71 | typedef ObservedContainer<std::map<atomId_t,atom*> > AtomSet;
|
---|
72 | typedef ObservedContainer<std::map<moleculeId_t,molecule*> > MoleculeSet;
|
---|
73 |
|
---|
74 | /***** getter and setter *****/
|
---|
75 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
|
---|
76 | /**
|
---|
77 | * returns the periodentafel for the world.
|
---|
78 | */
|
---|
79 | periodentafel *&getPeriode();
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * returns the configuration for the world.
|
---|
83 | */
|
---|
84 | config *&getConfig();
|
---|
85 |
|
---|
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 | */
|
---|
90 | atom* getAtom(AtomDescriptor descriptor);
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * returns a vector containing all atoms that match a given descriptor
|
---|
94 | */
|
---|
95 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
|
---|
96 | std::vector<atom*> getAllAtoms();
|
---|
97 |
|
---|
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 | */
|
---|
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);
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * get the number of atoms in the World
|
---|
108 | */
|
---|
109 | int numAtoms();
|
---|
110 |
|
---|
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);
|
---|
121 | std::vector<molecule*> getAllMolecules();
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * get the number of molecules in the World
|
---|
125 | */
|
---|
126 | int numMolecules();
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * get the domain size as a symmetric matrix (6 components)
|
---|
130 | */
|
---|
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);
|
---|
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 | */
|
---|
148 | std::string getDefaultName();
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * set the default name
|
---|
152 | */
|
---|
153 | void setDefaultName(std::string name);
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * get pointer to World's ThermoStatContainer
|
---|
157 | */
|
---|
158 | ThermoStatContainer * getThermostats();
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * get the ExitFlag
|
---|
162 | */
|
---|
163 | int getExitFlag();
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * set the ExitFlag
|
---|
167 | */
|
---|
168 | void setExitFlag(int flag);
|
---|
169 |
|
---|
170 | /***** Methods to work with the World *****/
|
---|
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 | */
|
---|
176 | molecule *createMolecule();
|
---|
177 |
|
---|
178 | void destroyMolecule(molecule*);
|
---|
179 | void destroyMolecule(moleculeId_t);
|
---|
180 |
|
---|
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 | */
|
---|
185 | atom *createAtom();
|
---|
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 | */
|
---|
191 | int registerAtom(atom*);
|
---|
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 | */
|
---|
197 | void destroyAtom(atom*);
|
---|
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 | */
|
---|
203 | void destroyAtom(atomId_t);
|
---|
204 |
|
---|
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 |
|
---|
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 | */
|
---|
217 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
|
---|
218 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
|
---|
219 |
|
---|
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 |
|
---|
239 | typedef SelectiveIterator<molecule*,MoleculeSet,MoleculeDescriptor> MoleculeIterator;
|
---|
240 |
|
---|
241 | protected:
|
---|
242 | /****
|
---|
243 | * Iterators to use internal data structures
|
---|
244 | * All these iterators are unobserved for speed reasons.
|
---|
245 | * There is a corresponding public section to these methods,
|
---|
246 | * which produce observed iterators.*/
|
---|
247 |
|
---|
248 | // Atoms
|
---|
249 | typedef SelectiveIterator<atom*,AtomSet::set_t,AtomDescriptor> internal_AtomIterator;
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * returns an iterator over all Atoms matching a given descriptor.
|
---|
253 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
254 | */
|
---|
255 | internal_AtomIterator getAtomIter_internal(AtomDescriptor descr);
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * returns an iterator to the end of the AtomSet. Due to overloading this iterator
|
---|
259 | * can be compared to iterators produced by getAtomIter (see the mis-matching types).
|
---|
260 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
261 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
262 | */
|
---|
263 | internal_AtomIterator atomEnd_internal();
|
---|
264 |
|
---|
265 | // Molecules
|
---|
266 | typedef SelectiveIterator<molecule*,MoleculeSet::set_t,MoleculeDescriptor> internal_MoleculeIterator;
|
---|
267 |
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * returns an iterator over all Molecules matching a given descriptor.
|
---|
271 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
|
---|
272 | */
|
---|
273 | internal_MoleculeIterator getMoleculeIter_internal(MoleculeDescriptor descr);
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
|
---|
277 | * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
|
---|
278 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
279 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
|
---|
280 | */
|
---|
281 | internal_MoleculeIterator moleculeEnd_internal();
|
---|
282 |
|
---|
283 |
|
---|
284 | /******* Internal manipulation routines for double callback and Observer mechanism ******/
|
---|
285 | void doManipulate(ManipulateAtomsProcess *);
|
---|
286 |
|
---|
287 | private:
|
---|
288 |
|
---|
289 | atomId_t getNextAtomId();
|
---|
290 | void releaseAtomId(atomId_t);
|
---|
291 | bool reserveAtomId(atomId_t);
|
---|
292 | void defragAtomIdPool();
|
---|
293 |
|
---|
294 | moleculeId_t getNextMoleculeId();
|
---|
295 | void releaseMoleculeId(moleculeId_t);
|
---|
296 | bool reserveMoleculeId(moleculeId_t);
|
---|
297 | void defragMoleculeIdPool();
|
---|
298 |
|
---|
299 | periodentafel *periode;
|
---|
300 | config *configuration;
|
---|
301 | Box *cell_size;
|
---|
302 | std::string defaultName;
|
---|
303 | class ThermoStatContainer *Thermostats;
|
---|
304 | int ExitFlag;
|
---|
305 | public:
|
---|
306 | AtomSet atoms;
|
---|
307 | private:
|
---|
308 | typedef std::set<std::pair<atomId_t, atomId_t> > atomIdPool_t;
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * stores the pool for all available AtomIds below currAtomId
|
---|
312 | *
|
---|
313 | * The pool contains ranges of free ids in the form [bottom,top).
|
---|
314 | */
|
---|
315 | atomIdPool_t atomIdPool;
|
---|
316 | atomId_t currAtomId; //!< stores the next available Id for atoms
|
---|
317 | size_t lastAtomPoolSize; //!< size of the pool after last defrag, to skip some defrags
|
---|
318 | unsigned int numAtomDefragSkips;
|
---|
319 |
|
---|
320 | MoleculeSet molecules;
|
---|
321 | typedef std::set<std::pair<moleculeId_t, moleculeId_t> > moleculeIdPool_t;
|
---|
322 | moleculeIdPool_t moleculeIdPool;
|
---|
323 | moleculeId_t currMoleculeId;
|
---|
324 | size_t lastMoleculePoolSize; //!< size of the pool after last defrag, to skip some defrags
|
---|
325 | unsigned int numMoleculeDefragSkips;
|
---|
326 | private:
|
---|
327 | /**
|
---|
328 | * private constructor to ensure creation of the world using
|
---|
329 | * the singleton pattern.
|
---|
330 | */
|
---|
331 | World();
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * private destructor to ensure destruction of the world using the
|
---|
335 | * singleton pattern.
|
---|
336 | */
|
---|
337 | virtual ~World();
|
---|
338 |
|
---|
339 | /*****
|
---|
340 | * some legacy stuff that is include for now but will be removed later
|
---|
341 | *****/
|
---|
342 | public:
|
---|
343 | MoleculeListClass *&getMolecules();
|
---|
344 |
|
---|
345 | private:
|
---|
346 | MoleculeListClass *molecules_deprecated;
|
---|
347 | };
|
---|
348 |
|
---|
349 | #endif /* WORLD_HPP_ */
|
---|