| [14de469] | 1 | /** \file molecules.cpp
 | 
|---|
| [69eb71] | 2 |  *
 | 
|---|
| [14de469] | 3 |  * Functions for the class molecule.
 | 
|---|
| [69eb71] | 4 |  *
 | 
|---|
| [14de469] | 5 |  */
 | 
|---|
 | 6 | 
 | 
|---|
| [f66195] | 7 | #include "atom.hpp"
 | 
|---|
 | 8 | #include "bond.hpp"
 | 
|---|
| [a80fbdf] | 9 | #include "config.hpp"
 | 
|---|
| [f66195] | 10 | #include "element.hpp"
 | 
|---|
 | 11 | #include "graph.hpp"
 | 
|---|
| [e9f8f9] | 12 | #include "helpers.hpp"
 | 
|---|
| [f66195] | 13 | #include "leastsquaremin.hpp"
 | 
|---|
 | 14 | #include "linkedcell.hpp"
 | 
|---|
 | 15 | #include "lists.hpp"
 | 
|---|
| [e138de] | 16 | #include "log.hpp"
 | 
|---|
| [cee0b57] | 17 | #include "molecule.hpp"
 | 
|---|
| [f66195] | 18 | #include "memoryallocator.hpp"
 | 
|---|
 | 19 | #include "periodentafel.hpp"
 | 
|---|
 | 20 | #include "stackclass.hpp"
 | 
|---|
 | 21 | #include "tesselation.hpp"
 | 
|---|
 | 22 | #include "vector.hpp"
 | 
|---|
| [14de469] | 23 | 
 | 
|---|
 | 24 | /************************************* Functions for class molecule *********************************/
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | /** Constructor of class molecule.
 | 
|---|
 | 27 |  * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
 | 
|---|
 | 28 |  */
 | 
|---|
| [fa649a] | 29 | molecule::molecule(const periodentafel * const teil) : elemente(teil), start(new atom), end(new atom), 
 | 
|---|
 | 30 |   first(new bond(start, end, 1, -1)), last(new bond(start, end, 1, -1)), MDSteps(0), AtomCount(0), 
 | 
|---|
 | 31 |   BondCount(0), ElementCount(0), NoNonHydrogen(0), NoNonBonds(0), NoCyclicBonds(0), BondDistance(0.), 
 | 
|---|
 | 32 |   ActiveFlag(false), IndexNr(-1), last_atom(0), InternalPointer(start)
 | 
|---|
| [69eb71] | 33 | {
 | 
|---|
| [042f82] | 34 |   // init atom chain list
 | 
|---|
 | 35 |   start->father = NULL;
 | 
|---|
 | 36 |   end->father = NULL;
 | 
|---|
 | 37 |   link(start,end);
 | 
|---|
| [fa649a] | 38 | 
 | 
|---|
| [042f82] | 39 |   // init bond chain list
 | 
|---|
 | 40 |   link(first,last);
 | 
|---|
| [fa649a] | 41 | 
 | 
|---|
| [042f82] | 42 |   // other stuff
 | 
|---|
 | 43 |   for(int i=MAX_ELEMENTS;i--;)
 | 
|---|
 | 44 |     ElementsInMolecule[i] = 0;
 | 
|---|
 | 45 |   cell_size[0] = cell_size[2] = cell_size[5]= 20.;
 | 
|---|
 | 46 |   cell_size[1] = cell_size[3] = cell_size[4]= 0.;
 | 
|---|
 | 47 |   strcpy(name,"none");
 | 
|---|
| [14de469] | 48 | };
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | /** Destructor of class molecule.
 | 
|---|
 | 51 |  * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
 | 
|---|
 | 52 |  */
 | 
|---|
| [69eb71] | 53 | molecule::~molecule()
 | 
|---|
| [14de469] | 54 | {
 | 
|---|
| [042f82] | 55 |   CleanupMolecule();
 | 
|---|
 | 56 |   delete(first);
 | 
|---|
 | 57 |   delete(last);
 | 
|---|
 | 58 |   delete(end);
 | 
|---|
 | 59 |   delete(start);
 | 
|---|
| [14de469] | 60 | };
 | 
|---|
 | 61 | 
 | 
|---|
| [357fba] | 62 | 
 | 
|---|
| [14de469] | 63 | /** Adds given atom \a *pointer from molecule list.
 | 
|---|
| [69eb71] | 64 |  * Increases molecule::last_atom and gives last number to added atom and names it according to its element::abbrev and molecule::AtomCount
 | 
|---|
| [14de469] | 65 |  * \param *pointer allocated and set atom
 | 
|---|
 | 66 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
 | 67 |  */
 | 
|---|
 | 68 | bool molecule::AddAtom(atom *pointer)
 | 
|---|
| [69eb71] | 69 | {
 | 
|---|
| [042f82] | 70 |   if (pointer != NULL) {
 | 
|---|
 | 71 |     pointer->sort = &pointer->nr;
 | 
|---|
 | 72 |     pointer->nr = last_atom++;  // increase number within molecule
 | 
|---|
 | 73 |     AtomCount++;
 | 
|---|
 | 74 |     if (pointer->type != NULL) {
 | 
|---|
 | 75 |       if (ElementsInMolecule[pointer->type->Z] == 0)
 | 
|---|
 | 76 |         ElementCount++;
 | 
|---|
 | 77 |       ElementsInMolecule[pointer->type->Z]++; // increase number of elements
 | 
|---|
 | 78 |       if (pointer->type->Z != 1)
 | 
|---|
 | 79 |         NoNonHydrogen++;
 | 
|---|
 | 80 |       if (pointer->Name == NULL) {
 | 
|---|
| [29812d] | 81 |         Free(&pointer->Name);
 | 
|---|
 | 82 |         pointer->Name = Malloc<char>(6, "molecule::AddAtom: *pointer->Name");
 | 
|---|
| [042f82] | 83 |         sprintf(pointer->Name, "%2s%02d", pointer->type->symbol, pointer->nr+1);
 | 
|---|
 | 84 |       }
 | 
|---|
 | 85 |     }
 | 
|---|
 | 86 |     return add(pointer, end);
 | 
|---|
 | 87 |   } else
 | 
|---|
 | 88 |     return false;
 | 
|---|
| [14de469] | 89 | };
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 | /** Adds a copy of the given atom \a *pointer from molecule list.
 | 
|---|
 | 92 |  * Increases molecule::last_atom and gives last number to added atom.
 | 
|---|
 | 93 |  * \param *pointer allocated and set atom
 | 
|---|
| [89c8b2] | 94 |  * \return pointer to the newly added atom
 | 
|---|
| [14de469] | 95 |  */
 | 
|---|
 | 96 | atom * molecule::AddCopyAtom(atom *pointer)
 | 
|---|
| [69eb71] | 97 | {
 | 
|---|
| [042f82] | 98 |   if (pointer != NULL) {
 | 
|---|
| [2319ed] | 99 |     atom *walker = new atom(pointer);
 | 
|---|
| [29812d] | 100 |     walker->Name = Malloc<char>(strlen(pointer->Name) + 1, "atom::atom: *Name");
 | 
|---|
| [042f82] | 101 |     strcpy (walker->Name, pointer->Name);
 | 
|---|
| [2319ed] | 102 |     walker->nr = last_atom++;  // increase number within molecule
 | 
|---|
| [042f82] | 103 |     add(walker, end);
 | 
|---|
 | 104 |     if ((pointer->type != NULL) && (pointer->type->Z != 1))
 | 
|---|
 | 105 |       NoNonHydrogen++;
 | 
|---|
 | 106 |     AtomCount++;
 | 
|---|
 | 107 |     return walker;
 | 
|---|
 | 108 |   } else
 | 
|---|
 | 109 |     return NULL;
 | 
|---|
| [14de469] | 110 | };
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 | /** Adds a Hydrogen atom in replacement for the given atom \a *partner in bond with a *origin.
 | 
|---|
 | 113 |  * Here, we have to distinguish between single, double or triple bonds as stated by \a BondDegree, that each demand
 | 
|---|
 | 114 |  * a different scheme when adding \a *replacement atom for the given one.
 | 
|---|
 | 115 |  * -# Single Bond: Simply add new atom with bond distance rescaled to typical hydrogen one
 | 
|---|
 | 116 |  * -# Double Bond: Here, we need the **BondList of the \a *origin atom, by scanning for the other bonds instead of
 | 
|---|
| [042f82] | 117 |  *    *Bond, we use the through these connected atoms to determine the plane they lie in, vector::MakeNormalvector().
 | 
|---|
 | 118 |  *    The orthonormal vector to this plane along with the vector in *Bond direction determines the plane the two
 | 
|---|
 | 119 |  *    replacing hydrogens shall lie in. Now, all remains to do is take the usual hydrogen double bond angle for the
 | 
|---|
 | 120 |  *    element of *origin and form the sin/cos admixture of both plane vectors for the new coordinates of the two
 | 
|---|
 | 121 |  *    hydrogens forming this angle with *origin.
 | 
|---|
| [14de469] | 122 |  * -# Triple Bond: The idea is to set up a tetraoid (C1-H1-H2-H3) (however the lengths \f$b\f$ of the sides of the base
 | 
|---|
| [042f82] | 123 |  *    triangle formed by the to be added hydrogens are not equal to the typical bond distance \f$l\f$ but have to be
 | 
|---|
 | 124 |  *    determined from the typical angle \f$\alpha\f$ for a hydrogen triple connected to the element of *origin):
 | 
|---|
 | 125 |  *    We have the height \f$d\f$ as the vector in *Bond direction (from triangle C1-H1-H2).
 | 
|---|
 | 126 |  *    \f[ h = l \cdot \cos{\left (\frac{\alpha}{2} \right )} \qquad b = 2l \cdot \sin{\left (\frac{\alpha}{2} \right)} \quad \rightarrow \quad d = l \cdot \sqrt{\cos^2{\left (\frac{\alpha}{2} \right)}-\frac{1}{3}\cdot\sin^2{\left (\frac{\alpha}{2}\right )}}
 | 
|---|
 | 127 |  *    \f]
 | 
|---|
 | 128 |  *    vector::GetNormalvector() creates one orthonormal vector from this *Bond vector and vector::MakeNormalvector creates
 | 
|---|
 | 129 |  *    the third one from the former two vectors. The latter ones form the plane of the base triangle mentioned above.
 | 
|---|
 | 130 |  *    The lengths for these are \f$f\f$ and \f$g\f$ (from triangle H1-H2-(center of H1-H2-H3)) with knowledge that
 | 
|---|
 | 131 |  *    the median lines in an isosceles triangle meet in the center point with a ratio 2:1.
 | 
|---|
 | 132 |  *    \f[ f = \frac{b}{\sqrt{3}} \qquad g = \frac{b}{2}
 | 
|---|
 | 133 |  *    \f]
 | 
|---|
 | 134 |  *    as the coordination of all three atoms in the coordinate system of these three vectors:
 | 
|---|
 | 135 |  *    \f$\pmatrix{d & f & 0}\f$, \f$\pmatrix{d & -0.5 \cdot f & g}\f$ and \f$\pmatrix{d & -0.5 \cdot f & -g}\f$.
 | 
|---|
| [69eb71] | 136 |  *
 | 
|---|
| [14de469] | 137 |  * \param *out output stream for debugging
 | 
|---|
| [69eb71] | 138 |  * \param *Bond pointer to bond between \a *origin and \a *replacement
 | 
|---|
 | 139 |  * \param *TopOrigin son of \a *origin of upper level molecule (the atom added to this molecule as a copy of \a *origin)
 | 
|---|
| [14de469] | 140 |  * \param *origin pointer to atom which acts as the origin for scaling the added hydrogen to correct bond length
 | 
|---|
 | 141 |  * \param *replacement pointer to the atom which shall be copied as a hydrogen atom in this molecule
 | 
|---|
 | 142 |  * \param isAngstroem whether the coordination of the given atoms is in AtomicLength (false) or Angstrom(true)
 | 
|---|
 | 143 |  * \return number of atoms added, if < bond::BondDegree then something went wrong
 | 
|---|
 | 144 |  * \todo double and triple bonds splitting (always use the tetraeder angle!)
 | 
|---|
 | 145 |  */
 | 
