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