| [cee0b57] | 1 | /*
 | 
|---|
 | 2 |  * molecule_geometry.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Oct 5, 2009
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [f66195] | 8 | #include "atom.hpp"
 | 
|---|
 | 9 | #include "bond.hpp"
 | 
|---|
| [cee0b57] | 10 | #include "config.hpp"
 | 
|---|
| [f66195] | 11 | #include "element.hpp"
 | 
|---|
 | 12 | #include "helpers.hpp"
 | 
|---|
 | 13 | #include "leastsquaremin.hpp"
 | 
|---|
| [e138de] | 14 | #include "log.hpp"
 | 
|---|
| [cee0b57] | 15 | #include "memoryallocator.hpp"
 | 
|---|
 | 16 | #include "molecule.hpp"
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | /************************************* Functions for class molecule *********************************/
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | /** Centers the molecule in the box whose lengths are defined by vector \a *BoxLengths.
 | 
|---|
 | 22 |  * \param *out output stream for debugging
 | 
|---|
 | 23 |  */
 | 
|---|
| [e138de] | 24 | bool molecule::CenterInBox()
 | 
|---|
| [cee0b57] | 25 | {
 | 
|---|
 | 26 |   bool status = true;
 | 
|---|
| [e138de] | 27 |   const Vector *Center = DetermineCenterOfAll();
 | 
|---|
| [cee0b57] | 28 |   double *M = ReturnFullMatrixforSymmetric(cell_size);
 | 
|---|
| [99593f] | 29 |   double *Minv = InverseMatrix(M);
 | 
|---|
| [cee0b57] | 30 | 
 | 
|---|
 | 31 |   // go through all atoms
 | 
|---|
| [273382] | 32 |   ActOnAllVectors( &Vector::SubtractVector, *Center);
 | 
|---|
| [cee0b57] | 33 |   ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
 | 
|---|
 | 34 | 
 | 
|---|
| [1614174] | 35 |   Free(&M);
 | 
|---|
 | 36 |   Free(&Minv);
 | 
|---|
| [cee0b57] | 37 |   delete(Center);
 | 
|---|
 | 38 |   return status;
 | 
|---|
 | 39 | };
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | /** Bounds the molecule in the box whose lengths are defined by vector \a *BoxLengths.
 | 
|---|
 | 43 |  * \param *out output stream for debugging
 | 
|---|
 | 44 |  */
 | 
|---|
| [e138de] | 45 | bool molecule::BoundInBox()
 | 
|---|
| [cee0b57] | 46 | {
 | 
|---|
 | 47 |   bool status = true;
 | 
|---|
 | 48 |   double *M = ReturnFullMatrixforSymmetric(cell_size);
 | 
|---|
| [99593f] | 49 |   double *Minv = InverseMatrix(M);
 | 
|---|
| [cee0b57] | 50 | 
 | 
|---|
 | 51 |   // go through all atoms
 | 
|---|
 | 52 |   ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
 | 
|---|
 | 53 | 
 | 
|---|
| [1614174] | 54 |   Free(&M);
 | 
|---|
 | 55 |   Free(&Minv);
 | 
|---|
| [cee0b57] | 56 |   return status;
 | 
|---|
 | 57 | };
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | /** Centers the edge of the atoms at (0,0,0).
 | 
|---|
 | 60 |  * \param *out output stream for debugging
 | 
|---|
 | 61 |  * \param *max coordinates of other edge, specifying box dimensions.
 | 
|---|
 | 62 |  */
 | 
|---|
| [e138de] | 63 | void molecule::CenterEdge(Vector *max)
 | 
|---|
| [cee0b57] | 64 | {
 | 
|---|
 | 65 |   Vector *min = new Vector;
 | 
|---|
 | 66 | 
 | 
|---|
| [e138de] | 67 | //  Log() << Verbose(3) << "Begin of CenterEdge." << endl;
 | 
|---|
| [cee0b57] | 68 |   atom *ptr = start->next;  // start at first in list
 | 
|---|
 | 69 |   if (ptr != end) {   //list not empty?
 | 
|---|
 | 70 |     for (int i=NDIM;i--;) {
 | 
|---|
| [0a4f7f] | 71 |       max->at(i) = ptr->x[i];
 | 
|---|
 | 72 |       min->at(i) = ptr->x[i];
 | 
|---|
| [cee0b57] | 73 |     }
 | 
|---|
 | 74 |     while (ptr->next != end) {  // continue with second if present
 | 
|---|
 | 75 |       ptr = ptr->next;
 | 
|---|
 | 76 |       //ptr->Output(1,1,out);
 | 
|---|
 | 77 |       for (int i=NDIM;i--;) {
 | 
|---|
| [0a4f7f] | 78 |         max->at(i) = (max->at(i) < ptr->x[i]) ? ptr->x[i] : max->at(i);
 | 
|---|
 | 79 |         min->at(i) = (min->at(i) > ptr->x[i]) ? ptr->x[i] : min->at(i);
 | 
|---|
| [cee0b57] | 80 |       }
 | 
|---|
 | 81 |     }
 | 
|---|
| [e138de] | 82 | //    Log() << Verbose(4) << "Maximum is ";
 | 
|---|
| [cee0b57] | 83 | //    max->Output(out);
 | 
|---|
| [e138de] | 84 | //    Log() << Verbose(0) << ", Minimum is ";
 | 
|---|
| [cee0b57] | 85 | //    min->Output(out);
 | 
|---|
| [e138de] | 86 | //    Log() << Verbose(0) << endl;
 | 
|---|
| [cee0b57] | 87 |     min->Scale(-1.);
 | 
|---|
| [273382] | 88 |     (*max) += (*min);
 | 
|---|
| [cee0b57] | 89 |     Translate(min);
 | 
|---|
 | 90 |     Center.Zero();
 | 
|---|
 | 91 |   }
 | 
|---|
 | 92 |   delete(min);
 | 
|---|
| [e138de] | 93 | //  Log() << Verbose(3) << "End of CenterEdge." << endl;
 | 
|---|
| [cee0b57] | 94 | };
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | /** Centers the center of the atoms at (0,0,0).
 | 
|---|
 | 97 |  * \param *out output stream for debugging
 | 
|---|
 | 98 |  * \param *center return vector for translation vector
 | 
|---|
 | 99 |  */
 | 