|---|
| [e138de] | 146 | bool molecule::AddHydrogenReplacementAtom(bond *TopBond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem)
 | 
|---|
| [14de469] | 147 | {
 | 
|---|
| [042f82] | 148 |   double bondlength;  // bond length of the bond to be replaced/cut
 | 
|---|
 | 149 |   double bondangle;  // bond angle of the bond to be replaced/cut
 | 
|---|
 | 150 |   double BondRescale;   // rescale value for the hydrogen bond length
 | 
|---|
 | 151 |   bool AllWentWell = true;    // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
 | 
|---|
 | 152 |   bond *FirstBond = NULL, *SecondBond = NULL; // Other bonds in double bond case to determine "other" plane
 | 
|---|
 | 153 |   atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
 | 
|---|
 | 154 |   double b,l,d,f,g, alpha, factors[NDIM];    // hold temporary values in triple bond case for coordination determination
 | 
|---|
 | 155 |   Vector Orthovector1, Orthovector2;  // temporary vectors in coordination construction
 | 
|---|
 | 156 |   Vector InBondvector;    // vector in direction of *Bond
 | 
|---|
| [1614174] | 157 |   double *matrix = NULL;
 | 
|---|
| [266237] | 158 |   bond *Binder = NULL;
 | 
|---|
| [042f82] | 159 | 
 | 
|---|
| [e138de] | 160 | //  Log() << Verbose(3) << "Begin of AddHydrogenReplacementAtom." << endl;
 | 
|---|
| [042f82] | 161 |   // create vector in direction of bond
 | 
|---|
 | 162 |   InBondvector.CopyVector(&TopReplacement->x);
 | 
|---|
 | 163 |   InBondvector.SubtractVector(&TopOrigin->x);
 | 
|---|
 | 164 |   bondlength = InBondvector.Norm();
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 |    // is greater than typical bond distance? Then we have to correct periodically
 | 
|---|
 | 167 |    // the problem is not the H being out of the box, but InBondvector have the wrong direction
 | 
|---|
 | 168 |    // due to TopReplacement or Origin being on the wrong side!
 | 
|---|
 | 169 |   if (bondlength > BondDistance) {
 | 
|---|
| [e138de] | 170 | //    Log() << Verbose(4) << "InBondvector is: ";
 | 
|---|
| [042f82] | 171 | //    InBondvector.Output(out);
 | 
|---|
| [e138de] | 172 | //    Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 173 |     Orthovector1.Zero();
 | 
|---|
 | 174 |     for (int i=NDIM;i--;) {
 | 
|---|
 | 175 |       l = TopReplacement->x.x[i] - TopOrigin->x.x[i];
 | 
|---|
 | 176 |       if (fabs(l) > BondDistance) { // is component greater than bond distance
 | 
|---|
 | 177 |         Orthovector1.x[i] = (l < 0) ? -1. : +1.;
 | 
|---|
 | 178 |       } // (signs are correct, was tested!)
 | 
|---|
 | 179 |     }
 | 
|---|
 | 180 |     matrix = ReturnFullMatrixforSymmetric(cell_size);
 | 
|---|
 | 181 |     Orthovector1.MatrixMultiplication(matrix);
 | 
|---|
 | 182 |     InBondvector.SubtractVector(&Orthovector1); // subtract just the additional translation
 | 
|---|
| [29812d] | 183 |     Free(&matrix);
 | 
|---|
| [042f82] | 184 |     bondlength = InBondvector.Norm();
 | 
|---|
| [e138de] | 185 | //    Log() << Verbose(4) << "Corrected InBondvector is now: ";
 | 
|---|
| [042f82] | 186 | //    InBondvector.Output(out);
 | 
|---|
| [e138de] | 187 | //    Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 188 |   } // periodic correction finished
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 |   InBondvector.Normalize();
 | 
|---|
 | 191 |   // get typical bond length and store as scale factor for later
 | 
|---|
 | 192 |   BondRescale = TopOrigin->type->HBondDistance[TopBond->BondDegree-1];
 | 
|---|
 | 193 |   if (BondRescale == -1) {
 | 
|---|
| [717e0c] | 194 |     eLog() << Verbose(1) << "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl;
 | 
|---|
| [042f82] | 195 |     return false;
 | 
|---|
 | 196 |     BondRescale = bondlength;
 | 
|---|
 | 197 |   } else {
 | 
|---|
 | 198 |     if (!IsAngstroem)
 | 
|---|
 | 199 |       BondRescale /= (1.*AtomicLengthToAngstroem);
 | 
|---|
 | 200 |   }
 | 
|---|
 | 201 | 
 | 
|---|
 | 202 |   // discern single, double and triple bonds
 | 
|---|
 | 203 |   switch(TopBond->BondDegree) {
 | 
|---|
 | 204 |     case 1:
 | 
|---|
 | 205 |       FirstOtherAtom = new atom();    // new atom
 | 
|---|
 | 206 |       FirstOtherAtom->type = elemente->FindElement(1);  // element is Hydrogen
 | 
|---|
 | 207 |       FirstOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
 | 
|---|
 | 208 |       FirstOtherAtom->FixedIon = TopReplacement->FixedIon;
 | 
|---|
 | 209 |       if (TopReplacement->type->Z == 1) { // neither rescale nor replace if it's already hydrogen
 | 
|---|
 | 210 |         FirstOtherAtom->father = TopReplacement;
 | 
|---|
 | 211 |         BondRescale = bondlength;
 | 
|---|
 | 212 |       } else {
 | 
|---|
 | 213 |         FirstOtherAtom->father = NULL;  // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
 | 
|---|
 | 214 |       }
 | 
|---|
 | 215 |       InBondvector.Scale(&BondRescale);   // rescale the distance vector to Hydrogen bond length
 | 
|---|
 | 216 |       FirstOtherAtom->x.CopyVector(&TopOrigin->x); // set coordination to origin ...
 | 
|---|
 | 217 |       FirstOtherAtom->x.AddVector(&InBondvector);  // ... and add distance vector to replacement atom
 | 
|---|
 | 218 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
| [e138de] | 219 | //      Log() << Verbose(4) << "Added " << *FirstOtherAtom << " at: ";
 | 
|---|
| [042f82] | 220 | //      FirstOtherAtom->x.Output(out);
 | 
|---|
| [e138de] | 221 | //      Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 222 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 223 |       Binder->Cyclic = false;
 | 
|---|
 | 224 |       Binder->Type = TreeEdge;
 | 
|---|
 | 225 |       break;
 | 
|---|
 | 226 |     case 2:
 | 
|---|
 | 227 |       // determine two other bonds (warning if there are more than two other) plus valence sanity check
 | 
|---|
| [266237] | 228 |       for (BondList::const_iterator Runner = TopOrigin->ListOfBonds.begin(); Runner != TopOrigin->ListOfBonds.end(); (++Runner)) {
 | 
|---|
 | 229 |         if ((*Runner) != TopBond) {
 | 
|---|
| [042f82] | 230 |           if (FirstBond == NULL) {
 | 
|---|
| [266237] | 231 |             FirstBond = (*Runner);
 | 
|---|
 | 232 |             FirstOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
 | 
|---|
| [042f82] | 233 |           } else if (SecondBond == NULL) {
 | 
|---|
| [266237] | 234 |             SecondBond = (*Runner);
 | 
|---|
 | 235 |             SecondOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
 | 
|---|
| [042f82] | 236 |           } else {
 | 
|---|
| [717e0c] | 237 |             eLog() << Verbose(2) << "Detected more than four bonds for atom " << TopOrigin->Name;
 | 
|---|
| [042f82] | 238 |           }
 | 
|---|
 | 239 |         }
 | 
|---|
 | 240 |       }
 | 
|---|
 | 241 |       if (SecondOtherAtom == NULL) {  // then we have an atom with valence four, but only 3 bonds: one to replace and one which is TopBond (third is FirstBond)
 | 
|---|
 | 242 |         SecondBond = TopBond;
 | 
|---|
 | 243 |         SecondOtherAtom = TopReplacement;
 | 
|---|
 | 244 |       }
 | 
|---|
 | 245 |       if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all
 | 
|---|
| [e138de] | 246 | //        Log() << Verbose(3) << "Regarding the double bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") to be constructed: Taking " << FirstOtherAtom->Name << " and " << SecondOtherAtom->Name << " along with " << TopOrigin->Name << " to determine orthogonal plane." << endl;
 | 
|---|
| [042f82] | 247 | 
 | 
|---|
 | 248 |         // determine the plane of these two with the *origin
 | 
|---|
 | 249 |         AllWentWell = AllWentWell && Orthovector1.MakeNormalVector(&TopOrigin->x, &FirstOtherAtom->x, &SecondOtherAtom->x);
 | 
|---|
 | 250 |       } else {
 | 
|---|
 | 251 |         Orthovector1.GetOneNormalVector(&InBondvector);
 | 
|---|
 | 252 |       }
 | 
|---|
| [e138de] | 253 |       //Log() << Verbose(3)<< "Orthovector1: ";
 | 
|---|
| [042f82] | 254 |       //Orthovector1.Output(out);
 | 
|---|
| [e138de] | 255 |       //Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 256 |       // orthogonal vector and bond vector between origin and replacement form the new plane
 | 
|---|
 | 257 |       Orthovector1.MakeNormalVector(&InBondvector);
 | 
|---|
 | 258 |       Orthovector1.Normalize();
 | 
|---|
| [e138de] | 259 |       //Log() << Verbose(3) << "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << "." << endl;
 | 
|---|
| [042f82] | 260 | 
 | 
|---|
 | 261 |       // create the two Hydrogens ...
 | 
|---|
 | 262 |       FirstOtherAtom = new atom();
 | 
|---|
 | 263 |       SecondOtherAtom = new atom();
 | 
|---|
 | 264 |       FirstOtherAtom->type = elemente->FindElement(1);
 | 
|---|
 | 265 |       SecondOtherAtom->type = elemente->FindElement(1);
 | 
|---|
 | 266 |       FirstOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
 | 
|---|
 | 267 |       FirstOtherAtom->FixedIon = TopReplacement->FixedIon;
 | 
|---|
 | 268 |       SecondOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
 | 
|---|
 | 269 |       SecondOtherAtom->FixedIon = TopReplacement->FixedIon;
 | 
|---|
 | 270 |       FirstOtherAtom->father = NULL;  // we are just an added hydrogen with no father
 | 
|---|
 | 271 |       SecondOtherAtom->father = NULL;  //  we are just an added hydrogen with no father
 | 
|---|
 | 272 |       bondangle = TopOrigin->type->HBondAngle[1];
 | 
|---|
 | 273 |       if (bondangle == -1) {
 | 
|---|
| [717e0c] | 274 |         eLog() << Verbose(1) << "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl;
 | 
|---|
| [042f82] | 275 |         return false;
 | 
|---|
 | 276 |         bondangle = 0;
 | 
|---|
 | 277 |       }
 | 
|---|
 | 278 |       bondangle *= M_PI/180./2.;
 | 
|---|
| [e138de] | 279 | //      Log() << Verbose(3) << "ReScaleCheck: InBondvector ";
 | 
|---|
| [042f82] | 280 | //      InBondvector.Output(out);
 | 
|---|
| [e138de] | 281 | //      Log() << Verbose(0) << endl;
 | 
|---|
 | 282 | //      Log() << Verbose(3) << "ReScaleCheck: Orthovector ";
 | 
|---|
| [042f82] | 283 | //      Orthovector1.Output(out);
 | 
|---|
| [e138de] | 284 | //      Log() << Verbose(0) << endl;
 | 
|---|
 | 285 | //      Log() << Verbose(3) << "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle) << endl;
 | 
|---|
| [042f82] | 286 |       FirstOtherAtom->x.Zero();
 | 
|---|
 | 287 |       SecondOtherAtom->x.Zero();
 | 
|---|
 | 288 |       for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
 | 
|---|
 | 289 |         FirstOtherAtom->x.x[i] = InBondvector.x[i] * cos(bondangle) + Orthovector1.x[i] * (sin(bondangle));
 | 
|---|
 | 290 |         SecondOtherAtom->x.x[i] = InBondvector.x[i] * cos(bondangle) + Orthovector1.x[i] * (-sin(bondangle));
 | 
|---|
 | 291 |       }
 | 
|---|
 | 292 |       FirstOtherAtom->x.Scale(&BondRescale);  // rescale by correct BondDistance
 | 
|---|
 | 293 |       SecondOtherAtom->x.Scale(&BondRescale);
 | 
|---|
| [e138de] | 294 |       //Log() << Verbose(3) << "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << "." << endl;
 | 
|---|
| [042f82] | 295 |       for(int i=NDIM;i--;) { // and make relative to origin atom
 | 
|---|
 | 296 |         FirstOtherAtom->x.x[i] += TopOrigin->x.x[i];
 | 
|---|
 | 297 |         SecondOtherAtom->x.x[i] += TopOrigin->x.x[i];
 | 
|---|
 | 298 |       }
 | 
|---|
 | 299 |       // ... and add to molecule
 | 
|---|
 | 300 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
 | 301 |       AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
 | 
|---|
| [e138de] | 302 | //      Log() << Verbose(4) << "Added " << *FirstOtherAtom << " at: ";
 | 
|---|
| [042f82] | 303 | //      FirstOtherAtom->x.Output(out);
 | 
|---|
| [e138de] | 304 | //      Log() << Verbose(0) << endl;
 | 
|---|
 | 305 | //      Log() << Verbose(4) << "Added " << *SecondOtherAtom << " at: ";
 | 
|---|
| [042f82] | 306 | //      SecondOtherAtom->x.Output(out);
 | 
|---|
| [e138de] | 307 | //      Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 308 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 309 |       Binder->Cyclic = false;
 | 
|---|
 | 310 |       Binder->Type = TreeEdge;
 | 
|---|
 | 311 |       Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
 | 
|---|
 | 312 |       Binder->Cyclic = false;
 | 
|---|
 | 313 |       Binder->Type = TreeEdge;
 | 
|---|
 | 314 |       break;
 | 
|---|
 | 315 |     case 3:
 | 
|---|
 | 316 |       // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
 | 
|---|
 | 317 |       FirstOtherAtom = new atom();
 | 
|---|
 | 318 |       SecondOtherAtom = new atom();
 | 
|---|
 | 319 |       ThirdOtherAtom = new atom();
 | 
|---|
 | 320 |       FirstOtherAtom->type = elemente->FindElement(1);
 | 
|---|
 | 321 |       SecondOtherAtom->type = elemente->FindElement(1);
 | 
|---|
 | 322 |       ThirdOtherAtom->type = elemente->FindElement(1);
 | 
|---|
 | 323 |       FirstOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
 | 
|---|
 | 324 |       FirstOtherAtom->FixedIon = TopReplacement->FixedIon;
 | 
|---|
 | 325 |       SecondOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
 | 
|---|
 | 326 |       SecondOtherAtom->FixedIon = TopReplacement->FixedIon;
 | 
|---|
 | 327 |       ThirdOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
 | 
|---|
 | 328 |       ThirdOtherAtom->FixedIon = TopReplacement->FixedIon;
 | 
|---|
 | 329 |       FirstOtherAtom->father = NULL;  //  we are just an added hydrogen with no father
 | 
|---|
 | 330 |       SecondOtherAtom->father = NULL;  //  we are just an added hydrogen with no father
 | 
|---|
 | 331 |       ThirdOtherAtom->father = NULL;  //  we are just an added hydrogen with no father
 | 
|---|
 | 332 | 
 | 
|---|
 | 333 |       // we need to vectors orthonormal the InBondvector
 | 
|---|
 | 334 |       AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(&InBondvector);
 | 
|---|
| [e138de] | 335 | //      Log() << Verbose(3) << "Orthovector1: ";
 | 
|---|
| [042f82] | 336 | //      Orthovector1.Output(out);
 | 
|---|
| [e138de] | 337 | //      Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 338 |       AllWentWell = AllWentWell && Orthovector2.MakeNormalVector(&InBondvector, &Orthovector1);
 | 
|---|
| [e138de] | 339 | //      Log() << Verbose(3) << "Orthovector2: ";
 | 
|---|
| [042f82] | 340 | //      Orthovector2.Output(out);
 | 
|---|
| [e138de] | 341 | //      Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 342 | 
 | 
|---|
 | 343 |       // create correct coordination for the three atoms
 | 
|---|
 | 344 |       alpha = (TopOrigin->type->HBondAngle[2])/180.*M_PI/2.;  // retrieve triple bond angle from database
 | 
|---|
 | 345 |       l = BondRescale;        // desired bond length
 | 
|---|
 | 346 |       b = 2.*l*sin(alpha);    // base length of isosceles triangle
 | 
|---|
 | 347 |       d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.);   // length for InBondvector
 | 
