source: src/World.hpp@ ce7fdc

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 ce7fdc was ce7fdc, checked in by Frederik Heber <heber@…>, 13 years ago

HUGE: All Action-related codes resides now in namespace MoleCuilder.

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