|---|
| [e138de] | 100 | void molecule::CenterOrigin()
 | 
|---|
| [cee0b57] | 101 | {
 | 
|---|
 | 102 |   int Num = 0;
 | 
|---|
| [3930eb] | 103 |   atom *ptr = start;  // start at first in list
 | 
|---|
| [cee0b57] | 104 | 
 | 
|---|
 | 105 |   Center.Zero();
 | 
|---|
 | 106 | 
 | 
|---|
| [3930eb] | 107 |   if (ptr->next != end) {   //list not empty?
 | 
|---|
| [cee0b57] | 108 |     while (ptr->next != end) {  // continue with second if present
 | 
|---|
 | 109 |       ptr = ptr->next;
 | 
|---|
 | 110 |       Num++;
 | 
|---|
| [273382] | 111 |       Center += ptr->x;
 | 
|---|
| [cee0b57] | 112 |     }
 | 
|---|
 | 113 |     Center.Scale(-1./Num); // divide through total number (and sign for direction)
 | 
|---|
 | 114 |     Translate(&Center);
 | 
|---|
 | 115 |     Center.Zero();
 | 
|---|
 | 116 |   }
 | 
|---|
 | 117 | };
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | /** Returns vector pointing to center of all atoms.
 | 
|---|
 | 120 |  * \return pointer to center of all vector
 | 
|---|
 | 121 |  */
 | 
|---|
| [e138de] | 122 | Vector * molecule::DetermineCenterOfAll() const
 | 
|---|
| [cee0b57] | 123 | {
 | 
|---|
 | 124 |   atom *ptr = start->next;  // start at first in list
 | 
|---|
 | 125 |   Vector *a = new Vector();
 | 
|---|
 | 126 |   Vector tmp;
 | 
|---|
 | 127 |   double Num = 0;
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |   a->Zero();
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 |   if (ptr != end) {   //list not empty?
 | 
|---|
 | 132 |     while (ptr->next != end) {  // continue with second if present
 | 
|---|
 | 133 |       ptr = ptr->next;
 | 
|---|
 | 134 |       Num += 1.;
 | 
|---|
| [273382] | 135 |       tmp = ptr->x;
 | 
|---|
 | 136 |       (*a) += tmp;
 | 
|---|
| [cee0b57] | 137 |     }
 | 
|---|
 | 138 |     a->Scale(1./Num); // divide through total mass (and sign for direction)
 | 
|---|
 | 139 |   }
 | 
|---|
 | 140 |   return a;
 | 
|---|
 | 141 | };
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | /** Returns vector pointing to center of gravity.
 | 
|---|
 | 144 |  * \param *out output stream for debugging
 | 
|---|
 | 145 |  * \return pointer to center of gravity vector
 | 
|---|
 | 146 |  */
 | 
|---|
| [e138de] | 147 | Vector * molecule::DetermineCenterOfGravity()
 | 
|---|
| [cee0b57] | 148 | {
 | 
|---|
 | 149 |   atom *ptr = start->next;  // start at first in list
 | 
|---|
 | 150 |   Vector *a = new Vector();
 | 
|---|
 | 151 |   Vector tmp;
 | 
|---|
 | 152 |   double Num = 0;
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 |   a->Zero();
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 |   if (ptr != end) {   //list not empty?
 | 
|---|
 | 157 |     while (ptr->next != end) {  // continue with second if present
 | 
|---|
 | 158 |       ptr = ptr->next;
 | 
|---|
 | 159 |       Num += ptr->type->mass;
 | 
|---|
| [273382] | 160 |       tmp = ptr->type->mass * ptr->x;
 | 
|---|
 | 161 |       (*a) += tmp;
 | 
|---|
| [cee0b57] | 162 |     }
 | 
|---|
 | 163 |     a->Scale(-1./Num); // divide through total mass (and sign for direction)
 | 
|---|
 | 164 |   }
 | 
|---|
| [e138de] | 165 | //  Log() << Verbose(1) << "Resulting center of gravity: ";
 | 
|---|
| [cee0b57] | 166 | //  a->Output(out);
 | 
|---|
| [e138de] | 167 | //  Log() << Verbose(0) << endl;
 | 
|---|
| [cee0b57] | 168 |   return a;
 | 
|---|
 | 169 | };
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 | /** Centers the center of gravity of the atoms at (0,0,0).
 | 
|---|
 | 172 |  * \param *out output stream for debugging
 | 
|---|
 | 173 |  * \param *center return vector for translation vector
 | 
|---|
 | 174 |  */
 | 
