source: src/molecule.hpp@ d93d2c

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

Implemented more efficient per-molecule bounding box information.

  • Property mode set to 100755
File size: 12.6 KB
Line 
1/** \file molecule.hpp
2 *
3 * Class definitions of atom and molecule, element and periodentafel
4 */
5
6#ifndef MOLECULES_HPP_
7#define MOLECULES_HPP_
8
9/*********************************************** includes ***********************************/
10
11#ifdef HAVE_CONFIG_H
12#include <config.h>
13#endif
14
15//// STL headers
16#include <map>
17#include <set>
18#include <stack>
19#include <deque>
20#include <list>
21#include <vector>
22
23#include <string>
24
25#include <boost/bimap/bimap.hpp>
26#include <boost/bimap/unordered_set_of.hpp>
27#include <boost/bimap/multiset_of.hpp>
28#include <boost/optional.hpp>
29#include <boost/shared_ptr.hpp>
30
31#include "AtomIdSet.hpp"
32#include "Atom/AtomSet.hpp"
33#include "CodePatterns/Cacheable.hpp"
34#include "CodePatterns/Observer/Observable.hpp"
35#include "Descriptors/AtomIdDescriptor.hpp"
36#include "Fragmentation/HydrogenSaturation_enum.hpp"
37#include "Formula.hpp"
38#include "Helpers/defs.hpp"
39#include "IdPool_policy.hpp"
40#include "IdPool.hpp"
41#include "Shapes/Shape.hpp"
42#include "types.hpp"
43
44/****************************************** forward declarations *****************************/
45
46class atom;
47class bond;
48class BondedParticle;
49class BondGraph;
50class DepthFirstSearchAnalysis;
51class element;
52class ForceMatrix;
53class Graph;
54class LinkedCell_deprecated;
55class ListOfLocalAtoms_t;
56class molecule;
57class MoleculeLeafClass;
58class MoleculeListClass;
59class MoleculeUnittest;
60class RealSpaceMatrix;
61class Vector;
62
63/************************************* Class definitions ****************************************/
64
65/** External function to remove all atoms since this will also delete the molecule
66 *
67 * \param _mol ref pointer to molecule to destroy
68 */
69void removeAtomsinMolecule(molecule *&_mol);
70
71/** The complete molecule.
72 * Class incorporates number of types
73 */
74class molecule : public Observable
75{
76 //!> grant unit test access
77 friend class MoleculeUnittest;
78 //!> function may access cstor
79 friend molecule *NewMolecule();
80 //!> function may access dstor
81 friend void DeleteMolecule(molecule *);
82
83public:
84 typedef AtomIdSet::atomIdSet atomIdSet;
85 typedef AtomIdSet::iterator iterator;
86 typedef AtomIdSet::const_iterator const_iterator;
87
88 int MDSteps; //!< The number of MD steps in Trajectories
89 mutable int NoNonBonds; //!< number of non-hydrogen bonds in molecule
90 mutable int NoCyclicBonds; //!< number of cyclic bonds in molecule, by DepthFirstSearchAnalysis()
91 bool ActiveFlag; //!< in a MoleculeListClass used to discern active from inactive molecules
92 int IndexNr; //!< index of molecule in a MoleculeListClass
93 char name[MAXSTRINGSIZE]; //!< arbitrary name
94
95private:
96 Formula formula;
97 Cacheable<size_t> NoNonHydrogen; //!< number of non-hydrogen atoms in molecule
98 Cacheable<int> BondCount; //!< number of atoms, brought up-to-date by doCountBonds()
99 moleculeId_t id;
100 AtomIdSet atomIds; //<!set of atomic ids to check uniqueness of atoms
101 IdPool<atomId_t, uniqueId> atomIdPool; //!< pool of internal ids such that way may guarantee uniqueness
102 typedef std::map<atomId_t,atom *> LocalToGlobalId_t;
103 LocalToGlobalId_t LocalToGlobalId; //!< internal map to ease FindAtom
104
105protected:
106
107 molecule();
108 virtual ~molecule();
109
110public:
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 AtomNrChanged,
119 AtomMoved,
120 FormulaChanged,
121 MoleculeNameChanged,
122 IndexChanged,
123 BoundingBoxChanged,
124 AboutToBeRemoved,
125 NotificationType_MAX
126 };
127
128 //>! access to last changed element (atom)
129 const atomId_t lastChangedAtomId() const
130 { return _lastchangedatomid; }
131
132public:
133 //getter and setter
134 const std::string getName() const;
135 int getAtomCount() const;
136 size_t doCountNoNonHydrogen() const;
137 size_t getNoNonHydrogen() const;
138 int getBondCount() const;
139 int doCountBonds() const;
140 moleculeId_t getId() const;
141 void setId(moleculeId_t);
142 void setName(const std::string);
143 const Formula &getFormula() const;
144 unsigned int getElementCount() const;
145 bool hasElement(const element*) const;
146 bool hasElement(atomicNumber_t) const;
147 bool hasElement(const std::string&) const;
148
149 virtual bool changeId(atomId_t newId);
150
151 World::AtomComposite getAtomSet();
152 World::ConstAtomComposite getAtomSet() const;
153
154 // simply pass on all functions to AtomIdSet
155 iterator begin() {
156 return atomIds.begin();
157 }
158 const_iterator begin() const
159 {
160 return atomIds.begin();
161 }
162 iterator end()
163 {
164 return atomIds.end();
165 }
166 const_iterator end() const
167 {
168 return atomIds.end();
169 }
170 bool empty() const
171 {
172 return atomIds.empty();
173 }
174 size_t size() const
175 {
176 return atomIds.size();
177 }
178 const_iterator find(atom * key) const
179 {
180 return atomIds.find(key);
181 }
182
183 /** Returns the set of atomic ids contained in this molecule.
184 *
185 * @return set of atomic ids
186 */
187 const atomIdSet & getAtomIds() const {
188 return atomIds.getAtomIds();
189 }
190
191 std::pair<iterator, bool> insert(atom * const key);
192
193 /** Predicate whether given \a key is contained in this molecule.
194 *
195 * @param key atom to check
196 * @return true - is contained, false - else
197 */
198 bool containsAtom(const atom* key) const
199 {
200 return atomIds.contains(key);
201 }
202
203 /** Predicate whether given \a id is contained in this molecule.
204 *
205 * @param id atomic id to check
206 * @return true - is contained, false - else
207 */
208 bool containsAtom(const atomId_t id) const
209 {
210 return atomIds.contains(id);
211 }
212
213private:
214 friend void atom::removeFromMolecule();
215 /** Erase an atom from the list.
216 * \note This should only be called by atom::removeFromMolecule(),
217 * otherwise it is not assured that the atom knows about it.
218 *
219 * @param loc locator to atom in list
220 * @return iterator to just after removed item (compliant with standard)
221 */
222 const_iterator erase(const_iterator loc);
223
224 /** Erase an atom from the list.
225 * \note This should only be called by atom::removeFromMolecule(),
226 * otherwise it is not assured that the atom knows about it.
227 *
228 * @param *key key to atom in list
229 * @return iterator to just after removed item (compliant with standard)
230 */
231 const_iterator erase(atom * key);
232
233private:
234 friend bool atom::changeNr(int newId);
235 /**
236 * used when changing an ParticleInfo::Nr.
237 * Note that this number is local with this molecule.
238 * Unless you are calling this method from inside an atom don't fiddle with the third parameter.
239 *
240 * @param oldNr old Nr
241 * @param newNr new Nr to set
242 * @param *target ref to atom
243 * @return indicates wether the change could be done or not.
244 */
245 bool changeAtomNr(int oldNr, int newNr, atom* target=0);
246
247 friend bool atom::changeId(atomId_t newId);
248 /**
249 * used when changing an ParticleInfo::Id.
250 * Note that this number is global (and the molecule uses it to know which atoms belong to it)
251 *
252 * @param oldId old Id
253 * @param newId new Id to set
254 * @return indicates wether the change could be done or not.
255 */
256 bool changeAtomId(int oldId, int newId);
257
258 /** Updates the internal lookup fro local to global indices.
259 *
260 * \param pointer pointer to atom
261 */
262 void InsertLocalToGlobalId(atom * const pointer);
263
264 /** Sets the name of the atom.
265 *
266 * The name is set via its element symbol and its internal ParticleInfo::Nr.
267 *
268 * @param _atom atom whose name to set
269 */
270 void setAtomName(atom *_atom) const;
271
272 /** Resets the formula for this molecule.
273 *
274 * This is required in case an atom changes its element as we then don't
275 * have any knowledge about its previous element anymore.
276 */
277 void resetFormula();
278
279public:
280
281 /** Structure for the required information on the bounding box.
282 *
283 */
284 struct BoundingBoxInfo {
285 //!> position of center
286 Vector position;
287 //!> radius of sphere
288 double radius;
289
290 /** Equivalence operator for bounding box.
291 *
292 * \return true - both bounding boxes have same position and radius
293 */
294 bool operator==(const BoundingBoxInfo &_other) const
295 { return (radius == _other.radius) && (position == _other.position); }
296
297 /** Inequivalence operator for bounding box.
298 *
299 * \return true - bounding boxes have either different positions or different radii or both
300 */
301 bool operator!=(const BoundingBoxInfo &_other) const
302 { return !(*this == _other); }
303 };
304
305private:
306
307 /** Returns the current bounding box.
308 *
309 * \return Shape with center and extension of box
310 */
311 BoundingBoxInfo updateBoundingBox() const;
312
313 // stuff for keeping bounding box up-to-date efficiently
314
315 //!> Cacheable for the bounding box, ptr such that
316 boost::shared_ptr< Cacheable<BoundingBoxInfo> > BoundingBox;
317 /** Bimap storing atomic ids and the component per axis.
318 *
319 * We need a bimap in order to have the components sorted and be able to
320 * access max and min values in linear time and also access the ids in
321 * constant time in order to update the map, when atoms move, are inserted,
322 * or removed.
323 */
324 typedef boost::bimaps::bimap<
325 boost::bimaps::unordered_set_of< atomId_t >,
326 boost::bimaps::multiset_of< double, std::greater<double> >
327 > AtomDistanceMap_t;
328 std::vector<AtomDistanceMap_t> BoundingBoxSweepingAxis;
329
330public:
331
332 /** Returns the current bounding box of this molecule.
333 *
334 * \return bounding box info with center and radius
335 */
336 BoundingBoxInfo getBoundingBox() const;
337
338 /** Function to create a bounding spherical shape for the currently associated atoms.
339 *
340 * \param boundary extra boundary of shape around (i.e. distance between outermost atom
341 * and the shape's surface)
342 */
343 Shape getBoundingSphere(const double boundary = 0.) const;
344
345 /** Creates the bounding box by adding van der Waals-Spheres around every atom.
346 *
347 * \param scale extra scale parameter to enlarge the spheres artifically
348 */
349 Shape getBoundingShape(const double scale = 1.) const;
350
351 /// remove atoms from molecule.
352 bool AddAtom(atom *pointer);
353 bool RemoveAtom(atom *pointer);
354 bool UnlinkAtom(atom *pointer);
355 bool CleanupMolecule();
356
357 /// Add/remove atoms to/from molecule.
358 atom * AddCopyAtom(atom *pointer);
359// bool AddHydrogenReplacementAtom(bond::ptr Bond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem);
360 bond::ptr AddBond(atom *first, atom *second, int degree = 1);
361 bool hasBondStructure() const;
362
363 /// Find atoms.
364 atom * FindAtom(int Nr) const;
365 atom * AskAtom(std::string text);
366 bool isInMolecule(const atom * const _atom) const;
367
368 /// Count and change present atoms' coordination.
369 bool CenterInBox();
370 bool BoundInBox();
371 void CenterEdge();
372 void CenterOrigin();
373 void CenterPeriodic();
374 void CenterAtVector(const Vector &newcenter);
375 void Translate(const Vector &x);
376 void TranslatePeriodically(const Vector &trans);
377 void Mirror(const Vector &x);
378 void Align(const Vector &n);
379 void Scale(const double *factor);
380 void DeterminePeriodicCenter(Vector &center, const enum HydrogenTreatment _treatment = ExcludeHydrogen);
381 const Vector DetermineCenterOfGravity() const;
382 const Vector DetermineCenterOfAll() const;
383 void SetNameFromFilename(const char *filename);
384 bool ScanForPeriodicCorrection();
385 double VolumeOfConvexEnvelope(bool IsAngstroem);
386 RealSpaceMatrix getInertiaTensor() const;
387 void RotateToPrincipalAxisSystem(const Vector &Axis);
388
389 bool CheckBounds(const Vector *x) const;
390 void GetAlignvector(struct lsq_params * par) const;
391
392 /// Initialising routines in fragmentation
393 void OutputBondsList() const;
394
395 bond::ptr CopyBond(atom *left, atom *right, bond::ptr CopyBond);
396
397 molecule *CopyMolecule(const Vector &offset = zeroVec);
398 molecule* CopyMoleculeFromSubRegion(const Shape&);
399
400 /// Fragment molecule by two different approaches:
401 bool StoreBondsToFile(std::string filename, std::string path = "");
402 bool CreateFatherLookupTable(ListOfLocalAtoms_t &LookupTable, int count = 0);
403
404 // Recognize doubly appearing molecules in a list of them
405 int * GetFatherSonAtomicMap(const molecule * const OtherMolecule);
406 bool FillBondStructureFromReference(const molecule * const reference, ListOfLocalAtoms_t &ListOfLocalAtoms, bool FreeList = false);
407 bool FillListOfLocalAtoms(ListOfLocalAtoms_t &ListOfLocalAtoms, const int GlobalAtomCount);
408
409 // Output routines.
410 bool Output(std::ostream * const output) const;
411 void OutputListOfBonds() const;
412
413 // Manipulation routines
414 void flipActiveFlag();
415
416 virtual void update(Observable *publisher);
417 virtual void recieveNotification(Observable *publisher, Notification_ptr notification);
418 virtual void subjectKilled(Observable *publisher);
419
420private:
421 atomId_t _lastchangedatomid;
422
423 int last_atom; //!< number given to last atom
424};
425
426molecule *NewMolecule();
427void DeleteMolecule(molecule* mol);
428
429
430
431#endif /*MOLECULES_HPP_*/
432
Note: See TracBrowser for help on using the repository browser.