| 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 "types.hpp"
 | 
|---|
| 26 | #include "graph.hpp"
 | 
|---|
| 27 | #include "CodePatterns/Observer.hpp"
 | 
|---|
| 28 | #include "CodePatterns/ObservedIterator.hpp"
 | 
|---|
| 29 | #include "CodePatterns/Cacheable.hpp"
 | 
|---|
| 30 | #include "Formula.hpp"
 | 
|---|
| 31 | #include "AtomSet.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
 | 
|---|
| 34 | 
 | 
|---|
| 35 | /****************************************** forward declarations *****************************/
 | 
|---|
| 36 | 
 | 
|---|
| 37 | class atom;
 | 
|---|
| 38 | class bond;
 | 
|---|
| 39 | class BondedParticle;
 | 
|---|
| 40 | class BondGraph;
 | 
|---|
| 41 | class element;
 | 
|---|
| 42 | class ForceMatrix;
 | 
|---|
| 43 | class LinkedCell;
 | 
|---|
| 44 | class molecule;
 | 
|---|
| 45 | class MoleculeLeafClass;
 | 
|---|
| 46 | class MoleculeListClass;
 | 
|---|
| 47 | class periodentafel;
 | 
|---|
| 48 | class RealSpaceMatrix;
 | 
|---|
| 49 | class Vector;
 | 
|---|
| 50 | class Shape;
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /******************************** Some definitions for easier reading **********************************/
 | 
|---|
| 53 | 
 | 
|---|
| 54 | #define MoleculeList list <molecule *>
 | 
|---|
| 55 | #define MoleculeListTest pair <MoleculeList::iterator, bool>
 | 
|---|
| 56 | 
 | 
|---|
| 57 | #define DistancePair pair < double, atom* >
 | 
|---|
| 58 | #define DistanceMap multimap < double, atom* >
 | 
|---|
| 59 | #define DistanceTestPair pair < DistanceMap::iterator, bool>
 | 
|---|
| 60 | 
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /************************************* Class definitions ****************************************/
 | 
|---|
| 63 | 
 | 
|---|
| 64 | /** Structure to contain parameters needed for evaluation of constraint potential.
 | 
|---|
| 65 |  */
 | 
|---|
| 66 | struct EvaluatePotential
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   int startstep; //!< start configuration (MDStep in atom::trajectory)
 | 
|---|
| 69 |   int endstep; //!< end configuration (MDStep in atom::trajectory)
 | 
|---|
| 70 |   atom **PermutationMap; //!< gives target ptr for each atom, array of size molecule::AtomCount (this is "x" in \f$ V^{con}(x) \f$ )
 | 
|---|
| 71 |   DistanceMap **DistanceList; //!< distance list of each atom to each atom
 | 
|---|
| 72 |   DistanceMap::iterator *StepList; //!< iterator to ascend through NearestNeighbours \a **DistanceList
 | 
|---|
| 73 |   int *DoubleList; //!< count of which sources want to move to this target, basically the injective measure (>1 -> not injective)
 | 
|---|
| 74 |   DistanceMap::iterator *DistanceIterators; //!< marks which was the last picked target as injective candidate with smallest distance
 | 
|---|
| 75 |   bool IsAngstroem; //!< whether coordinates are in angstroem (true) or bohrradius (false)
 | 
|---|
| 76 |   double *PenaltyConstants; //!<  penalty constant in front of each term
 | 
|---|
| 77 | };
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /** The complete molecule.
 | 
|---|
| 80 |  * Class incorporates number of types
 | 
|---|
| 81 |  */
 | 
|---|
| 82 | class molecule : public Observable
 | 
