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