|---|
 | 348 |       f = b/sqrt(3.);   // length for Orthvector1
 | 
|---|
 | 349 |       g = b/2.;         // length for Orthvector2
 | 
|---|
| [e138de] | 350 | //      Log() << Verbose(3) << "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", " << endl;
 | 
|---|
 | 351 | //      Log() << Verbose(3) << "The three Bond lengths: " << sqrt(d*d+f*f) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g) << ", "  << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g) << endl;
 | 
|---|
| [042f82] | 352 |       factors[0] = d;
 | 
|---|
 | 353 |       factors[1] = f;
 | 
|---|
 | 354 |       factors[2] = 0.;
 | 
|---|
 | 355 |       FirstOtherAtom->x.LinearCombinationOfVectors(&InBondvector, &Orthovector1, &Orthovector2, factors);
 | 
|---|
 | 356 |       factors[1] = -0.5*f;
 | 
|---|
 | 357 |       factors[2] = g;
 | 
|---|
 | 358 |       SecondOtherAtom->x.LinearCombinationOfVectors(&InBondvector, &Orthovector1, &Orthovector2, factors);
 | 
|---|
 | 359 |       factors[2] = -g;
 | 
|---|
 | 360 |       ThirdOtherAtom->x.LinearCombinationOfVectors(&InBondvector, &Orthovector1, &Orthovector2, factors);
 | 
|---|
 | 361 | 
 | 
|---|
 | 362 |       // rescale each to correct BondDistance
 | 
|---|
 | 363 | //      FirstOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 364 | //      SecondOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 365 | //      ThirdOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 366 | 
 | 
|---|
 | 367 |       // and relative to *origin atom
 | 
|---|
 | 368 |       FirstOtherAtom->x.AddVector(&TopOrigin->x);
 | 
|---|
 | 369 |       SecondOtherAtom->x.AddVector(&TopOrigin->x);
 | 
|---|
 | 370 |       ThirdOtherAtom->x.AddVector(&TopOrigin->x);
 | 
|---|
 | 371 | 
 | 
|---|
 | 372 |       // ... and add to molecule
 | 
|---|
 | 373 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
 | 374 |       AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
 | 
|---|
 | 375 |       AllWentWell = AllWentWell && AddAtom(ThirdOtherAtom);
 | 
|---|
| [e138de] | 376 | //      Log() << Verbose(4) << "Added " << *FirstOtherAtom << " at: ";
 | 
|---|
| [042f82] | 377 | //      FirstOtherAtom->x.Output(out);
 | 
|---|
| [e138de] | 378 | //      Log() << Verbose(0) << endl;
 | 
|---|
 | 379 | //      Log() << Verbose(4) << "Added " << *SecondOtherAtom << " at: ";
 | 
|---|
| [042f82] | 380 | //      SecondOtherAtom->x.Output(out);
 | 
|---|
| [e138de] | 381 | //      Log() << Verbose(0) << endl;
 | 
|---|
 | 382 | //      Log() << Verbose(4) << "Added " << *ThirdOtherAtom << " at: ";
 | 
|---|
| [042f82] | 383 | //      ThirdOtherAtom->x.Output(out);
 | 
|---|
| [e138de] | 384 | //      Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 385 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 386 |       Binder->Cyclic = false;
 | 
|---|
 | 387 |       Binder->Type = TreeEdge;
 | 
|---|
 | 388 |       Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
 | 
|---|
 | 389 |       Binder->Cyclic = false;
 | 
|---|
 | 390 |       Binder->Type = TreeEdge;
 | 
|---|
 | 391 |       Binder = AddBond(BottomOrigin, ThirdOtherAtom, 1);
 | 
|---|
 | 392 |       Binder->Cyclic = false;
 | 
|---|
 | 393 |       Binder->Type = TreeEdge;
 | 
|---|
 | 394 |       break;
 | 
