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