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