|---|
| [e138de] | 175 | void molecule::CenterPeriodic()
 | 
|---|
| [cee0b57] | 176 | {
 | 
|---|
 | 177 |   DeterminePeriodicCenter(Center);
 | 
|---|
 | 178 | };
 | 
|---|
 | 179 | 
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 | /** Centers the center of gravity of the atoms at (0,0,0).
 | 
|---|
 | 182 |  * \param *out output stream for debugging
 | 
|---|
 | 183 |  * \param *center return vector for translation vector
 | 
|---|
 | 184 |  */
 | 
|---|
| [e138de] | 185 | void molecule::CenterAtVector(Vector *newcenter)
 | 
|---|
| [cee0b57] | 186 | {
 | 
|---|
| [273382] | 187 |   Center = *newcenter;
 | 
|---|
| [cee0b57] | 188 | };
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 | /** Scales all atoms by \a *factor.
 | 
|---|
 | 192 |  * \param *factor pointer to scaling factor
 | 
|---|
| [1bd79e] | 193 |  *
 | 
|---|
 | 194 |  * TODO: Is this realy what is meant, i.e.
 | 
|---|
 | 195 |  * x=(x[0]*factor[0],x[1]*factor[1],x[2]*factor[2]) (current impl)
 | 
|---|
 | 196 |  * or rather
 | 
|---|
 | 197 |  * x=(**factor) * x (as suggested by comment)
 | 
|---|
| [cee0b57] | 198 |  */
 | 
|---|
| [776b64] | 199 | void molecule::Scale(const double ** const factor)
 | 
|---|
| [cee0b57] | 200 | {
 | 
|---|
 | 201 |   atom *ptr = start;
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |   while (ptr->next != end) {
 | 
|---|
 | 204 |     ptr = ptr->next;
 | 
|---|
 | 205 |     for (int j=0;j<MDSteps;j++)
 | 
|---|
| [1bd79e] | 206 |       ptr->Trajectory.R.at(j).ScaleAll(*factor);
 | 
|---|
 | 207 |     ptr->x.ScaleAll(*factor);
 | 
|---|
| [cee0b57] | 208 |   }
 | 
|---|
 | 209 | };
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 | /** Translate all atoms by given vector.
 | 
|---|
 | 212 |  * \param trans[] translation vector.
 | 
|---|
 | 213 |  */
 | 
|---|
 | 214 | void molecule::Translate(const Vector *trans)
 | 
|---|
 | 215 | {
 | 
|---|
 | 216 |   atom *ptr = start;
 | 
|---|
 | 217 | 
 | 
|---|
 | 218 |   while (ptr->next != end) {
 | 
|---|
 | 219 |     ptr = ptr->next;
 | 
|---|
 | 220 |     for (int j=0;j<MDSteps;j++)
 | 
|---|
| [1bd79e] | 221 |       ptr->Trajectory.R.at(j) += (*trans);
 | 
|---|
 | 222 |     ptr->x += (*trans);
 | 
|---|
| [cee0b57] | 223 |   }
 | 
|---|
 | 224 | };
 | 
|---|
 | 225 | 
 | 
|---|
 | 226 | /** Translate the molecule periodically in the box.
 | 
|---|
 | 227 |  * \param trans[] translation vector.
 | 
|---|
 | 228 |  * TODO treatment of trajetories missing
 | 
|---|
 | 229 |  */
 | 
|---|
 | 230 | void molecule::TranslatePeriodically(const Vector *trans)
 | 
|---|
 | 231 | {
 | 
|---|
 | 232 |   double *M = ReturnFullMatrixforSymmetric(cell_size);
 | 
|---|
| [99593f] | 233 |   double *Minv = InverseMatrix(M);
 | 
|---|
| [cee0b57] | 234 | 
 | 
|---|
 | 235 |   // go through all atoms
 | 
|---|
| [273382] | 236 |   ActOnAllVectors( &Vector::SubtractVector, *trans);
 | 
|---|
| [cee0b57] | 237 |   ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
 | 
|---|
 | 238 | 
 | 
|---|
| [1614174] | 239 |   Free(&M);
 | 
|---|
 | 240 |   Free(&Minv);
 | 
|---|
| [cee0b57] | 241 | };
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 | 
 | 
|---|
 | 244 | /** Mirrors all atoms against a given plane.
 | 
|---|
 | 245 |  * \param n[] normal vector of mirror plane.
 | 
|---|
 | 246 |  */
 | 
|---|
 | 247 | void molecule::Mirror(const Vector *n)
 | 
|---|
 | 248 | {
 | 
|---|
| [273382] | 249 |   ActOnAllVectors( &Vector::Mirror, *n );
 | 
|---|
| [cee0b57] | 250 | };
 | 