|---|
 | 395 |     default:
 | 
|---|
| [717e0c] | 396 |       eLog() << Verbose(1) << "BondDegree does not state single, double or triple bond!" << endl;
 | 
|---|
| [042f82] | 397 |       AllWentWell = false;
 | 
|---|
 | 398 |       break;
 | 
|---|
 | 399 |   }
 | 
|---|
| [1614174] | 400 |   Free(&matrix);
 | 
|---|
| [042f82] | 401 | 
 | 
|---|
| [e138de] | 402 | //  Log() << Verbose(3) << "End of AddHydrogenReplacementAtom." << endl;
 | 
|---|
| [042f82] | 403 |   return AllWentWell;
 | 
|---|
| [14de469] | 404 | };
 | 
|---|
 | 405 | 
 | 
|---|
 | 406 | /** Adds given atom \a *pointer from molecule list.
 | 
|---|
 | 407 |  * Increases molecule::last_atom and gives last number to added atom.
 | 
|---|
 | 408 |  * \param filename name and path of xyz file
 | 
|---|
 | 409 |  * \return true - succeeded, false - file not found
 | 
|---|
 | 410 |  */
 | 
|---|
 | 411 | bool molecule::AddXYZFile(string filename)
 | 
|---|
| [69eb71] | 412 | {
 | 
|---|
| [042f82] | 413 |   istringstream *input = NULL;
 | 
|---|
 | 414 |   int NumberOfAtoms = 0; // atom number in xyz read
 | 
|---|
 | 415 |   int i, j; // loop variables
 | 
|---|
 | 416 |   atom *Walker = NULL;  // pointer to added atom
 | 
|---|
 | 417 |   char shorthand[3];  // shorthand for atom name
 | 
|---|
 | 418 |   ifstream xyzfile;   // xyz file
 | 
|---|
 | 419 |   string line;    // currently parsed line
 | 
|---|
 | 420 |   double x[3];    // atom coordinates
 | 
|---|
 | 421 | 
 | 
|---|
 | 422 |   xyzfile.open(filename.c_str());
 | 
|---|
 | 423 |   if (!xyzfile)
 | 
|---|
 | 424 |     return false;
 | 
|---|
 | 425 | 
 | 
|---|
 | 426 |   getline(xyzfile,line,'\n'); // Read numer of atoms in file
 | 
|---|
 | 427 |   input = new istringstream(line);
 | 
|---|
 | 428 |   *input >> NumberOfAtoms;
 | 
|---|
| [e138de] | 429 |   Log() << Verbose(0) << "Parsing " << NumberOfAtoms << " atoms in file." << endl;
 | 
|---|
| [042f82] | 430 |   getline(xyzfile,line,'\n'); // Read comment
 | 
|---|
| [e138de] | 431 |   Log() << Verbose(1) << "Comment: " << line << endl;
 | 
|---|
| [042f82] | 432 | 
 | 
|---|
 | 433 |   if (MDSteps == 0) // no atoms yet present
 | 
|---|
 | 434 |     MDSteps++;
 | 
|---|
 | 435 |   for(i=0;i<NumberOfAtoms;i++){
 | 
|---|
 | 436 |     Walker = new atom;
 | 
|---|
 | 437 |     getline(xyzfile,line,'\n');
 | 
|---|
 | 438 |     istringstream *item = new istringstream(line);
 | 
|---|
 | 439 |     //istringstream input(line);
 | 
|---|
| [e138de] | 440 |     //Log() << Verbose(1) << "Reading: " << line << endl;
 | 
|---|
| [042f82] | 441 |     *item >> shorthand;
 | 
|---|
 | 442 |     *item >> x[0];
 | 
|---|
 | 443 |     *item >> x[1];
 | 
|---|
 | 444 |     *item >> x[2];
 | 
|---|
 | 445 |     Walker->type = elemente->FindElement(shorthand);
 | 
|---|
 | 446 |     if (Walker->type == NULL) {
 | 
|---|
| [e359a8] | 447 |       eLog() << Verbose(1) << "Could not parse the element at line: '" << line << "', setting to H.";
 | 
|---|
| [042f82] | 448 |       Walker->type = elemente->FindElement(1);
 | 
|---|
 | 449 |     }
 | 
|---|
| [fcd7b6] | 450 |     if (Walker->Trajectory.R.size() <= (unsigned int)MDSteps) {
 | 
|---|
 | 451 |       Walker->Trajectory.R.resize(MDSteps+10);
 | 
|---|
 | 452 |       Walker->Trajectory.U.resize(MDSteps+10);
 | 
|---|
 | 453 |       Walker->Trajectory.F.resize(MDSteps+10);
 | 
|---|
| [042f82] | 454 |     }
 | 
|---|
 | 455 |     for(j=NDIM;j--;) {
 | 
|---|
 | 456 |       Walker->x.x[j] = x[j];
 | 
|---|
| [fcd7b6] | 457 |       Walker->Trajectory.R.at(MDSteps-1).x[j] = x[j];
 | 
|---|
 | 458 |       Walker->Trajectory.U.at(MDSteps-1).x[j] = 0;
 | 
|---|
 | 459 |       Walker->Trajectory.F.at(MDSteps-1).x[j] = 0;
 | 
|---|
| [042f82] | 460 |     }
 | 
|---|
 | 461 |     AddAtom(Walker);  // add to molecule
 | 
|---|
 | 462 |     delete(item);
 | 
|---|
 | 463 |   }
 | 
|---|
 | 464 |   xyzfile.close();
 | 
|---|
 | 465 |   delete(input);
 | 
|---|
 | 466 |   return true;
 | 
|---|
| [14de469] | 467 | };
 | 
|---|
 | 468 | 
 | 
|---|
 | 469 | /** Creates a copy of this molecule.
 | 
|---|
 | 470 |  * \return copy of molecule
 | 
|---|
 | 471 |  */
 | 
|---|
 | 472 | molecule *molecule::CopyMolecule()
 | 
|---|
 | 473 | {
 | 
|---|
| [042f82] | 474 |   molecule *copy = new molecule(elemente);
 | 
|---|
 | 475 |   atom *LeftAtom = NULL, *RightAtom = NULL;
 | 
|---|
 | 476 | 
 | 
|---|
 | 477 |   // copy all atoms
 | 
|---|
| [e9f8f9] | 478 |   ActOnCopyWithEachAtom ( &molecule::AddCopyAtom, copy );
 | 
|---|
| [042f82] | 479 | 
 | 
|---|
 | 480 |   // copy all bonds
 | 
|---|
 | 481 |   bond *Binder = first;
 | 
|---|
 | 482 |   bond *NewBond = NULL;
 | 
|---|
 | 483 |   while(Binder->next != last) {
 | 
|---|
 | 484 |     Binder = Binder->next;
 | 
|---|
| [cee0b57] | 485 | 
 | 
|---|
| [042f82] | 486 |     // get the pendant atoms of current bond in the copy molecule
 | 
|---|
| [b453f9] | 487 |     copy->ActOnAllAtoms( &atom::EqualsFather, (const atom *)Binder->leftatom, (const atom **)&LeftAtom );
 | 
|---|
 | 488 |     copy->ActOnAllAtoms( &atom::EqualsFather, (const atom *)Binder->rightatom, (const atom **)&RightAtom );
 | 
|---|
| [cee0b57] | 489 | 
 | 
|---|
| [042f82] | 490 |     NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
 | 
|---|
 | 491 |     NewBond->Cyclic = Binder->Cyclic;
 | 
|---|
 | 492 |     if (Binder->Cyclic)
 | 
|---|
 | 493 |       copy->NoCyclicBonds++;
 | 
|---|
 | 494 |     NewBond->Type = Binder->Type;
 | 
|---|
 | 495 |   }
 | 
|---|
 | 496 |   // correct fathers
 | 
|---|
| [cee0b57] | 497 |   ActOnAllAtoms( &atom::CorrectFather );
 | 
|---|
 | 498 | 
 | 
|---|
| [042f82] | 499 |   // copy values
 | 
|---|
| [e138de] | 500 |   copy->CountAtoms();
 | 
|---|
| [042f82] | 501 |   copy->CountElements();
 | 
|---|
 | 502 |   if (first->next != last) {  // if adjaceny list is present
 | 
|---|
 | 503 |     copy->BondDistance = BondDistance;
 | 
|---|
 | 504 |   }
 | 
|---|
 | 505 | 
 | 
|---|
 | 506 |   return copy;
 | 
|---|
| [14de469] | 507 | };
 | 
|---|
 | 508 | 
 | 
|---|
| [89c8b2] | 509 | 
 | 
|---|
 | 510 | /**
 | 
|---|
 | 511 |  * Copies all atoms of a molecule which are within the defined parallelepiped.
 | 
|---|
 | 512 |  *
 | 
|---|
 | 513 |  * @param offest for the origin of the parallelepiped
 | 
|---|
 | 514 |  * @param three vectors forming the matrix that defines the shape of the parallelpiped
 | 
|---|
 | 515 |  */
 | 
|---|
| [b453f9] | 516 | molecule* molecule::CopyMoleculeFromSubRegion(const Vector offset, const double *parallelepiped) const {
 | 
|---|
| [89c8b2] | 517 |   molecule *copy = new molecule(elemente);
 | 
|---|
 | 518 | 
 | 
|---|
| [e9f8f9] | 519 |   ActOnCopyWithEachAtomIfTrue ( &molecule::AddCopyAtom, copy, &atom::IsInParallelepiped, offset, parallelepiped );
 | 
|---|
| [89c8b2] | 520 | 
 | 
|---|
| [e138de] | 521 |   //TODO: copy->BuildInducedSubgraph(this);
 | 
|---|
| [89c8b2] | 522 | 
 | 
|---|
 | 523 |   return copy;
 | 
|---|
 | 524 | }
 | 
|---|
 | 525 | 
 | 
|---|
| [14de469] | 526 | /** Adds a bond to a the molecule specified by two atoms, \a *first and \a *second.
 | 
|---|
 | 527 |  * Also updates molecule::BondCount and molecule::NoNonBonds.
 | 
|---|
 | 528 |  * \param *first first atom in bond
 | 
|---|
 | 529 |  * \param *second atom in bond
 | 
|---|
 | 530 |  * \return pointer to bond or NULL on failure
 | 
|---|
 | 531 |  */
 | 
|---|
| [cee0b57] | 532 | bond * molecule::AddBond(atom *atom1, atom *atom2, int degree)
 | 
