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 |
|
---|
26 | // include config.h
|
---|
27 | #ifdef HAVE_CONFIG_H
|
---|
28 | #include <config.h>
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | // forward declarations
|
---|
32 | class atom;
|
---|
33 | class AtomDescriptor;
|
---|
34 | class AtomDescriptor_impl;
|
---|
35 | template<typename T> class AtomsCalculation;
|
---|
36 | class config;
|
---|
37 | class ManipulateAtomsProcess;
|
---|
38 | class molecule;
|
---|
39 | class MoleculeDescriptor;
|
---|
40 | class MoleculeDescriptor_impl;
|
---|
41 | class MoleculeListClass;
|
---|
42 | class periodentafel;
|
---|
43 | class ThermoStatContainer;
|
---|
44 |
|
---|
45 | /****************************************** forward declarations *****************************/
|
---|
46 |
|
---|
47 | /********************************************** Class World *******************************/
|
---|
48 |
|
---|
49 | class World : public Singleton<World>, public Observable
|
---|
50 | {
|
---|
51 |
|
---|
52 | // Make access to constructor and destructor possible from inside the singleton
|
---|
53 | friend class Singleton<World>;
|
---|
54 |
|
---|
55 | // necessary for coupling with descriptors
|
---|
56 | friend class AtomDescriptor_impl;
|
---|
57 | friend class AtomDescriptor;
|
---|
58 | friend class MoleculeDescriptor_impl;
|
---|
59 | friend class MoleculeDescriptor;
|
---|
60 |
|
---|
61 | // Actions, calculations etc associated with the World
|
---|
62 | friend class ManipulateAtomsProcess;
|
---|
63 | template<typename> friend class AtomsCalculation;
|
---|
64 | public:
|
---|
65 |
|
---|
66 | // Types for Atom and Molecule structures
|
---|
67 | typedef std::map<atomId_t,atom*> AtomSet;
|
---|
68 | typedef std::map<moleculeId_t,molecule*> MoleculeSet;
|
---|
69 |
|
---|
70 | /***** getter and setter *****/
|
---|
71 | // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
|
---|
72 | /**
|
---|
73 | * returns the periodentafel for the world.
|
---|
74 | */
|
---|
75 | periodentafel *&getPeriode();
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * returns the configuration for the world.
|
---|
79 | */
|
---|
80 | config *&getConfig();
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * returns the first atom that matches a given descriptor.
|
---|
84 | * Do not rely on ordering for descriptors that match more than one atom.
|
---|
85 | */
|
---|
86 | atom* getAtom(AtomDescriptor descriptor);
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * returns a vector containing all atoms that match a given descriptor
|
---|
90 | */
|
---|
91 | std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
|
---|
92 | std::vector<atom*> getAllAtoms();
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * returns a calculation that calls a given function on all atoms matching a descriptor.
|
---|
96 | * the calculation is not called at this point and can be used as an action, i.e. be stored in
|
---|
97 | * menus, be kept around for later use etc.
|
---|
98 | */
|
---|
99 | template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string,AtomDescriptor);
|
---|
100 | template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string);
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * get the number of atoms in the World
|
---|
104 | */
|
---|
105 | int numAtoms();
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * returns the first molecule that matches a given descriptor.
|
---|
109 | * Do not rely on ordering for descriptors that match more than one molecule.
|
---|
110 | */
|
---|
111 | molecule *getMolecule(MoleculeDescriptor descriptor);
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * returns a vector containing all molecules that match a given descriptor
|
---|
115 | */
|
---|
116 | std::vector<molecule*> getAllMolecules(MoleculeDescriptor descriptor);
|
---|
117 | std::vector<molecule*> getAllMolecules();
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * get the number of molecules in the World
|
---|
121 | */
|
---|
122 | int numMolecules();
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * get the domain size as a symmetric matrix (6 components)
|
---|
126 | */
|
---|
127 | double * getDomain();
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * set the domain size as a symmetric matrix (6 components)
|
---|
131 | */
|
---|
132 | void setDomain(double * matrix);
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * get the default name
|
---|
136 | */
|
---|
137 | std::string getDefaultName();
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * set the default name
|
---|
141 | */
|
---|
142 | void setDefaultName(std::string name);
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * get pointer to World's ThermoStatContainer
|
---|
146 | */
|
---|
147 | ThermoStatContainer * getThermostats();
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * get the ExitFlag
|
---|
151 | */
|
---|
152 | int getExitFlag();
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * set the ExitFlag
|
---|
156 | */
|
---|
157 | void setExitFlag(int flag);
|
---|
158 |
|
---|
159 | /***** Methods to work with the World *****/
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * create a new molecule. This method should be used whenever any kind of molecule is needed. Assigns a unique
|
---|
163 | * ID to the molecule and stores it in the World for later retrieval. Do not create molecules directly.
|
---|
164 | */
|
---|
165 | molecule *createMolecule();
|
---|
166 |
|
---|
167 | void destroyMolecule(molecule*);
|
---|
168 | void destroyMolecule(moleculeId_t);
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Create a new atom. This method should be used whenever any atom is needed. Assigns a unique ID and stores
|
---|
172 | * the atom in the World. If the atom is not destroyed it will automatically be destroyed when the world ends.
|
---|
173 | */
|
---|
174 | atom *createAtom();
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Registers a Atom unknown to world. Needed in some rare cases, e.g. when cloning atoms, or in some unittests.
|
---|
178 | * Do not re-register Atoms already known to the world since this will cause double-frees.
|
---|
179 | */
|
---|
180 | int registerAtom(atom*);
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
|
---|
184 | * atom directly since this will leave the pointer inside the world.
|
---|
185 | */
|
---|
186 | void destroyAtom(atom*);
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
|
---|
190 | * atom directly since this will leave the pointer inside the world.
|
---|
191 | */
|
---|
192 | void destroyAtom(atomId_t);
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * used when changing an atom Id.
|
---|
196 | * Unless you are calling this method from inside an atom don't fiddle with the third parameter.
|
---|
197 | *
|
---|
198 | * Return value indicates wether the change could be done or not.
|
---|
199 | */
|
---|
200 | bool changeAtomId(atomId_t oldId, atomId_t newId, atom* target=0);
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not
|
---|
204 | * called at this time, so it can be passed around, stored inside menuItems etc.
|
---|
205 | */
|
---|
206 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
|
---|
207 | ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
|
---|
208 |
|
---|
209 | protected:
|
---|
210 | /**** Iterators to use internal data structures */
|
---|
211 |
|
---|
212 | // Atoms
|
---|
213 | typedef SelectiveIterator<atom*,AtomSet,AtomDescriptor> AtomIterator;
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * returns an iterator over all Atoms matching a given descriptor.
|
---|
217 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
218 | */
|
---|
219 | AtomIterator getAtomIter(AtomDescriptor descr);
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * returns an iterator to the end of the AtomSet. Due to overloading this iterator
|
---|
223 | * can be compared to iterators produced by getAtomIter (see the mis-matching types).
|
---|
224 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
225 | * used for internal purposes, like AtomProcesses and AtomCalculations.
|
---|
226 | */
|
---|
227 | AtomIterator atomEnd();
|
---|
228 |
|
---|
229 | // Molecules
|
---|
230 |
|
---|
231 | typedef SelectiveIterator<molecule*,MoleculeSet,MoleculeDescriptor> MoleculeIterator;
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * returns an iterator over all Molecules matching a given descriptor.
|
---|
235 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
|
---|
236 | */
|
---|
237 | MoleculeIterator getMoleculeIter(MoleculeDescriptor descr);
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
|
---|
241 | * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
|
---|
242 | * Thus it can be used to detect when such an iterator is at the end of the list.
|
---|
243 | * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
|
---|
244 | */
|
---|
245 | MoleculeIterator moleculeEnd();
|
---|
246 |
|
---|
247 |
|
---|
248 | /******* Internal manipulation routines for double callback and Observer mechanism ******/
|
---|
249 | void doManipulate(ManipulateAtomsProcess *);
|
---|
250 |
|
---|
251 | private:
|
---|
252 |
|
---|
253 | atomId_t getNextAtomId();
|
---|
254 | void releaseAtomId(atomId_t);
|
---|
255 | bool reserveAtomId(atomId_t);
|
---|
256 |
|
---|
257 | periodentafel *periode;
|
---|
258 | config *configuration;
|
---|
259 | static double *cell_size;
|
---|
260 | std::string defaultName;
|
---|
261 | class ThermoStatContainer *Thermostats;
|
---|
262 | int ExitFlag;
|
---|
263 | public:
|
---|
264 | AtomSet atoms;
|
---|
265 | private:
|
---|
266 | std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId
|
---|
267 | atomId_t currAtomId; //!< stores the next available Id for atoms
|
---|
268 | MoleculeSet molecules;
|
---|
269 | moleculeId_t currMoleculeId;
|
---|
270 | private:
|
---|
271 | /**
|
---|
272 | * private constructor to ensure creation of the world using
|
---|
273 | * the singleton pattern.
|
---|
274 | */
|
---|
275 | World();
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * private destructor to ensure destruction of the world using the
|
---|
279 | * singleton pattern.
|
---|
280 | */
|
---|
281 | virtual ~World();
|
---|
282 |
|
---|
283 | /*****
|
---|
284 | * some legacy stuff that is include for now but will be removed later
|
---|
285 | *****/
|
---|
286 | public:
|
---|
287 | MoleculeListClass *&getMolecules();
|
---|
288 |
|
---|
289 | private:
|
---|
290 | MoleculeListClass *molecules_deprecated;
|
---|
291 | };
|
---|
292 |
|
---|
293 | #endif /* WORLD_HPP_ */
|
---|