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