|---|
 | 251 | 
 | 
|---|
 | 252 | /** Determines center of molecule (yet not considering atom masses).
 | 
|---|
 | 253 |  * \param center reference to return vector
 | 
|---|
 | 254 |  */
 | 
|---|
 | 255 | void molecule::DeterminePeriodicCenter(Vector ¢er)
 | 
|---|
 | 256 | {
 | 
|---|
 | 257 |   atom *Walker = start;
 | 
|---|
 | 258 |   double *matrix = ReturnFullMatrixforSymmetric(cell_size);
 | 
|---|
| [1614174] | 259 |   double *inversematrix = InverseMatrix(cell_size);
 | 
|---|
| [cee0b57] | 260 |   double tmp;
 | 
|---|
 | 261 |   bool flag;
 | 
|---|
 | 262 |   Vector Testvector, Translationvector;
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 |   do {
 | 
|---|
 | 265 |     Center.Zero();
 | 
|---|
 | 266 |     flag = true;
 | 
|---|
 | 267 |     while (Walker->next != end) {
 | 
|---|
 | 268 |       Walker = Walker->next;
 | 
|---|
 | 269 | #ifdef ADDHYDROGEN
 | 
|---|
 | 270 |       if (Walker->type->Z != 1) {
 | 
|---|
 | 271 | #endif
 | 
|---|
| [273382] | 272 |         Testvector = Walker->x;
 | 
|---|
| [1614174] | 273 |         Testvector.MatrixMultiplication(inversematrix);
 | 
|---|
| [cee0b57] | 274 |         Translationvector.Zero();
 | 
|---|
| [266237] | 275 |         for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
 | 
|---|
 | 276 |          if (Walker->nr < (*Runner)->GetOtherAtom(Walker)->nr) // otherwise we shift one to, the other fro and gain nothing
 | 
|---|
| [cee0b57] | 277 |             for (int j=0;j<NDIM;j++) {
 | 
|---|
| [0a4f7f] | 278 |               tmp = Walker->x[j] - (*Runner)->GetOtherAtom(Walker)->x[j];
 | 
|---|
| [cee0b57] | 279 |               if ((fabs(tmp)) > BondDistance) {
 | 
|---|
 | 280 |                 flag = false;
 | 
|---|
| [e138de] | 281 |                 Log() << Verbose(0) << "Hit: atom " << Walker->Name << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl;
 | 
|---|
| [cee0b57] | 282 |                 if (tmp > 0)
 | 
|---|
| [0a4f7f] | 283 |                   Translationvector[j] -= 1.;
 | 
|---|
| [cee0b57] | 284 |                 else
 | 
|---|
| [0a4f7f] | 285 |                   Translationvector[j] += 1.;
 | 
|---|
| [cee0b57] | 286 |               }
 | 
|---|
 | 287 |             }
 | 
|---|
 | 288 |         }
 | 
|---|
| [273382] | 289 |         Testvector += Translationvector;
 | 
|---|
| [cee0b57] | 290 |         Testvector.MatrixMultiplication(matrix);
 | 
|---|
| [273382] | 291 |         Center += Testvector;
 | 
|---|
| [0a4f7f] | 292 |         Log() << Verbose(1) << "vector is: " << Testvector << endl;
 | 
|---|
| [cee0b57] | 293 | #ifdef ADDHYDROGEN
 | 
|---|
 | 294 |         // now also change all hydrogens
 | 
|---|
| [266237] | 295 |         for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
 | 
|---|
 | 296 |           if ((*Runner)->GetOtherAtom(Walker)->type->Z == 1) {
 | 
|---|
| [273382] | 297 |             Testvector = (*Runner)->GetOtherAtom(Walker)->x;
 | 
|---|
| [1614174] | 298 |             Testvector.MatrixMultiplication(inversematrix);
 | 
|---|
| [273382] | 299 |             Testvector += Translationvector;
 | 
|---|
| [cee0b57] | 300 |             Testvector.MatrixMultiplication(matrix);
 | 
|---|
| [273382] | 301 |             Center += Testvector;
 | 
|---|
| [0a4f7f] | 302 |             Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
 | 
|---|
| [cee0b57] | 303 |           }
 | 
|---|
 | 304 |         }
 | 
|---|
 | 305 |       }
 | 
|---|
 | 306 | #endif
 | 
|---|
 | 307 |     }
 | 
|---|
 | 308 |   } while (!flag);
 | 
|---|
 | 309 |   Free(&matrix);
 | 
|---|
| [1614174] | 310 |   Free(&inversematrix);
 | 
|---|
 | 311 | 
 | 
|---|
| [cee0b57] | 312 |   Center.Scale(1./(double)AtomCount);
 | 
|---|
 | 313 | };
 | 
|---|
 | 314 | 
 | 
|---|
 | 315 | /** Transforms/Rotates the given molecule into its principal axis system.
 | 
|---|
 | 316 |  * \param *out output stream for debugging
 | 
|---|
 | 317 |  * \param DoRotate whether to rotate (true) or only to determine the PAS.
 | 
|---|
 | 318 |  * TODO treatment of trajetories missing
 | 
|---|
 | 319 |  */
 | 
