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