source: src/World.hpp@ d25bec

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

Cleaned the structure of Observers, i.e. what they report.

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