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