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