|---|
| [14de469] | 533 | {
 | 
|---|
| [042f82] | 534 |   bond *Binder = NULL;
 | 
|---|
 | 535 |   if ((atom1 != NULL) && (FindAtom(atom1->nr) != NULL) && (atom2 != NULL) && (FindAtom(atom2->nr) != NULL)) {
 | 
|---|
 | 536 |     Binder = new bond(atom1, atom2, degree, BondCount++);
 | 
|---|
| [266237] | 537 |     atom1->RegisterBond(Binder);
 | 
|---|
 | 538 |     atom2->RegisterBond(Binder);
 | 
|---|
| [042f82] | 539 |     if ((atom1->type != NULL) && (atom1->type->Z != 1) && (atom2->type != NULL) && (atom2->type->Z != 1))
 | 
|---|
 | 540 |       NoNonBonds++;
 | 
|---|
 | 541 |     add(Binder, last);
 | 
|---|
 | 542 |   } else {
 | 
|---|
| [717e0c] | 543 |     eLog() << Verbose(1) << "Could not add bond between " << atom1->Name << " and " << atom2->Name << " as one or both are not present in the molecule." << endl;
 | 
|---|
| [042f82] | 544 |   }
 | 
|---|
 | 545 |   return Binder;
 | 
|---|
| [14de469] | 546 | };
 | 
|---|
 | 547 | 
 | 
|---|
| [fa649a] | 548 | /** Remove bond from bond chain list and from the both atom::ListOfBonds.
 | 
|---|
| [69eb71] | 549 |  * \todo Function not implemented yet
 | 
|---|
| [14de469] | 550 |  * \param *pointer bond pointer
 | 
|---|
 | 551 |  * \return true - bound found and removed, false - bond not found/removed
 | 
|---|
 | 552 |  */
 | 
|---|
 | 553 | bool molecule::RemoveBond(bond *pointer)
 | 
|---|
 | 554 | {
 | 
|---|
| [e138de] | 555 |   //eLog() << Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl;
 | 
|---|
| [fa649a] | 556 |   pointer->leftatom->RegisterBond(pointer);
 | 
|---|
 | 557 |   pointer->rightatom->RegisterBond(pointer);
 | 
|---|
| [042f82] | 558 |   removewithoutcheck(pointer);
 | 
|---|
 | 559 |   return true;
 | 
|---|
| [14de469] | 560 | };
 | 
|---|
 | 561 | 
 | 
|---|
 | 562 | /** Remove every bond from bond chain list that atom \a *BondPartner is a constituent of.
 | 
|---|
| [69eb71] | 563 |  * \todo Function not implemented yet
 | 
|---|
| [14de469] | 564 |  * \param *BondPartner atom to be removed
 | 
|---|
 | 565 |  * \return true - bounds found and removed, false - bonds not found/removed
 | 
|---|
 | 566 |  */
 | 
|---|
 | 567 | bool molecule::RemoveBonds(atom *BondPartner)
 | 
|---|
 | 568 | {
 | 
|---|
| [e138de] | 569 |   //eLog() << Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl;
 | 
|---|
| [266237] | 570 |   BondList::const_iterator ForeRunner;
 | 
|---|
 | 571 |   while (!BondPartner->ListOfBonds.empty()) {
 | 
|---|
 | 572 |     ForeRunner = BondPartner->ListOfBonds.begin();
 | 
|---|
 | 573 |     RemoveBond(*ForeRunner);
 | 
|---|
 | 574 |   }
 | 
|---|
| [042f82] | 575 |   return false;
 | 
|---|
| [14de469] | 576 | };
 | 
|---|
 | 577 | 
 | 
|---|
| [1907a7] | 578 | /** Set molecule::name from the basename without suffix in the given \a *filename.
 | 
|---|
 | 579 |  * \param *filename filename
 | 
|---|
 | 580 |  */
 | 
|---|
| [d67150] | 581 | void molecule::SetNameFromFilename(const char *filename)
 | 
|---|
| [1907a7] | 582 | {
 | 
|---|
 | 583 |   int length = 0;
 | 
|---|
| [f7f7a4] | 584 |   const char *molname = strrchr(filename, '/');
 | 
|---|
 | 585 |   if (molname != NULL)
 | 
|---|
 | 586 |     molname += sizeof(char);  // search for filename without dirs
 | 
|---|
 | 587 |   else
 | 
|---|
 | 588 |     molname = filename; // contains no slashes
 | 
|---|
| [d67150] | 589 |   char *endname = strchr(molname, '.');
 | 
|---|
| [1907a7] | 590 |   if ((endname == NULL) || (endname < molname))
 | 
|---|
 | 591 |     length = strlen(molname);
 | 
|---|
 | 592 |   else
 | 
|---|
 | 593 |     length = strlen(molname) - strlen(endname);
 | 
|---|
 | 594 |   strncpy(name, molname, length);
 | 
|---|
| [d67150] | 595 |   name[length]='\0';
 | 
|---|
| [1907a7] | 596 | };
 | 
|---|
 | 597 | 
 | 
|---|
| [14de469] | 598 | /** Sets the molecule::cell_size to the components of \a *dim (rectangular box)
 | 
|---|
 | 599 |  * \param *dim vector class
 | 
|---|
 | 600 |  */
 | 
|---|
| [e9b8bb] | 601 | void molecule::SetBoxDimension(Vector *dim)
 | 
|---|
| [14de469] | 602 | {
 | 
|---|
| [042f82] | 603 |   cell_size[0] = dim->x[0];
 | 
|---|
 | 604 |   cell_size[1] = 0.;
 | 
|---|
 | 605 |   cell_size[2] = dim->x[1];
 | 
|---|
 | 606 |   cell_size[3] = 0.;
 | 
|---|
 | 607 |   cell_size[4] = 0.;
 | 
|---|
 | 608 |   cell_size[5] = dim->x[2];
 | 
|---|
| [14de469] | 609 | };
 | 
|---|
 | 610 | 
 | 
|---|
| [cee0b57] | 611 | /** Removes atom from molecule list and deletes it.
 | 
|---|
 | 612 |  * \param *pointer atom to be removed
 | 
|---|
 | 613 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
| [a9d254] | 614 |  */
 | 
|---|
| [cee0b57] | 615 | bool molecule::RemoveAtom(atom *pointer)
 | 
|---|
| [a9d254] | 616 | {
 | 
|---|
| [cee0b57] | 617 |   if (ElementsInMolecule[pointer->type->Z] != 0)  { // this would indicate an error
 | 
|---|
 | 618 |     ElementsInMolecule[pointer->type->Z]--;  // decrease number of atom of this element
 | 
|---|
 | 619 |     AtomCount--;
 | 
|---|
 | 620 |   } else
 | 
|---|
| [717e0c] | 621 |     eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;
 | 
|---|
| [cee0b57] | 622 |   if (ElementsInMolecule[pointer->type->Z] == 0)  // was last atom of this element?
 | 
|---|
 | 623 |     ElementCount--;
 | 
|---|
| [266237] | 624 |   RemoveBonds(pointer);
 | 
|---|
| [cee0b57] | 625 |   return remove(pointer, start, end);
 | 
|---|
| [a9d254] | 626 | };
 | 
|---|
 | 627 | 
 | 
|---|
| [cee0b57] | 628 | /** Removes atom from molecule list, but does not delete it.
 | 
|---|
 | 629 |  * \param *pointer atom to be removed
 | 
|---|
 | 630 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
| [f3278b] | 631 |  */
 | 
|---|
| [cee0b57] | 632 | bool molecule::UnlinkAtom(atom *pointer)
 | 
|---|
| [f3278b] | 633 | {
 | 
|---|
| [cee0b57] | 634 |   if (pointer == NULL)
 | 
|---|
 | 635 |     return false;
 | 
|---|
 | 636 |   if (ElementsInMolecule[pointer->type->Z] != 0)  // this would indicate an error
 | 
|---|
 | 637 |     ElementsInMolecule[pointer->type->Z]--; // decrease number of atom of this element
 | 
|---|
 | 638 |   else
 | 
|---|
| [717e0c] | 639 |     eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;
 | 
|---|
| [cee0b57] | 640 |   if (ElementsInMolecule[pointer->type->Z] == 0)  // was last atom of this element?
 | 
|---|
 | 641 |     ElementCount--;
 | 
|---|
 | 642 |   unlink(pointer);
 | 
|---|
 | 643 |   return true;
 | 
|---|
| [f3278b] | 644 | };
 | 
|---|
 | 645 | 
 | 
|---|
| [cee0b57] | 646 | /** Removes every atom from molecule list.
 | 
|---|
 | 647 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
| [14de469] | 648 |  */
 | 
|---|
| [cee0b57] | 649 | bool molecule::CleanupMolecule()
 | 
|---|
| [14de469] | 650 | {
 | 
|---|
| [266237] | 651 |   return (cleanup(first,last) && cleanup(start,end));
 | 
|---|
| [69eb71] | 652 | };
 | 
|---|
| [14de469] | 653 | 
 | 
|---|
| [cee0b57] | 654 | /** Finds an atom specified by its continuous number.
 | 
|---|
 | 655 |  * \param Nr number of atom withim molecule
 | 
|---|
 | 656 |  * \return pointer to atom or NULL
 | 
|---|
| [14de469] | 657 |  */
 | 
|---|
| [cee0b57] | 658 | atom * molecule::FindAtom(int Nr)  const{
 | 
|---|
 | 659 |   atom * walker = find(&Nr, start,end);
 | 
|---|
 | 660 |   if (walker != NULL) {
 | 
|---|
| [e138de] | 661 |     //Log() << Verbose(0) << "Found Atom Nr. " << walker->nr << endl;
 | 
|---|
| [cee0b57] | 662 |     return walker;
 | 
|---|
 | 663 |   } else {
 | 
|---|
| [e138de] | 664 |     Log() << Verbose(0) << "Atom not found in list." << endl;
 | 
|---|
| [cee0b57] | 665 |     return NULL;
 | 
|---|
| [042f82] | 666 |   }
 | 
|---|
| [69eb71] | 667 | };
 | 
|---|
| [14de469] | 668 | 
 | 
|---|
| [cee0b57] | 669 | /** Asks for atom number, and checks whether in list.
 | 
|---|
 | 670 |  * \param *text question before entering
 | 
|---|
| [a6b7fb] | 671 |  */
 | 
|---|
| [cee0b57] | 672 | atom * molecule::AskAtom(string text)
 | 
|---|
| [a6b7fb] | 673 | {
 | 
|---|
| [cee0b57] | 674 |   int No;
 | 
|---|
 | 675 |   atom *ion = NULL;
 | 
|---|
 | 676 |   do {
 | 
|---|
| [e138de] | 677 |     //Log() << Verbose(0) << "============Atom list==========================" << endl;
 | 
|---|
| [cee0b57] | 678 |     //mol->Output((ofstream *)&cout);
 | 
|---|
| [e138de] | 679 |     //Log() << Verbose(0) << "===============================================" << endl;
 | 
|---|
 | 680 |     Log() << Verbose(0) << text;
 | 
|---|
| [cee0b57] | 681 |     cin >> No;
 | 
|---|
 | 682 |     ion = this->FindAtom(No);
 | 
|---|
 | 683 |   } while (ion == NULL);
 | 
|---|
 | 684 |   return ion;
 | 
|---|
| [a6b7fb] | 685 | };
 | 
|---|
 | 686 | 
 | 
