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