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