|---|
| [e138de] | 320 | void molecule::PrincipalAxisSystem(bool DoRotate)
 | 
|---|
| [cee0b57] | 321 | {
 | 
|---|
 | 322 |   atom *ptr = start;  // start at first in list
 | 
|---|
 | 323 |   double InertiaTensor[NDIM*NDIM];
 | 
|---|
| [e138de] | 324 |   Vector *CenterOfGravity = DetermineCenterOfGravity();
 | 
|---|
| [cee0b57] | 325 | 
 | 
|---|
| [e138de] | 326 |   CenterPeriodic();
 | 
|---|
| [cee0b57] | 327 | 
 | 
|---|
 | 328 |   // reset inertia tensor
 | 
|---|
 | 329 |   for(int i=0;i<NDIM*NDIM;i++)
 | 
|---|
 | 330 |     InertiaTensor[i] = 0.;
 | 
|---|
 | 331 | 
 | 
|---|
 | 332 |   // sum up inertia tensor
 | 
|---|
 | 333 |   while (ptr->next != end) {
 | 
|---|
 | 334 |     ptr = ptr->next;
 | 
|---|
| [273382] | 335 |     Vector x = ptr->x;
 | 
|---|
| [cee0b57] | 336 |     //x.SubtractVector(CenterOfGravity);
 | 
|---|
| [0a4f7f] | 337 |     InertiaTensor[0] += ptr->type->mass*(x[1]*x[1] + x[2]*x[2]);
 | 
|---|
 | 338 |     InertiaTensor[1] += ptr->type->mass*(-x[0]*x[1]);
 | 
|---|
 | 339 |     InertiaTensor[2] += ptr->type->mass*(-x[0]*x[2]);
 | 
|---|
 | 340 |     InertiaTensor[3] += ptr->type->mass*(-x[1]*x[0]);
 | 
|---|
 | 341 |     InertiaTensor[4] += ptr->type->mass*(x[0]*x[0] + x[2]*x[2]);
 | 
|---|
 | 342 |     InertiaTensor[5] += ptr->type->mass*(-x[1]*x[2]);
 | 
|---|
 | 343 |     InertiaTensor[6] += ptr->type->mass*(-x[2]*x[0]);
 | 
|---|
 | 344 |     InertiaTensor[7] += ptr->type->mass*(-x[2]*x[1]);
 | 
|---|
 | 345 |     InertiaTensor[8] += ptr->type->mass*(x[0]*x[0] + x[1]*x[1]);
 | 
|---|
| [cee0b57] | 346 |   }
 | 
|---|
 | 347 |   // print InertiaTensor for debugging
 | 
|---|
| [e138de] | 348 |   Log() << Verbose(0) << "The inertia tensor is:" << endl;
 | 
|---|
| [cee0b57] | 349 |   for(int i=0;i<NDIM;i++) {
 | 
|---|
 | 350 |     for(int j=0;j<NDIM;j++)
 | 
|---|
| [e138de] | 351 |       Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ";
 | 
|---|
 | 352 |     Log() << Verbose(0) << endl;
 | 
|---|
| [cee0b57] | 353 |   }
 | 
|---|
| [e138de] | 354 |   Log() << Verbose(0) << endl;
 | 
|---|
| [cee0b57] | 355 | 
 | 
|---|
 | 356 |   // diagonalize to determine principal axis system
 | 
|---|
 | 357 |   gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM);
 | 
|---|
 | 358 |   gsl_matrix_view m = gsl_matrix_view_array(InertiaTensor, NDIM, NDIM);
 | 
|---|
 | 359 |   gsl_vector *eval = gsl_vector_alloc(NDIM);
 | 
|---|
 | 360 |   gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM);
 | 
|---|
 | 361 |   gsl_eigen_symmv(&m.matrix, eval, evec, T);
 | 
|---|
 | 362 |   gsl_eigen_symmv_free(T);
 | 
|---|
 | 363 |   gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC);
 | 
|---|
 | 364 | 
 | 
|---|
 | 365 |   for(int i=0;i<NDIM;i++) {
 | 
|---|
| [e138de] | 366 |     Log() << Verbose(1) << "eigenvalue = " << gsl_vector_get(eval, i);
 | 
|---|
 | 367 |     Log() << Verbose(0) << ", eigenvector = (" << evec->data[i * evec->tda + 0] << "," << evec->data[i * evec->tda + 1] << "," << evec->data[i * evec->tda + 2] << ")" << endl;
 | 
|---|
| [cee0b57] | 368 |   }
 | 
|---|
 | 369 | 
 | 
|---|
 | 370 |   // check whether we rotate or not
 | 
