source: src/World.hpp@ cad383

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

Added push/pop[Atoms|Molecules]Selection to World.

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