| [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();
|
|---|
| [d0f111] | 37 | double *M_double = ReturnFullMatrixforSymmetric(cell_size);
|
|---|
| 38 | Matrix M = Matrix(M_double);
|
|---|
| 39 | delete[](M_double);
|
|---|
| 40 | Matrix Minv = M.invert();
|
|---|
| [cee0b57] | 41 |
|
|---|
| 42 | // go through all atoms
|
|---|
| [273382] | 43 | ActOnAllVectors( &Vector::SubtractVector, *Center);
|
|---|
| [eddea2] | 44 | ActOnAllVectors( &Vector::SubtractVector, *CenterBox);
|
|---|
| [d0f111] | 45 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 46 | iter->node->WrapPeriodically(M, Minv);
|
|---|
| 47 | }
|
|---|
| [cee0b57] | 48 |
|
|---|
| 49 | delete(Center);
|
|---|
| 50 | return status;
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | /** Bounds the molecule in the box whose lengths are defined by vector \a *BoxLengths.
|
|---|
| 55 | * \param *out output stream for debugging
|
|---|
| 56 | */
|
|---|
| [e138de] | 57 | bool molecule::BoundInBox()
|
|---|
| [cee0b57] | 58 | {
|
|---|
| 59 | bool status = true;
|
|---|
| [5f612ee] | 60 | double * const cell_size = World::getInstance().getDomain();
|
|---|
| [d0f111] | 61 | double *M_double = ReturnFullMatrixforSymmetric(cell_size);
|
|---|
| 62 | Matrix M = Matrix(M_double);
|
|---|
| 63 | delete[](M_double);
|
|---|
| 64 | Matrix Minv = M.invert();
|
|---|
| [cee0b57] | 65 |
|
|---|
| 66 | // go through all atoms
|
|---|
| [d0f111] | 67 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 68 | iter->node->WrapPeriodically(M, Minv);
|
|---|
| 69 | }
|
|---|
| [cee0b57] | 70 |
|
|---|
| 71 | return status;
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | /** Centers the edge of the atoms at (0,0,0).
|
|---|
| 75 | * \param *out output stream for debugging
|
|---|
| 76 | * \param *max coordinates of other edge, specifying box dimensions.
|
|---|
| 77 | */
|
|---|
| [e138de] | 78 | void molecule::CenterEdge(Vector *max)
|
|---|
| [cee0b57] | 79 | {
|
|---|
| 80 | Vector *min = new Vector;
|
|---|
| 81 |
|
|---|
| [e138de] | 82 | // Log() << Verbose(3) << "Begin of CenterEdge." << endl;
|
|---|
| [9879f6] | 83 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| 84 | if (iter != end()) { //list not empty?
|
|---|
| [cee0b57] | 85 | for (int i=NDIM;i--;) {
|
|---|
| [a7b761b] | 86 | max->at(i) = (*iter)->x[i];
|
|---|
| 87 | min->at(i) = (*iter)->x[i];
|
|---|
| [cee0b57] | 88 | }
|
|---|
| [9879f6] | 89 | for (; iter != end(); ++iter) {// continue with second if present
|
|---|
| 90 | //(*iter)->Output(1,1,out);
|
|---|
| [cee0b57] | 91 | for (int i=NDIM;i--;) {
|
|---|
| [a7b761b] | 92 | max->at(i) = (max->at(i) < (*iter)->x[i]) ? (*iter)->x[i] : max->at(i);
|
|---|
| 93 | min->at(i) = (min->at(i) > (*iter)->x[i]) ? (*iter)->x[i] : min->at(i);
|
|---|
| [cee0b57] | 94 | }
|
|---|
| 95 | }
|
|---|
| [e138de] | 96 | // Log() << Verbose(4) << "Maximum is ";
|
|---|
| [cee0b57] | 97 | // max->Output(out);
|
|---|
| [e138de] | 98 | // Log() << Verbose(0) << ", Minimum is ";
|
|---|
| [cee0b57] | 99 | // min->Output(out);
|
|---|
| [e138de] | 100 | // Log() << Verbose(0) << endl;
|
|---|
| [cee0b57] | 101 | min->Scale(-1.);
|
|---|
| [273382] | 102 | (*max) += (*min);
|
|---|
| [cee0b57] | 103 | Translate(min);
|
|---|
| 104 | Center.Zero();
|
|---|
| 105 | }
|
|---|
| 106 | delete(min);
|
|---|
| [e138de] | 107 | // Log() << Verbose(3) << "End of CenterEdge." << endl;
|
|---|
| [cee0b57] | 108 | };
|
|---|
| 109 |
|
|---|
| 110 | /** Centers the center of the atoms at (0,0,0).
|
|---|
| 111 | * \param *out output stream for debugging
|
|---|
| 112 | * \param *center return vector for translation vector
|
|---|
| 113 | */
|
|---|
| [e138de] | 114 | void molecule::CenterOrigin()
|
|---|
| [cee0b57] | 115 | {
|
|---|
| 116 | int Num = 0;
|
|---|
| [9879f6] | 117 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| [cee0b57] | 118 |
|
|---|
| 119 | Center.Zero();
|
|---|
| 120 |
|
|---|
| [9879f6] | 121 | if (iter != end()) { //list not empty?
|
|---|
| 122 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| [cee0b57] | 123 | Num++;
|
|---|
| [a7b761b] | 124 | Center += (*iter)->x;
|
|---|
| [cee0b57] | 125 | }
|
|---|
| 126 | Center.Scale(-1./Num); // divide through total number (and sign for direction)
|
|---|
| 127 | Translate(&Center);
|
|---|
| 128 | Center.Zero();
|
|---|
| 129 | }
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | /** Returns vector pointing to center of all atoms.
|
|---|
| 133 | * \return pointer to center of all vector
|
|---|
| 134 | */
|
|---|
| [e138de] | 135 | Vector * molecule::DetermineCenterOfAll() const
|
|---|
| [cee0b57] | 136 | {
|
|---|
| [9879f6] | 137 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| [cee0b57] | 138 | Vector *a = new Vector();
|
|---|
| 139 | double Num = 0;
|
|---|
| 140 |
|
|---|
| 141 | a->Zero();
|
|---|
| 142 |
|
|---|
| [9879f6] | 143 | if (iter != end()) { //list not empty?
|
|---|
| 144 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| [15b670] | 145 | Num++;
|
|---|
| [1024cb] | 146 | (*a) += (*iter)->x;
|
|---|
| [cee0b57] | 147 | }
|
|---|
| 148 | a->Scale(1./Num); // divide through total mass (and sign for direction)
|
|---|
| 149 | }
|
|---|
| 150 | return a;
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| [eddea2] | 153 | /** Returns vector pointing to center of the domain.
|
|---|
| 154 | * \return pointer to center of the domain
|
|---|
| 155 | */
|
|---|
| 156 | Vector * molecule::DetermineCenterOfBox() const
|
|---|
| 157 | {
|
|---|
| 158 | Vector *a = new Vector(0.5,0.5,0.5);
|
|---|
| 159 |
|
|---|
| 160 | const double *cell_size = World::getInstance().getDomain();
|
|---|
| [c94eeb] | 161 | double *M_double = ReturnFullMatrixforSymmetric(cell_size);
|
|---|
| 162 | Matrix M = Matrix(M_double);
|
|---|
| 163 | delete[](M_double);
|
|---|
| [eddea2] | 164 | a->MatrixMultiplication(M);
|
|---|
| 165 |
|
|---|
| 166 | return a;
|
|---|
| 167 | };
|
|---|
| 168 |
|
|---|
| [cee0b57] | 169 | /** Returns vector pointing to center of gravity.
|
|---|
| 170 | * \param *out output stream for debugging
|
|---|
| 171 | * \return pointer to center of gravity vector
|
|---|
| 172 | */
|
|---|
| [e138de] | 173 | Vector * molecule::DetermineCenterOfGravity()
|
|---|
| [cee0b57] | 174 | {
|
|---|
| [9879f6] | 175 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| [cee0b57] | 176 | Vector *a = new Vector();
|
|---|
| 177 | Vector tmp;
|
|---|
| 178 | double Num = 0;
|
|---|
| 179 |
|
|---|
| 180 | a->Zero();
|
|---|
| 181 |
|
|---|
| [9879f6] | 182 | if (iter != end()) { //list not empty?
|
|---|
| 183 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| 184 | Num += (*iter)->type->mass;
|
|---|
| [a7b761b] | 185 | tmp = (*iter)->type->mass * (*iter)->x;
|
|---|
| [273382] | 186 | (*a) += tmp;
|
|---|
| [cee0b57] | 187 | }
|
|---|
| [15b670] | 188 | a->Scale(1./Num); // divide through total mass (and sign for direction)
|
|---|
| [cee0b57] | 189 | }
|
|---|
| [e138de] | 190 | // Log() << Verbose(1) << "Resulting center of gravity: ";
|
|---|
| [cee0b57] | 191 | // a->Output(out);
|
|---|
| [e138de] | 192 | // Log() << Verbose(0) << endl;
|
|---|
| [cee0b57] | 193 | return a;
|
|---|
| 194 | };
|
|---|
| 195 |
|
|---|
| 196 | /** Centers the center of gravity of the atoms at (0,0,0).
|
|---|
| 197 | * \param *out output stream for debugging
|
|---|
| 198 | * \param *center return vector for translation vector
|
|---|
| 199 | */
|
|---|
| [e138de] | 200 | void molecule::CenterPeriodic()
|
|---|
| [cee0b57] | 201 | {
|
|---|
| 202 | DeterminePeriodicCenter(Center);
|
|---|
| 203 | };
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 | /** Centers the center of gravity of the atoms at (0,0,0).
|
|---|
| 207 | * \param *out output stream for debugging
|
|---|
| 208 | * \param *center return vector for translation vector
|
|---|
| 209 | */
|
|---|
| [e138de] | 210 | void molecule::CenterAtVector(Vector *newcenter)
|
|---|
| [cee0b57] | 211 | {
|
|---|
| [273382] | 212 | Center = *newcenter;
|
|---|
| [cee0b57] | 213 | };
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 | /** Scales all atoms by \a *factor.
|
|---|
| 217 | * \param *factor pointer to scaling factor
|
|---|
| [1bd79e] | 218 | *
|
|---|
| 219 | * TODO: Is this realy what is meant, i.e.
|
|---|
| 220 | * x=(x[0]*factor[0],x[1]*factor[1],x[2]*factor[2]) (current impl)
|
|---|
| 221 | * or rather
|
|---|
| 222 | * x=(**factor) * x (as suggested by comment)
|
|---|
| [cee0b57] | 223 | */
|
|---|
| [776b64] | 224 | void molecule::Scale(const double ** const factor)
|
|---|
| [cee0b57] | 225 | {
|
|---|
| [9879f6] | 226 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [cee0b57] | 227 | for (int j=0;j<MDSteps;j++)
|
|---|
| [a7b761b] | 228 | (*iter)->Trajectory.R.at(j).ScaleAll(*factor);
|
|---|
| 229 | (*iter)->x.ScaleAll(*factor);
|
|---|
| [cee0b57] | 230 | }
|
|---|
| 231 | };
|
|---|
| 232 |
|
|---|
| 233 | /** Translate all atoms by given vector.
|
|---|
| 234 | * \param trans[] translation vector.
|
|---|
| 235 | */
|
|---|
| 236 | void molecule::Translate(const Vector *trans)
|
|---|
| 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) += (*trans);
|
|---|
| 241 | (*iter)->x += (*trans);
|
|---|
| [cee0b57] | 242 | }
|
|---|
| 243 | };
|
|---|
| 244 |
|
|---|
| 245 | /** Translate the molecule periodically in the box.
|
|---|
| 246 | * \param trans[] translation vector.
|
|---|
| 247 | * TODO treatment of trajetories missing
|
|---|
| 248 | */
|
|---|
| 249 | void molecule::TranslatePeriodically(const Vector *trans)
|
|---|
| 250 | {
|
|---|
| [5f612ee] | 251 | double * const cell_size = World::getInstance().getDomain();
|
|---|
| [d0f111] | 252 | double *M_double = ReturnFullMatrixforSymmetric(cell_size);
|
|---|
| 253 | Matrix M = Matrix(M_double);
|
|---|
| 254 | delete[](M_double);
|
|---|
| 255 | Matrix Minv = M.invert();
|
|---|
| [cee0b57] | 256 |
|
|---|
| 257 | // go through all atoms
|
|---|
| [eddea2] | 258 | ActOnAllVectors( &Vector::AddVector, *trans);
|
|---|
| [d0f111] | 259 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 260 | iter->node->WrapPeriodically(M, Minv);
|
|---|
| 261 | }
|
|---|
| [cee0b57] | 262 |
|
|---|
| 263 | };
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 | /** Mirrors all atoms against a given plane.
|
|---|
| 267 | * \param n[] normal vector of mirror plane.
|
|---|
| 268 | */
|
|---|
| 269 | void molecule::Mirror(const Vector *n)
|
|---|
| 270 | {
|
|---|
| [76c0d6] | 271 | OBSERVE;
|
|---|
| [ccf826] | 272 | Plane p(*n,0);
|
|---|
| [d0f111] | 273 | BOOST_FOREACH(atom* iter, atoms ){
|
|---|
| [76c0d6] | 274 | (*iter->node) = p.mirrorVector(*iter->node);
|
|---|
| [ccf826] | 275 | }
|
|---|
| [cee0b57] | 276 | };
|
|---|
| 277 |
|
|---|
| 278 | /** Determines center of molecule (yet not considering atom masses).
|
|---|
| 279 | * \param center reference to return vector
|
|---|
| 280 | */
|
|---|
| 281 | void molecule::DeterminePeriodicCenter(Vector ¢er)
|
|---|
| 282 | {
|
|---|
| [5f612ee] | 283 | double * const cell_size = World::getInstance().getDomain();
|
|---|
| [c94eeb] | 284 | double *matrix_double = ReturnFullMatrixforSymmetric(cell_size);
|
|---|
| 285 | Matrix matrix = Matrix(matrix_double);
|
|---|
| 286 | delete[](matrix_double);
|
|---|
| 287 | Matrix inversematrix = matrix.invert();
|
|---|
| [cee0b57] | 288 | double tmp;
|
|---|
| 289 | bool flag;
|
|---|
| 290 | Vector Testvector, Translationvector;
|
|---|
| 291 |
|
|---|
| 292 | do {
|
|---|
| 293 | Center.Zero();
|
|---|
| 294 | flag = true;
|
|---|
| [9879f6] | 295 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [cee0b57] | 296 | #ifdef ADDHYDROGEN
|
|---|
| [9879f6] | 297 | if ((*iter)->type->Z != 1) {
|
|---|
| [cee0b57] | 298 | #endif
|
|---|
| [a7b761b] | 299 | Testvector = (*iter)->x;
|
|---|
| [1614174] | 300 | Testvector.MatrixMultiplication(inversematrix);
|
|---|
| [cee0b57] | 301 | Translationvector.Zero();
|
|---|
| [9879f6] | 302 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
|---|
| 303 | if ((*iter)->nr < (*Runner)->GetOtherAtom((*iter))->nr) // otherwise we shift one to, the other fro and gain nothing
|
|---|
| [cee0b57] | 304 | for (int j=0;j<NDIM;j++) {
|
|---|
| [a7b761b] | 305 | tmp = (*iter)->x[j] - (*Runner)->GetOtherAtom(*iter)->x[j];
|
|---|
| [cee0b57] | 306 | if ((fabs(tmp)) > BondDistance) {
|
|---|
| 307 | flag = false;
|
|---|
| [a7b761b] | 308 | DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
|
|---|
| [cee0b57] | 309 | if (tmp > 0)
|
|---|
| [0a4f7f] | 310 | Translationvector[j] -= 1.;
|
|---|
| [cee0b57] | 311 | else
|
|---|
| [0a4f7f] | 312 | Translationvector[j] += 1.;
|
|---|
| [cee0b57] | 313 | }
|
|---|
| 314 | }
|
|---|
| 315 | }
|
|---|
| [273382] | 316 | Testvector += Translationvector;
|
|---|
| [cee0b57] | 317 | Testvector.MatrixMultiplication(matrix);
|
|---|
| [273382] | 318 | Center += Testvector;
|
|---|
| [0a4f7f] | 319 | Log() << Verbose(1) << "vector is: " << Testvector << endl;
|
|---|
| [cee0b57] | 320 | #ifdef ADDHYDROGEN
|
|---|
| 321 | // now also change all hydrogens
|
|---|
| [9879f6] | 322 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
|---|
| 323 | if ((*Runner)->GetOtherAtom((*iter))->type->Z == 1) {
|
|---|
| [a7b761b] | 324 | Testvector = (*Runner)->GetOtherAtom((*iter))->x;
|
|---|
| [1614174] | 325 | Testvector.MatrixMultiplication(inversematrix);
|
|---|
| [273382] | 326 | Testvector += Translationvector;
|
|---|
| [cee0b57] | 327 | Testvector.MatrixMultiplication(matrix);
|
|---|
| [273382] | 328 | Center += Testvector;
|
|---|
| [0a4f7f] | 329 | Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
|
|---|
| [cee0b57] | 330 | }
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 | #endif
|
|---|
| 334 | }
|
|---|
| 335 | } while (!flag);
|
|---|
| [1614174] | 336 |
|
|---|
| [ea7176] | 337 | Center.Scale(1./static_cast<double>(getAtomCount()));
|
|---|
| [cee0b57] | 338 | };
|
|---|
| 339 |
|
|---|
| 340 | /** Transforms/Rotates the given molecule into its principal axis system.
|
|---|
| 341 | * \param *out output stream for debugging
|
|---|
| 342 | * \param DoRotate whether to rotate (true) or only to determine the PAS.
|
|---|
| 343 | * TODO treatment of trajetories missing
|
|---|
| 344 | */
|
|---|
| [e138de] | 345 | void molecule::PrincipalAxisSystem(bool DoRotate)
|
|---|
| [cee0b57] | 346 | {
|
|---|
| 347 | double InertiaTensor[NDIM*NDIM];
|
|---|
| [e138de] | 348 | Vector *CenterOfGravity = DetermineCenterOfGravity();
|
|---|
| [cee0b57] | 349 |
|
|---|
| [e138de] | 350 | CenterPeriodic();
|
|---|
| [cee0b57] | 351 |
|
|---|
| 352 | // reset inertia tensor
|
|---|
| 353 | for(int i=0;i<NDIM*NDIM;i++)
|
|---|
| 354 | InertiaTensor[i] = 0.;
|
|---|
| 355 |
|
|---|
| 356 | // sum up inertia tensor
|
|---|
| [9879f6] | 357 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [a7b761b] | 358 | Vector x = (*iter)->x;
|
|---|
| [cee0b57] | 359 | //x.SubtractVector(CenterOfGravity);
|
|---|
| [a7b761b] | 360 | InertiaTensor[0] += (*iter)->type->mass*(x[1]*x[1] + x[2]*x[2]);
|
|---|
| 361 | InertiaTensor[1] += (*iter)->type->mass*(-x[0]*x[1]);
|
|---|
| 362 | InertiaTensor[2] += (*iter)->type->mass*(-x[0]*x[2]);
|
|---|
| 363 | InertiaTensor[3] += (*iter)->type->mass*(-x[1]*x[0]);
|
|---|
| 364 | InertiaTensor[4] += (*iter)->type->mass*(x[0]*x[0] + x[2]*x[2]);
|
|---|
| 365 | InertiaTensor[5] += (*iter)->type->mass*(-x[1]*x[2]);
|
|---|
| 366 | InertiaTensor[6] += (*iter)->type->mass*(-x[2]*x[0]);
|
|---|
| 367 | InertiaTensor[7] += (*iter)->type->mass*(-x[2]*x[1]);
|
|---|
| 368 | InertiaTensor[8] += (*iter)->type->mass*(x[0]*x[0] + x[1]*x[1]);
|
|---|
| [cee0b57] | 369 | }
|
|---|
| 370 | // print InertiaTensor for debugging
|
|---|
| [a67d19] | 371 | DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
|
|---|
| [cee0b57] | 372 | for(int i=0;i<NDIM;i++) {
|
|---|
| 373 | for(int j=0;j<NDIM;j++)
|
|---|
| [a67d19] | 374 | DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
|
|---|
| 375 | DoLog(0) && (Log() << Verbose(0) << endl);
|
|---|
| [cee0b57] | 376 | }
|
|---|
| [a67d19] | 377 | DoLog(0) && (Log() << Verbose(0) << endl);
|
|---|
| [cee0b57] | 378 |
|
|---|
| 379 | // diagonalize to determine principal axis system
|
|---|
| 380 | gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM);
|
|---|
| 381 | gsl_matrix_view m = gsl_matrix_view_array(InertiaTensor, NDIM, NDIM);
|
|---|
| 382 | gsl_vector *eval = gsl_vector_alloc(NDIM);
|
|---|
| 383 | gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM);
|
|---|
| 384 | gsl_eigen_symmv(&m.matrix, eval, evec, T);
|
|---|
| 385 | gsl_eigen_symmv_free(T);
|
|---|
| 386 | gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC);
|
|---|
| 387 |
|
|---|
| 388 | for(int i=0;i<NDIM;i++) {
|
|---|
| [a67d19] | 389 | DoLog(1) && (Log() << Verbose(1) << "eigenvalue = " << gsl_vector_get(eval, i));
|
|---|
| 390 | 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] | 391 | }
|
|---|
| 392 |
|
|---|
| 393 | // check whether we rotate or not
|
|---|
| 394 | if (DoRotate) {
|
|---|
| [a67d19] | 395 | DoLog(1) && (Log() << Verbose(1) << "Transforming molecule into PAS ... ");
|
|---|
| [cee0b57] | 396 | // the eigenvectors specify the transformation matrix
|
|---|
| [c94eeb] | 397 | Matrix M = Matrix(evec->data);
|
|---|
| 398 | ActOnAllVectors( &Vector::MatrixMultiplication, static_cast<const Matrix>(M));
|
|---|
| [a67d19] | 399 | DoLog(0) && (Log() << Verbose(0) << "done." << endl);
|
|---|
| [cee0b57] | 400 |
|
|---|
| 401 | // summing anew for debugging (resulting matrix has to be diagonal!)
|
|---|
| 402 | // reset inertia tensor
|
|---|
| 403 | for(int i=0;i<NDIM*NDIM;i++)
|
|---|
| 404 | InertiaTensor[i] = 0.;
|
|---|
| 405 |
|
|---|
| 406 | // sum up inertia tensor
|
|---|
| [9879f6] | 407 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [a7b761b] | 408 | Vector x = (*iter)->x;
|
|---|
| 409 | InertiaTensor[0] += (*iter)->type->mass*(x[1]*x[1] + x[2]*x[2]);
|
|---|
| 410 | InertiaTensor[1] += (*iter)->type->mass*(-x[0]*x[1]);
|
|---|
| 411 | InertiaTensor[2] += (*iter)->type->mass*(-x[0]*x[2]);
|
|---|
| 412 | InertiaTensor[3] += (*iter)->type->mass*(-x[1]*x[0]);
|
|---|
| 413 | InertiaTensor[4] += (*iter)->type->mass*(x[0]*x[0] + x[2]*x[2]);
|
|---|
| 414 | InertiaTensor[5] += (*iter)->type->mass*(-x[1]*x[2]);
|
|---|
| 415 | InertiaTensor[6] += (*iter)->type->mass*(-x[2]*x[0]);
|
|---|
| 416 | InertiaTensor[7] += (*iter)->type->mass*(-x[2]*x[1]);
|
|---|
| 417 | InertiaTensor[8] += (*iter)->type->mass*(x[0]*x[0] + x[1]*x[1]);
|
|---|
| [cee0b57] | 418 | }
|
|---|
| 419 | // print InertiaTensor for debugging
|
|---|
| [a67d19] | 420 | DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
|
|---|
| [cee0b57] | 421 | for(int i=0;i<NDIM;i++) {
|
|---|
| 422 | for(int j=0;j<NDIM;j++)
|
|---|
| [a67d19] | 423 | DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
|
|---|
| 424 | DoLog(0) && (Log() << Verbose(0) << endl);
|
|---|
| [cee0b57] | 425 | }
|
|---|
| [a67d19] | 426 | DoLog(0) && (Log() << Verbose(0) << endl);
|
|---|
| [cee0b57] | 427 | }
|
|---|
| 428 |
|
|---|
| 429 | // free everything
|
|---|
| 430 | delete(CenterOfGravity);
|
|---|
| 431 | gsl_vector_free(eval);
|
|---|
| 432 | gsl_matrix_free(evec);
|
|---|
| 433 | };
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 | /** Align all atoms in such a manner that given vector \a *n is along z axis.
|
|---|
| 437 | * \param n[] alignment vector.
|
|---|
| 438 | */
|
|---|
| 439 | void molecule::Align(Vector *n)
|
|---|
| 440 | {
|
|---|
| 441 | double alpha, tmp;
|
|---|
| 442 | Vector z_axis;
|
|---|
| [0a4f7f] | 443 | z_axis[0] = 0.;
|
|---|
| 444 | z_axis[1] = 0.;
|
|---|
| 445 | z_axis[2] = 1.;
|
|---|
| [cee0b57] | 446 |
|
|---|
| 447 | // rotate on z-x plane
|
|---|
| [a67d19] | 448 | DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
|
|---|
| [0a4f7f] | 449 | alpha = atan(-n->at(0)/n->at(2));
|
|---|
| [a67d19] | 450 | DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
|
|---|
| [9879f6] | 451 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [a7b761b] | 452 | tmp = (*iter)->x[0];
|
|---|
| 453 | (*iter)->x[0] = cos(alpha) * tmp + sin(alpha) * (*iter)->x[2];
|
|---|
| 454 | (*iter)->x[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->x[2];
|
|---|
| [cee0b57] | 455 | for (int j=0;j<MDSteps;j++) {
|
|---|
| [a7b761b] | 456 | tmp = (*iter)->Trajectory.R.at(j)[0];
|
|---|
| 457 | (*iter)->Trajectory.R.at(j)[0] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
|---|
| 458 | (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
|---|
| [cee0b57] | 459 | }
|
|---|
| 460 | }
|
|---|
| 461 | // rotate n vector
|
|---|
| [0a4f7f] | 462 | tmp = n->at(0);
|
|---|
| 463 | n->at(0) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
|---|
| 464 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
|---|
| [8cbb97] | 465 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
|
|---|
| [cee0b57] | 466 |
|
|---|
| 467 | // rotate on z-y plane
|
|---|
| [0a4f7f] | 468 | alpha = atan(-n->at(1)/n->at(2));
|
|---|
| [a67d19] | 469 | DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
|
|---|
| [9879f6] | 470 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [a7b761b] | 471 | tmp = (*iter)->x[1];
|
|---|
| 472 | (*iter)->x[1] = cos(alpha) * tmp + sin(alpha) * (*iter)->x[2];
|
|---|
| 473 | (*iter)->x[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->x[2];
|
|---|
| [cee0b57] | 474 | for (int j=0;j<MDSteps;j++) {
|
|---|
| [a7b761b] | 475 | tmp = (*iter)->Trajectory.R.at(j)[1];
|
|---|
| 476 | (*iter)->Trajectory.R.at(j)[1] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
|---|
| 477 | (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
|---|
| [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) {
|
|---|
| 511 | if ((*iter)->type == ((struct lsq_params *)params)->type) { // for specific type
|
|---|
| [a7b761b] | 512 | c = (*iter)->x - 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 | };
|
|---|