|---|
 | 371 |   if (DoRotate) {
 | 
|---|
| [e138de] | 372 |     Log() << Verbose(1) << "Transforming molecule into PAS ... ";
 | 
|---|
| [cee0b57] | 373 |     // the eigenvectors specify the transformation matrix
 | 
|---|
 | 374 |     ActOnAllVectors( &Vector::MatrixMultiplication, (const double *) evec->data );
 | 
|---|
| [e138de] | 375 |     Log() << Verbose(0) << "done." << endl;
 | 
|---|
| [cee0b57] | 376 | 
 | 
|---|
 | 377 |     // summing anew for debugging (resulting matrix has to be diagonal!)
 | 
|---|
 | 378 |     // reset inertia tensor
 | 
|---|
 | 379 |     for(int i=0;i<NDIM*NDIM;i++)
 | 
|---|
 | 380 |       InertiaTensor[i] = 0.;
 | 
|---|
 | 381 | 
 | 
|---|
 | 382 |     // sum up inertia tensor
 | 
|---|
 | 383 |     ptr = start;
 | 
|---|
 | 384 |     while (ptr->next != end) {
 | 
|---|
 | 385 |       ptr = ptr->next;
 | 
|---|
| [273382] | 386 |       Vector x = ptr->x;
 | 
|---|
| [cee0b57] | 387 |       //x.SubtractVector(CenterOfGravity);
 | 
|---|
| [0a4f7f] | 388 |       InertiaTensor[0] += ptr->type->mass*(x[1]*x[1] + x[2]*x[2]);
 | 
|---|
 | 389 |       InertiaTensor[1] += ptr->type->mass*(-x[0]*x[1]);
 | 
|---|
 | 390 |       InertiaTensor[2] += ptr->type->mass*(-x[0]*x[2]);
 | 
|---|
 | 391 |       InertiaTensor[3] += ptr->type->mass*(-x[1]*x[0]);
 | 
|---|
 | 392 |       InertiaTensor[4] += ptr->type->mass*(x[0]*x[0] + x[2]*x[2]);
 | 
|---|
 | 393 |       InertiaTensor[5] += ptr->type->mass*(-x[1]*x[2]);
 | 
|---|
 | 394 |       InertiaTensor[6] += ptr->type->mass*(-x[2]*x[0]);
 | 
|---|
 | 395 |       InertiaTensor[7] += ptr->type->mass*(-x[2]*x[1]);
 | 
|---|
 | 396 |       InertiaTensor[8] += ptr->type->mass*(x[0]*x[0] + x[1]*x[1]);
 | 
|---|
| [cee0b57] | 397 |     }
 | 
|---|
 | 398 |     // print InertiaTensor for debugging
 | 
|---|
| [e138de] | 399 |     Log() << Verbose(0) << "The inertia tensor is:" << endl;
 | 
|---|
| [cee0b57] | 400 |     for(int i=0;i<NDIM;i++) {
 | 
|---|
 | 401 |       for(int j=0;j<NDIM;j++)
 | 
|---|
| [e138de] | 402 |         Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ";
 | 
|---|
 | 403 |       Log() << Verbose(0) << endl;
 | 
|---|
| [cee0b57] | 404 |     }
 | 
|---|
| [e138de] | 405 |     Log() << Verbose(0) << endl;
 | 
|---|
| [cee0b57] | 406 |   }
 | 
|---|
 | 407 | 
 | 
|---|
 | 408 |   // free everything
 | 
|---|
 | 409 |   delete(CenterOfGravity);
 | 
|---|
 | 410 |   gsl_vector_free(eval);
 | 
|---|
 | 411 |   gsl_matrix_free(evec);
 | 
|---|
 | 412 | };
 | 
|---|
 | 413 | 
 | 
|---|
 | 414 | 
 | 
|---|
 | 415 | /** Align all atoms in such a manner that given vector \a *n is along z axis.
 | 
|---|
 | 416 |  * \param n[] alignment vector.
 | 
|---|
 | 417 |  */
 | 
|---|
 | 418 | void molecule::Align(Vector *n)
 | 
