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