|---|
| 83 | {
 | 
|---|
| 84 |   friend molecule *NewMolecule();
 | 
|---|
| 85 |   friend void DeleteMolecule(molecule *);
 | 
|---|
| 86 | 
 | 
|---|
| 87 | public:
 | 
|---|
| 88 |   typedef ATOMSET(std::list) atomSet;
 | 
|---|
| 89 |   typedef ATOMSET(std::vector) atomVector;
 | 
|---|
| 90 |   typedef std::set<atomId_t> atomIdSet;
 | 
|---|
| 91 |   typedef ObservedIterator<atomSet> iterator;
 | 
|---|
| 92 |   typedef atomSet::const_iterator const_iterator;
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   const periodentafel * const elemente; //!< periodic table with each element
 | 
|---|
| 95 |   // old deprecated atom handling
 | 
|---|
| 96 |   //atom *start;        //!< start of atom list
 | 
|---|
| 97 |   //atom *end;          //!< end of atom list
 | 
|---|
| 98 |   //bond *first;        //!< start of bond list
 | 
|---|
| 99 |   //bond *last;         //!< end of bond list
 | 
|---|
| 100 |   int MDSteps; //!< The number of MD steps in Trajectories
 | 
|---|
| 101 |   mutable int NoNonHydrogen; //!< number of non-hydrogen atoms in molecule
 | 
|---|
| 102 |   mutable int NoNonBonds; //!< number of non-hydrogen bonds in molecule
 | 
|---|
| 103 |   mutable int NoCyclicBonds; //!< number of cyclic bonds in molecule, by DepthFirstSearchAnalysis()
 | 
|---|
| 104 |   bool ActiveFlag; //!< in a MoleculeListClass used to discern active from inactive molecules
 | 
|---|
| 105 |   //Vector Center;      //!< Center of molecule in a global box
 | 
|---|
| 106 |   int IndexNr; //!< index of molecule in a MoleculeListClass
 | 
|---|
| 107 |   char name[MAXSTRINGSIZE]; //!< arbitrary name
 | 
|---|
| 108 | 
 | 
|---|
| 109 | private:
 | 
|---|
| 110 |   Formula formula;
 | 
|---|
| 111 |   Cacheable<int> AtomCount; //!< number of atoms, brought up-to-date by doCountAtoms()
 | 
|---|
| 112 |   Cacheable<int> BondCount; //!< number of atoms, brought up-to-date by doCountBonds()
 | 
|---|
| 113 |   moleculeId_t id;
 | 
|---|
| 114 |   atomSet atoms; //<!list of atoms
 | 
|---|
| 115 |   atomIdSet atomIds; //<!set of atomic ids to check uniqueness of atoms
 | 
|---|
| 116 | protected:
 | 
|---|
| 117 |   //void CountAtoms();
 | 
|---|
| 118 |   /**
 | 
|---|
| 119 |    * this iterator type should be used for internal variables, \
 | 
|---|
| 120 |      * since it will not lock
 | 
|---|
| 121 |    */
 | 
|---|
| 122 |   typedef atomSet::iterator internal_iterator;
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   molecule(const periodentafel * const teil);
 | 
|---|
| 125 |   virtual ~molecule();
 | 
|---|
| 126 | 
 | 
|---|
| 127 | public:
 | 
|---|
| 128 |   //getter and setter
 | 
|---|
| 129 |   const std::string getName() const;
 | 
|---|
| 130 |   int getAtomCount() const;
 | 
|---|
| 131 |   int doCountAtoms();
 | 
|---|
| 132 |   int getBondCount() const;
 | 
|---|
| 133 |   int doCountBonds() const;
 | 
|---|
| 134 |   moleculeId_t getId() const;
 | 
|---|
| 135 |   void setId(moleculeId_t);
 | 
|---|
| 136 |   void setName(const std::string);
 | 
|---|
| 137 |   const Formula &getFormula() const;
 | 
|---|
| 138 |   unsigned int getElementCount() const;
 | 
|---|
| 139 |   bool hasElement(const element*) const;
 | 
|---|
| 140 |   bool hasElement(atomicNumber_t) const;
 | 
|---|
| 141 |   bool hasElement(const std::string&) const;
 | 
|---|
| 142 | 
 | 
|---|
| 143 |   virtual bool changeId(atomId_t newId);
 | 
|---|
| 144 | 
 | 
|---|
| 145 |   atomVector getAtomSet() const;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   iterator begin();
 | 
|---|
| 148 |   const_iterator begin() const;
 | 
|---|
| 149 |   iterator end();
 | 
|---|
| 150 |   const_iterator end() const;
 | 
|---|
| 151 |   bool empty() const;
 | 
|---|
| 152 |   size_t size() const;
 | 
|---|
| 153 |   const_iterator erase(const_iterator loc);
 | 
|---|
| 154 |   const_iterator erase(atom * key);
 | 
|---|
| 155 |   const_iterator find(atom * key) const;
 | 
|---|
| 156 |   pair<iterator, bool> insert(atom * const key);
 | 
|---|
| 157 |   bool containsAtom(atom* key);
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   /// remove atoms from molecule.
 | 
|---|
| 160 |   bool AddAtom(atom *pointer);
 | 
|---|
| 161 |   bool RemoveAtom(atom *pointer);
 | 
|---|
| 162 |   bool UnlinkAtom(atom *pointer);
 | 
|---|
| 163 |   bool CleanupMolecule();
 | 
|---|
| 164 |   void removeAtomsinMolecule();
 | 
|---|
| 165 | 
 | 
|---|
| 166 |   /// Add/remove atoms to/from molecule.
 | 
|---|
| 167 |   atom * AddCopyAtom(atom *pointer);
 | 
|---|
| 168 |   bool AddXYZFile(string filename);
 | 
|---|
| 169 |   bool AddHydrogenReplacementAtom(bond *Bond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem);
 | 
|---|
| 170 |   bond * AddBond(atom *first, atom *second, int degree = 1);
 | 
|---|
| 171 |   bool RemoveBond(bond *pointer);
 | 
|---|
| 172 |   bool RemoveBonds(atom *BondPartner);
 | 
|---|
| 173 |   bool hasBondStructure() const;
 | 
|---|
| 174 | 
 | 
|---|
| 175 |   /// Find atoms.
 | 
|---|
| 176 |   atom * FindAtom(int Nr) const;
 | 
|---|
| 177 |   atom * AskAtom(string text);
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   /// Count and change present atoms' coordination.
 | 
|---|
| 180 |   bool CenterInBox();
 | 
|---|
| 181 |   bool BoundInBox();
 | 
|---|
| 182 |   void CenterEdge(Vector *max);
 | 
|---|
| 183 |   void CenterOrigin();
 | 
|---|
| 184 |   void CenterPeriodic();
 | 
|---|
| 185 |   void CenterAtVector(Vector *newcenter);
 | 
|---|
| 186 |   void Translate(const Vector *x);
 | 
|---|
| 187 |   void TranslatePeriodically(const Vector *trans);
 | 
|---|
| 188 |   void Mirror(const Vector *x);
 | 
|---|
| 189 |   void Align(Vector *n);
 | 
|---|
| 190 |   void Scale(const double ** const factor);
 | 
|---|
| 191 |   void DeterminePeriodicCenter(Vector ¢er);
 | 
|---|
| 192 |   Vector * DetermineCenterOfGravity() const;
 | 
|---|
| 193 |   Vector * DetermineCenterOfAll() const;
 | 
|---|
| 194 |   Vector * DetermineCenterOfBox() const;
 | 
|---|
| 195 |   void SetNameFromFilename(const char *filename);
 | 
|---|
| 196 |   void SetBoxDimension(Vector *dim);
 | 
|---|
| 197 |   bool ScanForPeriodicCorrection();
 | 
|---|
| 198 |   bool VerletForceIntegration(char *file, config &configuration, const size_t offset);
 | 
|---|
| 199 |   double VolumeOfConvexEnvelope(bool IsAngstroem);
 | 
|---|
| 200 |   RealSpaceMatrix getInertiaTensor() const;
 | 
|---|
| 201 |   void RotateToPrincipalAxisSystem(Vector &Axis);
 | 
|---|
| 202 | 
 | 
|---|
| 203 |   double ConstrainedPotential(struct EvaluatePotential &Params);
 | 
|---|
| 204 |   double MinimiseConstrainedPotential(atom **&permutation, int startstep, int endstep, bool IsAngstroem);
 | 
|---|
| 205 |   void EvaluateConstrainedForces(int startstep, int endstep, atom **PermutationMap, ForceMatrix *Force);
 | 
|---|
| 206 |   bool LinearInterpolationBetweenConfiguration(int startstep, int endstep, std::string prefix, config &configuration, bool MapByIdentity);
 | 
|---|
| 207 | 
 | 
|---|
| 208 |   bool CheckBounds(const Vector *x) const;
 | 
|---|
| 209 |   void GetAlignvector(struct lsq_params * par) const;
 | 
|---|
| 210 | 
 | 
|---|
| 211 |   /// Initialising routines in fragmentation
 | 
|---|
| 212 |   void CreateAdjacencyListFromDbondFile(ifstream *output,unsigned int skiplines,int id_offset);
 | 
|---|
| 213 |   void OutputBondsList() const;
 | 
|---|
| 214 |   void CyclicBondAnalysis() const;
 | 
|---|
| 215 |   void OutputGraphInfoPerAtom() const;
 | 
|---|
| 216 |   void OutputGraphInfoPerBond() const;
 | 
|---|
| 217 | 
 | 
|---|
| 218 |   // Graph analysis
 | 
|---|
| 219 |   MoleculeLeafClass * DepthFirstSearchAnalysis(std::deque<bond *> *&BackEdgeStack) const;
 | 
|---|
| 220 |   void CyclicStructureAnalysis(std::deque<bond *> *BackEdgeStack, int *&MinimumRingSize) const;
 | 
|---|
| 221 |   bool PickLocalBackEdges(atom **ListOfLocalAtoms, std::deque<bond *> *&ReferenceStack, std::deque<bond *> *&LocalStack) const;
 | 
|---|
| 222 |   bond * FindNextUnused(atom *vertex) const;
 | 
|---|
| 223 |   void SetNextComponentNumber(atom *vertex, int nr) const;
 | 
|---|
| 224 |   void ResetAllBondsToUnused() const;
 | 
|---|
| 225 |   int CountCyclicBonds();
 | 
|---|
| 226 |   bool CheckForConnectedSubgraph(KeySet *Fragment);
 | 
|---|
| 227 |   bond * CopyBond(atom *left, atom *right, bond *CopyBond);
 | 
|---|
| 228 | 
 | 
|---|
| 229 |   molecule *CopyMolecule() const;
 | 
|---|
| 230 |   molecule* CopyMoleculeFromSubRegion(const Shape&) const;
 | 
|---|
| 231 | 
 | 
|---|
| 232 |   /// Fragment molecule by two different approaches:
 | 
|---|
| 233 |   int FragmentMolecule(int Order, std::string &prefix);
 | 
|---|
| 234 |   bool CheckOrderAtSite(bool *AtomMask, Graph *GlobalKeySetList, int Order, int *MinimumRingSize, std::string path = "");
 | 
|---|
| 235 |   bool StoreBondsToFile(std::string filename, std::string path = "");
 | 
|---|
| 236 |   bool StoreAdjacencyToFile(std::string filename, std::string path = "");
 | 
|---|
| 237 |   bool CheckAdjacencyFileAgainstMolecule(std::string &path, atom **ListOfAtoms);
 | 
|---|
| 238 |   bool ParseOrderAtSiteFromFile(std::string &path);
 | 
|---|
| 239 |   bool StoreOrderAtSiteFile(std::string &path);
 | 
|---|
| 240 |   bool StoreForcesFile(MoleculeListClass *BondFragments, std::string &path, int *SortIndex);
 | 
|---|
| 241 |   bool CreateMappingLabelsToConfigSequence(int *&SortIndex);
 | 
|---|
| 242 |   bool CreateFatherLookupTable(atom **&LookupTable, int count = 0);
 | 
|---|
| 243 |   void BreadthFirstSearchAdd(molecule *Mol, atom **&AddedAtomList, bond **&AddedBondList, atom *Root, bond *Bond, int BondOrder, bool IsAngstroem);
 | 
|---|
| 244 | 
 | 
|---|
| 245 |   /// -# BOSSANOVA
 | 
|---|
| 246 |   void FragmentBOSSANOVA(Graph *&FragmentList, KeyStack &RootStack, int *MinimumRingSize);
 | 
|---|
| 247 |   int PowerSetGenerator(int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet);
 | 
|---|
| 248 |   bool BuildInducedSubgraph(const molecule *Father);
 | 
|---|
| 249 |   molecule * StoreFragmentFromKeySet(KeySet &Leaflet, bool IsAngstroem);
 | 
|---|
| 250 |   void SPFragmentGenerator(struct UniqueFragments *FragmentSearch, int RootDistance, std::vector<bond *> &BondsSet, int SetDimension, int SubOrder);
 | 
|---|
| 251 |   int LookForRemovalCandidate(KeySet *&Leaf, int *&ShortestPathList);
 | 
|---|
| 252 |   int GuesstimateFragmentCount(int order);
 | 
|---|
| 253 | 
 | 
|---|
| 254 |   // Recognize doubly appearing molecules in a list of them
 | 
|---|
| 255 |   int * GetFatherSonAtomicMap(molecule *OtherMolecule);
 | 
|---|
| 256 | 
 | 
|---|
| 257 |   // Output routines.
 | 
|---|
| 258 |   bool Output(std::ostream * const output) const;
 | 
|---|
| 259 |   bool OutputTrajectories(ofstream * const output) const;
 | 
|---|
| 260 |   void OutputListOfBonds() const;
 | 
|---|
| 261 |   bool OutputXYZ(ofstream * const output) const;
 | 
|---|
| 262 |   bool OutputTrajectoriesXYZ(ofstream * const output);
 | 
|---|
| 263 |   bool Checkout(ofstream * const output) const;
 | 
|---|
| 264 |   bool OutputTemperatureFromTrajectories(ofstream * const output, int startstep, int endstep);
 | 
|---|
| 265 | 
 | 
|---|
| 266 |   // Manipulation routines
 | 
|---|
| 267 |   void flipActiveFlag();
 | 
|---|
| 268 | 
 | 
|---|
| 269 | private:
 | 
|---|
| 270 |   void init_DFS(struct DFSAccounting&) const;
 | 
|---|
| 271 |   int last_atom; //!< number given to last atom
 | 
|---|
| 272 | };
 | 
