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