|---|
 | 419 | {
 | 
|---|
 | 420 |   atom *ptr = start;
 | 
|---|
 | 421 |   double alpha, tmp;
 | 
|---|
 | 422 |   Vector z_axis;
 | 
|---|
| [0a4f7f] | 423 |   z_axis[0] = 0.;
 | 
|---|
 | 424 |   z_axis[1] = 0.;
 | 
|---|
 | 425 |   z_axis[2] = 1.;
 | 
|---|
| [cee0b57] | 426 | 
 | 
|---|
 | 427 |   // rotate on z-x plane
 | 
|---|
| [e138de] | 428 |   Log() << Verbose(0) << "Begin of Aligning all atoms." << endl;
 | 
|---|
| [0a4f7f] | 429 |   alpha = atan(-n->at(0)/n->at(2));
 | 
|---|
| [e138de] | 430 |   Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ";
 | 
|---|
| [cee0b57] | 431 |   while (ptr->next != end) {
 | 
|---|
 | 432 |     ptr = ptr->next;
 | 
|---|
| [0a4f7f] | 433 |     tmp = ptr->x[0];
 | 
|---|
 | 434 |     ptr->x[0] =  cos(alpha) * tmp + sin(alpha) * ptr->x[2];
 | 
|---|
 | 435 |     ptr->x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->x[2];
 | 
|---|
| [cee0b57] | 436 |     for (int j=0;j<MDSteps;j++) {
 | 
|---|
| [0a4f7f] | 437 |       tmp = ptr->Trajectory.R.at(j)[0];
 | 
|---|
 | 438 |       ptr->Trajectory.R.at(j)[0] =  cos(alpha) * tmp + sin(alpha) * ptr->Trajectory.R.at(j)[2];
 | 
|---|
 | 439 |       ptr->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * ptr->Trajectory.R.at(j)[2];
 | 
|---|
| [cee0b57] | 440 |     }
 | 
|---|
 | 441 |   }
 | 
|---|
 | 442 |   // rotate n vector
 | 
|---|
| [0a4f7f] | 443 |   tmp = n->at(0);
 | 
|---|
 | 444 |   n->at(0) =  cos(alpha) * tmp +  sin(alpha) * n->at(2);
 | 
|---|
 | 445 |   n->at(2) = -sin(alpha) * tmp +  cos(alpha) * n->at(2);
 | 
|---|
 | 446 |   Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl;
 | 
|---|
| [cee0b57] | 447 | 
 | 
|---|
 | 448 |   // rotate on z-y plane
 | 
|---|
 | 449 |   ptr = start;
 | 
|---|
| [0a4f7f] | 450 |   alpha = atan(-n->at(1)/n->at(2));
 | 
|---|
| [e138de] | 451 |   Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ";
 | 
|---|
| [cee0b57] | 452 |   while (ptr->next != end) {
 | 
|---|
 | 453 |     ptr = ptr->next;
 | 
|---|
| [0a4f7f] | 454 |     tmp = ptr->x[1];
 | 
|---|
 | 455 |     ptr->x[1] =  cos(alpha) * tmp + sin(alpha) * ptr->x[2];
 | 
|---|
 | 456 |     ptr->x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->x[2];
 | 
|---|
| [cee0b57] | 457 |     for (int j=0;j<MDSteps;j++) {
 | 
|---|
| [0a4f7f] | 458 |       tmp = ptr->Trajectory.R.at(j)[1];
 | 
|---|
 | 459 |       ptr->Trajectory.R.at(j)[1] =  cos(alpha) * tmp + sin(alpha) * ptr->Trajectory.R.at(j)[2];
 | 
|---|
 | 460 |       ptr->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * ptr->Trajectory.R.at(j)[2];
 | 
|---|
| [cee0b57] | 461 |     }
 | 
|---|
 | 462 |   }
 | 
|---|
 | 463 |   // rotate n vector (for consistency check)
 | 
|---|
| [0a4f7f] | 464 |   tmp = n->at(1);
 | 
|---|
 | 465 |   n->at(1) =  cos(alpha) * tmp +  sin(alpha) * n->at(2);
 | 
|---|
 | 466 |   n->at(2) = -sin(alpha) * tmp +  cos(alpha) * n->at(2);
 | 
|---|
| [cee0b57] | 467 | 
 | 
|---|
| [0a4f7f] | 468 |   Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl;
 | 
|---|
| [e138de] | 469 |   Log() << Verbose(0) << "End of Aligning all atoms." << endl;
 | 
|---|
| [cee0b57] | 470 | };
 | 
|---|
 | 471 | 
 | 
|---|
 | 472 | 
 | 
|---|
 | 473 | /** Calculates sum over least square distance to line hidden in \a *x.
 | 
|---|
 | 474 |  * \param *x offset and direction vector
 | 
|---|
 | 475 |  * \param *params pointer to lsq_params structure
 | 
|---|
 | 476 |  * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
 | 
|---|
 | 477 |  */
 | 
|---|
 | 478 | double LeastSquareDistance (const gsl_vector * x, void * params)
 | 
|---|
 | 479 | {
 | 
|---|
 | 480 |   double res = 0, t;
 | 
|---|
 | 481 |   Vector a,b,c,d;
 | 
|---|
 | 482 |   struct lsq_params *par = (struct lsq_params *)params;
 | 
|---|
 | 483 |   atom *ptr = par->mol->start;
 | 
|---|
 | 484 | 
 | 
|---|
 | 485 |   // initialize vectors
 | 
|---|
| [0a4f7f] | 486 |   a[0] = gsl_vector_get(x,0);
 | 
|---|
 | 487 |   a[1] = gsl_vector_get(x,1);
 | 
|---|
 | 488 |   a[2] = gsl_vector_get(x,2);
 | 
|---|
 | 489 |   b[0] = gsl_vector_get(x,3);
 | 
|---|
 | 490 |   b[1] = gsl_vector_get(x,4);
 | 
|---|
 | 491 |   b[2] = gsl_vector_get(x,5);
 | 
|---|
| [cee0b57] | 492 |   // go through all atoms
 | 
|---|
 | 493 |   while (ptr != par->mol->end) {
 | 
|---|
 | 494 |     ptr = ptr->next;
 | 
|---|
 | 495 |     if (ptr->type == ((struct lsq_params *)params)->type) { // for specific type
 | 
|---|
| [273382] | 496 |       c = ptr->x - a;
 | 
|---|
 | 497 |       t = c.ScalarProduct(b);           // get direction parameter
 | 
|---|
 | 498 |       d = t*b;       // and create vector
 | 
|---|
 | 499 |       c -= d;   // ... yielding distance vector
 | 
|---|
 | 500 |       res += d.ScalarProduct(d);        // add squared distance
 | 
|---|
| [cee0b57] | 501 |     }
 | 
|---|
 | 502 |   }
 | 
|---|
 | 503 |   return res;
 | 
|---|
 | 504 | };
 | 
