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