| 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 <boost/iterator/transform_iterator.hpp>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include <string>
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "Atom/AtomSet.hpp"
 | 
|---|
| 28 | #include "CodePatterns/Cacheable.hpp"
 | 
|---|
| 29 | #include "CodePatterns/Observer/Observable.hpp"
 | 
|---|
| 30 | #include "CodePatterns/Observer/ObservedIterator.hpp"
 | 
|---|
| 31 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 32 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
 | 
|---|
| 33 | #include "Formula.hpp"
 | 
|---|
| 34 | #include "Helpers/defs.hpp"
 | 
|---|
| 35 | #include "IdPool_policy.hpp"
 | 
|---|
| 36 | #include "IdPool.hpp"
 | 
|---|
| 37 | #include "types.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | // TODO: Was is the include of MoleculeDescriptor_impl.hpp doing in molecule.hpp
 | 
|---|
| 40 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | /****************************************** forward declarations *****************************/
 | 
|---|
| 43 | 
 | 
|---|
| 44 | class atom;
 | 
|---|
| 45 | class bond;
 | 
|---|
| 46 | class BondedParticle;
 | 
|---|
| 47 | class BondGraph;
 | 
|---|
| 48 | class DepthFirstSearchAnalysis;
 | 
|---|
| 49 | class element;
 | 
|---|
| 50 | class ForceMatrix;
 | 
|---|
| 51 | class Graph;
 | 
|---|
| 52 | class LinkedCell_deprecated;
 | 
|---|
| 53 | class molecule;
 | 
|---|
| 54 | class MoleculeLeafClass;
 | 
|---|
| 55 | class MoleculeListClass;
 | 
|---|
| 56 | class periodentafel;
 | 
|---|
| 57 | class RealSpaceMatrix;
 | 
|---|
| 58 | class Vector;
 | 
|---|
| 59 | class Shape;
 | 
|---|
| 60 | 
 | 
|---|
| 61 | /******************************** Some definitions for easier reading **********************************/
 | 
|---|
| 62 | 
 | 
|---|
| 63 | struct FromIdToAtom :
 | 
|---|
| 64 |   public std::unary_function<atom *, atomId_t>
 | 