|---|
| [cee0b57] | 687 | /** Checks if given coordinates are within cell volume.
 | 
|---|
 | 688 |  * \param *x array of coordinates
 | 
|---|
 | 689 |  * \return true - is within, false - out of cell
 | 
|---|
| [14de469] | 690 |  */
 | 
|---|
| [cee0b57] | 691 | bool molecule::CheckBounds(const Vector *x) const
 | 
|---|
| [14de469] | 692 | {
 | 
|---|
| [cee0b57] | 693 |   bool result = true;
 | 
|---|
 | 694 |   int j =-1;
 | 
|---|
 | 695 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 696 |     j += i+1;
 | 
|---|
 | 697 |     result = result && ((x->x[i] >= 0) && (x->x[i] < cell_size[j]));
 | 
|---|
| [042f82] | 698 |   }
 | 
|---|
| [cee0b57] | 699 |   //return result;
 | 
|---|
 | 700 |   return true; /// probably not gonna use the check no more
 | 
|---|
| [69eb71] | 701 | };
 | 
|---|
| [14de469] | 702 | 
 | 
|---|
| [cee0b57] | 703 | /** Prints molecule to *out.
 | 
|---|
 | 704 |  * \param *out output stream
 | 
|---|
| [14de469] | 705 |  */
 | 
|---|
| [e138de] | 706 | bool molecule::Output(ofstream * const output)
 | 
|---|
| [14de469] | 707 | {
 | 
|---|
| [cee0b57] | 708 |   int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
 | 
|---|
 | 709 |   CountElements();
 | 
|---|
| [042f82] | 710 | 
 | 
|---|
| [cee0b57] | 711 |   for (int i=0;i<MAX_ELEMENTS;++i) {
 | 
|---|
 | 712 |     AtomNo[i] = 0;
 | 
|---|
 | 713 |     ElementNo[i] = 0;
 | 
|---|
| [042f82] | 714 |   }
 | 
|---|
| [e138de] | 715 |   if (output == NULL) {
 | 
|---|
| [cee0b57] | 716 |     return false;
 | 
|---|
 | 717 |   } else {
 | 
|---|
| [e138de] | 718 |     *output << "#Ion_TypeNr._Nr.R[0]    R[1]    R[2]    MoveType (0 MoveIon, 1 FixedIon)" << endl;
 | 
|---|
| [e9f8f9] | 719 |     SetIndexedArrayForEachAtomTo ( ElementNo, &element::Z, &AbsoluteValue, 1);
 | 
|---|
| [cee0b57] | 720 |     int current=1;
 | 
|---|
 | 721 |     for (int i=0;i<MAX_ELEMENTS;++i) {
 | 
|---|
 | 722 |       if (ElementNo[i] == 1)
 | 
|---|
 | 723 |         ElementNo[i] = current++;
 | 
|---|
 | 724 |     }
 | 
|---|
| [e138de] | 725 |     ActOnAllAtoms( &atom::OutputArrayIndexed, output, (const int *)ElementNo, (int *)AtomNo, (const char *) NULL );
 | 
|---|
| [cee0b57] | 726 |     return true;
 | 
|---|
| [042f82] | 727 |   }
 | 
|---|
| [14de469] | 728 | };
 | 
|---|
 | 729 | 
 | 
|---|
| [cee0b57] | 730 | /** Prints molecule with all atomic trajectory positions to *out.
 | 
|---|
 | 731 |  * \param *out output stream
 | 
|---|
| [21c017] | 732 |  */
 | 
|---|
| [e138de] | 733 | bool molecule::OutputTrajectories(ofstream * const output)
 | 
|---|
| [21c017] | 734 | {
 | 
|---|
| [cee0b57] | 735 |   int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
 | 
|---|
 | 736 |   CountElements();
 | 
|---|
| [21c017] | 737 | 
 | 
|---|
| [e138de] | 738 |   if (output == NULL) {
 | 
|---|
| [cee0b57] | 739 |     return false;
 | 
|---|
 | 740 |   } else {
 | 
|---|
 | 741 |     for (int step = 0; step < MDSteps; step++) {
 | 
|---|
 | 742 |       if (step == 0) {
 | 
|---|
| [e138de] | 743 |         *output << "#Ion_TypeNr._Nr.R[0]    R[1]    R[2]    MoveType (0 MoveIon, 1 FixedIon)" << endl;
 | 
|---|
| [205ccd] | 744 |       } else {
 | 
|---|
| [e138de] | 745 |         *output << "# ====== MD step " << step << " =========" << endl;
 | 
|---|
| [cee0b57] | 746 |       }
 | 
|---|
 | 747 |       for (int i=0;i<MAX_ELEMENTS;++i) {
 | 
|---|
 | 748 |         AtomNo[i] = 0;
 | 
|---|
 | 749 |         ElementNo[i] = 0;
 | 
|---|
| [205ccd] | 750 |       }
 | 
|---|
| [e9f8f9] | 751 |       SetIndexedArrayForEachAtomTo ( ElementNo, &element::Z, &AbsoluteValue, 1);
 | 
|---|
 | 752 |       int current=1;
 | 
|---|
 | 753 |       for (int i=0;i<MAX_ELEMENTS;++i) {
 | 
|---|
 | 754 |         if (ElementNo[i] == 1)
 | 
|---|
 | 755 |           ElementNo[i] = current++;
 | 
|---|
 | 756 |       }
 | 
|---|
| [e138de] | 757 |       ActOnAllAtoms( &atom::OutputTrajectory, output, (const int *)ElementNo, AtomNo, (const int)step );
 | 
|---|
| [21c017] | 758 |     }
 | 
|---|
| [cee0b57] | 759 |     return true;
 | 
|---|
| [21c017] | 760 |   }
 | 
|---|
 | 761 | };
 | 
|---|
 | 762 | 
 | 
|---|
| [266237] | 763 | /** Outputs contents of each atom::ListOfBonds.
 | 
|---|
| [cee0b57] | 764 |  * \param *out output stream
 | 
|---|
| [14de469] | 765 |  */
 | 
|---|
| [e138de] | 766 | void molecule::OutputListOfBonds() const
 | 
|---|
| [14de469] | 767 | {
 | 
|---|
| [e138de] | 768 |   Log() << Verbose(2) << endl << "From Contents of ListOfBonds, all non-hydrogen atoms:" << endl;
 | 
|---|
 | 769 |   ActOnAllAtoms (&atom::OutputBondOfAtom );
 | 
|---|
 | 770 |   Log() << Verbose(0) << endl;
 | 
|---|
| [14de469] | 771 | };
 | 
|---|
 | 772 | 
 | 
|---|
| [cee0b57] | 773 | /** Output of element before the actual coordination list.
 | 
|---|
 | 774 |  * \param *out stream pointer
 | 
|---|
| [14de469] | 775 |  */
 | 
|---|
| [e138de] | 776 | bool molecule::Checkout(ofstream * const output)  const
 | 
|---|
| [14de469] | 777 | {
 | 
|---|
| [e138de] | 778 |   return elemente->Checkout(output, ElementsInMolecule);
 | 
|---|
| [6e9353] | 779 | };
 | 
|---|
 | 780 | 
 | 
|---|
| [cee0b57] | 781 | /** Prints molecule with all its trajectories to *out as xyz file.
 | 
|---|
 | 782 |  * \param *out output stream
 | 
|---|
| [d7e30c] | 783 |  */
 | 
|---|
| [e138de] | 784 | bool molecule::OutputTrajectoriesXYZ(ofstream * const output)
 | 
|---|
| [d7e30c] | 785 | {
 | 
|---|
| [cee0b57] | 786 |   time_t now;
 | 
|---|
| [042f82] | 787 | 
 | 
|---|
| [e138de] | 788 |   if (output != NULL) {
 | 
|---|
| [681a8a] | 789 |     now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| [cee0b57] | 790 |     for (int step=0;step<MDSteps;step++) {
 | 
|---|
| [e138de] | 791 |       *output << AtomCount << "\n\tCreated by molecuilder, step " << step << ", on " << ctime(&now);
 | 
|---|
 | 792 |       ActOnAllAtoms( &atom::OutputTrajectoryXYZ, output, step );
 | 
|---|
| [042f82] | 793 |     }
 | 
|---|
| [cee0b57] | 794 |     return true;
 | 
|---|
 | 795 |   } else
 | 
|---|
 | 796 |     return false;
 | 
|---|
| [14de469] | 797 | };
 | 
|---|
 | 798 | 
 | 
|---|
| [cee0b57] | 799 | /** Prints molecule to *out as xyz file.
 | 
|---|
 | 800 | * \param *out output stream
 | 
|---|
| [69eb71] | 801 |  */
 | 
|---|
| [e138de] | 802 | bool molecule::OutputXYZ(ofstream * const output) const
 | 
|---|
| [4aa03a] | 803 | {
 | 
|---|
| [cee0b57] | 804 |   time_t now;
 | 
|---|
| [042f82] | 805 | 
 | 
|---|
| [e138de] | 806 |   if (output != NULL) {
 | 
|---|
| [23b830] | 807 |     now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| [e138de] | 808 |     *output << AtomCount << "\n\tCreated by molecuilder on " << ctime(&now);
 | 
|---|
 | 809 |     ActOnAllAtoms( &atom::OutputXYZLine, output );
 | 
|---|
| [042f82] | 810 |     return true;
 | 
|---|
| [cee0b57] | 811 |   } else
 | 
|---|
 | 812 |     return false;
 | 
|---|
 | 813 | };
 | 
|---|
| [4aa03a] | 814 | 
 | 
|---|
| [cee0b57] | 815 | /** Brings molecule::AtomCount and atom::*Name up-to-date.
 | 
|---|
| [14de469] | 816 |  * \param *out output stream for debugging
 | 
|---|
 | 817 |  */
 | 
|---|
| [e138de] | 818 | void molecule::CountAtoms()
 | 