|---|
 | 505 | 
 | 
|---|
 | 506 | /** By minimizing the least square distance gains alignment vector.
 | 
|---|
 | 507 |  * \bug this is not yet working properly it seems
 | 
|---|
 | 508 |  */
 | 
|---|
 | 509 | void molecule::GetAlignvector(struct lsq_params * par) const
 | 
|---|
 | 510 | {
 | 
|---|
 | 511 |     int np = 6;
 | 
|---|
 | 512 | 
 | 
|---|
 | 513 |    const gsl_multimin_fminimizer_type *T =
 | 
|---|
 | 514 |      gsl_multimin_fminimizer_nmsimplex;
 | 
|---|
 | 515 |    gsl_multimin_fminimizer *s = NULL;
 | 
|---|
 | 516 |    gsl_vector *ss;
 | 
|---|
 | 517 |    gsl_multimin_function minex_func;
 | 
|---|
 | 518 | 
 | 
|---|
 | 519 |    size_t iter = 0, i;
 | 
|---|
 | 520 |    int status;
 | 
|---|
 | 521 |    double size;
 | 
|---|
 | 522 | 
 | 
|---|
 | 523 |    /* Initial vertex size vector */
 | 
|---|
 | 524 |    ss = gsl_vector_alloc (np);
 | 
|---|
 | 525 | 
 | 
|---|
 | 526 |    /* Set all step sizes to 1 */
 | 
|---|
 | 527 |    gsl_vector_set_all (ss, 1.0);
 | 
|---|
 | 528 | 
 | 
|---|
 | 529 |    /* Starting point */
 | 
|---|
 | 530 |    par->x = gsl_vector_alloc (np);
 | 
|---|
 | 531 |    par->mol = this;
 | 
|---|
 | 532 | 
 | 
|---|
 | 533 |    gsl_vector_set (par->x, 0, 0.0);  // offset
 | 
|---|
 | 534 |    gsl_vector_set (par->x, 1, 0.0);
 | 
|---|
 | 535 |    gsl_vector_set (par->x, 2, 0.0);
 | 
|---|
 | 536 |    gsl_vector_set (par->x, 3, 0.0);  // direction
 | 
|---|
 | 537 |    gsl_vector_set (par->x, 4, 0.0);
 | 
|---|
 | 538 |    gsl_vector_set (par->x, 5, 1.0);
 | 
|---|
 | 539 | 
 | 
|---|
 | 540 |    /* Initialize method and iterate */
 | 
|---|
 | 541 |    minex_func.f = &LeastSquareDistance;
 | 
|---|
 | 542 |    minex_func.n = np;
 | 
|---|
 | 543 |    minex_func.params = (void *)par;
 | 
|---|
 | 544 | 
 | 
|---|
 | 545 |    s = gsl_multimin_fminimizer_alloc (T, np);
 | 
|---|
 | 546 |    gsl_multimin_fminimizer_set (s, &minex_func, par->x, ss);
 | 
|---|
 | 547 | 
 | 
|---|
 | 548 |    do
 | 
|---|
 | 549 |      {
 | 
|---|
 | 550 |        iter++;
 | 
|---|
 | 551 |        status = gsl_multimin_fminimizer_iterate(s);
 | 
|---|
 | 552 | 
 | 
|---|
 | 553 |        if (status)
 | 
|---|
 | 554 |          break;
 | 
|---|
 | 555 | 
 | 
|---|
 | 556 |        size = gsl_multimin_fminimizer_size (s);
 | 
|---|
 | 557 |        status = gsl_multimin_test_size (size, 1e-2);
 | 
|---|
 | 558 | 
 | 
|---|
 | 559 |        if (status == GSL_SUCCESS)
 | 
|---|
 | 560 |          {
 | 
|---|
 | 561 |            printf ("converged to minimum at\n");
 | 
|---|
 | 562 |          }
 | 
|---|
 | 563 | 
 | 
|---|
 | 564 |        printf ("%5d ", (int)iter);
 | 
|---|
 | 565 |        for (i = 0; i < (size_t)np; i++)
 | 
|---|
 | 566 |          {
 | 
|---|
 | 567 |            printf ("%10.3e ", gsl_vector_get (s->x, i));
 | 
|---|
 | 568 |          }
 | 
|---|
 | 569 |        printf ("f() = %7.3f size = %.3f\n", s->fval, size);
 | 
|---|
 | 570 |      }
 | 
|---|
 | 571 |    while (status == GSL_CONTINUE && iter < 100);
 | 
|---|
 | 572 | 
 | 
|---|
 | 573 |   for (i=0;i<(size_t)np;i++)
 | 
|---|
 | 574 |     gsl_vector_set(par->x, i, gsl_vector_get(s->x, i));
 | 
|---|
 | 575 |    //gsl_vector_free(par->x);
 | 
|---|
 | 576 |    gsl_vector_free(ss);
 | 
|---|
 | 577 |    gsl_multimin_fminimizer_free (s);
 | 
|---|
 | 578 | };
 | 
|---|