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