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