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 "verbose.hpp"
|
---|
17 | #include "log.hpp"
|
---|
18 | #include "molecule.hpp"
|
---|
19 | #include "World.hpp"
|
---|
20 | #include "Plane.hpp"
|
---|
21 | #include "Matrix.hpp"
|
---|
22 | #include "Box.hpp"
|
---|
23 | #include <boost/foreach.hpp>
|
---|
24 |
|
---|
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 | */
|
---|
32 | bool molecule::CenterInBox()
|
---|
33 | {
|
---|
34 | bool status = true;
|
---|
35 | const Vector *Center = DetermineCenterOfAll();
|
---|
36 | const Vector *CenterBox = DetermineCenterOfBox();
|
---|
37 | Box &domain = World::getInstance().getDomain();
|
---|
38 |
|
---|
39 | // go through all atoms
|
---|
40 | ActOnAllVectors( &Vector::SubtractVector, *Center);
|
---|
41 | ActOnAllVectors( &Vector::SubtractVector, *CenterBox);
|
---|
42 | BOOST_FOREACH(atom* iter, atoms){
|
---|
43 | *iter->node = domain.WrapPeriodically(*iter->node);
|
---|
44 | }
|
---|
45 |
|
---|
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 | Box &domain = World::getInstance().getDomain();
|
---|
59 |
|
---|
60 | // go through all atoms
|
---|
61 | BOOST_FOREACH(atom* iter, atoms){
|
---|
62 | *iter->node = domain.WrapPeriodically(*iter->node);
|
---|
63 | }
|
---|
64 |
|
---|
65 | return status;
|
---|
66 | };
|
---|
67 |
|
---|
68 | /** Centers the edge of the atoms at (0,0,0).
|
---|
69 | * \param *out output stream for debugging
|
---|
70 | * \param *max coordinates of other edge, specifying box dimensions.
|
---|
71 | */
|
---|
72 | void molecule::CenterEdge(Vector *max)
|
---|
73 | {
|
---|
74 | Vector *min = new Vector;
|
---|
75 |
|
---|
76 | // Log() << Verbose(3) << "Begin of CenterEdge." << endl;
|
---|
77 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
78 | if (iter != end()) { //list not empty?
|
---|
79 | for (int i=NDIM;i--;) {
|
---|
80 | max->at(i) = (*iter)->x[i];
|
---|
81 | min->at(i) = (*iter)->x[i];
|
---|
82 | }
|
---|
83 | for (; iter != end(); ++iter) {// continue with second if present
|
---|
84 | //(*iter)->Output(1,1,out);
|
---|
85 | for (int i=NDIM;i--;) {
|
---|
86 | max->at(i) = (max->at(i) < (*iter)->x[i]) ? (*iter)->x[i] : max->at(i);
|
---|
87 | min->at(i) = (min->at(i) > (*iter)->x[i]) ? (*iter)->x[i] : min->at(i);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | // Log() << Verbose(4) << "Maximum is ";
|
---|
91 | // max->Output(out);
|
---|
92 | // Log() << Verbose(0) << ", Minimum is ";
|
---|
93 | // min->Output(out);
|
---|
94 | // Log() << Verbose(0) << endl;
|
---|
95 | min->Scale(-1.);
|
---|
96 | (*max) += (*min);
|
---|
97 | Translate(min);
|
---|
98 | Center.Zero();
|
---|
99 | }
|
---|
100 | delete(min);
|
---|
101 | // Log() << Verbose(3) << "End of CenterEdge." << endl;
|
---|
102 | };
|
---|
103 |
|
---|
104 | /** Centers the center of the atoms at (0,0,0).
|
---|
105 | * \param *out output stream for debugging
|
---|
106 | * \param *center return vector for translation vector
|
---|
107 | */
|
---|
108 | void molecule::CenterOrigin()
|
---|
109 | {
|
---|
110 | int Num = 0;
|
---|
111 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
112 |
|
---|
113 | Center.Zero();
|
---|
114 |
|
---|
115 | if (iter != end()) { //list not empty?
|
---|
116 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
117 | Num++;
|
---|
118 | Center += (*iter)->x;
|
---|
119 | }
|
---|
120 | Center.Scale(-1./Num); // divide through total number (and sign for direction)
|
---|
121 | Translate(&Center);
|
---|
122 | Center.Zero();
|
---|
123 | }
|
---|
124 | };
|
---|
125 |
|
---|
126 | /** Returns vector pointing to center of all atoms.
|
---|
127 | * \return pointer to center of all vector
|
---|
128 | */
|
---|
129 | Vector * molecule::DetermineCenterOfAll() const
|
---|
130 | {
|
---|
131 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
132 | Vector *a = new Vector();
|
---|
133 | double Num = 0;
|
---|
134 |
|
---|
135 | a->Zero();
|
---|
136 |
|
---|
137 | if (iter != end()) { //list not empty?
|
---|
138 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
139 | Num++;
|
---|
140 | (*a) += (*iter)->x;
|
---|
141 | }
|
---|
142 | a->Scale(1./Num); // divide through total mass (and sign for direction)
|
---|
143 | }
|
---|
144 | return a;
|
---|
145 | };
|
---|
146 |
|
---|
147 | /** Returns vector pointing to center of the domain.
|
---|
148 | * \return pointer to center of the domain
|
---|
149 | */
|
---|
150 | Vector * molecule::DetermineCenterOfBox() const
|
---|
151 | {
|
---|
152 | Vector *a = new Vector(0.5,0.5,0.5);
|
---|
153 | const Matrix &M = World::getInstance().getDomain().getM();
|
---|
154 | (*a) *= M;
|
---|
155 | return a;
|
---|
156 | };
|
---|
157 |
|
---|
158 | /** Returns vector pointing to center of gravity.
|
---|
159 | * \param *out output stream for debugging
|
---|
160 | * \return pointer to center of gravity vector
|
---|
161 | */
|
---|
162 | Vector * molecule::DetermineCenterOfGravity()
|
---|
163 | {
|
---|
164 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
165 | Vector *a = new Vector();
|
---|
166 | Vector tmp;
|
---|
167 | double Num = 0;
|
---|
168 |
|
---|
169 | a->Zero();
|
---|
170 |
|
---|
171 | if (iter != end()) { //list not empty?
|
---|
172 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
173 | Num += (*iter)->type->mass;
|
---|
174 | tmp = (*iter)->type->mass * (*iter)->x;
|
---|
175 | (*a) += tmp;
|
---|
176 | }
|
---|
177 | a->Scale(1./Num); // divide through total mass (and sign for direction)
|
---|
178 | }
|
---|
179 | // Log() << Verbose(1) << "Resulting center of gravity: ";
|
---|
180 | // a->Output(out);
|
---|
181 | // Log() << Verbose(0) << endl;
|
---|
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 | */
|
---|
189 | void molecule::CenterPeriodic()
|
---|
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 | */
|
---|
199 | void molecule::CenterAtVector(Vector *newcenter)
|
---|
200 | {
|
---|
201 | Center = *newcenter;
|
---|
202 | };
|
---|
203 |
|
---|
204 |
|
---|
205 | /** Scales all atoms by \a *factor.
|
---|
206 | * \param *factor pointer to scaling factor
|
---|
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)
|
---|
212 | */
|
---|
213 | void molecule::Scale(const double ** const factor)
|
---|
214 | {
|
---|
215 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
216 | for (int j=0;j<MDSteps;j++)
|
---|
217 | (*iter)->Trajectory.R.at(j).ScaleAll(*factor);
|
---|
218 | (*iter)->x.ScaleAll(*factor);
|
---|
219 | }
|
---|
220 | };
|
---|
221 |
|
---|
222 | /** Translate all atoms by given vector.
|
---|
223 | * \param trans[] translation vector.
|
---|
224 | */
|
---|
225 | void molecule::Translate(const Vector *trans)
|
---|
226 | {
|
---|
227 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
228 | for (int j=0;j<MDSteps;j++)
|
---|
229 | (*iter)->Trajectory.R.at(j) += (*trans);
|
---|
230 | (*iter)->x += (*trans);
|
---|
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 | {
|
---|
240 | Box &domain = World::getInstance().getDomain();
|
---|
241 |
|
---|
242 | // go through all atoms
|
---|
243 | ActOnAllVectors( &Vector::AddVector, *trans);
|
---|
244 | BOOST_FOREACH(atom* iter, atoms){
|
---|
245 | *iter->node = domain.WrapPeriodically(*iter->node);
|
---|
246 | }
|
---|
247 |
|
---|
248 | };
|
---|
249 |
|
---|
250 |
|
---|
251 | /** Mirrors all atoms against a given plane.
|
---|
252 | * \param n[] normal vector of mirror plane.
|
---|
253 | */
|
---|
254 | void molecule::Mirror(const Vector *n)
|
---|
255 | {
|
---|
256 | OBSERVE;
|
---|
257 | Plane p(*n,0);
|
---|
258 | BOOST_FOREACH(atom* iter, atoms ){
|
---|
259 | (*iter->node) = p.mirrorVector(*iter->node);
|
---|
260 | }
|
---|
261 | };
|
---|
262 |
|
---|
263 | /** Determines center of molecule (yet not considering atom masses).
|
---|
264 | * \param center reference to return vector
|
---|
265 | */
|
---|
266 | void molecule::DeterminePeriodicCenter(Vector ¢er)
|
---|
267 | {
|
---|
268 | const Matrix &matrix = World::getInstance().getDomain().getM();
|
---|
269 | const Matrix &inversematrix = World::getInstance().getDomain().getM();
|
---|
270 | double tmp;
|
---|
271 | bool flag;
|
---|
272 | Vector Testvector, Translationvector;
|
---|
273 |
|
---|
274 | do {
|
---|
275 | Center.Zero();
|
---|
276 | flag = true;
|
---|
277 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
278 | #ifdef ADDHYDROGEN
|
---|
279 | if ((*iter)->type->Z != 1) {
|
---|
280 | #endif
|
---|
281 | Testvector = inversematrix * (*iter)->x;
|
---|
282 | Translationvector.Zero();
|
---|
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
|
---|
285 | for (int j=0;j<NDIM;j++) {
|
---|
286 | tmp = (*iter)->x[j] - (*Runner)->GetOtherAtom(*iter)->x[j];
|
---|
287 | if ((fabs(tmp)) > BondDistance) {
|
---|
288 | flag = false;
|
---|
289 | DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
|
---|
290 | if (tmp > 0)
|
---|
291 | Translationvector[j] -= 1.;
|
---|
292 | else
|
---|
293 | Translationvector[j] += 1.;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 | Testvector += Translationvector;
|
---|
298 | Testvector *= matrix;
|
---|
299 | Center += Testvector;
|
---|
300 | Log() << Verbose(1) << "vector is: " << Testvector << endl;
|
---|
301 | #ifdef ADDHYDROGEN
|
---|
302 | // now also change all hydrogens
|
---|
303 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
---|
304 | if ((*Runner)->GetOtherAtom((*iter))->type->Z == 1) {
|
---|
305 | Testvector = inversematrix * (*Runner)->GetOtherAtom((*iter))->x;
|
---|
306 | Testvector += Translationvector;
|
---|
307 | Testvector *= matrix;
|
---|
308 | Center += Testvector;
|
---|
309 | Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 | #endif
|
---|
314 | }
|
---|
315 | } while (!flag);
|
---|
316 |
|
---|
317 | Center.Scale(1./static_cast<double>(getAtomCount()));
|
---|
318 | };
|
---|
319 |
|
---|
320 | /** Transforms/Rotates the given molecule into its principal axis system.
|
---|
321 | * \param *out output stream for debugging
|
---|
322 | * \param DoRotate whether to rotate (true) or only to determine the PAS.
|
---|
323 | * TODO treatment of trajetories missing
|
---|
324 | */
|
---|
325 | void molecule::PrincipalAxisSystem(bool DoRotate)
|
---|
326 | {
|
---|
327 | double InertiaTensor[NDIM*NDIM];
|
---|
328 | Vector *CenterOfGravity = DetermineCenterOfGravity();
|
---|
329 |
|
---|
330 | CenterPeriodic();
|
---|
331 |
|
---|
332 | // reset inertia tensor
|
---|
333 | for(int i=0;i<NDIM*NDIM;i++)
|
---|
334 | InertiaTensor[i] = 0.;
|
---|
335 |
|
---|
336 | // sum up inertia tensor
|
---|
337 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
338 | Vector x = (*iter)->x;
|
---|
339 | //x.SubtractVector(CenterOfGravity);
|
---|
340 | InertiaTensor[0] += (*iter)->type->mass*(x[1]*x[1] + x[2]*x[2]);
|
---|
341 | InertiaTensor[1] += (*iter)->type->mass*(-x[0]*x[1]);
|
---|
342 | InertiaTensor[2] += (*iter)->type->mass*(-x[0]*x[2]);
|
---|
343 | InertiaTensor[3] += (*iter)->type->mass*(-x[1]*x[0]);
|
---|
344 | InertiaTensor[4] += (*iter)->type->mass*(x[0]*x[0] + x[2]*x[2]);
|
---|
345 | InertiaTensor[5] += (*iter)->type->mass*(-x[1]*x[2]);
|
---|
346 | InertiaTensor[6] += (*iter)->type->mass*(-x[2]*x[0]);
|
---|
347 | InertiaTensor[7] += (*iter)->type->mass*(-x[2]*x[1]);
|
---|
348 | InertiaTensor[8] += (*iter)->type->mass*(x[0]*x[0] + x[1]*x[1]);
|
---|
349 | }
|
---|
350 | // print InertiaTensor for debugging
|
---|
351 | DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
|
---|
352 | for(int i=0;i<NDIM;i++) {
|
---|
353 | for(int j=0;j<NDIM;j++)
|
---|
354 | DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
|
---|
355 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
356 | }
|
---|
357 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
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++) {
|
---|
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);
|
---|
371 | }
|
---|
372 |
|
---|
373 | // check whether we rotate or not
|
---|
374 | if (DoRotate) {
|
---|
375 | DoLog(1) && (Log() << Verbose(1) << "Transforming molecule into PAS ... ");
|
---|
376 | // the eigenvectors specify the transformation matrix
|
---|
377 | Matrix M = Matrix(evec->data);
|
---|
378 | BOOST_FOREACH(atom* iter, atoms){
|
---|
379 | (*iter->node) *= M;
|
---|
380 | }
|
---|
381 | DoLog(0) && (Log() << Verbose(0) << "done." << endl);
|
---|
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
|
---|
389 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
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]);
|
---|
400 | }
|
---|
401 | // print InertiaTensor for debugging
|
---|
402 | DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
|
---|
403 | for(int i=0;i<NDIM;i++) {
|
---|
404 | for(int j=0;j<NDIM;j++)
|
---|
405 | DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
|
---|
406 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
407 | }
|
---|
408 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
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;
|
---|
425 | z_axis[0] = 0.;
|
---|
426 | z_axis[1] = 0.;
|
---|
427 | z_axis[2] = 1.;
|
---|
428 |
|
---|
429 | // rotate on z-x plane
|
---|
430 | DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
|
---|
431 | alpha = atan(-n->at(0)/n->at(2));
|
---|
432 | DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
|
---|
433 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
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];
|
---|
437 | for (int j=0;j<MDSteps;j++) {
|
---|
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];
|
---|
441 | }
|
---|
442 | }
|
---|
443 | // rotate n vector
|
---|
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);
|
---|
447 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
|
---|
448 |
|
---|
449 | // rotate on z-y plane
|
---|
450 | alpha = atan(-n->at(1)/n->at(2));
|
---|
451 | DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
|
---|
452 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
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];
|
---|
456 | for (int j=0;j<MDSteps;j++) {
|
---|
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];
|
---|
460 | }
|
---|
461 | }
|
---|
462 | // rotate n vector (for consistency check)
|
---|
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);
|
---|
466 |
|
---|
467 |
|
---|
468 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
|
---|
469 | DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
|
---|
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
|
---|
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);
|
---|
491 | // go through all atoms
|
---|
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
|
---|
494 | c = (*iter)->x - a;
|
---|
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
|
---|
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 | };
|
---|