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