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