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