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