source: src/World.hpp@ a1e0b6

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 Candidate_v1.7.0 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 a1e0b6 was 41aa39, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Made MoleculeIdDescriptor and AtomIdDescriptor faster by avoiding copy of the MoleculeSet/AtomSet

  • Property mode set to 100644
File size: 12.2 KB
RevLine 
[5d1611]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
[b34306]11/*********************************************** includes ***********************************/
12
[7c4e29]13#include <string>
[d346b6]14#include <map>
[fc1b24]15#include <vector>
[354859]16#include <set>
[7c4e29]17#include <boost/thread.hpp>
[865a945]18#include <boost/shared_ptr.hpp>
[5d1611]19
[ead4e6]20#include "types.hpp"
[6e97e5]21#include "Descriptors/SelectiveIterator.hpp"
[5d1611]22#include "Patterns/Observer.hpp"
23#include "Patterns/Cacheable.hpp"
[23b547]24#include "Patterns/Singleton.hpp"
[b99bf3]25#include "Patterns/ObservedContainer.hpp"
[23b547]26
[b34306]27// include config.h
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
[5d1611]31
32// forward declarations
[4d9c01]33class atom;
[fc1b24]34class AtomDescriptor;
[7a1ce5]35class AtomDescriptor_impl;
[43dad6]36template<typename T> class AtomsCalculation;
[84c494]37class Box;
[43dad6]38class config;
39class ManipulateAtomsProcess;
[84c494]40class Matrix;
[43dad6]41class molecule;
[1c51c8]42class MoleculeDescriptor;
43class MoleculeDescriptor_impl;
[43dad6]44class MoleculeListClass;
45class periodentafel;
46class ThermoStatContainer;
[5d1611]47
[fa0b18]48
[b34306]49/****************************************** forward declarations *****************************/
[23b547]50
[b34306]51/********************************************** Class World *******************************/
[23b547]52
53class World : public Singleton<World>, public Observable
[5d1611]54{
[23b547]55
56// Make access to constructor and destructor possible from inside the singleton
57friend class Singleton<World>;
58
[b54ac8]59// necessary for coupling with descriptors
[7a1ce5]60friend class AtomDescriptor_impl;
[865a945]61friend class AtomDescriptor;
[1c51c8]62friend class MoleculeDescriptor_impl;
63friend class MoleculeDescriptor;
[41aa39]64// coupling with descriptors over selection
65friend class AtomSelectionDescriptor_impl;
[865a945]66
[b54ac8]67// Actions, calculations etc associated with the World
[7c4e29]68friend class ManipulateAtomsProcess;
[b54ac8]69template<typename> friend class AtomsCalculation;
[5d1611]70public:
[23b547]71
72 // Types for Atom and Molecule structures
[4ef9b7]73 typedef ObservedContainer<std::map<atomId_t,atom*> > AtomSet;
[51be2a]74 typedef ObservedContainer<std::map<moleculeId_t,molecule*> > MoleculeSet;
[5d1611]75
76 /***** getter and setter *****/
[354859]77 // reference to pointer is used for legacy reason... reference will be removed latter to keep encapsulation of World object
[02ee15]78 /**
79 * returns the periodentafel for the world.
80 */
[354859]81 periodentafel *&getPeriode();
[02ee15]82
[8e1f7af]83 /**
84 * returns the configuration for the world.
85 */
86 config *&getConfig();
87
[02ee15]88 /**
89 * returns the first atom that matches a given descriptor.
90 * Do not rely on ordering for descriptors that match more than one atom.
91 */
[7a1ce5]92 atom* getAtom(AtomDescriptor descriptor);
[02ee15]93
94 /**
95 * returns a vector containing all atoms that match a given descriptor
96 */
[7a1ce5]97 std::vector<atom*> getAllAtoms(AtomDescriptor descriptor);
[0e2a47]98 std::vector<atom*> getAllAtoms();
[b54ac8]99
[02ee15]100 /**
101 * returns a calculation that calls a given function on all atoms matching a descriptor.
102 * the calculation is not called at this point and can be used as an action, i.e. be stored in
103 * menus, be kept around for later use etc.
104 */
[0e2a47]105 template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string,AtomDescriptor);
106 template<typename T> AtomsCalculation<T>* calcOnAtoms(boost::function<T(atom*)>,std::string);
[b54ac8]107
[02ee15]108 /**
109 * get the number of atoms in the World
110 */
[354859]111 int numAtoms();
[02ee15]112
[1c51c8]113 /**
114 * returns the first molecule that matches a given descriptor.
115 * Do not rely on ordering for descriptors that match more than one molecule.
116 */
117 molecule *getMolecule(MoleculeDescriptor descriptor);
118
119 /**
120 * returns a vector containing all molecules that match a given descriptor
121 */
122 std::vector<molecule*> getAllMolecules(MoleculeDescriptor descriptor);
[97ebf8]123 std::vector<molecule*> getAllMolecules();
[1c51c8]124
[02ee15]125 /**
126 * get the number of molecules in the World
127 */
[354859]128 int numMolecules();
129
[5f612ee]130 /**
131 * get the domain size as a symmetric matrix (6 components)
132 */
[84c494]133 Box& getDomain();
134
135 /**
136 * Set the domain size from a matrix object
137 *
138 * Matrix needs to be symmetric
139 */
140 void setDomain(const Matrix &mat);
[5f612ee]141
142 /**
143 * set the domain size as a symmetric matrix (6 components)
144 */
145 void setDomain(double * matrix);
146
147 /**
148 * get the default name
149 */
[387b36]150 std::string getDefaultName();
[5f612ee]151
152 /**
153 * set the default name
154 */
[387b36]155 void setDefaultName(std::string name);
[5f612ee]156
[43dad6]157 /**
158 * get pointer to World's ThermoStatContainer
159 */
160 ThermoStatContainer * getThermostats();
161
[e4b5de]162 /*
163 * get the ExitFlag
164 */
165 int getExitFlag();
166
167 /*
168 * set the ExitFlag
169 */
170 void setExitFlag(int flag);
171
[354859]172 /***** Methods to work with the World *****/
[02ee15]173
174 /**
175 * create a new molecule. This method should be used whenever any kind of molecule is needed. Assigns a unique
176 * ID to the molecule and stores it in the World for later retrieval. Do not create molecules directly.
177 */
[354859]178 molecule *createMolecule();
[02ee15]179
[cbc5fb]180 void destroyMolecule(molecule*);
181 void destroyMolecule(moleculeId_t);
182
[02ee15]183 /**
184 * Create a new atom. This method should be used whenever any atom is needed. Assigns a unique ID and stores
185 * the atom in the World. If the atom is not destroyed it will automatically be destroyed when the world ends.
186 */
[46d958]187 atom *createAtom();
[02ee15]188
189 /**
190 * Registers a Atom unknown to world. Needed in some rare cases, e.g. when cloning atoms, or in some unittests.
191 * Do not re-register Atoms already known to the world since this will cause double-frees.
192 */
[46d958]193 int registerAtom(atom*);
[02ee15]194
195 /**
196 * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
197 * atom directly since this will leave the pointer inside the world.
198 */
[46d958]199 void destroyAtom(atom*);
[02ee15]200
201 /**
202 * Delete some atom and erase it from the world. Use this whenever you need to destroy any atom. Do not call delete on
203 * atom directly since this will leave the pointer inside the world.
204 */
[cbc5fb]205 void destroyAtom(atomId_t);
[865a945]206
[88d586]207 /**
208 * used when changing an atom Id.
209 * Unless you are calling this method from inside an atom don't fiddle with the third parameter.
210 *
211 * Return value indicates wether the change could be done or not.
212 */
213 bool changeAtomId(atomId_t oldId, atomId_t newId, atom* target=0);
214
[02ee15]215 /**
216 * Produces a process that calls a function on all Atoms matching a given descriptor. The process is not
217 * called at this time, so it can be passed around, stored inside menuItems etc.
218 */
[7c4e29]219 ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string,AtomDescriptor);
[0e2a47]220 ManipulateAtomsProcess* manipulateAtoms(boost::function<void(atom*)>,std::string);
[7c4e29]221
[fa0b18]222 /****
223 * Iterators to use internal data structures
224 * All these iterators are observed to track changes.
225 * There is a corresponding protected section with unobserved iterators,
[90c4280]226 * which can be used internally when the extra speed is needed
[fa0b18]227 */
228
229 typedef SelectiveIterator<atom*,AtomSet,AtomDescriptor> AtomIterator;
230
231 /**
232 * returns an iterator over all Atoms matching a given descriptor.
233 * This iterator is observed, so don't keep it around unnecessary to
234 * avoid unintended blocking.
235 */
236 AtomIterator getAtomIter(AtomDescriptor descr);
237 AtomIterator getAtomIter();
238
239 AtomIterator atomEnd();
240
[e3d865]241 typedef SelectiveIterator<molecule*,MoleculeSet,MoleculeDescriptor> MoleculeIterator;
[51be2a]242
[90c4280]243 /**
244 * returns an iterator over all Molecules matching a given descriptor.
245 * This iterator is observed, so don't keep it around unnecessary to
246 * avoid unintended blocking.
247 */
[5d880e]248 MoleculeIterator getMoleculeIter(MoleculeDescriptor descr);
249 MoleculeIterator getMoleculeIter();
250
251 MoleculeIterator moleculeEnd();
252
[90c4280]253 /******** Selections of molecules and Atoms *************/
254 void clearAtomSelection();
255 void selectAtom(atom*);
256 void selectAtom(atomId_t);
257 void selectAllAtoms(AtomDescriptor);
258 void selectAtomsOfMolecule(molecule*);
259 void selectAtomsOfMolecule(moleculeId_t);
[61d655e]260 void unselectAtom(atom*);
261 void unselectAtom(atomId_t);
262 void unselectAllAtoms(AtomDescriptor);
263 void unselectAtomsOfMolecule(molecule*);
264 void unselectAtomsOfMolecule(moleculeId_t);
[90c4280]265
266 void clearMoleculeSelection();
267 void selectMolecule(molecule*);
268 void selectMolecule(moleculeId_t);
269 void selectAllMoleculess(MoleculeDescriptor);
270 void selectMoleculeOfAtom(atom*);
271 void selectMoleculeOfAtom(atomId_t);
[61d655e]272 void unselectMolecule(molecule*);
273 void unselectMolecule(moleculeId_t);
274 void unselectAllMoleculess(MoleculeDescriptor);
275 void unselectMoleculeOfAtom(atom*);
276 void unselectMoleculeOfAtom(atomId_t);
[90c4280]277
[3839e5]278 /******************** Iterators to selections *****************/
279 typedef AtomSet::iterator AtomSelectionIterator;
280 AtomSelectionIterator beginAtomSelection();
281 AtomSelectionIterator endAtomSelection();
282
283 typedef MoleculeSet::iterator MoleculeSelectionIterator;
284 MoleculeSelectionIterator beginMoleculeSelection();
285 MoleculeSelectionIterator endMoleculeSelection();
286
[865a945]287protected:
[fa0b18]288 /****
289 * Iterators to use internal data structures
290 * All these iterators are unobserved for speed reasons.
291 * There is a corresponding public section to these methods,
292 * which produce observed iterators.*/
[1c51c8]293
294 // Atoms
[e3d865]295 typedef SelectiveIterator<atom*,AtomSet::set_t,AtomDescriptor> internal_AtomIterator;
[865a945]296
[02ee15]297 /**
298 * returns an iterator over all Atoms matching a given descriptor.
299 * used for internal purposes, like AtomProcesses and AtomCalculations.
300 */
[fa0b18]301 internal_AtomIterator getAtomIter_internal(AtomDescriptor descr);
[02ee15]302
303 /**
[d2dbac0]304 * returns an iterator to the end of the AtomSet. Due to overloading this iterator
[02ee15]305 * can be compared to iterators produced by getAtomIter (see the mis-matching types).
306 * Thus it can be used to detect when such an iterator is at the end of the list.
307 * used for internal purposes, like AtomProcesses and AtomCalculations.
308 */
[fa0b18]309 internal_AtomIterator atomEnd_internal();
[865a945]310
[1c51c8]311 // Molecules
[e3d865]312 typedef SelectiveIterator<molecule*,MoleculeSet::set_t,MoleculeDescriptor> internal_MoleculeIterator;
[51be2a]313
[1c51c8]314
315 /**
316 * returns an iterator over all Molecules matching a given descriptor.
317 * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
318 */
[e3d865]319 internal_MoleculeIterator getMoleculeIter_internal(MoleculeDescriptor descr);
[1c51c8]320
321 /**
322 * returns an iterator to the end of the MoleculeSet. Due to overloading this iterator
323 * can be compared to iterators produced by getMoleculeIter (see the mis-matching types).
324 * Thus it can be used to detect when such an iterator is at the end of the list.
325 * used for internal purposes, like MoleculeProcesses and MoleculeCalculations.
326 */
[e3d865]327 internal_MoleculeIterator moleculeEnd_internal();
[1c51c8]328
329
[afb47f]330 /******* Internal manipulation routines for double callback and Observer mechanism ******/
331 void doManipulate(ManipulateAtomsProcess *);
332
[5d1611]333private:
[88d586]334
335 atomId_t getNextAtomId();
336 void releaseAtomId(atomId_t);
337 bool reserveAtomId(atomId_t);
[127a8e]338 void defragAtomIdPool();
339
340 moleculeId_t getNextMoleculeId();
341 void releaseMoleculeId(moleculeId_t);
342 bool reserveMoleculeId(moleculeId_t);
343 void defragMoleculeIdPool();
[88d586]344
[5d1611]345 periodentafel *periode;
[8e1f7af]346 config *configuration;
[84c494]347 Box *cell_size;
[387b36]348 std::string defaultName;
[43dad6]349 class ThermoStatContainer *Thermostats;
[e4b5de]350 int ExitFlag;
[6e97e5]351private:
[127a8e]352
[1a76a6]353 AtomSet atoms;
[90c4280]354 AtomSet selectedAtoms;
[1a76a6]355 typedef std::set<std::pair<atomId_t, atomId_t> > atomIdPool_t;
[127a8e]356 /**
357 * stores the pool for all available AtomIds below currAtomId
358 *
359 * The pool contains ranges of free ids in the form [bottom,top).
360 */
361 atomIdPool_t atomIdPool;
[cbc5fb]362 atomId_t currAtomId; //!< stores the next available Id for atoms
[127a8e]363 size_t lastAtomPoolSize; //!< size of the pool after last defrag, to skip some defrags
364 unsigned int numAtomDefragSkips;
365
[d2dbac0]366 MoleculeSet molecules;
[90c4280]367 MoleculeSet selectedMolecules;
[127a8e]368 typedef std::set<std::pair<moleculeId_t, moleculeId_t> > moleculeIdPool_t;
[1a76a6]369 /**
370 * stores the pool for all available AtomIds below currAtomId
371 *
372 * The pool contains ranges of free ids in the form [bottom,top).
373 */
[127a8e]374 moleculeIdPool_t moleculeIdPool;
[cbc5fb]375 moleculeId_t currMoleculeId;
[127a8e]376 size_t lastMoleculePoolSize; //!< size of the pool after last defrag, to skip some defrags
377 unsigned int numMoleculeDefragSkips;
[5d1611]378private:
[02ee15]379 /**
380 * private constructor to ensure creation of the world using
381 * the singleton pattern.
382 */
[5d1611]383 World();
[02ee15]384
385 /**
386 * private destructor to ensure destruction of the world using the
387 * singleton pattern.
388 */
[5d1611]389 virtual ~World();
390
391 /*****
392 * some legacy stuff that is include for now but will be removed later
393 *****/
394public:
[354859]395 MoleculeListClass *&getMolecules();
[4d9c01]396
[5d1611]397private:
[354859]398 MoleculeListClass *molecules_deprecated;
[5d1611]399};
400
401#endif /* WORLD_HPP_ */
Note: See TracBrowser for help on using the repository browser.