source: molecuilder/src/molecules.hpp@ 461e93

Last change on this file since 461e93 was 461e93, checked in by Frederik Heber <heber@…>, 17 years ago

molecule::CheckOrderAtSite() returns bool and used as threshold criterium to break while in molecule::FragmentMolecule()

As we are now near the end of the switch to adaptive increase of the bond order, the first step was to increment the OrderAtSite on all sites equally. This is now implemented. CheckOrderAtSite() checks whether the order is still below the desired value at one site and if so continues. In this while-loop we fill the RootStack and call BOSSANOVA. The sequence of AssignKeySets.. and FillRootStack... was flipped for this.

  • Property mode set to 100644
File size: 18.4 KB
Line 
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
9using namespace std;
10
11// GSL headers
12#include <gsl/gsl_multimin.h>
13#include <gsl/gsl_vector.h>
14#include <gsl/gsl_matrix.h>
15#include <gsl/gsl_heapsort.h>
16
17// STL headers
18#include <map>
19#include <set>
20#include <deque>
21
22#include "helpers.hpp"
23#include "stackclass.hpp"
24
25class atom;
26class bond;
27class config;
28class element;
29class molecule;
30class MoleculeListClass;
31class periodentafel;
32class vector;
33class Verbose;
34
35/******************************** Some definitions for easier reading **********************************/
36
37#define KeyStack deque<int>
38#define KeySet set<int>
39#define Graph map<KeySet, pair<int, double>, KeyCompare >
40#define GraphPair pair<KeySet, pair<int, double> >
41#define KeySetTestPair pair<KeySet::iterator, bool>
42#define GraphTestPair pair<Graph::iterator, bool>
43
44struct KeyCompare
45{
46 bool operator() (const KeySet SubgraphA, const KeySet SubgraphB) const;
47};
48
49//bool operator < (KeySet SubgraphA, KeySet SubgraphB); //note: this declaration is important, otherwise normal < is used (producing wrong order)
50inline void InsertFragmentIntoGraph(ofstream *out, struct UniqueFragments *Fragment); // Insert a KeySet into a Graph
51inline void InsertGraphIntoGraph(ofstream *out, Graph &graph1, Graph &graph2, int *counter); // Insert all KeySet's in a Graph into another Graph
52int CompareDoubles (const void * a, const void * b);
53
54
55/************************************* Class definitions ****************************************/
56
57/** Chemical element.
58 * Class incorporates data for a certain chemical element to be referenced from atom class.
59 */
60class element {
61 public:
62 double mass; //!< mass in g/mol
63 double CovalentRadius; //!< covalent radius
64 double VanDerWaalsRadius; //!< can-der-Waals radius
65 int Z; //!< atomic number
66 char name[64]; //!< atom name, i.e. "Hydrogren"
67 char symbol[3]; //!< short form of the atom, i.e. "H"
68 char period[8]; //!< period: n quantum number
69 char group[8]; //!< group: l quantum number
70 char block[8]; //!< block: l quantum number
71 element *previous; //!< previous item in list
72 element *next; //!< next element in list
73 int *sort; //!< sorc criteria
74 int No; //!< number of element set on periodentafel::Output()
75 double Valence; //!< number of valence electrons for this element
76 int NoValenceOrbitals; //!< number of valence orbitals, used for determining bond degree in molecule::CreateConnectmatrix()
77 double HBondDistance[NDIM]; //!< distance in Angstrom of this element to hydrogen (for single, double and triple bonds)
78 double HBondAngle[NDIM]; //!< typical angle for one, two, three bonded hydrogen (in degrees)
79
80 element();
81 ~element();
82
83 //> print element entries to screen
84 bool Output(ofstream *out) const;
85 bool Checkout(ofstream *out, const int No, const int NoOfAtoms) const;
86
87 private:
88};
89
90/** Periodentafel is a list of all elements sorted by their atomic number.
91 */
92class periodentafel {
93 public:
94 element *start; //!< start of element list
95 element *end; //!< end of element list
96 char header1[MAXSTRINGSIZE]; //!< store first header line
97 char header2[MAXSTRINGSIZE]; //!< store second header line
98
99 periodentafel();
100 ~periodentafel();
101
102 bool AddElement(element *pointer);
103 bool RemoveElement(element *pointer);
104 bool CleanupPeriodtable();
105 element * FindElement(int Z);
106 element * FindElement(char *shorthand) const;
107 element * AskElement();
108 bool Output(ofstream *output) const;
109 bool Checkout(ofstream *output, const int *checkliste) const;
110 bool LoadPeriodentafel(char *filename = NULL);
111 bool StorePeriodentafel(char *filename = NULL) const;
112
113 private:
114};
115
116// some algebraic matrix stuff
117#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
118#define RDET2(a0,a1,a2,a3) ((a0)*(a3)-(a1)*(a2)) //!< hard-coded determinant of a 2x2 matrix
119
120/** Single vector.
121 * basically, just a x[3] but with helpful functions
122 */
123class vector {
124 public:
125 double x[NDIM];
126
127 vector();
128 ~vector();
129
130 double Distance(const vector *y) const;
131 double PeriodicDistance(const vector *y, const double *cell_size) const;
132 double ScalarProduct(const vector *y) const;
133 double Projection(const vector *y) const;
134 double Norm() const ;
135 double Angle(vector *y) const;
136
137 void AddVector(const vector *y);
138 void SubtractVector(const vector *y);
139 void CopyVector(const vector *y);
140 void RotateVector(const vector *y, const double alpha);
141 void Zero();
142 void Normalize();
143 void Translate(const vector *x);
144 void Mirror(const vector *x);
145 void Scale(double **factor);
146 void Scale(double *factor);
147 void Scale(double factor);
148 void MatrixMultiplication(double *M);
149 void InverseMatrixMultiplication(double *M);
150 void KeepPeriodic(ofstream *out, double *matrix);
151 void LinearCombinationOfVectors(const vector *x1, const vector *x2, const vector *x3, double *factors);
152
153 bool GetOneNormalVector(const vector *x1);
154 bool MakeNormalVector(const vector *y1);
155 bool MakeNormalVector(const vector *y1, const vector *y2);
156 bool MakeNormalVector(const vector *x1, const vector *x2, const vector *x3);
157 bool SolveSystem(vector *x1, vector *x2, vector *y, double alpha, double beta, double c);
158 bool LSQdistance(vector **vectors, int dim);
159
160 void AskPosition(double *cell_size, bool check);
161 bool Output(ofstream *out) const;
162};
163
164ofstream& operator<<(ofstream& ost, vector& m);
165
166/** Parameter structure for least square minimsation.
167 */
168struct LSQ_params {
169 vector **vectors;
170 int num;
171};
172
173double LSQ(const gsl_vector * x, void * params);
174
175/** Parameter structure for least square minimsation.
176 */
177struct lsq_params {
178 gsl_vector *x;
179 const molecule *mol;
180 element *type;
181};
182
183
184
185/** Single atom.
186 * Class incoporates position, type
187 */
188class atom {
189 public:
190 vector x; //!< coordinate array of atom, giving position within cell
191 vector v; //!< velocity array of atom
192 element *type; //!< pointing to element
193 atom *previous; //!< previous atom in molecule list
194 atom *next; //!< next atom in molecule list
195 atom *father; //!< In many-body bond order fragmentations points to originating atom
196 atom *Ancestor; //!< "Father" in Depth-First-Search
197 char *Name; //!< unique name used during many-body bond-order fragmentation
198 int FixedIon; //!< config variable that states whether forces act on the ion or not
199 int *sort; //!< sort criteria
200 int nr; //!< continuous, unique number
201 int GraphNr; //!< unique number, given in DepthFirstSearchAnalysis()
202 int *ComponentNr;//!< belongs to this nonseparable components, given in DepthFirstSearchAnalysis() (if more than one, then is SeparationVertex)
203 int LowpointNr; //!< needed in DepthFirstSearchAnalysis() to detect nonseparable components, is the lowest possible number of an atom to reach via tree edges only followed by at most one back edge.
204 bool SeparationVertex; //!< whether this atom separates off subsets of atoms or not, given in DepthFirstSearchAnalysis()
205 unsigned char AdaptiveOrder; //!< current present bond order at site (0 means "not set")
206
207 atom();
208 ~atom();
209
210 bool Output(int ElementNo, int AtomNo, ofstream *out) const;
211 bool OutputXYZLine(ofstream *out) const;
212 atom *GetTrueFather();
213 bool Compare(atom &ptr);
214
215 private:
216};
217
218ostream & operator << (ostream &ost, atom &a);
219
220/** Bonds between atoms.
221 * Class incorporates bonds between atoms in a molecule,
222 * used to derive tge fragments in many-body bond order
223 * calculations.
224 */
225class bond {
226 public:
227 atom *leftatom; //!< first bond partner
228 atom *rightatom; //!< second bond partner
229 bond *previous; //!< previous atom in molecule list
230 bond *next; //!< next atom in molecule list
231 int HydrogenBond; //!< Number of hydrogen atoms in the bond
232 int BondDegree; //!< single, double, triple, ... bond
233 int nr; //!< unique number in a molecule, updated by molecule::CreateAdjacencyList()
234 bool Cyclic; //!< flag whether bond is part of a cycle or not, given in DepthFirstSearchAnalysis()
235 enum EdgeType Type;//!< whether this is a tree or back edge
236
237 atom * GetOtherAtom(atom *Atom) const;
238 bond * GetFirstBond();
239 bond * GetLastBond();
240
241 bool MarkUsed(enum Shading color);
242 enum Shading IsUsed();
243 void ResetUsed();
244 bool Contains(const atom *ptr);
245 bool Contains(const int nr);
246
247 bond();
248 bond(atom *left, atom *right);
249 bond(atom *left, atom *right, int degree);
250 bond(atom *left, atom *right, int degree, int number);
251 ~bond();
252
253 private:
254 enum Shading Used; //!< marker in depth-first search, DepthFirstSearchAnalysis()
255};
256
257ostream & operator << (ostream &ost, bond &b);
258
259class MoleculeLeafClass;
260
261/** The complete molecule.
262 * Class incorporates number of types
263 */
264class molecule {
265 public:
266 double cell_size[6];//!< cell size
267 periodentafel *elemente; //!< periodic table with each element
268 atom *start; //!< start of atom list
269 atom *end; //!< end of atom list
270 bond *first; //!< start of bond list
271 bond *last; //!< end of bond list
272 bond ***ListOfBondsPerAtom; //!< pointer list for each atom and each bond it has
273 int *NumberOfBondsPerAtom; //!< Number of Bonds each atom has
274 int AtomCount; //!< number of atoms, brought up-to-date by CountAtoms()
275 int BondCount; //!< number of atoms, brought up-to-date by CountBonds()
276 int ElementCount; //!< how many unique elements are therein
277 int ElementsInMolecule[MAX_ELEMENTS]; //!< list whether element (sorted by atomic number) is alread present or not
278 int NoNonHydrogen; //!< number of non-hydrogen atoms in molecule
279 int NoNonBonds; //!< number of non-hydrogen bonds in molecule
280 int NoCyclicBonds; //!< number of cyclic bonds in molecule, by DepthFirstSearchAnalysis()
281 double BondDistance; //!< typical bond distance used in CreateAdjacencyList() and furtheron
282
283 molecule(periodentafel *teil);
284 ~molecule();
285
286 /// remove atoms from molecule.
287 bool AddAtom(atom *pointer);
288 bool RemoveAtom(atom *pointer);
289 bool CleanupMolecule();
290
291 /// Add/remove atoms to/from molecule.
292 atom * AddCopyAtom(atom *pointer);
293 bool AddXYZFile(string filename);
294 bool AddHydrogenReplacementAtom(ofstream *out, bond *Bond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bond **BondList, int NumBond, bool IsAngstroem);
295 bond * AddBond(atom *first, atom *second, int degree);
296 bool RemoveBond(bond *pointer);
297 bool RemoveBonds(atom *BondPartner);
298
299 /// Find atoms.
300 atom * FindAtom(int Nr) const;
301 atom * AskAtom(char *text);
302
303 /// Count and change present atoms' coordination.
304 void CountAtoms(ofstream *out);
305 void CountElements();
306 void CalculateOrbitals(class config &configuration);
307 void CenterEdge(ofstream *out, vector *max);
308 void CenterOrigin(ofstream *out, vector *max);
309 void CenterGravity(ofstream *out, vector *max);
310 void Translate(const vector *x);
311 void Mirror(const vector *x);
312 void Align(vector *n);
313 void Scale(double **factor);
314 void DetermineCenterOfGravity(vector &CenterOfGravity);
315 void SetBoxDimension(vector *dim);
316 double * ReturnFullMatrixforSymmetric(double *cell_size);
317 void ScanForPeriodicCorrection(ofstream *out);
318
319 bool CheckBounds(const vector *x) const;
320 void GetAlignVector(struct lsq_params * par) const;
321
322 /// Initialising routines in fragmentation
323 void CreateAdjacencyList(ofstream *out, double bonddistance);
324 void CreateListOfBondsPerAtom(ofstream *out);
325
326 // Graph analysis
327 MoleculeLeafClass * DepthFirstSearchAnalysis(ofstream *out, bool ReturnStack, int &MinimumRingSize);
328 void CyclicStructureAnalysis(ofstream *out, class StackClass<bond *> *BackEdgeStack, int &MinimumRingSize);
329 bond * FindNextUnused(atom *vertex);
330 void SetNextComponentNumber(atom *vertex, int nr);
331 void InitComponentNumbers();
332 void OutputComponentNumber(ofstream *out, atom *vertex);
333 void ResetAllBondsToUnused();
334 void ResetAllAtomNumbers();
335 int CountCyclicBonds(ofstream *out);
336 char * GetColor(enum Shading color);
337
338 molecule *CopyMolecule();
339
340 /// Fragment molecule by two different approaches:
341 void FragmentMolecule(ofstream *out, int Order, config *configuration);
342 bool CheckOrderAtSite(ofstream *out, int Order);
343 bool StoreAdjacencyToFile(ofstream *out, char *path);
344 bool CheckAdjacencyFileAgainstMolecule(ofstream *out, char *path, atom **ListOfAtoms);
345 bool ParseOrderAtSiteFromFile(ofstream *out, char *path);
346 bool StoreOrderAtSiteFile(ofstream *out, char *path);
347 bool ParseKeySetFile(ofstream *out, char *filename, Graph *&FragmentList, bool IsAngstroem);
348 bool StoreKeySetFile(ofstream *out, Graph &KeySetList, char *path);
349 bool StoreForcesFile(ofstream *out, MoleculeListClass *BondFragments, char *path, int *SortIndex);
350 bool CreateMappingLabelsToConfigSequence(ofstream *out, int *&SortIndex);
351 bool ScanBufferIntoKeySet(ofstream *out, char *buffer, KeySet &CurrentSet);
352 void BreadthFirstSearchAdd(ofstream *out, molecule *Mol, atom **&AddedAtomList, bond **&AddedBondList, atom *Root, bond *Bond, int BondOrder, bool IsAngstroem);
353 /// -# BOSSANOVA
354 void FragmentBOSSANOVA(ofstream *out, Graph *&FragmentList, KeyStack &RootStack);
355 int PowerSetGenerator(ofstream *out, int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet);
356 bool BuildInducedSubgraph(ofstream *out, const molecule *Father);
357 molecule * StoreFragmentFromKeySet(ofstream *out, KeySet &Leaflet, bool IsAngstroem);
358 void SPFragmentGenerator(ofstream *out, struct UniqueFragments *FragmentSearch, int RootDistance, bond **BondsSet, int SetDimension, int SubOrder);
359 int LookForRemovalCandidate(ofstream *&out, KeySet *&Leaf, int *&ShortestPathList);
360 int GuesstimateFragmentCount(ofstream *out, int order);
361
362 // Recognize doubly appearing molecules in a list of them
363 int * IsEqualToWithinThreshold(ofstream *out, molecule *OtherMolecule, double threshold);
364 int * GetFatherSonAtomicMap(ofstream *out, molecule *OtherMolecule);
365
366 // Output routines.
367 bool Output(ofstream *out);
368 void OutputListOfBonds(ofstream *out) const;
369 bool OutputXYZ(ofstream *out) const;
370 bool Checkout(ofstream *out) const;
371
372 private:
373 int last_atom; //!< number given to last atom
374};
375
376/** A list of \a molecule classes.
377 */
378class MoleculeListClass {
379 public:
380 molecule **ListOfMolecules; //!< pointer list of fragment molecules to check for equality
381 int NumberOfMolecules; //!< Number of entries in \a **FragmentList and of to be returned one.
382 int NumberOfTopAtoms; //!< Number of atoms in the molecule from which all fragments originate
383
384 MoleculeListClass();
385 MoleculeListClass(int Num, int NumAtoms);
386 ~MoleculeListClass();
387
388 /// Output configs.
389 bool StoreForcesFile(ofstream *out, char *path, int *SortIndex);
390 bool OutputConfigForListOfFragments(ofstream *out, config *configuration, int *SortIndex);
391 void Output(ofstream *out);
392
393 private:
394};
395
396
397/** A leaf for a tree of \a molecule class
398 * Wraps molecules in a tree structure
399 */
400class MoleculeLeafClass {
401 public:
402 molecule *Leaf; //!< molecule of this leaf
403 //MoleculeLeafClass *UpLeaf; //!< Leaf one level up
404 //MoleculeLeafClass *DownLeaf; //!< First leaf one level down
405 MoleculeLeafClass *previous; //!< Previous leaf on this level
406 MoleculeLeafClass *next; //!< Next leaf on this level
407
408 //MoleculeLeafClass(MoleculeLeafClass *Up, MoleculeLeafClass *Previous);
409 MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf);
410 ~MoleculeLeafClass();
411
412 bool AddLeaf(molecule *ptr, MoleculeLeafClass *Previous);
413 bool FillBondStructureFromReference(ofstream *out, molecule *reference, int &FragmentCounter, atom ***&ListOfLocalAtoms, bool FreeList = false);
414 bool FillRootStackForSubgraphs(ofstream *out, KeyStack *&RootStack, int Order, int &FragmentCounter);
415 bool AssignKeySetsToFragment(ofstream *out, molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList = false);
416 bool FillListOfLocalAtoms(ofstream *out, atom ***&ListOfLocalAtoms, int &FragmentCounter, int GlobalAtomCount, bool &FreeList);
417 void TranslateIndicesToGlobalIDs(ofstream *out, Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph);
418 int Count() const;
419};
420
421/** The config file.
422 * The class contains all parameters that control a dft run also functions to load and save.
423 */
424class config {
425 public:
426 int PsiType;
427 int MaxPsiDouble;
428 int PsiMaxNoUp;
429 int PsiMaxNoDown;
430 int MaxMinStopStep;
431 int InitMaxMinStopStep;
432 int ProcPEGamma;
433 int ProcPEPsi;
434 char *configpath;
435 char *configname;
436
437 private:
438 char *mainname;
439 char *defaultpath;
440 char *pseudopotpath;
441
442 int DoOutVis;
443 int DoOutMes;
444 int DoOutNICS;
445 int DoOutOrbitals;
446 int DoOutCurrent;
447 int DoFullCurrent;
448 int DoPerturbation;
449 int CommonWannier;
450 double SawtoothStart;
451 int VectorPlane;
452 double VectorCut;
453 int UseAddGramSch;
454 int Seed;
455
456 int MaxOuterStep;
457 double Deltat;
458 int OutVisStep;
459 int OutSrcStep;
460 double TargetTemp;
461 int ScaleTempStep;
462 int MaxPsiStep;
463 double EpsWannier;
464
465 int MaxMinStep;
466 double RelEpsTotalEnergy;
467 double RelEpsKineticEnergy;
468 int MaxMinGapStopStep;
469 int MaxInitMinStep;
470 double InitRelEpsTotalEnergy;
471 double InitRelEpsKineticEnergy;
472 int InitMaxMinGapStopStep;
473
474 //double BoxLength[NDIM*NDIM];
475
476 double ECut;
477 int MaxLevel;
478 int RiemannTensor;
479 int LevRFactor;
480 int RiemannLevel;
481 int Lev0Factor;
482 int RTActualUse;
483 int AddPsis;
484
485 double RCut;
486 int StructOpt;
487 int IsAngstroem;
488 int RelativeCoord;
489 int MaxTypes;
490
491
492 int ParseForParameter(int verbose, ifstream *file, const char *name, int sequential, int const xth, int const yth, int type, void *value, int repetition, int critical);
493
494 public:
495 config();
496 ~config();
497
498 int TestSyntax(char *filename, periodentafel *periode, molecule *mol);
499 void Load(char *filename, periodentafel *periode, molecule *mol);
500 void LoadOld(char *filename, periodentafel *periode, molecule *mol);
501 void RetrieveConfigPathAndName(char *filename);
502 bool Save(ofstream *file, periodentafel *periode, molecule *mol) const;
503 void Edit(molecule *mol);
504 bool GetIsAngstroem() const;
505 char *GetDefaultPath() const;
506 void config::SetDefaultPath(const char *path);
507};
508
509#endif /*MOLECULES_HPP_*/
510
Note: See TracBrowser for help on using the repository browser.