|---|
| [14de469] | 819 | {
 | 
|---|
| [cee0b57] | 820 |   int i = 0;
 | 
|---|
 | 821 |   atom *Walker = start;
 | 
|---|
 | 822 |   while (Walker->next != end) {
 | 
|---|
 | 823 |     Walker = Walker->next;
 | 
|---|
 | 824 |     i++;
 | 
|---|
 | 825 |   }
 | 
|---|
 | 826 |   if ((AtomCount == 0) || (i != AtomCount)) {
 | 
|---|
| [e138de] | 827 |     Log() << Verbose(3) << "Mismatch in AtomCount " << AtomCount << " and recounted number " << i << ", renaming all." << endl;
 | 
|---|
| [cee0b57] | 828 |     AtomCount = i;
 | 
|---|
| [042f82] | 829 | 
 | 
|---|
| [cee0b57] | 830 |     // count NonHydrogen atoms and give each atom a unique name
 | 
|---|
 | 831 |     if (AtomCount != 0) {
 | 
|---|
 | 832 |       i=0;
 | 
|---|
 | 833 |       NoNonHydrogen = 0;
 | 
|---|
 | 834 |       Walker = start;
 | 
|---|
 | 835 |       while (Walker->next != end) {
 | 
|---|
 | 836 |         Walker = Walker->next;
 | 
|---|
 | 837 |         Walker->nr = i;   // update number in molecule (for easier referencing in FragmentMolecule lateron)
 | 
|---|
 | 838 |         if (Walker->type->Z != 1) // count non-hydrogen atoms whilst at it
 | 
|---|
 | 839 |           NoNonHydrogen++;
 | 
|---|
 | 840 |         Free(&Walker->Name);
 | 
|---|
 | 841 |         Walker->Name = Malloc<char>(6, "molecule::CountAtoms: *walker->Name");
 | 
|---|
 | 842 |         sprintf(Walker->Name, "%2s%02d", Walker->type->symbol, Walker->nr+1);
 | 
|---|
| [e138de] | 843 |         Log() << Verbose(3) << "Naming atom nr. " << Walker->nr << " " << Walker->Name << "." << endl;
 | 
|---|
| [cee0b57] | 844 |         i++;
 | 
|---|
| [042f82] | 845 |       }
 | 
|---|
| [cee0b57] | 846 |     } else
 | 
|---|
| [e138de] | 847 |       Log() << Verbose(3) << "AtomCount is still " << AtomCount << ", thus counting nothing." << endl;
 | 
|---|
| [042f82] | 848 |   }
 | 
|---|
| [cee0b57] | 849 | };
 | 
|---|
| [042f82] | 850 | 
 | 
|---|
| [cee0b57] | 851 | /** Brings molecule::ElementCount and molecule::ElementsInMolecule up-to-date.
 | 
|---|
 | 852 |  */
 | 
|---|
 | 853 | void molecule::CountElements()
 | 
|---|
 | 854 | {
 | 
|---|
| [23b830] | 855 |   for(int i=MAX_ELEMENTS;i--;)
 | 
|---|
| [cee0b57] | 856 |     ElementsInMolecule[i] = 0;
 | 
|---|
 | 857 |   ElementCount = 0;
 | 
|---|
| [042f82] | 858 | 
 | 
|---|
| [23b830] | 859 |   SetIndexedArrayForEachAtomTo ( ElementsInMolecule, &element::Z, &Increment, 1);
 | 
|---|
 | 860 | 
 | 
|---|
 | 861 |   for(int i=MAX_ELEMENTS;i--;)
 | 
|---|
| [cee0b57] | 862 |     ElementCount += (ElementsInMolecule[i] != 0 ? 1 : 0);
 | 
|---|
 | 863 | };
 | 
|---|
| [042f82] | 864 | 
 | 
|---|
 | 865 | 
 | 
|---|
| [cee0b57] | 866 | /** Counts necessary number of valence electrons and returns number and SpinType.
 | 
|---|
 | 867 |  * \param configuration containing everything
 | 
|---|
 | 868 |  */
 | 
|---|
 | 869 | void molecule::CalculateOrbitals(class config &configuration)
 | 
|---|
 | 870 | {
 | 
|---|
 | 871 |   configuration.MaxPsiDouble = configuration.PsiMaxNoDown = configuration.PsiMaxNoUp = configuration.PsiType = 0;
 | 
|---|
 | 872 |   for(int i=MAX_ELEMENTS;i--;) {
 | 
|---|
 | 873 |     if (ElementsInMolecule[i] != 0) {
 | 
|---|
| [e138de] | 874 |       //Log() << Verbose(0) << "CalculateOrbitals: " << elemente->FindElement(i)->name << " has a valence of " << (int)elemente->FindElement(i)->Valence << " and there are " << ElementsInMolecule[i] << " of it." << endl;
 | 
|---|
| [cee0b57] | 875 |       configuration.MaxPsiDouble += ElementsInMolecule[i]*((int)elemente->FindElement(i)->Valence);
 | 
|---|
| [042f82] | 876 |     }
 | 
|---|
 | 877 |   }
 | 
|---|
| [cee0b57] | 878 |   configuration.PsiMaxNoDown = configuration.MaxPsiDouble/2 + (configuration.MaxPsiDouble % 2);
 | 
|---|
 | 879 |   configuration.PsiMaxNoUp = configuration.MaxPsiDouble/2;
 | 
|---|
 | 880 |   configuration.MaxPsiDouble /= 2;
 | 
|---|
 | 881 |   configuration.PsiType = (configuration.PsiMaxNoDown == configuration.PsiMaxNoUp) ? 0 : 1;
 | 
|---|
 | 882 |   if ((configuration.PsiType == 1) && (configuration.ProcPEPsi < 2)) {
 | 
|---|
 | 883 |     configuration.ProcPEGamma /= 2;
 | 
|---|
 | 884 |     configuration.ProcPEPsi *= 2;
 | 
|---|
 | 885 |   } else {
 | 
|---|
 | 886 |     configuration.ProcPEGamma *= configuration.ProcPEPsi;
 | 
|---|
 | 887 |     configuration.ProcPEPsi = 1;
 | 
|---|
 | 888 |   }
 | 
|---|
 | 889 |   configuration.InitMaxMinStopStep = configuration.MaxMinStopStep = configuration.MaxPsiDouble;
 | 
|---|
| [14de469] | 890 | };
 | 
|---|
 | 891 | 
 | 
|---|
 | 892 | /** Determines whether two molecules actually contain the same atoms and coordination.
 | 
|---|
 | 893 |  * \param *out output stream for debugging
 | 
|---|
 | 894 |  * \param *OtherMolecule the molecule to compare this one to
 | 
|---|
 | 895 |  * \param threshold upper limit of difference when comparing the coordination.
 | 
|---|
 | 896 |  * \return NULL - not equal, otherwise an allocated (molecule::AtomCount) permutation map of the atom numbers (which corresponds to which)
 | 
|---|
 | 897 |  */
 | 
|---|
| [e138de] | 898 | int * molecule::IsEqualToWithinThreshold(molecule *OtherMolecule, double threshold)
 | 
|---|
| [14de469] | 899 | {
 | 
|---|
| [042f82] | 900 |   int flag;
 | 
|---|
 | 901 |   double *Distances = NULL, *OtherDistances = NULL;
 | 
|---|
 | 902 |   Vector CenterOfGravity, OtherCenterOfGravity;
 | 
|---|
 | 903 |   size_t *PermMap = NULL, *OtherPermMap = NULL;
 | 
|---|
 | 904 |   int *PermutationMap = NULL;
 | 
|---|
 | 905 |   bool result = true; // status of comparison
 | 
|---|
 | 906 | 
 | 
|---|
| [e138de] | 907 |   Log() << Verbose(3) << "Begin of IsEqualToWithinThreshold." << endl;
 | 
|---|
| [042f82] | 908 |   /// first count both their atoms and elements and update lists thereby ...
 | 
|---|
| [e138de] | 909 |   //Log() << Verbose(0) << "Counting atoms, updating list" << endl;
 | 
|---|
 | 910 |   CountAtoms();
 | 
|---|
 | 911 |   OtherMolecule->CountAtoms();
 | 
|---|
| [042f82] | 912 |   CountElements();
 | 
|---|
 | 913 |   OtherMolecule->CountElements();
 | 
|---|
 | 914 | 
 | 
|---|
 | 915 |   /// ... and compare:
 | 
|---|
 | 916 |   /// -# AtomCount
 | 
|---|
 | 917 |   if (result) {
 | 
|---|
 | 918 |     if (AtomCount != OtherMolecule->AtomCount) {
 | 
|---|
| [e138de] | 919 |       Log() << Verbose(4) << "AtomCounts don't match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;
 | 
|---|
| [042f82] | 920 |       result = false;
 | 
|---|
| [e138de] | 921 |     } else Log() << Verbose(4) << "AtomCounts match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;
 | 
|---|
| [042f82] | 922 |   }
 | 
|---|
 | 923 |   /// -# ElementCount
 | 
|---|
 | 924 |   if (result) {
 | 
|---|
 | 925 |     if (ElementCount != OtherMolecule->ElementCount) {
 | 
|---|
| [e138de] | 926 |       Log() << Verbose(4) << "ElementCount don't match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl;
 | 
|---|
| [042f82] | 927 |       result = false;
 | 
|---|
| [e138de] | 928 |     } else Log() << Verbose(4) << "ElementCount match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl;
 | 
|---|
| [042f82] | 929 |   }
 | 
|---|
 | 930 |   /// -# ElementsInMolecule
 | 
|---|
 | 931 |   if (result) {
 | 
|---|
 | 932 |     for (flag=MAX_ELEMENTS;flag--;) {
 | 
|---|
| [e138de] | 933 |       //Log() << Verbose(5) << "Element " <<  flag << ": " << ElementsInMolecule[flag] << " <-> " << OtherMolecule->ElementsInMolecule[flag] << "." << endl;
 | 
|---|
| [042f82] | 934 |       if (ElementsInMolecule[flag] != OtherMolecule->ElementsInMolecule[flag])
 | 
|---|
 | 935 |         break;
 | 
|---|
 | 936 |     }
 | 
|---|
 | 937 |     if (flag < MAX_ELEMENTS) {
 | 
|---|
| [e138de] | 938 |       Log() << Verbose(4) << "ElementsInMolecule don't match." << endl;
 | 
|---|
| [042f82] | 939 |       result = false;
 | 
|---|
| [e138de] | 940 |     } else Log() << Verbose(4) << "ElementsInMolecule match." << endl;
 | 
|---|
| [042f82] | 941 |   }
 | 
|---|
 | 942 |   /// then determine and compare center of gravity for each molecule ...
 | 
|---|
 | 943 |   if (result) {
 | 
|---|
| [e138de] | 944 |     Log() << Verbose(5) << "Calculating Centers of Gravity" << endl;
 | 
|---|
| [437922] | 945 |     DeterminePeriodicCenter(CenterOfGravity);
 | 
|---|
 | 946 |     OtherMolecule->DeterminePeriodicCenter(OtherCenterOfGravity);
 | 
|---|
| [e138de] | 947 |     Log() << Verbose(5) << "Center of Gravity: ";
 | 
|---|
 | 948 |     CenterOfGravity.Output();
 | 
|---|
 | 949 |     Log() << Verbose(0) << endl << Verbose(5) << "Other Center of Gravity: ";
 | 
|---|
 | 950 |     OtherCenterOfGravity.Output();
 | 
|---|
 | 951 |     Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 952 |     if (CenterOfGravity.DistanceSquared(&OtherCenterOfGravity) > threshold*threshold) {
 | 
|---|
| [e138de] | 953 |       Log() << Verbose(4) << "Centers of gravity don't match." << endl;
 | 
|---|
| [042f82] | 954 |       result = false;
 | 
|---|
 | 955 |     }
 | 
|---|
 | 956 |   }
 | 
|---|
 | 957 | 
 | 
|---|
 | 958 |   /// ... then make a list with the euclidian distance to this center for each atom of both molecules
 | 
|---|
 | 959 |   if (result) {
 | 
|---|
| [e138de] | 960 |     Log() << Verbose(5) << "Calculating distances" << endl;
 | 
|---|
| [7218f8] | 961 |     Distances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: Distances");
 | 
|---|
 | 962 |     OtherDistances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: OtherDistances");
 | 
|---|
| [b453f9] | 963 |     SetIndexedArrayForEachAtomTo ( Distances, &atom::nr, &atom::DistanceSquaredToVector, (const Vector &)CenterOfGravity);
 | 
|---|
 | 964 |     SetIndexedArrayForEachAtomTo ( OtherDistances, &atom::nr, &atom::DistanceSquaredToVector, (const Vector &)CenterOfGravity);
 | 
|---|
| [042f82] | 965 | 
 | 
|---|
 | 966 |     /// ... sort each list (using heapsort (o(N log N)) from GSL)
 | 
|---|
| [e138de] | 967 |     Log() << Verbose(5) << "Sorting distances" << endl;
 | 
|---|
| [7218f8] | 968 |     PermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermMap");
 | 
|---|
 | 969 |     OtherPermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *OtherPermMap");
 | 
|---|
| [042f82] | 970 |     gsl_heapsort_index (PermMap, Distances, AtomCount, sizeof(double), CompareDoubles);
 | 
|---|
 | 971 |     gsl_heapsort_index (OtherPermMap, OtherDistances, AtomCount, sizeof(double), CompareDoubles);
 | 
|---|
| [7218f8] | 972 |     PermutationMap = Calloc<int>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermutationMap");
 | 
|---|
| [e138de] | 973 |     Log() << Verbose(5) << "Combining Permutation Maps" << endl;
 | 
|---|
| [042f82] | 974 |     for(int i=AtomCount;i--;)
 | 
|---|
 | 975 |       PermutationMap[PermMap[i]] = (int) OtherPermMap[i];
 | 
|---|
 | 976 | 
 | 
|---|
| [29812d] | 977 |     /// ... and compare them step by step, whether the difference is individually(!) below \a threshold for all
 | 
|---|
| [e138de] | 978 |     Log() << Verbose(4) << "Comparing distances" << endl;
 | 
|---|
| [042f82] | 979 |     flag = 0;
 | 
|---|
 | 980 |     for (int i=0;i<AtomCount;i++) {
 | 
|---|
| [e138de] | 981 |       Log() << Verbose(5) << "Distances squared: |" << Distances[PermMap[i]] << " - " << OtherDistances[OtherPermMap[i]] << "| = " << fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) << " ?<? " <<  threshold << endl;
 | 
|---|
| [042f82] | 982 |       if (fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) > threshold*threshold)
 | 
|---|
 | 983 |         flag = 1;
 | 
|---|
 | 984 |     }
 | 
