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