source: molecuilder/src/molecules.hpp@ d11f22

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

Working version of PAS transformation (tested on C-S-H cluster) and code scaffold of VolumeOfConvexEnvelope that's untested so far

PAS simply calculates inertia tensor, diagonalizes it via GSL and transforms molecule into eigenvector system such that z axis is eigenvector of biggest eigenvalue.

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