source: src/molecule.hpp@ 15c75f8

SegFault_gcc4.6
Last change on this file since 15c75f8 was 15c75f8, checked in by Frederik Heber <heber@…>, 10 years ago

molecule has a lastchangedatom member variable now.

  • FIX: molecule::getBoundingSphere() did not check for no atoms.
  • Property mode set to 100755
File size: 9.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 "AtomIdSet.hpp"
26#include "Atom/AtomSet.hpp"
27#include "CodePatterns/Cacheable.hpp"
28#include "CodePatterns/Observer/Observable.hpp"
29#include "Descriptors/AtomIdDescriptor.hpp"
30#include "Fragmentation/HydrogenSaturation_enum.hpp"
31#include "Formula.hpp"
32#include "Helpers/defs.hpp"
33#include "IdPool_policy.hpp"
34#include "IdPool.hpp"
35#include "Shapes/Shape.hpp"
36#include "types.hpp"
37
38/****************************************** forward declarations *****************************/
39
40class atom;
41class bond;
42class BondedParticle;
43class BondGraph;
44class DepthFirstSearchAnalysis;
45class element;
46class ForceMatrix;
47class Graph;
48class LinkedCell_deprecated;
49class ListOfLocalAtoms_t;
50class molecule;
51class MoleculeLeafClass;
52class MoleculeListClass;
53class MoleculeUnittest;
54class RealSpaceMatrix;
55class Vector;
56
57/************************************* Class definitions ****************************************/
58
59/** The complete molecule.
60 * Class incorporates number of types
61 */
62class molecule : public Observable
63{
64 //!> grant unit test access
65 friend class MoleculeUnittest;
66 //!> function may access cstor
67 friend molecule *NewMolecule();
68 //!> function may access dstor
69 friend void DeleteMolecule(molecule *);
70
71public:
72 typedef AtomIdSet::atomIdSet atomIdSet;
73 typedef AtomIdSet::iterator iterator;
74 typedef AtomIdSet::const_iterator const_iterator;
75
76 int MDSteps; //!< The number of MD steps in Trajectories
77 mutable int NoNonBonds; //!< number of non-hydrogen bonds in molecule
78 mutable int NoCyclicBonds; //!< number of cyclic bonds in molecule, by DepthFirstSearchAnalysis()
79 bool ActiveFlag; //!< in a MoleculeListClass used to discern active from inactive molecules
80 int IndexNr; //!< index of molecule in a MoleculeListClass
81 char name[MAXSTRINGSIZE]; //!< arbitrary name
82
83private:
84 Formula formula;
85 Cacheable<size_t> NoNonHydrogen; //!< number of non-hydrogen atoms in molecule
86 Cacheable<int> BondCount; //!< number of atoms, brought up-to-date by doCountBonds()
87 moleculeId_t id;
88 AtomIdSet atomIds; //<!set of atomic ids to check uniqueness of atoms
89 IdPool<atomId_t, uniqueId> atomIdPool; //!< pool of internal ids such that way may guarantee uniqueness
90 typedef std::map<atomId_t,atom *> LocalToGlobalId_t;
91 LocalToGlobalId_t LocalToGlobalId; //!< internal map to ease FindAtom
92
93protected:
94
95 molecule();
96 virtual ~molecule();
97
98public:
99
100 /******* Notifications *******/
101
102 //!> enumeration of present notification types: only insertion/removal of atoms or molecules
103 enum NotificationType {
104 AtomInserted,
105 AtomRemoved,
106 AtomNrChanged,
107 MoleculeNameChanged,
108 NotificationType_MAX
109 };
110
111 //>! access to last changed element (atom)
112 const atom* lastChanged() const
113 { return _lastchangedatom; }
114
115public:
116 //getter and setter
117 const std::string getName() const;
118 int getAtomCount() const;
119 size_t doCountNoNonHydrogen() const;
120 size_t getNoNonHydrogen() const;
121 int getBondCount() const;
122 int doCountBonds() const;
123 moleculeId_t getId() const;
124 void setId(moleculeId_t);
125 void setName(const std::string);
126 const Formula &getFormula() const;
127 unsigned int getElementCount() const;
128 bool hasElement(const element*) const;
129 bool hasElement(atomicNumber_t) const;
130 bool hasElement(const std::string&) const;
131
132 virtual bool changeId(atomId_t newId);
133
134 World::AtomComposite getAtomSet() const;
135
136 // simply pass on all functions to AtomIdSet
137 iterator begin() {
138 return atomIds.begin();
139 }
140 const_iterator begin() const
141 {
142 return atomIds.begin();
143 }
144 iterator end()
145 {
146 return atomIds.end();
147 }
148 const_iterator end() const
149 {
150 return atomIds.end();
151 }
152 bool empty() const
153 {
154 return atomIds.empty();
155 }
156 size_t size() const
157 {
158 return atomIds.size();
159 }
160 const_iterator find(atom * key) const
161 {
162 return atomIds.find(key);
163 }
164
165 /** Returns the set of atomic ids contained in this molecule.
166 *
167 * @return set of atomic ids
168 */
169 const atomIdSet & getAtomIds() const {
170 return atomIds.getAtomIds();
171 }
172
173 std::pair<iterator, bool> insert(atom * const key);
174
175 /** Predicate whether given \a key is contained in this molecule.
176 *
177 * @param key atom to check
178 * @return true - is contained, false - else
179 */
180 bool containsAtom(const atom* key) const
181 {
182 return atomIds.contains(key);
183 }
184
185 /** Predicate whether given \a id is contained in this molecule.
186 *
187 * @param id atomic id to check
188 * @return true - is contained, false - else
189 */
190 bool containsAtom(const atomId_t id) const
191 {
192 return atomIds.contains(id);
193 }
194
195private:
196 friend void atom::removeFromMolecule();
197 /** Erase an atom from the list.
198 * \note This should only be called by atom::removeFromMolecule(),
199 * otherwise it is not assured that the atom knows about it.
200 *
201 * @param loc locator to atom in list
202 * @return iterator to just after removed item (compliant with standard)
203 */
204 const_iterator erase(const_iterator loc);
205
206 /** Erase an atom from the list.
207 * \note This should only be called by atom::removeFromMolecule(),
208 * otherwise it is not assured that the atom knows about it.
209 *
210 * @param *key key to atom in list
211 * @return iterator to just after removed item (compliant with standard)
212 */
213 const_iterator erase(atom * key);
214
215private:
216 friend bool atom::changeNr(int newId);
217 /**
218 * used when changing an ParticleInfo::Nr.
219 * Note that this number is local with this molecule.
220 * Unless you are calling this method from inside an atom don't fiddle with the third parameter.
221 *
222 * @param oldNr old Nr
223 * @param newNr new Nr to set
224 * @param *target ref to atom
225 * @return indicates wether the change could be done or not.
226 */
227 bool changeAtomNr(int oldNr, int newNr, atom* target=0);
228
229 /** Updates the internal lookup fro local to global indices.
230 *
231 * \param pointer pointer to atom
232 */
233 void InsertLocalToGlobalId(atom * const pointer);
234
235 /** Sets the name of the atom.
236 *
237 * The name is set via its element symbol and its internal ParticleInfo::Nr.
238 *
239 * @param _atom atom whose name to set
240 */
241 void setAtomName(atom *_atom) const;
242
243public:
244
245 /** Function to create a bounding spherical shape for the currently associated atoms.
246 *
247 * \param boundary extra boundary of shape around (i.e. distance between outermost atom
248 * and the shape's surface)
249 */
250 Shape getBoundingSphere(const double boundary = 0.) const;
251
252 /** Creates the bounding box by adding van der Waals-Spheres around every atom.
253 *
254 * \param scale extra scale parameter to enlarge the spheres artifically
255 */
256 Shape getBoundingShape(const double scale = 1.) const;
257
258 /// remove atoms from molecule.
259 bool AddAtom(atom *pointer);
260 bool RemoveAtom(atom *pointer);
261 bool UnlinkAtom(atom *pointer);
262 bool CleanupMolecule();
263 void removeAtomsinMolecule();
264
265 /// Add/remove atoms to/from molecule.
266 atom * AddCopyAtom(atom *pointer);
267// bool AddHydrogenReplacementAtom(bond::ptr Bond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem);
268 bond::ptr AddBond(atom *first, atom *second, int degree = 1);
269 bool hasBondStructure() const;
270
271 /// Find atoms.
272 atom * FindAtom(int Nr) const;
273 atom * AskAtom(std::string text);
274 bool isInMolecule(const atom * const _atom);
275
276 /// Count and change present atoms' coordination.
277 bool CenterInBox();
278 bool BoundInBox();
279 void CenterEdge();
280 void CenterOrigin();
281 void CenterPeriodic();
282 void CenterAtVector(const Vector &newcenter);
283 void Translate(const Vector &x);
284 void TranslatePeriodically(const Vector &trans);
285 void Mirror(const Vector &x);
286 void Align(const Vector &n);
287 void Scale(const double *factor);
288 void DeterminePeriodicCenter(Vector &center, const enum HydrogenTreatment _treatment = ExcludeHydrogen);
289 const Vector DetermineCenterOfGravity() const;
290 const Vector DetermineCenterOfAll() const;
291 void SetNameFromFilename(const char *filename);
292 bool ScanForPeriodicCorrection();
293 double VolumeOfConvexEnvelope(bool IsAngstroem);
294 RealSpaceMatrix getInertiaTensor() const;
295 void RotateToPrincipalAxisSystem(const Vector &Axis);
296
297 bool CheckBounds(const Vector *x) const;
298 void GetAlignvector(struct lsq_params * par) const;
299
300 /// Initialising routines in fragmentation
301 void OutputBondsList() const;
302
303 bond::ptr CopyBond(atom *left, atom *right, bond::ptr CopyBond);
304
305 molecule *CopyMolecule(const Vector &offset = zeroVec) const;
306 molecule* CopyMoleculeFromSubRegion(const Shape&) const;
307
308 /// Fragment molecule by two different approaches:
309 bool StoreBondsToFile(std::string filename, std::string path = "");
310 bool CreateFatherLookupTable(ListOfLocalAtoms_t &LookupTable, int count = 0);
311
312 // Recognize doubly appearing molecules in a list of them
313 int * GetFatherSonAtomicMap(molecule *OtherMolecule);
314 bool FillBondStructureFromReference(const molecule * const reference, ListOfLocalAtoms_t &ListOfLocalAtoms, bool FreeList = false);
315 bool FillListOfLocalAtoms(ListOfLocalAtoms_t &ListOfLocalAtoms, const int GlobalAtomCount);
316
317 // Output routines.
318 bool Output(std::ostream * const output) const;
319 void OutputListOfBonds() const;
320
321 // Manipulation routines
322 void flipActiveFlag();
323
324private:
325 friend const atom *detail::lastChanged<atom>();
326 atom *_lastchangedatom;
327
328 int last_atom; //!< number given to last atom
329};
330
331molecule *NewMolecule();
332void DeleteMolecule(molecule* mol);
333
334
335
336#endif /*MOLECULES_HPP_*/
337
Note: See TracBrowser for help on using the repository browser.