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