|---|
| 273 | 
 | 
|---|
| 274 | molecule *NewMolecule();
 | 
|---|
| 275 | void DeleteMolecule(molecule* mol);
 | 
|---|
| 276 | 
 | 
|---|
| 277 | /** A list of \a molecule classes.
 | 
|---|
| 278 |  */
 | 
|---|
| 279 | class MoleculeListClass : public Observable
 | 
|---|
| 280 | {
 | 
|---|
| 281 | public:
 | 
|---|
| 282 |   MoleculeList ListOfMolecules; //!< List of the contained molecules
 | 
|---|
| 283 |   int MaxIndex;
 | 
|---|
| 284 | 
 | 
|---|
| 285 |   MoleculeListClass(World *world);
 | 
|---|
| 286 |   ~MoleculeListClass();
 | 
|---|
| 287 | 
 | 
|---|
| 288 |   bool AddHydrogenCorrection(std::string &path);
 | 
|---|
| 289 |   bool StoreForcesFile(std::string &path, int *SortIndex);
 | 
|---|
| 290 |   void insert(molecule *mol);
 | 
|---|
| 291 |   void erase(molecule *mol);
 | 
|---|
| 292 |   molecule * ReturnIndex(int index);
 | 
|---|
| 293 |   bool OutputConfigForListOfFragments(std::string &prefix, int *SortIndex);
 | 
|---|
| 294 |   int NumberOfActiveMolecules();
 | 
|---|
| 295 |   void Enumerate(ostream *out);
 | 
|---|
| 296 |   void Output(ofstream *out);
 | 
|---|
| 297 |   int CountAllAtoms() const;
 | 
|---|
| 298 | 
 | 
|---|
| 299 |   // Methods moved here from the menus
 | 
|---|
| 300 |   // TODO: more refactoring needed on these methods
 | 
|---|
| 301 |   void createNewMolecule(periodentafel *periode);
 | 
|---|
| 302 |   void loadFromXYZ(periodentafel *periode);
 | 
|---|
| 303 |   void setMoleculeFilename();
 | 
|---|
| 304 |   void parseXYZIntoMolecule();
 | 
|---|
| 305 |   void eraseMolecule();
 | 
|---|
| 306 | 
 | 
|---|
| 307 | private:
 | 
|---|
| 308 |   World *world; //!< The world this List belongs to. Needed to avoid deadlocks in the destructor
 | 
|---|
| 309 | };
 | 
