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