|---|
| 65 | {
 | 
|---|
| 66 |     atom * operator()(atomId_t id) const {
 | 
|---|
| 67 |      return World::getInstance().getAtom(AtomById(id));
 | 
|---|
| 68 |     }
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | /************************************* Class definitions ****************************************/
 | 
|---|
| 72 | 
 | 
|---|
| 73 | /** The complete molecule.
 | 
|---|
| 74 |  * Class incorporates number of types
 | 
|---|
| 75 |  */
 | 
|---|
| 76 | class molecule : public Observable
 | 
|---|
| 77 | {
 | 
|---|
| 78 |   friend molecule *NewMolecule();
 | 
|---|
| 79 |   friend void DeleteMolecule(molecule *);
 | 
|---|
| 80 | 
 | 
|---|
| 81 | public:
 | 
|---|
| 82 |   typedef std::set<atomId_t> atomIdSet;
 | 
|---|
| 83 |   typedef boost::transform_iterator<FromIdToAtom, atomIdSet::iterator, atom *, atomId_t> iterator;
 | 
|---|
| 84 |   typedef boost::transform_iterator<FromIdToAtom, atomIdSet::const_iterator, const atom *, atomId_t const &> const_iterator;
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   int MDSteps; //!< The number of MD steps in Trajectories
 | 
|---|
| 87 |   mutable int NoNonHydrogen; //!< number of non-hydrogen atoms in molecule
 | 
|---|
| 88 |   mutable int NoNonBonds; //!< number of non-hydrogen bonds in molecule
 | 
|---|
| 89 |   mutable int NoCyclicBonds; //!< number of cyclic bonds in molecule, by DepthFirstSearchAnalysis()
 | 
|---|
| 90 |   bool ActiveFlag; //!< in a MoleculeListClass used to discern active from inactive molecules
 | 
|---|
| 91 |   int IndexNr; //!< index of molecule in a MoleculeListClass
 | 
|---|
| 92 |   char name[MAXSTRINGSIZE]; //!< arbitrary name
 | 
|---|
| 93 | 
 | 
|---|
| 94 | private:
 | 
|---|
| 95 |   Formula formula;
 | 
|---|
| 96 |   Cacheable<int> AtomCount; //!< number of atoms, brought up-to-date by doCountAtoms()
 | 
|---|
| 97 |   Cacheable<int> BondCount; //!< number of atoms, brought up-to-date by doCountBonds()
 | 
|---|
| 98 |   moleculeId_t id;
 | 
|---|
| 99 |   atomIdSet atomIds; //<!set of atomic ids to check uniqueness of atoms
 | 
|---|
| 100 |   IdPool<atomId_t, uniqueId> atomIdPool;  //!< pool of internal ids such that way may guarantee uniqueness
 | 
|---|
| 101 | 
 | 
|---|
| 102 | protected:
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   molecule();
 | 
|---|
| 105 |   virtual ~molecule();
 | 
|---|
| 106 | 
 | 
|---|
| 107 | public:
 | 
|---|
| 108 |   //getter and setter
 | 
|---|
| 109 |   const std::string getName() const;
 | 
|---|
| 110 |   int getAtomCount() const;
 | 
|---|
| 111 |   int doCountAtoms();
 | 
|---|
| 112 |   int getBondCount() const;
 | 
|---|
| 113 |   int doCountBonds() const;
 | 
|---|
| 114 |   moleculeId_t getId() const;
 | 
|---|
| 115 |   void setId(moleculeId_t);
 | 
|---|
| 116 |   void setName(const std::string);
 | 
|---|
| 117 |   const Formula &getFormula() const;
 | 
|---|
| 118 |   unsigned int getElementCount() const;
 | 
|---|
| 119 |   bool hasElement(const element*) const;
 | 
|---|
| 120 |   bool hasElement(atomicNumber_t) const;
 | 
|---|
| 121 |   bool hasElement(const std::string&) const;
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   virtual bool changeId(atomId_t newId);
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   World::AtomComposite getAtomSet() const;
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   iterator begin();
 | 
|---|
| 128 |   const_iterator begin() const;
 | 
|---|
| 129 |   iterator end();
 | 
|---|
| 130 |   const_iterator end() const;
 | 
|---|
| 131 |   bool empty() const;
 | 
|---|
| 132 |   size_t size() const;
 | 
|---|
| 133 |   const_iterator find(atom * key) const;
 | 
|---|
| 134 |   pair<iterator, bool> insert(atom * const key);
 | 
|---|
| 135 |   bool containsAtom(atom* key);
 | 
|---|
| 136 | 
 | 
|---|
| 137 | private:
 | 
|---|
| 138 |   friend void atom::removeFromMolecule();
 | 
|---|
| 139 |   /** Erase an atom from the list.
 | 
|---|
| 140 |    * \note This should only be called by atom::removeFromMolecule(),
 | 
|---|
| 141 |    * otherwise it is not assured that the atom knows about it.
 | 
|---|
| 142 |    *
 | 
|---|
| 143 |    * @param loc locator to atom in list
 | 
|---|
| 144 |    * @return iterator to just after removed item (compliant with standard)
 | 
|---|
| 145 |    */
 | 
|---|
| 146 |   const_iterator erase(const_iterator loc);
 | 
|---|
| 147 |   /** Erase an atom from the list.
 | 
|---|
| 148 |    * \note This should only be called by atom::removeFromMolecule(),
 | 
|---|
| 149 |    * otherwise it is not assured that the atom knows about it.
 | 
|---|
| 150 |    *
 | 
|---|
| 151 |    * @param *key key to atom in list
 | 
|---|
| 152 |    * @return iterator to just after removed item (compliant with standard)
 | 
|---|
| 153 |    */
 | 
|---|
| 154 |   const_iterator erase(atom * key);
 | 
|---|
| 155 | 
 | 
|---|
| 156 | private:
 | 
|---|
| 157 |   friend bool atom::changeNr(int newId);
 | 
|---|
| 158 |   /**
 | 
|---|
| 159 |    * used when changing an ParticleInfo::Nr.
 | 
|---|
| 160 |    * Note that this number is local with this molecule.
 | 
|---|
| 161 |    * Unless you are calling this method from inside an atom don't fiddle with the third parameter.
 | 
|---|
| 162 |    *
 | 
|---|
| 163 |    * @param oldNr old Nr
 | 
|---|
| 164 |    * @param newNr new Nr to set
 | 
|---|
| 165 |    * @param *target ref to atom
 | 
|---|
| 166 |    * @return indicates wether the change could be done or not.
 | 
|---|
| 167 |    */
 | 
|---|
| 168 |   bool changeAtomNr(int oldNr, int newNr, atom* target=0);
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   /** Sets the name of the atom.
 | 
|---|
| 171 |    *
 | 
|---|
| 172 |    * The name is set via its element symbol and its internal ParticleInfo::Nr.
 | 
|---|
| 173 |    *
 | 
|---|
| 174 |    * @param _atom atom whose name to set
 | 
|---|
| 175 |    */
 | 
|---|
| 176 |   void setAtomName(atom *_atom) const;
 | 
|---|
| 177 | 
 | 
|---|
| 178 | public:
 | 
|---|
| 179 | 
 | 
|---|
| 180 |   /// remove atoms from molecule.
 | 
|---|
| 181 |   bool AddAtom(atom *pointer);
 | 
|---|
| 182 |   bool RemoveAtom(atom *pointer);
 | 
|---|
| 183 |   bool UnlinkAtom(atom *pointer);
 | 
|---|
| 184 |   bool CleanupMolecule();
 | 
|---|
| 185 |   void removeAtomsinMolecule();
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   /// Add/remove atoms to/from molecule.
 | 
|---|
| 188 |   atom * AddCopyAtom(atom *pointer);
 | 
|---|
| 189 |   bool AddHydrogenReplacementAtom(bond *Bond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem);
 | 
|---|
| 190 |   bond * AddBond(atom *first, atom *second, int degree = 1);
 | 
|---|
| 191 |   bool RemoveBond(bond *pointer);
 | 
|---|
| 192 |   bool RemoveBonds(atom *BondPartner);
 | 
|---|
| 193 |   bool hasBondStructure() const;
 | 
|---|
| 194 | 
 | 
|---|
| 195 |   /// Find atoms.
 | 
|---|
| 196 |   atom * FindAtom(int Nr) const;
 | 
|---|
| 197 |   atom * AskAtom(std::string text);
 | 
|---|
| 198 |   bool isInMolecule(const atom * const _atom);
 | 
|---|
| 199 | 
 | 
|---|
| 200 |   /// Count and change present atoms' coordination.
 | 
|---|
| 201 |   bool CenterInBox();
 | 
|---|
| 202 |   bool BoundInBox();
 | 
|---|
| 203 |   void CenterEdge(Vector *max);
 | 
|---|
| 204 |   void CenterOrigin();
 | 
|---|
| 205 |   void CenterPeriodic();
 | 
|---|
| 206 |   void CenterAtVector(Vector *newcenter);
 | 
|---|
| 207 |   void Translate(const Vector *x);
 | 
|---|
| 208 |   void TranslatePeriodically(const Vector *trans);
 | 
|---|
| 209 |   void Mirror(const Vector *x);
 | 
|---|
| 210 |   void Align(Vector *n);
 | 
|---|
| 211 |   void Scale(const double ** const factor);
 | 
|---|
| 212 |   void DeterminePeriodicCenter(Vector ¢er, const enum HydrogenSaturation _saturation = DoSaturate);
 | 
|---|
| 213 |   Vector * DetermineCenterOfGravity() const;
 | 
|---|
| 214 |   Vector * DetermineCenterOfAll() const;
 | 
|---|
| 215 |   Vector * DetermineCenterOfBox() const;
 | 
|---|
| 216 |   void SetNameFromFilename(const char *filename);
 | 
|---|
| 217 |   void SetBoxDimension(Vector *dim);
 | 
|---|
| 218 |   bool ScanForPeriodicCorrection();
 | 
|---|
| 219 |   double VolumeOfConvexEnvelope(bool IsAngstroem);
 | 
|---|
| 220 |   RealSpaceMatrix getInertiaTensor() const;
 | 
|---|
| 221 |   void RotateToPrincipalAxisSystem(Vector &Axis);
 | 
|---|
| 222 | 
 | 
|---|
| 223 |   bool CheckBounds(const Vector *x) const;
 | 
|---|
| 224 |   void GetAlignvector(struct lsq_params * par) const;
 | 
|---|
| 225 | 
 | 
|---|
| 226 |   /// Initialising routines in fragmentation
 | 
|---|
| 227 |   void OutputBondsList() const;
 | 
|---|
| 228 | 
 | 
|---|
| 229 |   bond * CopyBond(atom *left, atom *right, bond *CopyBond);
 | 
|---|
| 230 | 
 | 
|---|
| 231 |   molecule *CopyMolecule() const;
 | 
|---|
| 232 |   molecule* CopyMoleculeFromSubRegion(const Shape&) const;
 | 
|---|
| 233 | 
 | 
|---|
| 234 |   /// Fragment molecule by two different approaches:
 | 
|---|
| 235 |   bool StoreBondsToFile(std::string filename, std::string path = "");
 | 
|---|
| 236 |   bool StoreAdjacencyToFile(std::string filename, std::string path = "");
 | 
|---|
| 237 |   bool CreateFatherLookupTable(atom **&LookupTable, int count = 0);
 | 
|---|
| 238 | 
 | 
|---|
| 239 |   // Recognize doubly appearing molecules in a list of them
 | 
|---|
| 240 |   int * GetFatherSonAtomicMap(molecule *OtherMolecule);
 | 
|---|
| 241 |   bool FillBondStructureFromReference(const molecule * const reference, atom **&ListOfLocalAtoms, bool FreeList = false);
 | 
|---|
| 242 |   bool FillListOfLocalAtoms(atom **&ListOfLocalAtoms, const int GlobalAtomCount);
 | 
|---|
| 243 | 
 | 
|---|
| 244 |   // Output routines.
 | 
|---|
| 245 |   bool Output(std::ostream * const output) const;
 | 
|---|
| 246 |   bool OutputTrajectories(ofstream * const output) const;
 | 
|---|
| 247 |   void OutputListOfBonds() const;
 | 
|---|
| 248 |   bool OutputXYZ(ofstream * const output) const;
 | 
|---|
| 249 |   bool OutputTrajectoriesXYZ(ofstream * const output);
 | 
|---|
| 250 |   bool Checkout(ofstream * const output) const;
 | 
|---|
| 251 | 
 | 
|---|
| 252 |   // Manipulation routines
 | 
|---|
| 253 |   void flipActiveFlag();
 | 
|---|
| 254 | 
 | 
|---|
| 255 | private:
 | 
|---|
| 256 |   int last_atom; //!< number given to last atom
 | 
|---|
| 257 | };
 | 
|---|
| 258 | 
 | 
|---|
| 259 | molecule *NewMolecule();
 | 
|---|
| 260 | void DeleteMolecule(molecule* mol);
 | 
|---|
| 261 | 
 | 
|---|
| 262 | 
 | 
|---|
| 263 | 
 | 
|---|
| 264 | #endif /*MOLECULES_HPP_*/
 | 
|---|
| 265 | 
 | 
|---|