|---|
 | 985 | 
 | 
|---|
| [29812d] | 986 |     // free memory
 | 
|---|
 | 987 |     Free(&PermMap);
 | 
|---|
 | 988 |     Free(&OtherPermMap);
 | 
|---|
 | 989 |     Free(&Distances);
 | 
|---|
 | 990 |     Free(&OtherDistances);
 | 
|---|
| [042f82] | 991 |     if (flag) { // if not equal
 | 
|---|
| [29812d] | 992 |       Free(&PermutationMap);
 | 
|---|
| [042f82] | 993 |       result = false;
 | 
|---|
 | 994 |     }
 | 
|---|
 | 995 |   }
 | 
|---|
 | 996 |   /// return pointer to map if all distances were below \a threshold
 | 
|---|
| [e138de] | 997 |   Log() << Verbose(3) << "End of IsEqualToWithinThreshold." << endl;
 | 
|---|
| [042f82] | 998 |   if (result) {
 | 
|---|
| [e138de] | 999 |     Log() << Verbose(3) << "Result: Equal." << endl;
 | 
|---|
| [042f82] | 1000 |     return PermutationMap;
 | 
|---|
 | 1001 |   } else {
 | 
|---|
| [e138de] | 1002 |     Log() << Verbose(3) << "Result: Not equal." << endl;
 | 
|---|
| [042f82] | 1003 |     return NULL;
 | 
|---|
 | 1004 |   }
 | 
|---|
| [14de469] | 1005 | };
 | 
|---|
 | 1006 | 
 | 
|---|
 | 1007 | /** Returns an index map for two father-son-molecules.
 | 
|---|
 | 1008 |  * The map tells which atom in this molecule corresponds to which one in the other molecul with their fathers.
 | 
|---|
 | 1009 |  * \param *out output stream for debugging
 | 
|---|
 | 1010 |  * \param *OtherMolecule corresponding molecule with fathers
 | 
|---|
 | 1011 |  * \return allocated map of size molecule::AtomCount with map
 | 
|---|
 | 1012 |  * \todo make this with a good sort O(n), not O(n^2)
 | 
|---|
 | 1013 |  */
 | 
|---|
| [e138de] | 1014 | int * molecule::GetFatherSonAtomicMap(molecule *OtherMolecule)
 | 
|---|
| [14de469] | 1015 | {
 | 
|---|
| [042f82] | 1016 |   atom *Walker = NULL, *OtherWalker = NULL;
 | 
|---|
| [e138de] | 1017 |   Log() << Verbose(3) << "Begin of GetFatherAtomicMap." << endl;
 | 
|---|
| [29812d] | 1018 |   int *AtomicMap = Malloc<int>(AtomCount, "molecule::GetAtomicMap: *AtomicMap");
 | 
|---|
| [042f82] | 1019 |   for (int i=AtomCount;i--;)
 | 
|---|
 | 1020 |     AtomicMap[i] = -1;
 | 
|---|
 | 1021 |   if (OtherMolecule == this) {  // same molecule
 | 
|---|
 | 1022 |     for (int i=AtomCount;i--;) // no need as -1 means already that there is trivial correspondence
 | 
|---|
 | 1023 |       AtomicMap[i] = i;
 | 
|---|
| [e138de] | 1024 |     Log() << Verbose(4) << "Map is trivial." << endl;
 | 
|---|
| [042f82] | 1025 |   } else {
 | 
|---|
| [e138de] | 1026 |     Log() << Verbose(4) << "Map is ";
 | 
|---|
| [042f82] | 1027 |     Walker = start;
 | 
|---|
 | 1028 |     while (Walker->next != end) {
 | 
|---|
 | 1029 |       Walker = Walker->next;
 | 
|---|
 | 1030 |       if (Walker->father == NULL) {
 | 
|---|
 | 1031 |         AtomicMap[Walker->nr] = -2;
 | 
|---|
 | 1032 |       } else {
 | 
|---|
 | 1033 |         OtherWalker = OtherMolecule->start;
 | 
|---|
 | 1034 |         while (OtherWalker->next != OtherMolecule->end) {
 | 
|---|
 | 1035 |           OtherWalker = OtherWalker->next;
 | 
|---|
 | 1036 |       //for (int i=0;i<AtomCount;i++) { // search atom
 | 
|---|
 | 1037 |         //for (int j=0;j<OtherMolecule->AtomCount;j++) {
 | 
|---|
| [e138de] | 1038 |           //Log() << Verbose(4) << "Comparing father " << Walker->father << " with the other one " << OtherWalker->father << "." << endl;
 | 
|---|
| [042f82] | 1039 |           if (Walker->father == OtherWalker)
 | 
|---|
 | 1040 |             AtomicMap[Walker->nr] = OtherWalker->nr;
 | 
|---|
 | 1041 |         }
 | 
|---|
 | 1042 |       }
 | 
|---|
| [e138de] | 1043 |       Log() << Verbose(0) << AtomicMap[Walker->nr] << "\t";
 | 
|---|
| [042f82] | 1044 |     }
 | 
|---|
| [e138de] | 1045 |     Log() << Verbose(0) << endl;
 | 
|---|
| [042f82] | 1046 |   }
 | 
|---|
| [e138de] | 1047 |   Log() << Verbose(3) << "End of GetFatherAtomicMap." << endl;
 | 
|---|
| [042f82] | 1048 |   return AtomicMap;
 | 
|---|
| [14de469] | 1049 | };
 | 
|---|
 | 1050 | 
 | 
|---|
| [698b04] | 1051 | /** Stores the temperature evaluated from velocities in molecule::Trajectories.
 | 
|---|
 | 1052 |  * We simply use the formula equivaleting temperature and kinetic energy:
 | 
|---|
 | 1053 |  * \f$k_B T = \sum_i m_i v_i^2\f$
 | 
|---|
| [e138de] | 1054 |  * \param *output output stream of temperature file
 | 
|---|
| [698b04] | 1055 |  * \param startstep first MD step in molecule::Trajectories
 | 
|---|
 | 1056 |  * \param endstep last plus one MD step in molecule::Trajectories
 | 
|---|
 | 1057 |  * \return file written (true), failure on writing file (false)
 | 
|---|
| [69eb71] | 1058 |  */
 | 
|---|
| [e138de] | 1059 | bool molecule::OutputTemperatureFromTrajectories(ofstream * const output, int startstep, int endstep)
 | 
|---|
| [698b04] | 1060 | {
 | 
|---|
| [042f82] | 1061 |   double temperature;
 | 
|---|
 | 1062 |   // test stream
 | 
|---|
 | 1063 |   if (output == NULL)
 | 
|---|
 | 1064 |     return false;
 | 
|---|
 | 1065 |   else
 | 
|---|
 | 1066 |     *output << "# Step Temperature [K] Temperature [a.u.]" << endl;
 | 
|---|
 | 1067 |   for (int step=startstep;step < endstep; step++) { // loop over all time steps
 | 
|---|
 | 1068 |     temperature = 0.;
 | 
|---|
| [4455f4] | 1069 |     ActOnAllAtoms( &TrajectoryParticle::AddKineticToTemperature, &temperature, step);
 | 
|---|
| [042f82] | 1070 |     *output << step << "\t" << temperature*AtomicEnergyToKelvin << "\t" << temperature << endl;
 | 
|---|
 | 1071 |   }
 | 
|---|
 | 1072 |   return true;
 | 
|---|
| [65de9b] | 1073 | };
 | 
|---|
| [4a7776a] | 1074 | 
 | 
|---|
| [b453f9] | 1075 | void molecule::SetIndexedArrayForEachAtomTo ( atom **array, int ParticleInfo::*index) const
 | 
|---|
| [4a7776a] | 1076 | {
 | 
|---|
 | 1077 |   atom *Walker = start;
 | 
|---|
 | 1078 |   while (Walker->next != end) {
 | 
|---|
 | 1079 |     Walker = Walker->next;
 | 
|---|
 | 1080 |     array[(Walker->*index)] = Walker;
 | 
|---|
 | 1081 |   }
 | 
|---|
 | 1082 | };
 | 
|---|
| [c68025] | 1083 | 
 | 
|---|
 | 1084 | void molecule::flipActiveFlag(){
 | 
|---|
 | 1085 |   ActiveFlag = !ActiveFlag;
 | 
|---|
 | 1086 | }
 | 
|---|