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