| 1 | /** \file molecules.hpp | 
|---|
| 2 | * | 
|---|
| 3 | * Class definitions of atom and molecule, element and periodentafel | 
|---|
| 4 | */ | 
|---|
| 5 |  | 
|---|
| 6 | #ifndef MOLECULES_HPP_ | 
|---|
| 7 | #define MOLECULES_HPP_ | 
|---|
| 8 |  | 
|---|
| 9 | using namespace std; | 
|---|
| 10 |  | 
|---|
| 11 | // GSL headers | 
|---|
| 12 | #include <gsl/gsl_eigen.h> | 
|---|
| 13 | #include <gsl/gsl_heapsort.h> | 
|---|
| 14 | #include <gsl/gsl_linalg.h> | 
|---|
| 15 | #include <gsl/gsl_matrix.h> | 
|---|
| 16 | #include <gsl/gsl_multimin.h> | 
|---|
| 17 | #include <gsl/gsl_vector.h> | 
|---|
| 18 | #include <gsl/gsl_randist.h> | 
|---|
| 19 |  | 
|---|
| 20 | //// STL headers | 
|---|
| 21 | #include <map> | 
|---|
| 22 | #include <set> | 
|---|
| 23 | #include <deque> | 
|---|
| 24 | #include <list> | 
|---|
| 25 | #include <vector> | 
|---|
| 26 |  | 
|---|
| 27 | #include "atom.hpp" | 
|---|
| 28 | #include "bond.hpp" | 
|---|
| 29 | #include "element.hpp" | 
|---|
| 30 | #include "linkedcell.hpp" | 
|---|
| 31 | #include "parser.hpp" | 
|---|
| 32 | #include "periodentafel.hpp" | 
|---|
| 33 | #include "stackclass.hpp" | 
|---|
| 34 | #include "tesselation.hpp" | 
|---|
| 35 | #include "vector.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | class molecule; | 
|---|
| 38 | class MoleculeLeafClass; | 
|---|
| 39 | class MoleculeListClass; | 
|---|
| 40 |  | 
|---|
| 41 | /******************************** Some definitions for easier reading **********************************/ | 
|---|
| 42 |  | 
|---|
| 43 | #define KeyStack deque<int> | 
|---|
| 44 | #define KeySet set<int> | 
|---|
| 45 | #define NumberValuePair pair<int, double> | 
|---|
| 46 | #define Graph map <KeySet, NumberValuePair, KeyCompare > | 
|---|
| 47 | #define GraphPair pair <KeySet, NumberValuePair > | 
|---|
| 48 | #define KeySetTestPair pair<KeySet::iterator, bool> | 
|---|
| 49 | #define GraphTestPair pair<Graph::iterator, bool> | 
|---|
| 50 |  | 
|---|
| 51 | #define MoleculeList list <molecule *> | 
|---|
| 52 | #define MoleculeListTest pair <MoleculeList::iterator, bool> | 
|---|
| 53 |  | 
|---|
| 54 | #define DistancePair pair < double, atom* > | 
|---|
| 55 | #define DistanceMap multimap < double, atom* > | 
|---|
| 56 | #define DistanceTestPair pair < DistanceMap::iterator, bool> | 
|---|
| 57 |  | 
|---|
| 58 |  | 
|---|
| 59 | //#define LinkedAtoms list <atom *> | 
|---|
| 60 |  | 
|---|
| 61 | /******************************** Some small functions and/or structures **********************************/ | 
|---|
| 62 |  | 
|---|
| 63 | struct KeyCompare | 
|---|
| 64 | { | 
|---|
| 65 | bool operator() (const KeySet SubgraphA, const KeySet SubgraphB) const; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | struct Trajectory | 
|---|
| 69 | { | 
|---|
| 70 | vector<Vector> R;  //!< position vector | 
|---|
| 71 | vector<Vector> U;  //!< velocity vector | 
|---|
| 72 | vector<Vector> F;  //!< last force vector | 
|---|
| 73 | atom *ptr;         //!< pointer to atom whose trajectory we contain | 
|---|
| 74 | }; | 
|---|
| 75 |  | 
|---|
| 76 | //bool operator < (KeySet SubgraphA, KeySet SubgraphB);   //note: this declaration is important, otherwise normal < is used (producing wrong order) | 
|---|
| 77 | inline void InsertFragmentIntoGraph(ofstream *out, struct UniqueFragments *Fragment); // Insert a KeySet into a Graph | 
|---|
| 78 | inline void InsertGraphIntoGraph(ofstream *out, Graph &graph1, Graph &graph2, int *counter);  // Insert all KeySet's in a Graph into another Graph | 
|---|
| 79 | int CompareDoubles (const void * a, const void * b); | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | /************************************* Class definitions ****************************************/ | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 | // some algebraic matrix stuff | 
|---|
| 86 | #define RDET3(a) ((a)[0]*(a)[4]*(a)[8] + (a)[3]*(a)[7]*(a)[2] + (a)[6]*(a)[1]*(a)[5] - (a)[2]*(a)[4]*(a)[6] - (a)[5]*(a)[7]*(a)[0] - (a)[8]*(a)[1]*(a)[3])  //!< hard-coded determinant of a 3x3 matrix | 
|---|
| 87 | #define RDET2(a0,a1,a2,a3) ((a0)*(a3)-(a1)*(a2))                      //!< hard-coded determinant of a 2x2 matrix | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 | /** Parameter structure for least square minimsation. | 
|---|
| 91 | */ | 
|---|
| 92 | struct LSQ_params { | 
|---|
| 93 | Vector **vectors; | 
|---|
| 94 | int num; | 
|---|
| 95 | }; | 
|---|
| 96 |  | 
|---|
| 97 | double LSQ(const gsl_vector * x, void * params); | 
|---|
| 98 |  | 
|---|
| 99 | /** Parameter structure for least square minimsation. | 
|---|
| 100 | */ | 
|---|
| 101 | struct lsq_params { | 
|---|
| 102 | gsl_vector *x; | 
|---|
| 103 | const molecule *mol; | 
|---|
| 104 | element *type; | 
|---|
| 105 | }; | 
|---|
| 106 |  | 
|---|
| 107 | #define MaxThermostats 6      //!< maximum number of thermostat entries in Ions#ThermostatNames and Ions#ThermostatImplemented | 
|---|
| 108 | enum thermostats { None, Woodcock, Gaussian, Langevin, Berendsen, NoseHoover };   //!< Thermostat names for output | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | /** The complete molecule. | 
|---|
| 112 | * Class incorporates number of types | 
|---|
| 113 | */ | 
|---|
| 114 | class molecule : public PointCloud { | 
|---|
| 115 | public: | 
|---|
| 116 | double cell_size[6];//!< cell size | 
|---|
| 117 | periodentafel *elemente; //!< periodic table with each element | 
|---|
| 118 | atom *start;        //!< start of atom list | 
|---|
| 119 | atom *end;          //!< end of atom list | 
|---|
| 120 | bond *first;        //!< start of bond list | 
|---|
| 121 | bond *last;         //!< end of bond list | 
|---|
| 122 | bond ***ListOfBondsPerAtom; //!< pointer list for each atom and each bond it has | 
|---|
| 123 | map<atom *, struct Trajectory> Trajectories; //!< contains old trajectory points | 
|---|
| 124 | int MDSteps;        //!< The number of MD steps in Trajectories | 
|---|
| 125 | int *NumberOfBondsPerAtom;  //!< Number of Bonds each atom has | 
|---|
| 126 | int AtomCount;          //!< number of atoms, brought up-to-date by CountAtoms() | 
|---|
| 127 | int BondCount;          //!< number of atoms, brought up-to-date by CountBonds() | 
|---|
| 128 | int ElementCount;       //!< how many unique elements are therein | 
|---|
| 129 | int ElementsInMolecule[MAX_ELEMENTS]; //!< list whether element (sorted by atomic number) is alread present or not | 
|---|
| 130 | int NoNonHydrogen;  //!< number of non-hydrogen atoms in molecule | 
|---|
| 131 | int NoNonBonds;     //!< number of non-hydrogen bonds in molecule | 
|---|
| 132 | int NoCyclicBonds;  //!< number of cyclic bonds in molecule, by DepthFirstSearchAnalysis() | 
|---|
| 133 | double BondDistance;  //!< typical bond distance used in CreateAdjacencyList() and furtheron | 
|---|
| 134 | bool ActiveFlag;    //!< in a MoleculeListClass used to discern active from inactive molecules | 
|---|
| 135 | Vector Center;      //!< Center of molecule in a global box | 
|---|
| 136 | char name[MAXSTRINGSIZE];         //!< arbitrary name | 
|---|
| 137 | int IndexNr;        //!< index of molecule in a MoleculeListClass | 
|---|
| 138 | class Tesselation *TesselStruct; | 
|---|
| 139 |  | 
|---|
| 140 | molecule(periodentafel *teil); | 
|---|
| 141 | virtual ~molecule(); | 
|---|
| 142 |  | 
|---|
| 143 | // re-definition of virtual functions from PointCloud | 
|---|
| 144 | Vector *GetCenter(ofstream *out); | 
|---|
| 145 | TesselPoint *GetPoint(); | 
|---|
| 146 | TesselPoint *GetTerminalPoint(); | 
|---|
| 147 | void GoToNext(); | 
|---|
| 148 | void GoToPrevious(); | 
|---|
| 149 | void GoToFirst(); | 
|---|
| 150 | void GoToLast(); | 
|---|
| 151 | bool IsEmpty(); | 
|---|
| 152 | bool IsLast(); | 
|---|
| 153 |  | 
|---|
| 154 | /// remove atoms from molecule. | 
|---|
| 155 | bool AddAtom(atom *pointer); | 
|---|
| 156 | bool RemoveAtom(atom *pointer); | 
|---|
| 157 | bool UnlinkAtom(atom *pointer); | 
|---|
| 158 | bool CleanupMolecule(); | 
|---|
| 159 |  | 
|---|
| 160 | /// Add/remove atoms to/from molecule. | 
|---|
| 161 | atom * AddCopyAtom(atom *pointer); | 
|---|
| 162 | bool AddXYZFile(string filename); | 
|---|
| 163 | bool AddHydrogenReplacementAtom(ofstream *out, bond *Bond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bond **BondList, int NumBond, bool IsAngstroem); | 
|---|
| 164 | bond * AddBond(atom *first, atom *second, int degree); | 
|---|
| 165 | bool RemoveBond(bond *pointer); | 
|---|
| 166 | bool RemoveBonds(atom *BondPartner); | 
|---|
| 167 |  | 
|---|
| 168 | /// Find atoms. | 
|---|
| 169 | atom * FindAtom(int Nr) const; | 
|---|
| 170 | atom * AskAtom(string text); | 
|---|
| 171 |  | 
|---|
| 172 | /// Count and change present atoms' coordination. | 
|---|
| 173 | void CountAtoms(ofstream *out); | 
|---|
| 174 | void CountElements(); | 
|---|
| 175 | void CalculateOrbitals(class config &configuration); | 
|---|
| 176 | bool CenterInBox(ofstream *out); | 
|---|
| 177 | void CenterEdge(ofstream *out, Vector *max); | 
|---|
| 178 | void CenterOrigin(ofstream *out); | 
|---|
| 179 | void CenterPeriodic(ofstream *out); | 
|---|
| 180 | void CenterAtVector(ofstream *out, Vector *newcenter); | 
|---|
| 181 | void Translate(const Vector *x); | 
|---|
| 182 | void TranslatePeriodically(const Vector *trans); | 
|---|
| 183 | void Mirror(const Vector *x); | 
|---|
| 184 | void Align(Vector *n); | 
|---|
| 185 | void Scale(double **factor); | 
|---|
| 186 | void DeterminePeriodicCenter(Vector ¢er); | 
|---|
| 187 | Vector * DetermineCenterOfGravity(ofstream *out); | 
|---|
| 188 | Vector * DetermineCenterOfAll(ofstream *out); | 
|---|
| 189 | void SetNameFromFilename(const char *filename); | 
|---|
| 190 | void SetBoxDimension(Vector *dim); | 
|---|
| 191 | double * ReturnFullMatrixforSymmetric(double *cell_size); | 
|---|
| 192 | void ScanForPeriodicCorrection(ofstream *out); | 
|---|
| 193 | bool VerletForceIntegration(ofstream *out, char *file, config &configuration); | 
|---|
| 194 | void Thermostats(config &configuration, double ActualTemp, int Thermostat); | 
|---|
| 195 | void PrincipalAxisSystem(ofstream *out, bool DoRotate); | 
|---|
| 196 | double VolumeOfConvexEnvelope(ofstream *out, bool IsAngstroem); | 
|---|
| 197 | Vector* FindEmbeddingHole(ofstream *out, molecule *srcmol); | 
|---|
| 198 |  | 
|---|
| 199 |  | 
|---|
| 200 | double ConstrainedPotential(ofstream *out, atom **permutation, int start, int end, double *constants, bool IsAngstroem); | 
|---|
| 201 | double MinimiseConstrainedPotential(ofstream *out, atom **&permutation, int startstep, int endstep, bool IsAngstroem); | 
|---|
| 202 | void EvaluateConstrainedForces(ofstream *out, int startstep, int endstep, atom **PermutationMap, ForceMatrix *Force); | 
|---|
| 203 | bool LinearInterpolationBetweenConfiguration(ofstream *out, int startstep, int endstep, const char *prefix, config &configuration); | 
|---|
| 204 |  | 
|---|
| 205 | bool CheckBounds(const Vector *x) const; | 
|---|
| 206 | void GetAlignvector(struct lsq_params * par) const; | 
|---|
| 207 |  | 
|---|
| 208 | /// Initialising routines in fragmentation | 
|---|
| 209 | void CreateAdjacencyList2(ofstream *out, ifstream *output); | 
|---|
| 210 | void CreateAdjacencyList(ofstream *out, double bonddistance, bool IsAngstroem); | 
|---|
| 211 | void CreateListOfBondsPerAtom(ofstream *out); | 
|---|
| 212 |  | 
|---|
| 213 | // Graph analysis | 
|---|
| 214 | MoleculeLeafClass * DepthFirstSearchAnalysis(ofstream *out, class StackClass<bond *> *&BackEdgeStack); | 
|---|
| 215 | void CyclicStructureAnalysis(ofstream *out, class StackClass<bond *> *BackEdgeStack, int *&MinimumRingSize); | 
|---|
| 216 | bool PickLocalBackEdges(ofstream *out, atom **ListOfLocalAtoms, class StackClass<bond *> *&ReferenceStack, class StackClass<bond *> *&LocalStack); | 
|---|
| 217 | bond * FindNextUnused(atom *vertex); | 
|---|
| 218 | void SetNextComponentNumber(atom *vertex, int nr); | 
|---|
| 219 | void InitComponentNumbers(); | 
|---|
| 220 | void OutputComponentNumber(ofstream *out, atom *vertex); | 
|---|
| 221 | void ResetAllBondsToUnused(); | 
|---|
| 222 | void ResetAllAtomNumbers(); | 
|---|
| 223 | int CountCyclicBonds(ofstream *out); | 
|---|
| 224 | bool CheckForConnectedSubgraph(ofstream *out, KeySet *Fragment); | 
|---|
| 225 | string GetColor(enum Shading color); | 
|---|
| 226 |  | 
|---|
| 227 | molecule *CopyMolecule(); | 
|---|
| 228 |  | 
|---|
| 229 | /// Fragment molecule by two different approaches: | 
|---|
| 230 | int FragmentMolecule(ofstream *out, int Order, config *configuration); | 
|---|
| 231 | bool CheckOrderAtSite(ofstream *out, bool *AtomMask, Graph *GlobalKeySetList, int Order, int *MinimumRingSize, char *path = NULL); | 
|---|
| 232 | bool StoreAdjacencyToFile(ofstream *out, char *path); | 
|---|
| 233 | bool CheckAdjacencyFileAgainstMolecule(ofstream *out, char *path, atom **ListOfAtoms); | 
|---|
| 234 | bool ParseOrderAtSiteFromFile(ofstream *out, char *path); | 
|---|
| 235 | bool StoreOrderAtSiteFile(ofstream *out, char *path); | 
|---|
| 236 | bool ParseKeySetFile(ofstream *out, char *filename, Graph *&FragmentList); | 
|---|
| 237 | bool StoreKeySetFile(ofstream *out, Graph &KeySetList, char *path); | 
|---|
| 238 | bool StoreForcesFile(ofstream *out, MoleculeListClass *BondFragments, char *path, int *SortIndex); | 
|---|
| 239 | bool CreateMappingLabelsToConfigSequence(ofstream *out, int *&SortIndex); | 
|---|
| 240 | bool ScanBufferIntoKeySet(ofstream *out, char *buffer, KeySet &CurrentSet); | 
|---|
| 241 | void BreadthFirstSearchAdd(ofstream *out, molecule *Mol, atom **&AddedAtomList, bond **&AddedBondList, atom *Root, bond *Bond, int BondOrder, bool IsAngstroem); | 
|---|
| 242 | /// -# BOSSANOVA | 
|---|
| 243 | void FragmentBOSSANOVA(ofstream *out, Graph *&FragmentList, KeyStack &RootStack, int *MinimumRingSize); | 
|---|
| 244 | int PowerSetGenerator(ofstream *out, int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet); | 
|---|
| 245 | bool BuildInducedSubgraph(ofstream *out, const molecule *Father); | 
|---|
| 246 | molecule * StoreFragmentFromKeySet(ofstream *out, KeySet &Leaflet, bool IsAngstroem); | 
|---|
| 247 | void SPFragmentGenerator(ofstream *out, struct UniqueFragments *FragmentSearch, int RootDistance, bond **BondsSet, int SetDimension, int SubOrder); | 
|---|
| 248 | int LookForRemovalCandidate(ofstream *&out, KeySet *&Leaf, int *&ShortestPathList); | 
|---|
| 249 | int GuesstimateFragmentCount(ofstream *out, int order); | 
|---|
| 250 |  | 
|---|
| 251 | // Recognize doubly appearing molecules in a list of them | 
|---|
| 252 | int * IsEqualToWithinThreshold(ofstream *out, molecule *OtherMolecule, double threshold); | 
|---|
| 253 | int * GetFatherSonAtomicMap(ofstream *out, molecule *OtherMolecule); | 
|---|
| 254 |  | 
|---|
| 255 | // Output routines. | 
|---|
| 256 | bool Output(ofstream *out); | 
|---|
| 257 | bool OutputTrajectories(ofstream *out); | 
|---|
| 258 | void OutputListOfBonds(ofstream *out) const; | 
|---|
| 259 | bool OutputXYZ(ofstream *out) const; | 
|---|
| 260 | bool OutputTrajectoriesXYZ(ofstream *out); | 
|---|
| 261 | bool Checkout(ofstream *out) const; | 
|---|
| 262 | bool OutputTemperatureFromTrajectories(ofstream *out, int startstep, int endstep, ofstream *output); | 
|---|
| 263 |  | 
|---|
| 264 | private: | 
|---|
| 265 | int last_atom;      //!< number given to last atom | 
|---|
| 266 | atom *InternalPointer;  //!< internal pointer for PointCloud | 
|---|
| 267 | }; | 
|---|
| 268 |  | 
|---|
| 269 | /** A list of \a molecule classes. | 
|---|
| 270 | */ | 
|---|
| 271 | class MoleculeListClass { | 
|---|
| 272 | public: | 
|---|
| 273 | MoleculeList ListOfMolecules; //!< List of the contained molecules | 
|---|
| 274 | int MaxIndex; | 
|---|
| 275 |  | 
|---|
| 276 | MoleculeListClass(); | 
|---|
| 277 | ~MoleculeListClass(); | 
|---|
| 278 |  | 
|---|
| 279 | bool AddHydrogenCorrection(ofstream *out, char *path); | 
|---|
| 280 | bool StoreForcesFile(ofstream *out, char *path, int *SortIndex); | 
|---|
| 281 | void insert(molecule *mol); | 
|---|
| 282 | molecule * ReturnIndex(int index); | 
|---|
| 283 | bool OutputConfigForListOfFragments(ofstream *out, config *configuration, int *SortIndex); | 
|---|
| 284 | int NumberOfActiveMolecules(); | 
|---|
| 285 | void Enumerate(ofstream *out); | 
|---|
| 286 | void Output(ofstream *out); | 
|---|
| 287 |  | 
|---|
| 288 | // merging of molecules | 
|---|
| 289 | bool SimpleMerge(molecule *mol, molecule *srcmol); | 
|---|
| 290 | bool SimpleAdd(molecule *mol, molecule *srcmol); | 
|---|
| 291 | bool SimpleMultiMerge(molecule *mol, int *src, int N); | 
|---|
| 292 | bool SimpleMultiAdd(molecule *mol, int *src, int N); | 
|---|
| 293 | bool ScatterMerge(molecule *mol, int *src, int N); | 
|---|
| 294 | bool EmbedMerge(molecule *mol, molecule *srcmol); | 
|---|
| 295 |  | 
|---|
| 296 | private: | 
|---|
| 297 | }; | 
|---|
| 298 |  | 
|---|
| 299 |  | 
|---|
| 300 | /** A leaf for a tree of \a molecule class | 
|---|
| 301 | * Wraps molecules in a tree structure | 
|---|
| 302 | */ | 
|---|
| 303 | class MoleculeLeafClass { | 
|---|
| 304 | public: | 
|---|
| 305 | molecule *Leaf;                   //!< molecule of this leaf | 
|---|
| 306 | //MoleculeLeafClass *UpLeaf;        //!< Leaf one level up | 
|---|
| 307 | //MoleculeLeafClass *DownLeaf;      //!< First leaf one level down | 
|---|
| 308 | MoleculeLeafClass *previous;  //!< Previous leaf on this level | 
|---|
| 309 | MoleculeLeafClass *next;      //!< Next leaf on this level | 
|---|
| 310 |  | 
|---|
| 311 | //MoleculeLeafClass(MoleculeLeafClass *Up, MoleculeLeafClass *Previous); | 
|---|
| 312 | MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf); | 
|---|
| 313 | ~MoleculeLeafClass(); | 
|---|
| 314 |  | 
|---|
| 315 | bool AddLeaf(molecule *ptr, MoleculeLeafClass *Previous); | 
|---|
| 316 | bool FillBondStructureFromReference(ofstream *out, molecule *reference, int &FragmentCounter, atom ***&ListOfLocalAtoms, bool FreeList = false); | 
|---|
| 317 | bool FillRootStackForSubgraphs(ofstream *out, KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter); | 
|---|
| 318 | bool AssignKeySetsToFragment(ofstream *out, molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList = false); | 
|---|
| 319 | bool FillListOfLocalAtoms(ofstream *out, atom ***&ListOfLocalAtoms, const int FragmentCounter, const int GlobalAtomCount, bool &FreeList); | 
|---|
| 320 | void TranslateIndicesToGlobalIDs(ofstream *out, Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph); | 
|---|
| 321 | int Count() const; | 
|---|
| 322 | }; | 
|---|
| 323 |  | 
|---|
| 324 | class ConfigFileBuffer { | 
|---|
| 325 | public: | 
|---|
| 326 | char **buffer; | 
|---|
| 327 | int *LineMapping; | 
|---|
| 328 | int CurrentLine; | 
|---|
| 329 | int NoLines; | 
|---|
| 330 |  | 
|---|
| 331 | ConfigFileBuffer(); | 
|---|
| 332 | ConfigFileBuffer(char *filename); | 
|---|
| 333 | ~ConfigFileBuffer(); | 
|---|
| 334 |  | 
|---|
| 335 | void InitMapping(); | 
|---|
| 336 | void MapIonTypesInBuffer(int NoAtoms); | 
|---|
| 337 | }; | 
|---|
| 338 |  | 
|---|
| 339 |  | 
|---|
| 340 | #endif /*MOLECULES_HPP_*/ | 
|---|
| 341 |  | 
|---|