[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[cee0b57] | 8 | /*
|
---|
| 9 | * molecule_geometry.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Oct 5, 2009
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
[aafd77] | 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
[bf3817] | 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[aafd77] | 21 |
|
---|
[6e5084] | 22 | #include "Helpers/helpers.hpp"
|
---|
[ad011c] | 23 | #include "CodePatterns/Log.hpp"
|
---|
| 24 | #include "CodePatterns/Verbose.hpp"
|
---|
[6e5084] | 25 | #include "LinearAlgebra/Line.hpp"
|
---|
[cca9ef] | 26 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[6e5084] | 27 | #include "LinearAlgebra/Plane.hpp"
|
---|
[112b09] | 28 |
|
---|
[f66195] | 29 | #include "atom.hpp"
|
---|
| 30 | #include "bond.hpp"
|
---|
[7adf0f] | 31 | #include "bondgraph.hpp"
|
---|
[cee0b57] | 32 | #include "config.hpp"
|
---|
[f66195] | 33 | #include "element.hpp"
|
---|
[13d150] | 34 | #include "LinearAlgebra/leastsquaremin.hpp"
|
---|
[cee0b57] | 35 | #include "molecule.hpp"
|
---|
[b34306] | 36 | #include "World.hpp"
|
---|
[6e5084] | 37 |
|
---|
[84c494] | 38 | #include "Box.hpp"
|
---|
[6e5084] | 39 |
|
---|
[76c0d6] | 40 | #include <boost/foreach.hpp>
|
---|
| 41 |
|
---|
[aafd77] | 42 | #include <gsl/gsl_eigen.h>
|
---|
| 43 | #include <gsl/gsl_multimin.h>
|
---|
| 44 |
|
---|
[cee0b57] | 45 |
|
---|
| 46 | /************************************* Functions for class molecule *********************************/
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | /** Centers the molecule in the box whose lengths are defined by vector \a *BoxLengths.
|
---|
| 50 | * \param *out output stream for debugging
|
---|
| 51 | */
|
---|
[e138de] | 52 | bool molecule::CenterInBox()
|
---|
[cee0b57] | 53 | {
|
---|
| 54 | bool status = true;
|
---|
[e138de] | 55 | const Vector *Center = DetermineCenterOfAll();
|
---|
[eddea2] | 56 | const Vector *CenterBox = DetermineCenterOfBox();
|
---|
[f429d7] | 57 | Box &domain = World::getInstance().getDomain();
|
---|
[cee0b57] | 58 |
|
---|
| 59 | // go through all atoms
|
---|
[d0f111] | 60 | BOOST_FOREACH(atom* iter, atoms){
|
---|
[6625c3] | 61 | std::cout << "atom before is at " << *iter << std::endl;
|
---|
[d74077] | 62 | *iter -= *Center;
|
---|
[6625c3] | 63 | *iter += *CenterBox;
|
---|
| 64 | std::cout << "atom after is at " << *iter << std::endl;
|
---|
[d0f111] | 65 | }
|
---|
[0632c5] | 66 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
---|
[cee0b57] | 67 |
|
---|
| 68 | delete(Center);
|
---|
[52d777] | 69 | delete(CenterBox);
|
---|
[cee0b57] | 70 | return status;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | /** Bounds the molecule in the box whose lengths are defined by vector \a *BoxLengths.
|
---|
| 75 | * \param *out output stream for debugging
|
---|
| 76 | */
|
---|
[e138de] | 77 | bool molecule::BoundInBox()
|
---|
[cee0b57] | 78 | {
|
---|
| 79 | bool status = true;
|
---|
[f429d7] | 80 | Box &domain = World::getInstance().getDomain();
|
---|
[cee0b57] | 81 |
|
---|
| 82 | // go through all atoms
|
---|
[0632c5] | 83 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
---|
[cee0b57] | 84 |
|
---|
| 85 | return status;
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | /** Centers the edge of the atoms at (0,0,0).
|
---|
| 89 | * \param *out output stream for debugging
|
---|
| 90 | * \param *max coordinates of other edge, specifying box dimensions.
|
---|
| 91 | */
|
---|
[e138de] | 92 | void molecule::CenterEdge(Vector *max)
|
---|
[cee0b57] | 93 | {
|
---|
| 94 | Vector *min = new Vector;
|
---|
| 95 |
|
---|
[e138de] | 96 | // Log() << Verbose(3) << "Begin of CenterEdge." << endl;
|
---|
[9879f6] | 97 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
| 98 | if (iter != end()) { //list not empty?
|
---|
[cee0b57] | 99 | for (int i=NDIM;i--;) {
|
---|
[d74077] | 100 | max->at(i) = (*iter)->at(i);
|
---|
| 101 | min->at(i) = (*iter)->at(i);
|
---|
[cee0b57] | 102 | }
|
---|
[9879f6] | 103 | for (; iter != end(); ++iter) {// continue with second if present
|
---|
| 104 | //(*iter)->Output(1,1,out);
|
---|
[cee0b57] | 105 | for (int i=NDIM;i--;) {
|
---|
[d74077] | 106 | max->at(i) = (max->at(i) < (*iter)->at(i)) ? (*iter)->at(i) : max->at(i);
|
---|
| 107 | min->at(i) = (min->at(i) > (*iter)->at(i)) ? (*iter)->at(i) : min->at(i);
|
---|
[cee0b57] | 108 | }
|
---|
| 109 | }
|
---|
[e138de] | 110 | // Log() << Verbose(4) << "Maximum is ";
|
---|
[cee0b57] | 111 | // max->Output(out);
|
---|
[e138de] | 112 | // Log() << Verbose(0) << ", Minimum is ";
|
---|
[cee0b57] | 113 | // min->Output(out);
|
---|
[e138de] | 114 | // Log() << Verbose(0) << endl;
|
---|
[cee0b57] | 115 | min->Scale(-1.);
|
---|
[273382] | 116 | (*max) += (*min);
|
---|
[cee0b57] | 117 | Translate(min);
|
---|
| 118 | }
|
---|
| 119 | delete(min);
|
---|
[e138de] | 120 | // Log() << Verbose(3) << "End of CenterEdge." << endl;
|
---|
[cee0b57] | 121 | };
|
---|
| 122 |
|
---|
| 123 | /** Centers the center of the atoms at (0,0,0).
|
---|
| 124 | * \param *out output stream for debugging
|
---|
| 125 | * \param *center return vector for translation vector
|
---|
| 126 | */
|
---|
[e138de] | 127 | void molecule::CenterOrigin()
|
---|
[cee0b57] | 128 | {
|
---|
| 129 | int Num = 0;
|
---|
[9879f6] | 130 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
[1883f9] | 131 | Vector Center;
|
---|
[cee0b57] | 132 |
|
---|
| 133 | Center.Zero();
|
---|
[9879f6] | 134 | if (iter != end()) { //list not empty?
|
---|
| 135 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
[cee0b57] | 136 | Num++;
|
---|
[d74077] | 137 | Center += (*iter)->getPosition();
|
---|
[cee0b57] | 138 | }
|
---|
[bdc91e] | 139 | Center.Scale(-1./(double)Num); // divide through total number (and sign for direction)
|
---|
[cee0b57] | 140 | Translate(&Center);
|
---|
| 141 | }
|
---|
| 142 | };
|
---|
| 143 |
|
---|
| 144 | /** Returns vector pointing to center of all atoms.
|
---|
| 145 | * \return pointer to center of all vector
|
---|
| 146 | */
|
---|
[e138de] | 147 | Vector * molecule::DetermineCenterOfAll() const
|
---|
[cee0b57] | 148 | {
|
---|
[9879f6] | 149 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
[cee0b57] | 150 | Vector *a = new Vector();
|
---|
| 151 | double Num = 0;
|
---|
| 152 |
|
---|
| 153 | a->Zero();
|
---|
| 154 |
|
---|
[9879f6] | 155 | if (iter != end()) { //list not empty?
|
---|
| 156 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
[15b670] | 157 | Num++;
|
---|
[d74077] | 158 | (*a) += (*iter)->getPosition();
|
---|
[cee0b57] | 159 | }
|
---|
[bdc91e] | 160 | a->Scale(1./(double)Num); // divide through total mass (and sign for direction)
|
---|
[cee0b57] | 161 | }
|
---|
| 162 | return a;
|
---|
| 163 | };
|
---|
| 164 |
|
---|
[eddea2] | 165 | /** Returns vector pointing to center of the domain.
|
---|
| 166 | * \return pointer to center of the domain
|
---|
| 167 | */
|
---|
| 168 | Vector * molecule::DetermineCenterOfBox() const
|
---|
| 169 | {
|
---|
| 170 | Vector *a = new Vector(0.5,0.5,0.5);
|
---|
[cca9ef] | 171 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
|
---|
[5108e1] | 172 | (*a) *= M;
|
---|
[eddea2] | 173 | return a;
|
---|
| 174 | };
|
---|
| 175 |
|
---|
[cee0b57] | 176 | /** Returns vector pointing to center of gravity.
|
---|
| 177 | * \param *out output stream for debugging
|
---|
| 178 | * \return pointer to center of gravity vector
|
---|
| 179 | */
|
---|
[4bb63c] | 180 | Vector * molecule::DetermineCenterOfGravity() const
|
---|
[cee0b57] | 181 | {
|
---|
[9879f6] | 182 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
[cee0b57] | 183 | Vector *a = new Vector();
|
---|
| 184 | Vector tmp;
|
---|
| 185 | double Num = 0;
|
---|
| 186 |
|
---|
| 187 | a->Zero();
|
---|
| 188 |
|
---|
[9879f6] | 189 | if (iter != end()) { //list not empty?
|
---|
| 190 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
[83f176] | 191 | Num += (*iter)->getType()->getMass();
|
---|
| 192 | tmp = (*iter)->getType()->getMass() * (*iter)->getPosition();
|
---|
[273382] | 193 | (*a) += tmp;
|
---|
[cee0b57] | 194 | }
|
---|
[bdc91e] | 195 | a->Scale(1./Num); // divide through total mass
|
---|
[cee0b57] | 196 | }
|
---|
[e138de] | 197 | // Log() << Verbose(1) << "Resulting center of gravity: ";
|
---|
[cee0b57] | 198 | // a->Output(out);
|
---|
[e138de] | 199 | // Log() << Verbose(0) << endl;
|
---|
[cee0b57] | 200 | return a;
|
---|
| 201 | };
|
---|
| 202 |
|
---|
| 203 | /** Centers the center of gravity of the atoms at (0,0,0).
|
---|
| 204 | * \param *out output stream for debugging
|
---|
| 205 | * \param *center return vector for translation vector
|
---|
| 206 | */
|
---|
[e138de] | 207 | void molecule::CenterPeriodic()
|
---|
[cee0b57] | 208 | {
|
---|
[1883f9] | 209 | Vector NewCenter;
|
---|
| 210 | DeterminePeriodicCenter(NewCenter);
|
---|
| 211 | // go through all atoms
|
---|
| 212 | BOOST_FOREACH(atom* iter, atoms){
|
---|
| 213 | *iter -= NewCenter;
|
---|
| 214 | }
|
---|
[cee0b57] | 215 | };
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | /** Centers the center of gravity of the atoms at (0,0,0).
|
---|
| 219 | * \param *out output stream for debugging
|
---|
| 220 | * \param *center return vector for translation vector
|
---|
| 221 | */
|
---|
[e138de] | 222 | void molecule::CenterAtVector(Vector *newcenter)
|
---|
[cee0b57] | 223 | {
|
---|
[1883f9] | 224 | // go through all atoms
|
---|
| 225 | BOOST_FOREACH(atom* iter, atoms){
|
---|
| 226 | *iter -= *newcenter;
|
---|
| 227 | }
|
---|
[cee0b57] | 228 | };
|
---|
| 229 |
|
---|
[1f91f4] | 230 | /** Calculate the inertia tensor of a the molecule.
|
---|
| 231 | *
|
---|
| 232 | * @return inertia tensor
|
---|
| 233 | */
|
---|
| 234 | RealSpaceMatrix molecule::getInertiaTensor() const
|
---|
| 235 | {
|
---|
| 236 | RealSpaceMatrix InertiaTensor;
|
---|
| 237 | Vector *CenterOfGravity = DetermineCenterOfGravity();
|
---|
| 238 |
|
---|
| 239 | // reset inertia tensor
|
---|
| 240 | InertiaTensor.setZero();
|
---|
| 241 |
|
---|
| 242 | // sum up inertia tensor
|
---|
| 243 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
| 244 | Vector x = (*iter)->getPosition();
|
---|
| 245 | x -= *CenterOfGravity;
|
---|
| 246 | const double mass = (*iter)->getType()->getMass();
|
---|
| 247 | InertiaTensor.at(0,0) += mass*(x[1]*x[1] + x[2]*x[2]);
|
---|
| 248 | InertiaTensor.at(0,1) += mass*(-x[0]*x[1]);
|
---|
| 249 | InertiaTensor.at(0,2) += mass*(-x[0]*x[2]);
|
---|
| 250 | InertiaTensor.at(1,0) += mass*(-x[1]*x[0]);
|
---|
| 251 | InertiaTensor.at(1,1) += mass*(x[0]*x[0] + x[2]*x[2]);
|
---|
| 252 | InertiaTensor.at(1,2) += mass*(-x[1]*x[2]);
|
---|
| 253 | InertiaTensor.at(2,0) += mass*(-x[2]*x[0]);
|
---|
| 254 | InertiaTensor.at(2,1) += mass*(-x[2]*x[1]);
|
---|
| 255 | InertiaTensor.at(2,2) += mass*(x[0]*x[0] + x[1]*x[1]);
|
---|
| 256 | }
|
---|
| 257 | // print InertiaTensor
|
---|
| 258 | DoLog(0) && (Log() << Verbose(0) << "The inertia tensor of molecule "
|
---|
| 259 | << getName() << " is:"
|
---|
| 260 | << InertiaTensor << endl);
|
---|
| 261 |
|
---|
| 262 | delete CenterOfGravity;
|
---|
| 263 | return InertiaTensor;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /** Rotates the molecule in such a way that biggest principal axis corresponds
|
---|
| 267 | * to given \a Axis.
|
---|
| 268 | *
|
---|
| 269 | * @param Axis Axis to align with biggest principal axis
|
---|
| 270 | */
|
---|
| 271 | void molecule::RotateToPrincipalAxisSystem(Vector &Axis)
|
---|
| 272 | {
|
---|
| 273 | Vector *CenterOfGravity = DetermineCenterOfGravity();
|
---|
| 274 | RealSpaceMatrix InertiaTensor = getInertiaTensor();
|
---|
| 275 |
|
---|
| 276 | // diagonalize to determine principal axis system
|
---|
| 277 | Vector Eigenvalues = InertiaTensor.transformToEigenbasis();
|
---|
| 278 |
|
---|
| 279 | for(int i=0;i<NDIM;i++)
|
---|
| 280 | DoLog(0) && (Log() << Verbose(0) << "eigenvalue = " << Eigenvalues[i] << ", eigenvector = " << InertiaTensor.column(i) << endl);
|
---|
| 281 |
|
---|
| 282 | DoLog(0) && (Log() << Verbose(0) << "Transforming to PAS ... ");
|
---|
| 283 |
|
---|
| 284 | // obtain first column, eigenvector to biggest eigenvalue
|
---|
| 285 | Vector BiggestEigenvector(InertiaTensor.column(Eigenvalues.SmallestComponent()));
|
---|
| 286 | Vector DesiredAxis(Axis);
|
---|
| 287 |
|
---|
| 288 | // Creation Line that is the rotation axis
|
---|
| 289 | DesiredAxis.VectorProduct(BiggestEigenvector);
|
---|
| 290 | Line RotationAxis(Vector(0.,0.,0.), DesiredAxis);
|
---|
| 291 |
|
---|
| 292 | // determine angle
|
---|
| 293 | const double alpha = BiggestEigenvector.Angle(Axis);
|
---|
| 294 |
|
---|
| 295 | DoLog(0) && (Log() << Verbose(0) << "Rotation angle is " << alpha << endl);
|
---|
| 296 |
|
---|
| 297 | // and rotate
|
---|
| 298 | for (molecule::iterator iter = begin(); iter != end(); ++iter) {
|
---|
| 299 | *(*iter) -= *CenterOfGravity;
|
---|
| 300 | (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), alpha));
|
---|
| 301 | *(*iter) += *CenterOfGravity;
|
---|
| 302 | }
|
---|
| 303 | DoLog(0) && (Log() << Verbose(0) << "done." << endl);
|
---|
| 304 |
|
---|
| 305 | delete CenterOfGravity;
|
---|
| 306 | }
|
---|
[cee0b57] | 307 |
|
---|
| 308 | /** Scales all atoms by \a *factor.
|
---|
| 309 | * \param *factor pointer to scaling factor
|
---|
[1bd79e] | 310 | *
|
---|
| 311 | * TODO: Is this realy what is meant, i.e.
|
---|
| 312 | * x=(x[0]*factor[0],x[1]*factor[1],x[2]*factor[2]) (current impl)
|
---|
| 313 | * or rather
|
---|
| 314 | * x=(**factor) * x (as suggested by comment)
|
---|
[cee0b57] | 315 | */
|
---|
[776b64] | 316 | void molecule::Scale(const double ** const factor)
|
---|
[cee0b57] | 317 | {
|
---|
[9879f6] | 318 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[6625c3] | 319 | for (size_t j=0;j<(*iter)->getTrajectorySize();j++) {
|
---|
[056e70] | 320 | Vector temp = (*iter)->getPositionAtStep(j);
|
---|
[6625c3] | 321 | temp.ScaleAll(*factor);
|
---|
[056e70] | 322 | (*iter)->setPositionAtStep(j,temp);
|
---|
[6625c3] | 323 | }
|
---|
[cee0b57] | 324 | }
|
---|
| 325 | };
|
---|
| 326 |
|
---|
| 327 | /** Translate all atoms by given vector.
|
---|
| 328 | * \param trans[] translation vector.
|
---|
| 329 | */
|
---|
| 330 | void molecule::Translate(const Vector *trans)
|
---|
| 331 | {
|
---|
[9879f6] | 332 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[6625c3] | 333 | for (size_t j=0;j<(*iter)->getTrajectorySize();j++) {
|
---|
[056e70] | 334 | (*iter)->setPositionAtStep(j, (*iter)->getPositionAtStep(j) + (*trans));
|
---|
[6625c3] | 335 | }
|
---|
[cee0b57] | 336 | }
|
---|
| 337 | };
|
---|
| 338 |
|
---|
| 339 | /** Translate the molecule periodically in the box.
|
---|
| 340 | * \param trans[] translation vector.
|
---|
[6625c3] | 341 | * TODO treatment of trajectories missing
|
---|
[cee0b57] | 342 | */
|
---|
| 343 | void molecule::TranslatePeriodically(const Vector *trans)
|
---|
| 344 | {
|
---|
[f429d7] | 345 | Box &domain = World::getInstance().getDomain();
|
---|
[cee0b57] | 346 |
|
---|
| 347 | // go through all atoms
|
---|
[d0f111] | 348 | BOOST_FOREACH(atom* iter, atoms){
|
---|
[d74077] | 349 | *iter += *trans;
|
---|
[d0f111] | 350 | }
|
---|
[0632c5] | 351 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
---|
[cee0b57] | 352 |
|
---|
| 353 | };
|
---|
| 354 |
|
---|
| 355 |
|
---|
| 356 | /** Mirrors all atoms against a given plane.
|
---|
| 357 | * \param n[] normal vector of mirror plane.
|
---|
| 358 | */
|
---|
| 359 | void molecule::Mirror(const Vector *n)
|
---|
| 360 | {
|
---|
[76c0d6] | 361 | OBSERVE;
|
---|
[ccf826] | 362 | Plane p(*n,0);
|
---|
[0632c5] | 363 | atoms.transformNodes(boost::bind(&Plane::mirrorVector,p,_1));
|
---|
[cee0b57] | 364 | };
|
---|
| 365 |
|
---|
| 366 | /** Determines center of molecule (yet not considering atom masses).
|
---|
| 367 | * \param center reference to return vector
|
---|
| 368 | */
|
---|
| 369 | void molecule::DeterminePeriodicCenter(Vector ¢er)
|
---|
| 370 | {
|
---|
[cca9ef] | 371 | const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
|
---|
| 372 | const RealSpaceMatrix &inversematrix = World::getInstance().getDomain().getM();
|
---|
[cee0b57] | 373 | double tmp;
|
---|
| 374 | bool flag;
|
---|
| 375 | Vector Testvector, Translationvector;
|
---|
[1883f9] | 376 | Vector Center;
|
---|
[7adf0f] | 377 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
[cee0b57] | 378 |
|
---|
| 379 | do {
|
---|
| 380 | Center.Zero();
|
---|
| 381 | flag = true;
|
---|
[9879f6] | 382 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[cee0b57] | 383 | #ifdef ADDHYDROGEN
|
---|
[83f176] | 384 | if ((*iter)->getType()->getAtomicNumber() != 1) {
|
---|
[cee0b57] | 385 | #endif
|
---|
[d74077] | 386 | Testvector = inversematrix * (*iter)->getPosition();
|
---|
[cee0b57] | 387 | Translationvector.Zero();
|
---|
[9d83b6] | 388 | const BondList& ListOfBonds = (*iter)->getListOfBonds();
|
---|
| 389 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 390 | Runner != ListOfBonds.end();
|
---|
| 391 | ++Runner) {
|
---|
[735b1c] | 392 | if ((*iter)->getNr() < (*Runner)->GetOtherAtom((*iter))->getNr()) // otherwise we shift one to, the other fro and gain nothing
|
---|
[cee0b57] | 393 | for (int j=0;j<NDIM;j++) {
|
---|
[d74077] | 394 | tmp = (*iter)->at(j) - (*Runner)->GetOtherAtom(*iter)->at(j);
|
---|
[7adf0f] | 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
|
---|
[cee0b57] | 399 | flag = false;
|
---|
[a7b761b] | 400 | DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
|
---|
[cee0b57] | 401 | if (tmp > 0)
|
---|
[0a4f7f] | 402 | Translationvector[j] -= 1.;
|
---|
[cee0b57] | 403 | else
|
---|
[0a4f7f] | 404 | Translationvector[j] += 1.;
|
---|
[cee0b57] | 405 | }
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
[273382] | 408 | Testvector += Translationvector;
|
---|
[5108e1] | 409 | Testvector *= matrix;
|
---|
[273382] | 410 | Center += Testvector;
|
---|
[0a4f7f] | 411 | Log() << Verbose(1) << "vector is: " << Testvector << endl;
|
---|
[cee0b57] | 412 | #ifdef ADDHYDROGEN
|
---|
| 413 | // now also change all hydrogens
|
---|
[9d83b6] | 414 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 415 | Runner != ListOfBonds.end();
|
---|
| 416 | ++Runner) {
|
---|
[83f176] | 417 | if ((*Runner)->GetOtherAtom((*iter))->getType()->getAtomicNumber() == 1) {
|
---|
[d74077] | 418 | Testvector = inversematrix * (*Runner)->GetOtherAtom((*iter))->getPosition();
|
---|
[273382] | 419 | Testvector += Translationvector;
|
---|
[5108e1] | 420 | Testvector *= matrix;
|
---|
[273382] | 421 | Center += Testvector;
|
---|
[0a4f7f] | 422 | Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
|
---|
[cee0b57] | 423 | }
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
| 426 | #endif
|
---|
| 427 | }
|
---|
| 428 | } while (!flag);
|
---|
[1614174] | 429 |
|
---|
[ea7176] | 430 | Center.Scale(1./static_cast<double>(getAtomCount()));
|
---|
[1883f9] | 431 | CenterAtVector(&Center);
|
---|
[cee0b57] | 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;
|
---|
[0a4f7f] | 441 | z_axis[0] = 0.;
|
---|
| 442 | z_axis[1] = 0.;
|
---|
| 443 | z_axis[2] = 1.;
|
---|
[cee0b57] | 444 |
|
---|
| 445 | // rotate on z-x plane
|
---|
[a67d19] | 446 | DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
|
---|
[0a4f7f] | 447 | alpha = atan(-n->at(0)/n->at(2));
|
---|
[a67d19] | 448 | DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
|
---|
[9879f6] | 449 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[d74077] | 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));
|
---|
[cee0b57] | 453 | for (int j=0;j<MDSteps;j++) {
|
---|
[6625c3] | 454 | Vector temp;
|
---|
[056e70] | 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);
|
---|
[cee0b57] | 458 | }
|
---|
| 459 | }
|
---|
| 460 | // rotate n vector
|
---|
[0a4f7f] | 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);
|
---|
[8cbb97] | 464 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
|
---|
[cee0b57] | 465 |
|
---|
| 466 | // rotate on z-y plane
|
---|
[0a4f7f] | 467 | alpha = atan(-n->at(1)/n->at(2));
|
---|
[a67d19] | 468 | DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
|
---|
[9879f6] | 469 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[d74077] | 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));
|
---|
[cee0b57] | 473 | for (int j=0;j<MDSteps;j++) {
|
---|
[6625c3] | 474 | Vector temp;
|
---|
[056e70] | 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);
|
---|
[cee0b57] | 478 | }
|
---|
| 479 | }
|
---|
| 480 | // rotate n vector (for consistency check)
|
---|
[0a4f7f] | 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);
|
---|
[cee0b57] | 484 |
|
---|
| 485 |
|
---|
[8cbb97] | 486 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
|
---|
[a67d19] | 487 | DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
|
---|
[cee0b57] | 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
|
---|
[0a4f7f] | 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);
|
---|
[cee0b57] | 509 | // go through all atoms
|
---|
[9879f6] | 510 | for (molecule::const_iterator iter = par->mol->begin(); iter != par->mol->end(); ++iter) {
|
---|
[d74077] | 511 | if ((*iter)->getType() == ((struct lsq_params *)params)->type) { // for specific type
|
---|
| 512 | c = (*iter)->getPosition() - a;
|
---|
[273382] | 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
|
---|
[cee0b57] | 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 | };
|
---|