|---|
| 310 | 
 | 
|---|
| 311 | /** A leaf for a tree of \a molecule class
 | 
|---|
| 312 |  * Wraps molecules in a tree structure
 | 
|---|
| 313 |  */
 | 
|---|
| 314 | class MoleculeLeafClass
 | 
|---|
| 315 | {
 | 
|---|
| 316 | public:
 | 
|---|
| 317 |   molecule *Leaf; //!< molecule of this leaf
 | 
|---|
| 318 |   //MoleculeLeafClass *UpLeaf;        //!< Leaf one level up
 | 
|---|
| 319 |   //MoleculeLeafClass *DownLeaf;      //!< First leaf one level down
 | 
|---|
| 320 |   MoleculeLeafClass *previous; //!< Previous leaf on this level
 | 
|---|
| 321 |   MoleculeLeafClass *next; //!< Next leaf on this level
 | 
|---|
| 322 | 
 | 
|---|
| 323 |   //MoleculeLeafClass(MoleculeLeafClass *Up, MoleculeLeafClass *Previous);
 | 
|---|
| 324 |   MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf);
 | 
|---|
| 325 |   ~MoleculeLeafClass();
 | 
|---|
| 326 | 
 | 
|---|
| 327 |   bool AddLeaf(molecule *ptr, MoleculeLeafClass *Previous);
 | 
|---|
| 328 |   bool FillBondStructureFromReference(const molecule * const reference, atom **&ListOfLocalAtoms, bool FreeList = false);
 | 
|---|
| 329 |   bool FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter);
 | 
|---|
| 330 |   bool AssignKeySetsToFragment(molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList = false);
 | 
|---|
| 331 |   bool FillListOfLocalAtoms(atom **&ListOfLocalAtoms, const int GlobalAtomCount, bool &FreeList);
 | 
|---|
| 332 |   void TranslateIndicesToGlobalIDs(Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph);
 | 
|---|
| 333 |   int Count() const;
 | 
|---|
| 334 | };
 | 
|---|
| 335 | 
 | 
|---|
| 336 | #endif /*MOLECULES_HPP_*/
 | 
|---|
| 337 | 
 | 
|---|