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