source: src/World.hpp@ 5d880e

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 5d880e was 5d880e, checked in by Tillmann Crueger <crueger@…>, 14 years ago

Allowed observed access to iterators on Molecules

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