[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 |
|
---|
[bbbad5] | 20 | #include "Helpers/MemDebug.hpp"
|
---|
[aafd77] | 21 |
|
---|
[6e5084] | 22 | #include "Helpers/helpers.hpp"
|
---|
| 23 | #include "Helpers/Log.hpp"
|
---|
| 24 | #include "Helpers/Verbose.hpp"
|
---|
| 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"
|
---|
[cee0b57] | 31 | #include "config.hpp"
|
---|
[f66195] | 32 | #include "element.hpp"
|
---|
[13d150] | 33 | #include "LinearAlgebra/leastsquaremin.hpp"
|
---|
[cee0b57] | 34 | #include "molecule.hpp"
|
---|
[b34306] | 35 | #include "World.hpp"
|
---|
[6e5084] | 36 |
|
---|
[84c494] | 37 | #include "Box.hpp"
|
---|
[6e5084] | 38 |
|
---|
[76c0d6] | 39 | #include <boost/foreach.hpp>
|
---|
| 40 |
|
---|
[aafd77] | 41 | #include <gsl/gsl_eigen.h>
|
---|
| 42 | #include <gsl/gsl_multimin.h>
|
---|
| 43 |
|
---|
[cee0b57] | 44 |
|
---|
| 45 | /************************************* Functions for class molecule *********************************/
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | /** Centers the molecule in the box whose lengths are defined by vector \a *BoxLengths.
|
---|
| 49 | * \param *out output stream for debugging
|
---|
| 50 | */
|
---|
[e138de] | 51 | bool molecule::CenterInBox()
|
---|
[cee0b57] | 52 | {
|
---|
| 53 | bool status = true;
|
---|
[e138de] | 54 | const Vector *Center = DetermineCenterOfAll();
|
---|
[eddea2] | 55 | const Vector *CenterBox = DetermineCenterOfBox();
|
---|
[f429d7] | 56 | Box &domain = World::getInstance().getDomain();
|
---|
[cee0b57] | 57 |
|
---|
| 58 | // go through all atoms
|
---|
[d0f111] | 59 | BOOST_FOREACH(atom* iter, atoms){
|
---|
[d74077] | 60 | *iter -= *Center;
|
---|
| 61 | *iter -= *CenterBox;
|
---|
[d0f111] | 62 | }
|
---|
[0632c5] | 63 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
---|
[cee0b57] | 64 |
|
---|
| 65 | delete(Center);
|
---|
[52d777] | 66 | delete(CenterBox);
|
---|
[cee0b57] | 67 | return status;
|
---|
| 68 | };
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | /** Bounds the molecule in the box whose lengths are defined by vector \a *BoxLengths.
|
---|
| 72 | * \param *out output stream for debugging
|
---|
| 73 | */
|
---|
[e138de] | 74 | bool molecule::BoundInBox()
|
---|
[cee0b57] | 75 | {
|
---|
| 76 | bool status = true;
|
---|
[f429d7] | 77 | Box &domain = World::getInstance().getDomain();
|
---|
[cee0b57] | 78 |
|
---|
| 79 | // go through all atoms
|
---|
[0632c5] | 80 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
---|
[cee0b57] | 81 |
|
---|
| 82 | return status;
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | /** Centers the edge of the atoms at (0,0,0).
|
---|
| 86 | * \param *out output stream for debugging
|
---|
| 87 | * \param *max coordinates of other edge, specifying box dimensions.
|
---|
| 88 | */
|
---|
[e138de] | 89 | void molecule::CenterEdge(Vector *max)
|
---|
[cee0b57] | 90 | {
|
---|
| 91 | Vector *min = new Vector;
|
---|
| 92 |
|
---|
[e138de] | 93 | // Log() << Verbose(3) << "Begin of CenterEdge." << endl;
|
---|
[9879f6] | 94 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
| 95 | if (iter != end()) { //list not empty?
|
---|
[cee0b57] | 96 | for (int i=NDIM;i--;) {
|
---|
[d74077] | 97 | max->at(i) = (*iter)->at(i);
|
---|
| 98 | min->at(i) = (*iter)->at(i);
|
---|
[cee0b57] | 99 | }
|
---|
[9879f6] | 100 | for (; iter != end(); ++iter) {// continue with second if present
|
---|
| 101 | //(*iter)->Output(1,1,out);
|
---|
[cee0b57] | 102 | for (int i=NDIM;i--;) {
|
---|
[d74077] | 103 | max->at(i) = (max->at(i) < (*iter)->at(i)) ? (*iter)->at(i) : max->at(i);
|
---|
| 104 | min->at(i) = (min->at(i) > (*iter)->at(i)) ? (*iter)->at(i) : min->at(i);
|
---|
[cee0b57] | 105 | }
|
---|
| 106 | }
|
---|
[e138de] | 107 | // Log() << Verbose(4) << "Maximum is ";
|
---|
[cee0b57] | 108 | // max->Output(out);
|
---|
[e138de] | 109 | // Log() << Verbose(0) << ", Minimum is ";
|
---|
[cee0b57] | 110 | // min->Output(out);
|
---|
[e138de] | 111 | // Log() << Verbose(0) << endl;
|
---|
[cee0b57] | 112 | min->Scale(-1.);
|
---|
[273382] | 113 | (*max) += (*min);
|
---|
[cee0b57] | 114 | Translate(min);
|
---|
| 115 | }
|
---|
| 116 | delete(min);
|
---|
[e138de] | 117 | // Log() << Verbose(3) << "End of CenterEdge." << endl;
|
---|
[cee0b57] | 118 | };
|
---|
| 119 |
|
---|
| 120 | /** Centers the center of the atoms at (0,0,0).
|
---|
| 121 | * \param *out output stream for debugging
|
---|
| 122 | * \param *center return vector for translation vector
|
---|
| 123 | */
|
---|
[e138de] | 124 | void molecule::CenterOrigin()
|
---|
[cee0b57] | 125 | {
|
---|
| 126 | int Num = 0;
|
---|
[9879f6] | 127 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
[1883f9] | 128 | Vector Center;
|
---|
[cee0b57] | 129 |
|
---|
| 130 | Center.Zero();
|
---|
[9879f6] | 131 | if (iter != end()) { //list not empty?
|
---|
| 132 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
[cee0b57] | 133 | Num++;
|
---|
[d74077] | 134 | Center += (*iter)->getPosition();
|
---|
[cee0b57] | 135 | }
|
---|
[bdc91e] | 136 | Center.Scale(-1./(double)Num); // divide through total number (and sign for direction)
|
---|
[cee0b57] | 137 | Translate(&Center);
|
---|
| 138 | }
|
---|
| 139 | };
|
---|
| 140 |
|
---|
| 141 | /** Returns vector pointing to center of all atoms.
|
---|
| 142 | * \return pointer to center of all vector
|
---|
| 143 | */
|
---|
[e138de] | 144 | Vector * molecule::DetermineCenterOfAll() const
|
---|
[cee0b57] | 145 | {
|
---|
[9879f6] | 146 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
[cee0b57] | 147 | Vector *a = new Vector();
|
---|
| 148 | double Num = 0;
|
---|
| 149 |
|
---|
| 150 | a->Zero();
|
---|
| 151 |
|
---|
[9879f6] | 152 | if (iter != end()) { //list not empty?
|
---|
| 153 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
[15b670] | 154 | Num++;
|
---|
[d74077] | 155 | (*a) += (*iter)->getPosition();
|
---|
[cee0b57] | 156 | }
|
---|
[bdc91e] | 157 | a->Scale(1./(double)Num); // divide through total mass (and sign for direction)
|
---|
[cee0b57] | 158 | }
|
---|
| 159 | return a;
|
---|
| 160 | };
|
---|
| 161 |
|
---|
[eddea2] | 162 | /** Returns vector pointing to center of the domain.
|
---|
| 163 | * \return pointer to center of the domain
|
---|
| 164 | */
|
---|
| 165 | Vector * molecule::DetermineCenterOfBox() const
|
---|
| 166 | {
|
---|
| 167 | Vector *a = new Vector(0.5,0.5,0.5);
|
---|
[cca9ef] | 168 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
|
---|
[5108e1] | 169 | (*a) *= M;
|
---|
[eddea2] | 170 | return a;
|
---|
| 171 | };
|
---|
| 172 |
|
---|
[cee0b57] | 173 | /** Returns vector pointing to center of gravity.
|
---|
| 174 | * \param *out output stream for debugging
|
---|
| 175 | * \return pointer to center of gravity vector
|
---|
| 176 | */
|
---|
[4bb63c] | 177 | Vector * molecule::DetermineCenterOfGravity() const
|
---|
[cee0b57] | 178 | {
|
---|
[9879f6] | 179 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
[cee0b57] | 180 | Vector *a = new Vector();
|
---|
| 181 | Vector tmp;
|
---|
| 182 | double Num = 0;
|
---|
| 183 |
|
---|
| 184 | a->Zero();
|
---|
| 185 |
|
---|
[9879f6] | 186 | if (iter != end()) { //list not empty?
|
---|
| 187 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
[83f176] | 188 | Num += (*iter)->getType()->getMass();
|
---|
| 189 | tmp = (*iter)->getType()->getMass() * (*iter)->getPosition();
|
---|
[273382] | 190 | (*a) += tmp;
|
---|
[cee0b57] | 191 | }
|
---|
[bdc91e] | 192 | a->Scale(1./Num); // divide through total mass
|
---|
[cee0b57] | 193 | }
|
---|
[e138de] | 194 | // Log() << Verbose(1) << "Resulting center of gravity: ";
|
---|
[cee0b57] | 195 | // a->Output(out);
|
---|
[e138de] | 196 | // Log() << Verbose(0) << endl;
|
---|
[cee0b57] | 197 | return a;
|
---|
| 198 | };
|
---|
| 199 |
|
---|
| 200 | /** Centers the center of gravity of the atoms at (0,0,0).
|
---|
| 201 | * \param *out output stream for debugging
|
---|
| 202 | * \param *center return vector for translation vector
|
---|
| 203 | */
|
---|
[e138de] | 204 | void molecule::CenterPeriodic()
|
---|
[cee0b57] | 205 | {
|
---|
[1883f9] | 206 | Vector NewCenter;
|
---|
| 207 | DeterminePeriodicCenter(NewCenter);
|
---|
| 208 | // go through all atoms
|
---|
| 209 | BOOST_FOREACH(atom* iter, atoms){
|
---|
| 210 | *iter -= NewCenter;
|
---|
| 211 | }
|
---|
[cee0b57] | 212 | };
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | /** Centers the center of gravity of the atoms at (0,0,0).
|
---|
| 216 | * \param *out output stream for debugging
|
---|
| 217 | * \param *center return vector for translation vector
|
---|
| 218 | */
|
---|
[e138de] | 219 | void molecule::CenterAtVector(Vector *newcenter)
|
---|
[cee0b57] | 220 | {
|
---|
[1883f9] | 221 | // go through all atoms
|
---|
| 222 | BOOST_FOREACH(atom* iter, atoms){
|
---|
| 223 | *iter -= *newcenter;
|
---|
| 224 | }
|
---|
[cee0b57] | 225 | };
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 | /** Scales all atoms by \a *factor.
|
---|
| 229 | * \param *factor pointer to scaling factor
|
---|
[1bd79e] | 230 | *
|
---|
| 231 | * TODO: Is this realy what is meant, i.e.
|
---|
| 232 | * x=(x[0]*factor[0],x[1]*factor[1],x[2]*factor[2]) (current impl)
|
---|
| 233 | * or rather
|
---|
| 234 | * x=(**factor) * x (as suggested by comment)
|
---|
[cee0b57] | 235 | */
|
---|
[776b64] | 236 | void molecule::Scale(const double ** const factor)
|
---|
[cee0b57] | 237 | {
|
---|
[9879f6] | 238 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[cee0b57] | 239 | for (int j=0;j<MDSteps;j++)
|
---|
[a7b761b] | 240 | (*iter)->Trajectory.R.at(j).ScaleAll(*factor);
|
---|
[d74077] | 241 | (*iter)->ScaleAll(*factor);
|
---|
[cee0b57] | 242 | }
|
---|
| 243 | };
|
---|
| 244 |
|
---|
| 245 | /** Translate all atoms by given vector.
|
---|
| 246 | * \param trans[] translation vector.
|
---|
| 247 | */
|
---|
| 248 | void molecule::Translate(const Vector *trans)
|
---|
| 249 | {
|
---|
[9879f6] | 250 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[cee0b57] | 251 | for (int j=0;j<MDSteps;j++)
|
---|
[a7b761b] | 252 | (*iter)->Trajectory.R.at(j) += (*trans);
|
---|
[d74077] | 253 | *(*iter) += (*trans);
|
---|
[cee0b57] | 254 | }
|
---|
| 255 | };
|
---|
| 256 |
|
---|
| 257 | /** Translate the molecule periodically in the box.
|
---|
| 258 | * \param trans[] translation vector.
|
---|
| 259 | * TODO treatment of trajetories missing
|
---|
| 260 | */
|
---|
| 261 | void molecule::TranslatePeriodically(const Vector *trans)
|
---|
| 262 | {
|
---|
[f429d7] | 263 | Box &domain = World::getInstance().getDomain();
|
---|
[cee0b57] | 264 |
|
---|
| 265 | // go through all atoms
|
---|
[d0f111] | 266 | BOOST_FOREACH(atom* iter, atoms){
|
---|
[d74077] | 267 | *iter += *trans;
|
---|
[d0f111] | 268 | }
|
---|
[0632c5] | 269 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
---|
[cee0b57] | 270 |
|
---|
| 271 | };
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | /** Mirrors all atoms against a given plane.
|
---|
| 275 | * \param n[] normal vector of mirror plane.
|
---|
| 276 | */
|
---|
| 277 | void molecule::Mirror(const Vector *n)
|
---|
| 278 | {
|
---|
[76c0d6] | 279 | OBSERVE;
|
---|
[ccf826] | 280 | Plane p(*n,0);
|
---|
[0632c5] | 281 | atoms.transformNodes(boost::bind(&Plane::mirrorVector,p,_1));
|
---|
[cee0b57] | 282 | };
|
---|
| 283 |
|
---|
| 284 | /** Determines center of molecule (yet not considering atom masses).
|
---|
| 285 | * \param center reference to return vector
|
---|
| 286 | */
|
---|
| 287 | void molecule::DeterminePeriodicCenter(Vector ¢er)
|
---|
| 288 | {
|
---|
[cca9ef] | 289 | const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
|
---|
| 290 | const RealSpaceMatrix &inversematrix = World::getInstance().getDomain().getM();
|
---|
[cee0b57] | 291 | double tmp;
|
---|
| 292 | bool flag;
|
---|
| 293 | Vector Testvector, Translationvector;
|
---|
[1883f9] | 294 | Vector Center;
|
---|
[cee0b57] | 295 |
|
---|
| 296 | do {
|
---|
| 297 | Center.Zero();
|
---|
| 298 | flag = true;
|
---|
[9879f6] | 299 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[cee0b57] | 300 | #ifdef ADDHYDROGEN
|
---|
[83f176] | 301 | if ((*iter)->getType()->getAtomicNumber() != 1) {
|
---|
[cee0b57] | 302 | #endif
|
---|
[d74077] | 303 | Testvector = inversematrix * (*iter)->getPosition();
|
---|
[cee0b57] | 304 | Translationvector.Zero();
|
---|
[9879f6] | 305 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
---|
| 306 | if ((*iter)->nr < (*Runner)->GetOtherAtom((*iter))->nr) // otherwise we shift one to, the other fro and gain nothing
|
---|
[cee0b57] | 307 | for (int j=0;j<NDIM;j++) {
|
---|
[d74077] | 308 | tmp = (*iter)->at(j) - (*Runner)->GetOtherAtom(*iter)->at(j);
|
---|
[cee0b57] | 309 | if ((fabs(tmp)) > BondDistance) {
|
---|
| 310 | flag = false;
|
---|
[a7b761b] | 311 | DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
|
---|
[cee0b57] | 312 | if (tmp > 0)
|
---|
[0a4f7f] | 313 | Translationvector[j] -= 1.;
|
---|
[cee0b57] | 314 | else
|
---|
[0a4f7f] | 315 | Translationvector[j] += 1.;
|
---|
[cee0b57] | 316 | }
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
[273382] | 319 | Testvector += Translationvector;
|
---|
[5108e1] | 320 | Testvector *= matrix;
|
---|
[273382] | 321 | Center += Testvector;
|
---|
[0a4f7f] | 322 | Log() << Verbose(1) << "vector is: " << Testvector << endl;
|
---|
[cee0b57] | 323 | #ifdef ADDHYDROGEN
|
---|
| 324 | // now also change all hydrogens
|
---|
[9879f6] | 325 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
---|
[83f176] | 326 | if ((*Runner)->GetOtherAtom((*iter))->getType()->getAtomicNumber() == 1) {
|
---|
[d74077] | 327 | Testvector = inversematrix * (*Runner)->GetOtherAtom((*iter))->getPosition();
|
---|
[273382] | 328 | Testvector += Translationvector;
|
---|
[5108e1] | 329 | Testvector *= matrix;
|
---|
[273382] | 330 | Center += Testvector;
|
---|
[0a4f7f] | 331 | Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
|
---|
[cee0b57] | 332 | }
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 | #endif
|
---|
| 336 | }
|
---|
| 337 | } while (!flag);
|
---|
[1614174] | 338 |
|
---|
[ea7176] | 339 | Center.Scale(1./static_cast<double>(getAtomCount()));
|
---|
[1883f9] | 340 | CenterAtVector(&Center);
|
---|
[cee0b57] | 341 | };
|
---|
| 342 |
|
---|
| 343 | /** Align all atoms in such a manner that given vector \a *n is along z axis.
|
---|
| 344 | * \param n[] alignment vector.
|
---|
| 345 | */
|
---|
| 346 | void molecule::Align(Vector *n)
|
---|
| 347 | {
|
---|
| 348 | double alpha, tmp;
|
---|
| 349 | Vector z_axis;
|
---|
[0a4f7f] | 350 | z_axis[0] = 0.;
|
---|
| 351 | z_axis[1] = 0.;
|
---|
| 352 | z_axis[2] = 1.;
|
---|
[cee0b57] | 353 |
|
---|
| 354 | // rotate on z-x plane
|
---|
[a67d19] | 355 | DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
|
---|
[0a4f7f] | 356 | alpha = atan(-n->at(0)/n->at(2));
|
---|
[a67d19] | 357 | DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
|
---|
[9879f6] | 358 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[d74077] | 359 | tmp = (*iter)->at(0);
|
---|
| 360 | (*iter)->set(0, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
|
---|
| 361 | (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
|
---|
[cee0b57] | 362 | for (int j=0;j<MDSteps;j++) {
|
---|
[a7b761b] | 363 | tmp = (*iter)->Trajectory.R.at(j)[0];
|
---|
| 364 | (*iter)->Trajectory.R.at(j)[0] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
---|
| 365 | (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
---|
[cee0b57] | 366 | }
|
---|
| 367 | }
|
---|
| 368 | // rotate n vector
|
---|
[0a4f7f] | 369 | tmp = n->at(0);
|
---|
| 370 | n->at(0) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
---|
| 371 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
---|
[8cbb97] | 372 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
|
---|
[cee0b57] | 373 |
|
---|
| 374 | // rotate on z-y plane
|
---|
[0a4f7f] | 375 | alpha = atan(-n->at(1)/n->at(2));
|
---|
[a67d19] | 376 | DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
|
---|
[9879f6] | 377 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[d74077] | 378 | tmp = (*iter)->at(1);
|
---|
| 379 | (*iter)->set(1, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
|
---|
| 380 | (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
|
---|
[cee0b57] | 381 | for (int j=0;j<MDSteps;j++) {
|
---|
[a7b761b] | 382 | tmp = (*iter)->Trajectory.R.at(j)[1];
|
---|
| 383 | (*iter)->Trajectory.R.at(j)[1] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
---|
| 384 | (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
---|
[cee0b57] | 385 | }
|
---|
| 386 | }
|
---|
| 387 | // rotate n vector (for consistency check)
|
---|
[0a4f7f] | 388 | tmp = n->at(1);
|
---|
| 389 | n->at(1) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
---|
| 390 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
---|
[cee0b57] | 391 |
|
---|
| 392 |
|
---|
[8cbb97] | 393 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
|
---|
[a67d19] | 394 | DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
|
---|
[cee0b57] | 395 | };
|
---|
| 396 |
|
---|
| 397 |
|
---|
| 398 | /** Calculates sum over least square distance to line hidden in \a *x.
|
---|
| 399 | * \param *x offset and direction vector
|
---|
| 400 | * \param *params pointer to lsq_params structure
|
---|
| 401 | * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
|
---|
| 402 | */
|
---|
| 403 | double LeastSquareDistance (const gsl_vector * x, void * params)
|
---|
| 404 | {
|
---|
| 405 | double res = 0, t;
|
---|
| 406 | Vector a,b,c,d;
|
---|
| 407 | struct lsq_params *par = (struct lsq_params *)params;
|
---|
| 408 |
|
---|
| 409 | // initialize vectors
|
---|
[0a4f7f] | 410 | a[0] = gsl_vector_get(x,0);
|
---|
| 411 | a[1] = gsl_vector_get(x,1);
|
---|
| 412 | a[2] = gsl_vector_get(x,2);
|
---|
| 413 | b[0] = gsl_vector_get(x,3);
|
---|
| 414 | b[1] = gsl_vector_get(x,4);
|
---|
| 415 | b[2] = gsl_vector_get(x,5);
|
---|
[cee0b57] | 416 | // go through all atoms
|
---|
[9879f6] | 417 | for (molecule::const_iterator iter = par->mol->begin(); iter != par->mol->end(); ++iter) {
|
---|
[d74077] | 418 | if ((*iter)->getType() == ((struct lsq_params *)params)->type) { // for specific type
|
---|
| 419 | c = (*iter)->getPosition() - a;
|
---|
[273382] | 420 | t = c.ScalarProduct(b); // get direction parameter
|
---|
| 421 | d = t*b; // and create vector
|
---|
| 422 | c -= d; // ... yielding distance vector
|
---|
| 423 | res += d.ScalarProduct(d); // add squared distance
|
---|
[cee0b57] | 424 | }
|
---|
| 425 | }
|
---|
| 426 | return res;
|
---|
| 427 | };
|
---|
| 428 |
|
---|
| 429 | /** By minimizing the least square distance gains alignment vector.
|
---|
| 430 | * \bug this is not yet working properly it seems
|
---|
| 431 | */
|
---|
| 432 | void molecule::GetAlignvector(struct lsq_params * par) const
|
---|
| 433 | {
|
---|
| 434 | int np = 6;
|
---|
| 435 |
|
---|
| 436 | const gsl_multimin_fminimizer_type *T =
|
---|
| 437 | gsl_multimin_fminimizer_nmsimplex;
|
---|
| 438 | gsl_multimin_fminimizer *s = NULL;
|
---|
| 439 | gsl_vector *ss;
|
---|
| 440 | gsl_multimin_function minex_func;
|
---|
| 441 |
|
---|
| 442 | size_t iter = 0, i;
|
---|
| 443 | int status;
|
---|
| 444 | double size;
|
---|
| 445 |
|
---|
| 446 | /* Initial vertex size vector */
|
---|
| 447 | ss = gsl_vector_alloc (np);
|
---|
| 448 |
|
---|
| 449 | /* Set all step sizes to 1 */
|
---|
| 450 | gsl_vector_set_all (ss, 1.0);
|
---|
| 451 |
|
---|
| 452 | /* Starting point */
|
---|
| 453 | par->x = gsl_vector_alloc (np);
|
---|
| 454 | par->mol = this;
|
---|
| 455 |
|
---|
| 456 | gsl_vector_set (par->x, 0, 0.0); // offset
|
---|
| 457 | gsl_vector_set (par->x, 1, 0.0);
|
---|
| 458 | gsl_vector_set (par->x, 2, 0.0);
|
---|
| 459 | gsl_vector_set (par->x, 3, 0.0); // direction
|
---|
| 460 | gsl_vector_set (par->x, 4, 0.0);
|
---|
| 461 | gsl_vector_set (par->x, 5, 1.0);
|
---|
| 462 |
|
---|
| 463 | /* Initialize method and iterate */
|
---|
| 464 | minex_func.f = &LeastSquareDistance;
|
---|
| 465 | minex_func.n = np;
|
---|
| 466 | minex_func.params = (void *)par;
|
---|
| 467 |
|
---|
| 468 | s = gsl_multimin_fminimizer_alloc (T, np);
|
---|
| 469 | gsl_multimin_fminimizer_set (s, &minex_func, par->x, ss);
|
---|
| 470 |
|
---|
| 471 | do
|
---|
| 472 | {
|
---|
| 473 | iter++;
|
---|
| 474 | status = gsl_multimin_fminimizer_iterate(s);
|
---|
| 475 |
|
---|
| 476 | if (status)
|
---|
| 477 | break;
|
---|
| 478 |
|
---|
| 479 | size = gsl_multimin_fminimizer_size (s);
|
---|
| 480 | status = gsl_multimin_test_size (size, 1e-2);
|
---|
| 481 |
|
---|
| 482 | if (status == GSL_SUCCESS)
|
---|
| 483 | {
|
---|
| 484 | printf ("converged to minimum at\n");
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | printf ("%5d ", (int)iter);
|
---|
| 488 | for (i = 0; i < (size_t)np; i++)
|
---|
| 489 | {
|
---|
| 490 | printf ("%10.3e ", gsl_vector_get (s->x, i));
|
---|
| 491 | }
|
---|
| 492 | printf ("f() = %7.3f size = %.3f\n", s->fval, size);
|
---|
| 493 | }
|
---|
| 494 | while (status == GSL_CONTINUE && iter < 100);
|
---|
| 495 |
|
---|
| 496 | for (i=0;i<(size_t)np;i++)
|
---|
| 497 | gsl_vector_set(par->x, i, gsl_vector_get(s->x, i));
|
---|
| 498 | //gsl_vector_free(par->x);
|
---|
| 499 | gsl_vector_free(ss);
|
---|
| 500 | gsl_multimin_fminimizer_free (s);
|
---|
| 501 | };
|
---|