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