| 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 |
|
|---|
| 8 | /*
|
|---|
| 9 | * molecule_geometry.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Oct 5, 2009
|
|---|
| 12 | * Author: heber
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include "Helpers/helpers.hpp"
|
|---|
| 23 | #include "CodePatterns/Log.hpp"
|
|---|
| 24 | #include "CodePatterns/Verbose.hpp"
|
|---|
| 25 | #include "LinearAlgebra/Line.hpp"
|
|---|
| 26 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
|---|
| 27 | #include "LinearAlgebra/Plane.hpp"
|
|---|
| 28 |
|
|---|
| 29 | #include "atom.hpp"
|
|---|
| 30 | #include "bond.hpp"
|
|---|
| 31 | #include "bondgraph.hpp"
|
|---|
| 32 | #include "config.hpp"
|
|---|
| 33 | #include "element.hpp"
|
|---|
| 34 | #include "LinearAlgebra/leastsquaremin.hpp"
|
|---|
| 35 | #include "molecule.hpp"
|
|---|
| 36 | #include "World.hpp"
|
|---|
| 37 |
|
|---|
| 38 | #include "Box.hpp"
|
|---|
| 39 |
|
|---|
| 40 | #include <boost/foreach.hpp>
|
|---|
| 41 |
|
|---|
| 42 | #include <gsl/gsl_eigen.h>
|
|---|
| 43 | #include <gsl/gsl_multimin.h>
|
|---|
| 44 |
|
|---|
| 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 | */
|
|---|
| 52 | bool molecule::CenterInBox()
|
|---|
| 53 | {
|
|---|
| 54 | bool status = true;
|
|---|
| 55 | const Vector *Center = DetermineCenterOfAll();
|
|---|
| 56 | const Vector *CenterBox = DetermineCenterOfBox();
|
|---|
| 57 | Box &domain = World::getInstance().getDomain();
|
|---|
| 58 |
|
|---|
| 59 | // go through all atoms
|
|---|
| 60 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 61 | std::cout << "atom before is at " << *iter << std::endl;
|
|---|
| 62 | *iter -= *Center;
|
|---|
| 63 | *iter += *CenterBox;
|
|---|
| 64 | std::cout << "atom after is at " << *iter << std::endl;
|
|---|
| 65 | }
|
|---|
| 66 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
|---|
| 67 |
|
|---|
| 68 | delete(Center);
|
|---|
| 69 | delete(CenterBox);
|
|---|
| 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 | */
|
|---|
| 77 | bool molecule::BoundInBox()
|
|---|
| 78 | {
|
|---|
| 79 | bool status = true;
|
|---|
| 80 | Box &domain = World::getInstance().getDomain();
|
|---|
| 81 |
|
|---|
| 82 | // go through all atoms
|
|---|
| 83 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
|---|
| 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 | */
|
|---|
| 92 | void molecule::CenterEdge(Vector *max)
|
|---|
| 93 | {
|
|---|
| 94 | Vector *min = new Vector;
|
|---|
| 95 |
|
|---|
| 96 | // Log() << Verbose(3) << "Begin of CenterEdge." << endl;
|
|---|
| 97 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| 98 | if (iter != end()) { //list not empty?
|
|---|
| 99 | for (int i=NDIM;i--;) {
|
|---|
| 100 | max->at(i) = (*iter)->at(i);
|
|---|
| 101 | min->at(i) = (*iter)->at(i);
|
|---|
| 102 | }
|
|---|
| 103 | for (; iter != end(); ++iter) {// continue with second if present
|
|---|
| 104 | //(*iter)->Output(1,1,out);
|
|---|
| 105 | for (int i=NDIM;i--;) {
|
|---|
| 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);
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | // Log() << Verbose(4) << "Maximum is ";
|
|---|
| 111 | // max->Output(out);
|
|---|
| 112 | // Log() << Verbose(0) << ", Minimum is ";
|
|---|
| 113 | // min->Output(out);
|
|---|
| 114 | // Log() << Verbose(0) << endl;
|
|---|
| 115 | min->Scale(-1.);
|
|---|
| 116 | (*max) += (*min);
|
|---|
| 117 | Translate(min);
|
|---|
| 118 | }
|
|---|
| 119 | delete(min);
|
|---|
| 120 | // Log() << Verbose(3) << "End of CenterEdge." << endl;
|
|---|
| 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 | */
|
|---|
| 127 | void molecule::CenterOrigin()
|
|---|
| 128 | {
|
|---|
| 129 | int Num = 0;
|
|---|
| 130 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| 131 | Vector Center;
|
|---|
| 132 |
|
|---|
| 133 | Center.Zero();
|
|---|
| 134 | if (iter != end()) { //list not empty?
|
|---|
| 135 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| 136 | Num++;
|
|---|
| 137 | Center += (*iter)->getPosition();
|
|---|
| 138 | }
|
|---|
| 139 | Center.Scale(-1./(double)Num); // divide through total number (and sign for direction)
|
|---|
| 140 | Translate(&Center);
|
|---|
| 141 | }
|
|---|
| 142 | };
|
|---|
| 143 |
|
|---|
| 144 | /** Returns vector pointing to center of all atoms.
|
|---|
| 145 | * \return pointer to center of all vector
|
|---|
| 146 | */
|
|---|
| 147 | Vector * molecule::DetermineCenterOfAll() const
|
|---|
| 148 | {
|
|---|
| 149 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| 150 | Vector *a = new Vector();
|
|---|
| 151 | double Num = 0;
|
|---|
| 152 |
|
|---|
| 153 | a->Zero();
|
|---|
| 154 |
|
|---|
| 155 | if (iter != end()) { //list not empty?
|
|---|
| 156 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| 157 | Num++;
|
|---|
| 158 | (*a) += (*iter)->getPosition();
|
|---|
| 159 | }
|
|---|
| 160 | a->Scale(1./(double)Num); // divide through total mass (and sign for direction)
|
|---|
| 161 | }
|
|---|
| 162 | return a;
|
|---|
| 163 | };
|
|---|
| 164 |
|
|---|
| 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);
|
|---|
| 171 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
|
|---|
| 172 | (*a) *= M;
|
|---|
| 173 | return a;
|
|---|
| 174 | };
|
|---|
| 175 |
|
|---|
| 176 | /** Returns vector pointing to center of gravity.
|
|---|
| 177 | * \param *out output stream for debugging
|
|---|
| 178 | * \return pointer to center of gravity vector
|
|---|
| 179 | */
|
|---|
| 180 | Vector * molecule::DetermineCenterOfGravity() const
|
|---|
| 181 | {
|
|---|
| 182 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| 183 | Vector *a = new Vector();
|
|---|
| 184 | Vector tmp;
|
|---|
| 185 | double Num = 0;
|
|---|
| 186 |
|
|---|
| 187 | a->Zero();
|
|---|
| 188 |
|
|---|
| 189 | if (iter != end()) { //list not empty?
|
|---|
| 190 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| 191 | Num += (*iter)->getType()->getMass();
|
|---|
| 192 | tmp = (*iter)->getType()->getMass() * (*iter)->getPosition();
|
|---|
| 193 | (*a) += tmp;
|
|---|
| 194 | }
|
|---|
| 195 | a->Scale(1./Num); // divide through total mass
|
|---|
| 196 | }
|
|---|
| 197 | // Log() << Verbose(1) << "Resulting center of gravity: ";
|
|---|
| 198 | // a->Output(out);
|
|---|
| 199 | // Log() << Verbose(0) << endl;
|
|---|
| 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 | */
|
|---|
| 207 | void molecule::CenterPeriodic()
|
|---|
| 208 | {
|
|---|
| 209 | Vector NewCenter;
|
|---|
| 210 | DeterminePeriodicCenter(NewCenter);
|
|---|
| 211 | // go through all atoms
|
|---|
| 212 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 213 | *iter -= NewCenter;
|
|---|
| 214 | }
|
|---|
| 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 | */
|
|---|
| 222 | void molecule::CenterAtVector(Vector *newcenter)
|
|---|
| 223 | {
|
|---|
| 224 | // go through all atoms
|
|---|
| 225 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 226 | *iter -= *newcenter;
|
|---|
| 227 | }
|
|---|
| 228 | };
|
|---|
| 229 |
|
|---|
| 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 | }
|
|---|
| 307 |
|
|---|
| 308 | /** Scales all atoms by \a *factor.
|
|---|
| 309 | * \param *factor pointer to scaling factor
|
|---|
| 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)
|
|---|
| 315 | */
|
|---|
| 316 | void molecule::Scale(const double ** const factor)
|
|---|
| 317 | {
|
|---|
| 318 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| 319 | for (size_t j=0;j<(*iter)->getTrajectorySize();j++) {
|
|---|
| 320 | Vector temp = (*iter)->getPositionAtStep(j);
|
|---|
| 321 | temp.ScaleAll(*factor);
|
|---|
| 322 | (*iter)->setPositionAtStep(j,temp);
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 | };
|
|---|
| 326 |
|
|---|
| 327 | /** Translate all atoms by given vector.
|
|---|
| 328 | * \param trans[] translation vector.
|
|---|
| 329 | */
|
|---|
| 330 | void molecule::Translate(const Vector *trans)
|
|---|
| 331 | {
|
|---|
| 332 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| 333 | for (size_t j=0;j<(*iter)->getTrajectorySize();j++) {
|
|---|
| 334 | (*iter)->setPositionAtStep(j, (*iter)->getPositionAtStep(j) + (*trans));
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 | };
|
|---|
| 338 |
|
|---|
| 339 | /** Translate the molecule periodically in the box.
|
|---|
| 340 | * \param trans[] translation vector.
|
|---|
| 341 | * TODO treatment of trajectories missing
|
|---|
| 342 | */
|
|---|
| 343 | void molecule::TranslatePeriodically(const Vector *trans)
|
|---|
| 344 | {
|
|---|
| 345 | Box &domain = World::getInstance().getDomain();
|
|---|
| 346 |
|
|---|
| 347 | // go through all atoms
|
|---|
| 348 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 349 | *iter += *trans;
|
|---|
| 350 | }
|
|---|
| 351 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
|---|
| 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 | {
|
|---|
| 361 | OBSERVE;
|
|---|
| 362 | Plane p(*n,0);
|
|---|
| 363 | atoms.transformNodes(boost::bind(&Plane::mirrorVector,p,_1));
|
|---|
| 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 | {
|
|---|
| 371 | const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
|
|---|
| 372 | const RealSpaceMatrix &inversematrix = World::getInstance().getDomain().getM();
|
|---|
| 373 | double tmp;
|
|---|
| 374 | bool flag;
|
|---|
| 375 | Vector Testvector, Translationvector;
|
|---|
| 376 | Vector Center;
|
|---|
| 377 | BondGraph *BG = World::getInstance().getBondGraph();
|
|---|
| 378 |
|
|---|
| 379 | do {
|
|---|
| 380 | Center.Zero();
|
|---|
| 381 | flag = true;
|
|---|
| 382 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| 383 | #ifdef ADDHYDROGEN
|
|---|
| 384 | if ((*iter)->getType()->getAtomicNumber() != 1) {
|
|---|
| 385 | #endif
|
|---|
| 386 | Testvector = inversematrix * (*iter)->getPosition();
|
|---|
| 387 | Translationvector.Zero();
|
|---|
| 388 | const BondList& ListOfBonds = (*iter)->getListOfBonds();
|
|---|
| 389 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
|---|
| 390 | Runner != ListOfBonds.end();
|
|---|
| 391 | ++Runner) {
|
|---|
| 392 | if ((*iter)->getNr() < (*Runner)->GetOtherAtom((*iter))->getNr()) // otherwise we shift one to, the other fro and gain nothing
|
|---|
| 393 | for (int j=0;j<NDIM;j++) {
|
|---|
| 394 | tmp = (*iter)->at(j) - (*Runner)->GetOtherAtom(*iter)->at(j);
|
|---|
| 395 | double MinBondDistance;
|
|---|
| 396 | double MaxBondDistance;
|
|---|
| 397 | BG->getMinMaxDistance((*iter), (*Runner)->GetOtherAtom(*iter), MinBondDistance, MaxBondDistance, true);
|
|---|
| 398 | if (fabs(tmp) > MaxBondDistance) { // check against Min is not useful for components
|
|---|
| 399 | flag = false;
|
|---|
| 400 | DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
|
|---|
| 401 | if (tmp > 0)
|
|---|
| 402 | Translationvector[j] -= 1.;
|
|---|
| 403 | else
|
|---|
| 404 | Translationvector[j] += 1.;
|
|---|
| 405 | }
|
|---|
| 406 | }
|
|---|
| 407 | }
|
|---|
| 408 | Testvector += Translationvector;
|
|---|
| 409 | Testvector *= matrix;
|
|---|
| 410 | Center += Testvector;
|
|---|
| 411 | Log() << Verbose(1) << "vector is: " << Testvector << endl;
|
|---|
| 412 | #ifdef ADDHYDROGEN
|
|---|
| 413 | // now also change all hydrogens
|
|---|
| 414 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
|---|
| 415 | Runner != ListOfBonds.end();
|
|---|
| 416 | ++Runner) {
|
|---|
| 417 | if ((*Runner)->GetOtherAtom((*iter))->getType()->getAtomicNumber() == 1) {
|
|---|
| 418 | Testvector = inversematrix * (*Runner)->GetOtherAtom((*iter))->getPosition();
|
|---|
| 419 | Testvector += Translationvector;
|
|---|
| 420 | Testvector *= matrix;
|
|---|
| 421 | Center += Testvector;
|
|---|
| 422 | Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
|
|---|
| 423 | }
|
|---|
| 424 | }
|
|---|
| 425 | }
|
|---|
| 426 | #endif
|
|---|
| 427 | }
|
|---|
| 428 | } while (!flag);
|
|---|
| 429 |
|
|---|
| 430 | Center.Scale(1./static_cast<double>(getAtomCount()));
|
|---|
| 431 | CenterAtVector(&Center);
|
|---|
| 432 | };
|
|---|
| 433 |
|
|---|
| 434 | /** Align all atoms in such a manner that given vector \a *n is along z axis.
|
|---|
| 435 | * \param n[] alignment vector.
|
|---|
| 436 | */
|
|---|
| 437 | void molecule::Align(Vector *n)
|
|---|
| 438 | {
|
|---|
| 439 | double alpha, tmp;
|
|---|
| 440 | Vector z_axis;
|
|---|
| 441 | z_axis[0] = 0.;
|
|---|
| 442 | z_axis[1] = 0.;
|
|---|
| 443 | z_axis[2] = 1.;
|
|---|
| 444 |
|
|---|
| 445 | // rotate on z-x plane
|
|---|
| 446 | DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
|
|---|
| 447 | alpha = atan(-n->at(0)/n->at(2));
|
|---|
| 448 | DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
|
|---|
| 449 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| 450 | tmp = (*iter)->at(0);
|
|---|
| 451 | (*iter)->set(0, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
|
|---|
| 452 | (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
|
|---|
| 453 | for (int j=0;j<MDSteps;j++) {
|
|---|
| 454 | Vector temp;
|
|---|
| 455 | temp[0] = cos(alpha) * (*iter)->getPositionAtStep(j)[0] + sin(alpha) * (*iter)->getPositionAtStep(j)[2];
|
|---|
| 456 | temp[2] = -sin(alpha) * (*iter)->getPositionAtStep(j)[0] + cos(alpha) * (*iter)->getPositionAtStep(j)[2];
|
|---|
| 457 | (*iter)->setPositionAtStep(j,temp);
|
|---|
| 458 | }
|
|---|
| 459 | }
|
|---|
| 460 | // rotate n vector
|
|---|
| 461 | tmp = n->at(0);
|
|---|
| 462 | n->at(0) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
|---|
| 463 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
|---|
| 464 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
|
|---|
| 465 |
|
|---|
| 466 | // rotate on z-y plane
|
|---|
| 467 | alpha = atan(-n->at(1)/n->at(2));
|
|---|
| 468 | DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
|
|---|
| 469 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| 470 | tmp = (*iter)->at(1);
|
|---|
| 471 | (*iter)->set(1, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
|
|---|
| 472 | (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
|
|---|
| 473 | for (int j=0;j<MDSteps;j++) {
|
|---|
| 474 | Vector temp;
|
|---|
| 475 | temp[1] = cos(alpha) * (*iter)->getPositionAtStep(j)[1] + sin(alpha) * (*iter)->getPositionAtStep(j)[2];
|
|---|
| 476 | temp[2] = -sin(alpha) * (*iter)->getPositionAtStep(j)[1] + cos(alpha) * (*iter)->getPositionAtStep(j)[2];
|
|---|
| 477 | (*iter)->setPositionAtStep(j,temp);
|
|---|
| 478 | }
|
|---|
| 479 | }
|
|---|
| 480 | // rotate n vector (for consistency check)
|
|---|
| 481 | tmp = n->at(1);
|
|---|
| 482 | n->at(1) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
|---|
| 483 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
|
|---|
| 487 | DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
|
|---|
| 488 | };
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 | /** Calculates sum over least square distance to line hidden in \a *x.
|
|---|
| 492 | * \param *x offset and direction vector
|
|---|
| 493 | * \param *params pointer to lsq_params structure
|
|---|
| 494 | * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
|
|---|
| 495 | */
|
|---|
| 496 | double LeastSquareDistance (const gsl_vector * x, void * params)
|
|---|
| 497 | {
|
|---|
| 498 | double res = 0, t;
|
|---|
| 499 | Vector a,b,c,d;
|
|---|
| 500 | struct lsq_params *par = (struct lsq_params *)params;
|
|---|
| 501 |
|
|---|
| 502 | // initialize vectors
|
|---|
| 503 | a[0] = gsl_vector_get(x,0);
|
|---|
| 504 | a[1] = gsl_vector_get(x,1);
|
|---|
| 505 | a[2] = gsl_vector_get(x,2);
|
|---|
| 506 | b[0] = gsl_vector_get(x,3);
|
|---|
| 507 | b[1] = gsl_vector_get(x,4);
|
|---|
| 508 | b[2] = gsl_vector_get(x,5);
|
|---|
| 509 | // go through all atoms
|
|---|
| 510 | for (molecule::const_iterator iter = par->mol->begin(); iter != par->mol->end(); ++iter) {
|
|---|
| 511 | if ((*iter)->getType() == ((struct lsq_params *)params)->type) { // for specific type
|
|---|
| 512 | c = (*iter)->getPosition() - a;
|
|---|
| 513 | t = c.ScalarProduct(b); // get direction parameter
|
|---|
| 514 | d = t*b; // and create vector
|
|---|
| 515 | c -= d; // ... yielding distance vector
|
|---|
| 516 | res += d.ScalarProduct(d); // add squared distance
|
|---|
| 517 | }
|
|---|
| 518 | }
|
|---|
| 519 | return res;
|
|---|
| 520 | };
|
|---|
| 521 |
|
|---|
| 522 | /** By minimizing the least square distance gains alignment vector.
|
|---|
| 523 | * \bug this is not yet working properly it seems
|
|---|
| 524 | */
|
|---|
| 525 | void molecule::GetAlignvector(struct lsq_params * par) const
|
|---|
| 526 | {
|
|---|
| 527 | int np = 6;
|
|---|
| 528 |
|
|---|
| 529 | const gsl_multimin_fminimizer_type *T =
|
|---|
| 530 | gsl_multimin_fminimizer_nmsimplex;
|
|---|
| 531 | gsl_multimin_fminimizer *s = NULL;
|
|---|
| 532 | gsl_vector *ss;
|
|---|
| 533 | gsl_multimin_function minex_func;
|
|---|
| 534 |
|
|---|
| 535 | size_t iter = 0, i;
|
|---|
| 536 | int status;
|
|---|
| 537 | double size;
|
|---|
| 538 |
|
|---|
| 539 | /* Initial vertex size vector */
|
|---|
| 540 | ss = gsl_vector_alloc (np);
|
|---|
| 541 |
|
|---|
| 542 | /* Set all step sizes to 1 */
|
|---|
| 543 | gsl_vector_set_all (ss, 1.0);
|
|---|
| 544 |
|
|---|
| 545 | /* Starting point */
|
|---|
| 546 | par->x = gsl_vector_alloc (np);
|
|---|
| 547 | par->mol = this;
|
|---|
| 548 |
|
|---|
| 549 | gsl_vector_set (par->x, 0, 0.0); // offset
|
|---|
| 550 | gsl_vector_set (par->x, 1, 0.0);
|
|---|
| 551 | gsl_vector_set (par->x, 2, 0.0);
|
|---|
| 552 | gsl_vector_set (par->x, 3, 0.0); // direction
|
|---|
| 553 | gsl_vector_set (par->x, 4, 0.0);
|
|---|
| 554 | gsl_vector_set (par->x, 5, 1.0);
|
|---|
| 555 |
|
|---|
| 556 | /* Initialize method and iterate */
|
|---|
| 557 | minex_func.f = &LeastSquareDistance;
|
|---|
| 558 | minex_func.n = np;
|
|---|
| 559 | minex_func.params = (void *)par;
|
|---|
| 560 |
|
|---|
| 561 | s = gsl_multimin_fminimizer_alloc (T, np);
|
|---|
| 562 | gsl_multimin_fminimizer_set (s, &minex_func, par->x, ss);
|
|---|
| 563 |
|
|---|
| 564 | do
|
|---|
| 565 | {
|
|---|
| 566 | iter++;
|
|---|
| 567 | status = gsl_multimin_fminimizer_iterate(s);
|
|---|
| 568 |
|
|---|
| 569 | if (status)
|
|---|
| 570 | break;
|
|---|
| 571 |
|
|---|
| 572 | size = gsl_multimin_fminimizer_size (s);
|
|---|
| 573 | status = gsl_multimin_test_size (size, 1e-2);
|
|---|
| 574 |
|
|---|
| 575 | if (status == GSL_SUCCESS)
|
|---|
| 576 | {
|
|---|
| 577 | printf ("converged to minimum at\n");
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | printf ("%5d ", (int)iter);
|
|---|
| 581 | for (i = 0; i < (size_t)np; i++)
|
|---|
| 582 | {
|
|---|
| 583 | printf ("%10.3e ", gsl_vector_get (s->x, i));
|
|---|
| 584 | }
|
|---|
| 585 | printf ("f() = %7.3f size = %.3f\n", s->fval, size);
|
|---|
| 586 | }
|
|---|
| 587 | while (status == GSL_CONTINUE && iter < 100);
|
|---|
| 588 |
|
|---|
| 589 | for (i=0;i<(size_t)np;i++)
|
|---|
| 590 | gsl_vector_set(par->x, i, gsl_vector_get(s->x, i));
|
|---|
| 591 | //gsl_vector_free(par->x);
|
|---|
| 592 | gsl_vector_free(ss);
|
|---|
| 593 | gsl_multimin_fminimizer_free (s);
|
|---|
| 594 | };
|
|---|