source: src/World.hpp@ 99db9b

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

Replaced all World::getSelected...() to const version where possible.

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