source: src/World.hpp@ 02ce36

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

We now require CodePatterns 1.2.0.

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