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