source: src/World.hpp@ 143263

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

Added getSelected..Ids() for both atoms and molecules.

  • added static function getKey() to _take in Helpers.
  • Property mode set to 100644
File size: 18.4 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 std::vector<atomId_t> getSelectedAtomIds() const;
434
435 void clearMoleculeSelection();
436 void invertMoleculeSelection();
437 void popMoleculeSelection();
438 void pushMoleculeSelection();
439 void selectMolecule(const molecule*);
440 void selectMolecule(const moleculeId_t);
441 void selectAllMolecules(MoleculeDescriptor);
442 void selectMoleculeOfAtom(const atom*);
443 void selectMoleculeOfAtom(const atomId_t);
444 void unselectMolecule(const molecule*);
445 void unselectMolecule(const moleculeId_t);
446 void unselectAllMolecules(MoleculeDescriptor);
447 void unselectMoleculeOfAtom(const atom*);
448 void unselectMoleculeOfAtom(const atomId_t);
449 size_t countSelectedMolecules() const;
450 bool isSelected(const molecule *_mol) const;
451 bool isMoleculeSelected(const moleculeId_t no) const;
452 std::vector<molecule *> getSelectedMolecules();
453 std::vector<const molecule *> getSelectedMolecules() const;
454 std::vector<moleculeId_t> getSelectedMoleculeIds() const;
455
456 /******************** Iterators to selections *****************/
457 typedef AtomSet::iterator AtomSelectionIterator;
458 AtomSelectionIterator beginAtomSelection();
459 AtomSelectionIterator endAtomSelection();
460 typedef AtomSet::const_iterator AtomSelectionConstIterator;
461 AtomSelectionConstIterator beginAtomSelection() const;
462 AtomSelectionConstIterator endAtomSelection() const;
463
464 typedef MoleculeSet::iterator MoleculeSelectionIterator;
465 MoleculeSelectionIterator beginMoleculeSelection();
466 MoleculeSelectionIterator endMoleculeSelection();
467 typedef MoleculeSet::const_iterator MoleculeSelectionConstIterator;
468 MoleculeSelectionConstIterator beginMoleculeSelection() const;
469 MoleculeSelectionConstIterator endMoleculeSelection() const;
470
471protected:
472 /****
473 * Iterators to use internal data structures
474 * All these iterators are unobserved for speed reasons.
475 * There is a corresponding public section to these methods,
476 * which produce observed iterators.*/
477
478 // Atoms
479 typedef SelectiveIterator<atom*,AtomSet::set_t,AtomDescriptor> internal_AtomIterator;
480
481 /**
482 * returns an iterator over all Atoms matching a given descriptor.
483 * used for internal purposes, like AtomProcesses and AtomCalculations.
484 */
485 internal_AtomIterator getAtomIter_internal(AtomDescriptor descr);
486
487 /**
488 * returns an iterator to the end of the AtomSet. Due to overloading this iterator
489 * can be compared to iterators produced by getAtomIter (see the mis-matching types).
490 * Thus it can be used to detect when such an iterator is at the end of the list.
491 * used for internal purposes, like AtomProcesses and AtomCalculations.
492 */
493 internal_AtomIterator atomEnd_internal();
494
495 // Molecules
496 typedef SelectiveIterator<molecule*,MoleculeSet::set_t,MoleculeDescriptor> internal_MoleculeIterator;
497
498
499 /**
500 * returns an iterator over all Molecules matching a given descriptor.
501 * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
502 */
503 internal_MoleculeIterator getMoleculeIter_internal(MoleculeDescriptor descr);
504
505 /**
506 * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
507 * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
508 * Thus it can be used to detect when such an iterator is at the end of the list.
509 * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
510 */
511 internal_MoleculeIterator moleculeEnd_internal();
512
513
514 /******* Internal manipulation routines for double callback and Observer mechanism ******/
515 void doManipulate(MoleCuilder::ManipulateAtomsProcess *);
516
517private:
518
519 friend const atom *detail::lastChanged<atom>();
520 friend const molecule *detail::lastChanged<molecule>();
521 static atom *_lastchangedatom;
522 static molecule*_lastchangedmol;
523
524 BondGraph *BG;
525 periodentafel *periode;
526 config *configuration;
527 HomologyContainer *homologies;
528 Box *cell_size;
529 LinkedCell::LinkedCell_Controller *LCcontroller;
530 std::string defaultName;
531 class ThermoStatContainer *Thermostats;
532 int ExitFlag;
533private:
534
535 AtomSet atoms;
536 AtomSet selectedAtoms;
537 typedef std::vector<atomId_t> atomIdsVector_t;
538 std::stack<atomIdsVector_t> selectedAtoms_Stack;
539 /**
540 * stores the pool for all available AtomIds below currAtomId
541 *
542 * The pool contains ranges of free ids in the form [bottom,top).
543 */
544 IdPool<atomId_t, uniqueId> atomIdPool;
545
546 MoleculeSet molecules;
547 MoleculeSet selectedMolecules;
548 typedef std::vector<moleculeId_t> moleculeIdsVector_t;
549 std::stack<moleculeIdsVector_t> selectedMolecules_Stack;
550 /**
551 * stores the pool for all available AtomIds below currAtomId
552 *
553 * The pool contains ranges of free ids in the form [bottom,top).
554 */
555 IdPool<moleculeId_t, continuousId> moleculeIdPool;
556
557private:
558 /**
559 * private constructor to ensure creation of the world using
560 * the singleton pattern.
561 */
562 World();
563
564 /**
565 * private destructor to ensure destruction of the world using the
566 * singleton pattern.
567 */
568 virtual ~World();
569
570 /*****
571 * some legacy stuff that is include for now but will be removed later
572 *****/
573public:
574 MoleculeListClass *&getMolecules();
575
576private:
577 MoleculeListClass *molecules_deprecated;
578};
579
580/** Externalized stuff as member functions cannot be specialized without
581 * specializing the class, too.
582 */
583namespace detail {
584 template <> inline const atom* lastChanged<atom>() { return World::_lastchangedatom; }
585 template <> inline const molecule* lastChanged<molecule>() { return World::_lastchangedmol; }
586}
587
588
589#endif /* WORLD_HPP_ */
Note: See TracBrowser for help on using the repository browser.