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