| 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 | IndexChanged, | 
|---|
| 117 | AboutToBeRemoved, | 
|---|
| 118 | NotificationType_MAX | 
|---|
| 119 | }; | 
|---|
| 120 |  | 
|---|
| 121 | //>! access to last changed element (atom) | 
|---|
| 122 | const atomId_t lastChangedAtomId() const | 
|---|
| 123 | { return _lastchangedatomid; } | 
|---|
| 124 |  | 
|---|
| 125 | public: | 
|---|
| 126 | //getter and setter | 
|---|
| 127 | const std::string getName() const; | 
|---|
| 128 | int getAtomCount() const; | 
|---|
| 129 | size_t doCountNoNonHydrogen() const; | 
|---|
| 130 | size_t getNoNonHydrogen() const; | 
|---|
| 131 | int getBondCount() const; | 
|---|
| 132 | int doCountBonds() const; | 
|---|
| 133 | moleculeId_t getId() const; | 
|---|
| 134 | void setId(moleculeId_t); | 
|---|
| 135 | void setName(const std::string); | 
|---|
| 136 | const Formula &getFormula() const; | 
|---|
| 137 | unsigned int getElementCount() const; | 
|---|
| 138 | bool hasElement(const element*) const; | 
|---|
| 139 | bool hasElement(atomicNumber_t) const; | 
|---|
| 140 | bool hasElement(const std::string&) const; | 
|---|
| 141 |  | 
|---|
| 142 | virtual bool changeId(atomId_t newId); | 
|---|
| 143 |  | 
|---|
| 144 | World::AtomComposite getAtomSet(); | 
|---|
| 145 | World::ConstAtomComposite getAtomSet() const; | 
|---|
| 146 |  | 
|---|
| 147 | // simply pass on all functions to AtomIdSet | 
|---|
| 148 | iterator begin() { | 
|---|
| 149 | return atomIds.begin(); | 
|---|
| 150 | } | 
|---|
| 151 | const_iterator begin() const | 
|---|
| 152 | { | 
|---|
| 153 | return atomIds.begin(); | 
|---|
| 154 | } | 
|---|
| 155 | iterator end() | 
|---|
| 156 | { | 
|---|
| 157 | return atomIds.end(); | 
|---|
| 158 | } | 
|---|
| 159 | const_iterator end() const | 
|---|
| 160 | { | 
|---|
| 161 | return atomIds.end(); | 
|---|
| 162 | } | 
|---|
| 163 | bool empty() const | 
|---|
| 164 | { | 
|---|
| 165 | return atomIds.empty(); | 
|---|
| 166 | } | 
|---|
| 167 | size_t size() const | 
|---|
| 168 | { | 
|---|
| 169 | return atomIds.size(); | 
|---|
| 170 | } | 
|---|
| 171 | const_iterator find(atom * key) const | 
|---|
| 172 | { | 
|---|
| 173 | return atomIds.find(key); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | /** Returns the set of atomic ids contained in this molecule. | 
|---|
| 177 | * | 
|---|
| 178 | * @return set of atomic ids | 
|---|
| 179 | */ | 
|---|
| 180 | const atomIdSet & getAtomIds() const { | 
|---|
| 181 | return atomIds.getAtomIds(); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | std::pair<iterator, bool> insert(atom * const key); | 
|---|
| 185 |  | 
|---|
| 186 | /** Predicate whether given \a key is contained in this molecule. | 
|---|
| 187 | * | 
|---|
| 188 | * @param key atom to check | 
|---|
| 189 | * @return true - is contained, false - else | 
|---|
| 190 | */ | 
|---|
| 191 | bool containsAtom(const atom* key) const | 
|---|
| 192 | { | 
|---|
| 193 | return atomIds.contains(key); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | /** Predicate whether given \a id is contained in this molecule. | 
|---|
| 197 | * | 
|---|
| 198 | * @param id atomic id to check | 
|---|
| 199 | * @return true - is contained, false - else | 
|---|
| 200 | */ | 
|---|
| 201 | bool containsAtom(const atomId_t id) const | 
|---|
| 202 | { | 
|---|
| 203 | return atomIds.contains(id); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | private: | 
|---|
| 207 | friend void atom::removeFromMolecule(); | 
|---|
| 208 | /** Erase an atom from the list. | 
|---|
| 209 | * \note This should only be called by atom::removeFromMolecule(), | 
|---|
| 210 | * otherwise it is not assured that the atom knows about it. | 
|---|
| 211 | * | 
|---|
| 212 | * @param loc locator to atom in list | 
|---|
| 213 | * @return iterator to just after removed item (compliant with standard) | 
|---|
| 214 | */ | 
|---|
| 215 | const_iterator erase(const_iterator loc); | 
|---|
| 216 |  | 
|---|
| 217 | /** Erase an atom from the list. | 
|---|
| 218 | * \note This should only be called by atom::removeFromMolecule(), | 
|---|
| 219 | * otherwise it is not assured that the atom knows about it. | 
|---|
| 220 | * | 
|---|
| 221 | * @param *key key to atom in list | 
|---|
| 222 | * @return iterator to just after removed item (compliant with standard) | 
|---|
| 223 | */ | 
|---|
| 224 | const_iterator erase(atom * key); | 
|---|
| 225 |  | 
|---|
| 226 | private: | 
|---|
| 227 | friend bool atom::changeNr(int newId); | 
|---|
| 228 | /** | 
|---|
| 229 | * used when changing an ParticleInfo::Nr. | 
|---|
| 230 | * Note that this number is local with this molecule. | 
|---|
| 231 | * Unless you are calling this method from inside an atom don't fiddle with the third parameter. | 
|---|
| 232 | * | 
|---|
| 233 | * @param oldNr old Nr | 
|---|
| 234 | * @param newNr new Nr to set | 
|---|
| 235 | * @param *target ref to atom | 
|---|
| 236 | * @return indicates wether the change could be done or not. | 
|---|
| 237 | */ | 
|---|
| 238 | bool changeAtomNr(int oldNr, int newNr, atom* target=0); | 
|---|
| 239 |  | 
|---|
| 240 | friend bool atom::changeId(atomId_t newId); | 
|---|
| 241 | /** | 
|---|
| 242 | * used when changing an ParticleInfo::Id. | 
|---|
| 243 | * Note that this number is global (and the molecule uses it to know which atoms belong to it) | 
|---|
| 244 | * | 
|---|
| 245 | * @param oldId old Id | 
|---|
| 246 | * @param newId new Id to set | 
|---|
| 247 | * @return indicates wether the change could be done or not. | 
|---|
| 248 | */ | 
|---|
| 249 | bool changeAtomId(int oldId, int newId); | 
|---|
| 250 |  | 
|---|
| 251 | /** Updates the internal lookup fro local to global indices. | 
|---|
| 252 | * | 
|---|
| 253 | * \param pointer pointer to atom | 
|---|
| 254 | */ | 
|---|
| 255 | void InsertLocalToGlobalId(atom * const pointer); | 
|---|
| 256 |  | 
|---|
| 257 | /** Sets the name of the atom. | 
|---|
| 258 | * | 
|---|
| 259 | * The name is set via its element symbol and its internal ParticleInfo::Nr. | 
|---|
| 260 | * | 
|---|
| 261 | * @param _atom atom whose name to set | 
|---|
| 262 | */ | 
|---|
| 263 | void setAtomName(atom *_atom) const; | 
|---|
| 264 |  | 
|---|
| 265 | /** Resets the formula for this molecule. | 
|---|
| 266 | * | 
|---|
| 267 | * This is required in case an atom changes its element as we then don't | 
|---|
| 268 | * have any knowledge about its previous element anymore. | 
|---|
| 269 | */ | 
|---|
| 270 | void resetFormula(); | 
|---|
| 271 |  | 
|---|
| 272 | public: | 
|---|
| 273 |  | 
|---|
| 274 | /** Function to create a bounding spherical shape for the currently associated atoms. | 
|---|
| 275 | * | 
|---|
| 276 | * \param boundary extra boundary of shape around (i.e. distance between outermost atom | 
|---|
| 277 | *        and the shape's surface) | 
|---|
| 278 | */ | 
|---|
| 279 | Shape getBoundingSphere(const double boundary = 0.) const; | 
|---|
| 280 |  | 
|---|
| 281 | /** Creates the bounding box by adding van der Waals-Spheres around every atom. | 
|---|
| 282 | * | 
|---|
| 283 | * \param scale extra scale parameter to enlarge the spheres artifically | 
|---|
| 284 | */ | 
|---|
| 285 | Shape getBoundingShape(const double scale = 1.) const; | 
|---|
| 286 |  | 
|---|
| 287 | /// remove atoms from molecule. | 
|---|
| 288 | bool AddAtom(atom *pointer); | 
|---|
| 289 | bool RemoveAtom(atom *pointer); | 
|---|
| 290 | bool UnlinkAtom(atom *pointer); | 
|---|
| 291 | bool CleanupMolecule(); | 
|---|
| 292 |  | 
|---|
| 293 | /// Add/remove atoms to/from molecule. | 
|---|
| 294 | atom * AddCopyAtom(atom *pointer); | 
|---|
| 295 | //  bool AddHydrogenReplacementAtom(bond::ptr Bond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem); | 
|---|
| 296 | bond::ptr AddBond(atom *first, atom *second, int degree = 1); | 
|---|
| 297 | bool hasBondStructure() const; | 
|---|
| 298 |  | 
|---|
| 299 | /// Find atoms. | 
|---|
| 300 | atom * FindAtom(int Nr) const; | 
|---|
| 301 | atom * AskAtom(std::string text); | 
|---|
| 302 | bool isInMolecule(const atom * const _atom) const; | 
|---|
| 303 |  | 
|---|
| 304 | /// Count and change present atoms' coordination. | 
|---|
| 305 | bool CenterInBox(); | 
|---|
| 306 | bool BoundInBox(); | 
|---|
| 307 | void CenterEdge(); | 
|---|
| 308 | void CenterOrigin(); | 
|---|
| 309 | void CenterPeriodic(); | 
|---|
| 310 | void CenterAtVector(const Vector &newcenter); | 
|---|
| 311 | void Translate(const Vector &x); | 
|---|
| 312 | void TranslatePeriodically(const Vector &trans); | 
|---|
| 313 | void Mirror(const Vector &x); | 
|---|
| 314 | void Align(const Vector &n); | 
|---|
| 315 | void Scale(const double *factor); | 
|---|
| 316 | void DeterminePeriodicCenter(Vector ¢er, const enum HydrogenTreatment _treatment = ExcludeHydrogen); | 
|---|
| 317 | const Vector DetermineCenterOfGravity() const; | 
|---|
| 318 | const Vector DetermineCenterOfAll() const; | 
|---|
| 319 | void SetNameFromFilename(const char *filename); | 
|---|
| 320 | bool ScanForPeriodicCorrection(); | 
|---|
| 321 | double VolumeOfConvexEnvelope(bool IsAngstroem); | 
|---|
| 322 | RealSpaceMatrix getInertiaTensor() const; | 
|---|
| 323 | void RotateToPrincipalAxisSystem(const Vector &Axis); | 
|---|
| 324 |  | 
|---|
| 325 | bool CheckBounds(const Vector *x) const; | 
|---|
| 326 | void GetAlignvector(struct lsq_params * par) const; | 
|---|
| 327 |  | 
|---|
| 328 | /// Initialising routines in fragmentation | 
|---|
| 329 | void OutputBondsList() const; | 
|---|
| 330 |  | 
|---|
| 331 | bond::ptr CopyBond(atom *left, atom *right, bond::ptr CopyBond); | 
|---|
| 332 |  | 
|---|
| 333 | molecule *CopyMolecule(const Vector &offset = zeroVec); | 
|---|
| 334 | molecule* CopyMoleculeFromSubRegion(const Shape&); | 
|---|
| 335 |  | 
|---|
| 336 | /// Fragment molecule by two different approaches: | 
|---|
| 337 | bool StoreBondsToFile(std::string filename, std::string path = ""); | 
|---|
| 338 | bool CreateFatherLookupTable(ListOfLocalAtoms_t &LookupTable, int count = 0); | 
|---|
| 339 |  | 
|---|
| 340 | // Recognize doubly appearing molecules in a list of them | 
|---|
| 341 | int * GetFatherSonAtomicMap(const molecule * const OtherMolecule); | 
|---|
| 342 | bool FillBondStructureFromReference(const molecule * const reference, ListOfLocalAtoms_t &ListOfLocalAtoms, bool FreeList = false); | 
|---|
| 343 | bool FillListOfLocalAtoms(ListOfLocalAtoms_t &ListOfLocalAtoms, const int GlobalAtomCount); | 
|---|
| 344 |  | 
|---|
| 345 | // Output routines. | 
|---|
| 346 | bool Output(std::ostream * const output) const; | 
|---|
| 347 | void OutputListOfBonds() const; | 
|---|
| 348 |  | 
|---|
| 349 | // Manipulation routines | 
|---|
| 350 | void flipActiveFlag(); | 
|---|
| 351 |  | 
|---|
| 352 | virtual void update(Observable *publisher); | 
|---|
| 353 | virtual void recieveNotification(Observable *publisher, Notification_ptr notification); | 
|---|
| 354 | virtual void subjectKilled(Observable *publisher); | 
|---|
| 355 |  | 
|---|
| 356 | private: | 
|---|
| 357 | atomId_t _lastchangedatomid; | 
|---|
| 358 |  | 
|---|
| 359 | int last_atom; //!< number given to last atom | 
|---|
| 360 | }; | 
|---|
| 361 |  | 
|---|
| 362 | molecule *NewMolecule(); | 
|---|
| 363 | void DeleteMolecule(molecule* mol); | 
|---|
| 364 |  | 
|---|
| 365 |  | 
|---|
| 366 |  | 
|---|
| 367 | #endif /*MOLECULES_HPP_*/ | 
|---|
| 368 |  | 
|---|