source: molecuilder/src/vector.cpp@ 66ce7a

Last change on this file since 66ce7a was da84d6, checked in by Frederik Heber <heber@…>, 16 years ago

Incorporation of Unit test on class Vector.

  • new file leastsquaremin.[ch]pp has least square minimisation which is otherwise unclean between classes molecules and Vector

Unit test (later tests rely on good results of earlier ones)

changes to class Vector:

  • Vector::IsNull() -> IsZero()
  • new function Vector::IsOne() similar to IsZero()
  • BUGFIX: Vector::IsNULL() (thx to unit test :)
  • Tesselation::getAngle() changed due to above rename
  • Property mode set to 100755
File size: 33.6 KB
Line 
1/** \file vector.cpp
2 *
3 * Function implementations for the class vector.
4 *
5 */
6
7
8#include "defs.hpp"
9#include "helpers.hpp"
10#include "leastsquaremin.hpp"
11#include "vector.hpp"
12#include "verbose.hpp"
13
14/************************************ Functions for class vector ************************************/
15
16/** Constructor of class vector.
17 */
18Vector::Vector() { x[0] = x[1] = x[2] = 0.; };
19
20/** Constructor of class vector.
21 */
22Vector::Vector(double x1, double x2, double x3) { x[0] = x1; x[1] = x2; x[2] = x3; };
23
24/** Desctructor of class vector.
25 */
26Vector::~Vector() {};
27
28/** Calculates square of distance between this and another vector.
29 * \param *y array to second vector
30 * \return \f$| x - y |^2\f$
31 */
32double Vector::DistanceSquared(const Vector *y) const
33{
34 double res = 0.;
35 for (int i=NDIM;i--;)
36 res += (x[i]-y->x[i])*(x[i]-y->x[i]);
37 return (res);
38};
39
40/** Calculates distance between this and another vector.
41 * \param *y array to second vector
42 * \return \f$| x - y |\f$
43 */
44double Vector::Distance(const Vector *y) const
45{
46 double res = 0.;
47 for (int i=NDIM;i--;)
48 res += (x[i]-y->x[i])*(x[i]-y->x[i]);
49 return (sqrt(res));
50};
51
52/** Calculates distance between this and another vector in a periodic cell.
53 * \param *y array to second vector
54 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
55 * \return \f$| x - y |\f$
56 */
57double Vector::PeriodicDistance(const Vector *y, const double *cell_size) const
58{
59 double res = Distance(y), tmp, matrix[NDIM*NDIM];
60 Vector Shiftedy, TranslationVector;
61 int N[NDIM];
62 matrix[0] = cell_size[0];
63 matrix[1] = cell_size[1];
64 matrix[2] = cell_size[3];
65 matrix[3] = cell_size[1];
66 matrix[4] = cell_size[2];
67 matrix[5] = cell_size[4];
68 matrix[6] = cell_size[3];
69 matrix[7] = cell_size[4];
70 matrix[8] = cell_size[5];
71 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
72 for (N[0]=-1;N[0]<=1;N[0]++)
73 for (N[1]=-1;N[1]<=1;N[1]++)
74 for (N[2]=-1;N[2]<=1;N[2]++) {
75 // create the translation vector
76 TranslationVector.Zero();
77 for (int i=NDIM;i--;)
78 TranslationVector.x[i] = (double)N[i];
79 TranslationVector.MatrixMultiplication(matrix);
80 // add onto the original vector to compare with
81 Shiftedy.CopyVector(y);
82 Shiftedy.AddVector(&TranslationVector);
83 // get distance and compare with minimum so far
84 tmp = Distance(&Shiftedy);
85 if (tmp < res) res = tmp;
86 }
87 return (res);
88};
89
90/** Calculates distance between this and another vector in a periodic cell.
91 * \param *y array to second vector
92 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
93 * \return \f$| x - y |^2\f$
94 */
95double Vector::PeriodicDistanceSquared(const Vector *y, const double *cell_size) const
96{
97 double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
98 Vector Shiftedy, TranslationVector;
99 int N[NDIM];
100 matrix[0] = cell_size[0];
101 matrix[1] = cell_size[1];
102 matrix[2] = cell_size[3];
103 matrix[3] = cell_size[1];
104 matrix[4] = cell_size[2];
105 matrix[5] = cell_size[4];
106 matrix[6] = cell_size[3];
107 matrix[7] = cell_size[4];
108 matrix[8] = cell_size[5];
109 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
110 for (N[0]=-1;N[0]<=1;N[0]++)
111 for (N[1]=-1;N[1]<=1;N[1]++)
112 for (N[2]=-1;N[2]<=1;N[2]++) {
113 // create the translation vector
114 TranslationVector.Zero();
115 for (int i=NDIM;i--;)
116 TranslationVector.x[i] = (double)N[i];
117 TranslationVector.MatrixMultiplication(matrix);
118 // add onto the original vector to compare with
119 Shiftedy.CopyVector(y);
120 Shiftedy.AddVector(&TranslationVector);
121 // get distance and compare with minimum so far
122 tmp = DistanceSquared(&Shiftedy);
123 if (tmp < res) res = tmp;
124 }
125 return (res);
126};
127
128/** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
129 * \param *out ofstream for debugging messages
130 * Tries to translate a vector into each adjacent neighbouring cell.
131 */
132void Vector::KeepPeriodic(ofstream *out, double *matrix)
133{
134// int N[NDIM];
135// bool flag = false;
136 //vector Shifted, TranslationVector;
137 Vector TestVector;
138// *out << Verbose(1) << "Begin of KeepPeriodic." << endl;
139// *out << Verbose(2) << "Vector is: ";
140// Output(out);
141// *out << endl;
142 TestVector.CopyVector(this);
143 TestVector.InverseMatrixMultiplication(matrix);
144 for(int i=NDIM;i--;) { // correct periodically
145 if (TestVector.x[i] < 0) { // get every coefficient into the interval [0,1)
146 TestVector.x[i] += ceil(TestVector.x[i]);
147 } else {
148 TestVector.x[i] -= floor(TestVector.x[i]);
149 }
150 }
151 TestVector.MatrixMultiplication(matrix);
152 CopyVector(&TestVector);
153// *out << Verbose(2) << "New corrected vector is: ";
154// Output(out);
155// *out << endl;
156// *out << Verbose(1) << "End of KeepPeriodic." << endl;
157};
158
159/** Calculates scalar product between this and another vector.
160 * \param *y array to second vector
161 * \return \f$\langle x, y \rangle\f$
162 */
163double Vector::ScalarProduct(const Vector *y) const
164{
165 double res = 0.;
166 for (int i=NDIM;i--;)
167 res += x[i]*y->x[i];
168 return (res);
169};
170
171
172/** Calculates VectorProduct between this and another vector.
173 * -# returns the Product in place of vector from which it was initiated
174 * -# ATTENTION: Only three dim.
175 * \param *y array to vector with which to calculate crossproduct
176 * \return \f$ x \times y \f&
177 */
178void Vector::VectorProduct(const Vector *y)
179{
180 Vector tmp;
181 tmp.x[0] = x[1]* (y->x[2]) - x[2]* (y->x[1]);
182 tmp.x[1] = x[2]* (y->x[0]) - x[0]* (y->x[2]);
183 tmp.x[2] = x[0]* (y->x[1]) - x[1]* (y->x[0]);
184 this->CopyVector(&tmp);
185
186};
187
188
189/** projects this vector onto plane defined by \a *y.
190 * \param *y normal vector of plane
191 * \return \f$\langle x, y \rangle\f$
192 */
193void Vector::ProjectOntoPlane(const Vector *y)
194{
195 Vector tmp;
196 tmp.CopyVector(y);
197 tmp.Normalize();
198 tmp.Scale(ScalarProduct(&tmp));
199 this->SubtractVector(&tmp);
200};
201
202/** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
203 * According to [Bronstein] the vectorial plane equation is:
204 * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
205 * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
206 * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
207 * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
208 * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
209 * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
210 * of the line yields the intersection point on the plane.
211 * \param *out output stream for debugging
212 * \param *PlaneNormal Plane's normal vector
213 * \param *PlaneOffset Plane's offset vector
214 * \param *LineVector first vector of line
215 * \param *LineVector2 second vector of line
216 * \return true - \a this contains intersection point on return, false - line is parallel to plane
217 */
218bool Vector::GetIntersectionWithPlane(ofstream *out, Vector *PlaneNormal, Vector *PlaneOffset, Vector *Origin, Vector *LineVector)
219{
220 double factor;
221 Vector Direction, helper;
222
223 // find intersection of a line defined by Offset and Direction with a plane defined by triangle
224 Direction.CopyVector(LineVector);
225 Direction.SubtractVector(Origin);
226 factor = Direction.ScalarProduct(PlaneNormal);
227 if (factor < MYEPSILON) { // Uniqueness: line parallel to plane?
228 *out << Verbose(2) << "WARNING: Line is parallel to plane, no intersection." << endl;
229 return false;
230 }
231 helper.CopyVector(PlaneOffset);
232 helper.SubtractVector(LineVector);
233 factor = helper.ScalarProduct(PlaneNormal)/factor;
234 //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
235 Direction.Scale(factor);
236 CopyVector(LineVector);
237 AddVector(&Direction);
238
239 // test whether resulting vector really is on plane
240 helper.CopyVector(this);
241 helper.SubtractVector(PlaneOffset);
242 if (helper.ScalarProduct(PlaneNormal) < MYEPSILON) {
243 *out << Verbose(2) << "INFO: Intersection at " << *this << " is good." << endl;
244 return true;
245 } else {
246 *out << Verbose(2) << "WARNING: Intersection point " << *this << " is not on plane." << endl;
247 return false;
248 }
249};
250
251/** Calculates the intersection of the two lines that are both on the same plane.
252 * Note that we do not check whether they are on the same plane. Vector is calculated with respecy to second line.
253 * \param *out output stream for debugging
254 * \param *Line1a first vector of first line
255 * \param *Line1b second vector of first line
256 * \param *Line2a first vector of second line
257 * \param *Line2b second vector of second line
258 * \param *PlaneNormal normal of plane, is supplemental/arbitrary
259 * \return true - \a this will contain the intersection on return, false - lines are parallel
260 */
261bool Vector::GetIntersectionOfTwoLinesOnPlane(ofstream *out, Vector *Line1a, Vector *Line1b, Vector *Line2a, Vector *Line2b, const Vector *PlaneNormal)
262{
263 double factor1, factor2;
264 Vector helper, Line, LineNormal, *OtherNormal = NULL;
265 const Vector *Normal;
266 bool result = false;
267
268 // create Plane normal vector
269 if (PlaneNormal == NULL) {
270 OtherNormal = new Vector(0.,0.,0.);
271 if (!OtherNormal->MakeNormalVector(Line1a, Line1b, Line2a))
272 if (!OtherNormal->MakeNormalVector(Line1a, Line1b, Line2b)) {
273 *out << Verbose(1) << "ERROR: GetIntersectionOfTwoLinesOnPlane() cannot create a normal of the plane, everything is linear dependent." << endl;
274 return false;
275 }
276 Normal = OtherNormal;
277 } else
278 Normal = PlaneNormal;
279 *out << Verbose(3) << "INFO: Normal of plane is " << *Normal << "." << endl;
280
281 // check if lines are parallel
282 helper.CopyVector(Line2b);
283 helper.SubtractVector(Line2a);
284 if (fabs(helper.ScalarProduct(Normal)) < MYEPSILON) {
285 *out << Verbose(1) << "Lines " << helper << " and " << Line << " are parallel, no cross point!" << endl;
286 result = false;
287 } else {
288 helper.CopyVector(Line2a);
289 helper.SubtractVector(Line1a);
290 factor1 = helper.ScalarProduct(Normal);
291 helper.CopyVector(Line2b);
292 helper.SubtractVector(Line1a);
293 factor2 = helper.ScalarProduct(Normal);
294 if (fabs(factor2) > MYEPSILON) {
295 CopyVector(Line2a);
296 helper.Scale(factor1/factor2);
297 AddVector(&helper);
298 result = true;
299 } else {
300 Zero();
301 result = false;
302 }
303 }
304
305 if (OtherNormal != NULL)
306 delete(OtherNormal);
307
308 return result;
309};
310
311/** Calculates the projection of a vector onto another \a *y.
312 * \param *y array to second vector
313 * \return \f$\langle x, y \rangle\f$
314 */
315double Vector::Projection(const Vector *y) const
316{
317 return (ScalarProduct(y));
318};
319
320/** Calculates norm of this vector.
321 * \return \f$|x|\f$
322 */
323double Vector::Norm() const
324{
325 double res = 0.;
326 for (int i=NDIM;i--;)
327 res += this->x[i]*this->x[i];
328 return (sqrt(res));
329};
330
331/** Calculates squared norm of this vector.
332 * \return \f$|x|^2\f$
333 */
334double Vector::NormSquared() const
335{
336 return (ScalarProduct(this));
337};
338
339/** Normalizes this vector.
340 */
341void Vector::Normalize()
342{
343 double res = 0.;
344 for (int i=NDIM;i--;)
345 res += this->x[i]*this->x[i];
346 if (fabs(res) > MYEPSILON)
347 res = 1./sqrt(res);
348 Scale(&res);
349};
350
351/** Zeros all components of this vector.
352 */
353void Vector::Zero()
354{
355 for (int i=NDIM;i--;)
356 this->x[i] = 0.;
357};
358
359/** Zeros all components of this vector.
360 */
361void Vector::One(double one)
362{
363 for (int i=NDIM;i--;)
364 this->x[i] = one;
365};
366
367/** Initialises all components of this vector.
368 */
369void Vector::Init(double x1, double x2, double x3)
370{
371 x[0] = x1;
372 x[1] = x2;
373 x[2] = x3;
374};
375
376/** Checks whether vector has all components zero.
377 * @return true - vector is zero, false - vector is not
378 */
379bool Vector::IsZero() const
380{
381 return (fabs(x[0])+fabs(x[1])+fabs(x[2]) < MYEPSILON);
382};
383
384/** Checks whether vector has length of 1.
385 * @return true - vector is normalized, false - vector is not
386 */
387bool Vector::IsOne() const
388{
389 return (fabs(Norm() - 1.) < MYEPSILON);
390};
391
392/** Calculates the angle between this and another vector.
393 * \param *y array to second vector
394 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
395 */
396double Vector::Angle(const Vector *y) const
397{
398 double norm1 = Norm(), norm2 = y->Norm();
399 double angle = 1;
400 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
401 angle = this->ScalarProduct(y)/norm1/norm2;
402 // -1-MYEPSILON occured due to numerical imprecision, catch ...
403 //cout << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
404 if (angle < -1)
405 angle = -1;
406 if (angle > 1)
407 angle = 1;
408 return acos(angle);
409};
410
411/** Rotates the vector around the axis given by \a *axis by an angle of \a alpha.
412 * \param *axis rotation axis
413 * \param alpha rotation angle in radian
414 */
415void Vector::RotateVector(const Vector *axis, const double alpha)
416{
417 Vector a,y;
418 // normalise this vector with respect to axis
419 a.CopyVector(this);
420 a.Scale(Projection(axis));
421 SubtractVector(&a);
422 // construct normal vector
423 y.MakeNormalVector(axis,this);
424 y.Scale(Norm());
425 // scale normal vector by sine and this vector by cosine
426 y.Scale(sin(alpha));
427 Scale(cos(alpha));
428 // add scaled normal vector onto this vector
429 AddVector(&y);
430 // add part in axis direction
431 AddVector(&a);
432};
433
434/** Sums vector \a to this lhs component-wise.
435 * \param a base vector
436 * \param b vector components to add
437 * \return lhs + a
438 */
439Vector& operator+=(Vector& a, const Vector& b)
440{
441 a.AddVector(&b);
442 return a;
443};
444
445/** Subtracts vector \a from this lhs component-wise.
446 * \param a base vector
447 * \param b vector components to add
448 * \return lhs - a
449 */
450Vector& operator-=(Vector& a, const Vector& b)
451{
452 a.SubtractVector(&b);
453 return a;
454};
455
456/** factor each component of \a a times a double \a m.
457 * \param a base vector
458 * \param m factor
459 * \return lhs.x[i] * m
460 */
461Vector& operator*=(Vector& a, const double m)
462{
463 a.Scale(m);
464 return a;
465};
466
467/** Sums two vectors \a and \b component-wise.
468 * \param a first vector
469 * \param b second vector
470 * \return a + b
471 */
472Vector& operator+(const Vector& a, const Vector& b)
473{
474 Vector *x = new Vector;
475 x->CopyVector(&a);
476 x->AddVector(&b);
477 return *x;
478};
479
480/** Subtracts vector \a from \b component-wise.
481 * \param a first vector
482 * \param b second vector
483 * \return a - b
484 */
485Vector& operator-(const Vector& a, const Vector& b)
486{
487 Vector *x = new Vector;
488 x->CopyVector(&a);
489 x->SubtractVector(&b);
490 return *x;
491};
492
493/** Factors given vector \a a times \a m.
494 * \param a vector
495 * \param m factor
496 * \return m * a
497 */
498Vector& operator*(const Vector& a, const double m)
499{
500 Vector *x = new Vector;
501 x->CopyVector(&a);
502 x->Scale(m);
503 return *x;
504};
505
506/** Factors given vector \a a times \a m.
507 * \param m factor
508 * \param a vector
509 * \return m * a
510 */
511Vector& operator*(const double m, const Vector& a )
512{
513 Vector *x = new Vector;
514 x->CopyVector(&a);
515 x->Scale(m);
516 return *x;
517};
518
519/** Prints a 3dim vector.
520 * prints no end of line.
521 * \param *out output stream
522 */
523bool Vector::Output(ofstream *out) const
524{
525 if (out != NULL) {
526 *out << "(";
527 for (int i=0;i<NDIM;i++) {
528 *out << x[i];
529 if (i != 2)
530 *out << ",";
531 }
532 *out << ")";
533 return true;
534 } else
535 return false;
536};
537
538ostream& operator<<(ostream& ost, const Vector& m)
539{
540 ost << "(";
541 for (int i=0;i<NDIM;i++) {
542 ost << m.x[i];
543 if (i != 2)
544 ost << ",";
545 }
546 ost << ")";
547 return ost;
548};
549
550/** Scales each atom coordinate by an individual \a factor.
551 * \param *factor pointer to scaling factor
552 */
553void Vector::Scale(double **factor)
554{
555 for (int i=NDIM;i--;)
556 x[i] *= (*factor)[i];
557};
558
559void Vector::Scale(double *factor)
560{
561 for (int i=NDIM;i--;)
562 x[i] *= *factor;
563};
564
565void Vector::Scale(double factor)
566{
567 for (int i=NDIM;i--;)
568 x[i] *= factor;
569};
570
571/** Translate atom by given vector.
572 * \param trans[] translation vector.
573 */
574void Vector::Translate(const Vector *trans)
575{
576 for (int i=NDIM;i--;)
577 x[i] += trans->x[i];
578};
579
580/** Do a matrix multiplication.
581 * \param *matrix NDIM_NDIM array
582 */
583void Vector::MatrixMultiplication(double *M)
584{
585 Vector C;
586 // do the matrix multiplication
587 C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
588 C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
589 C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
590 // transfer the result into this
591 for (int i=NDIM;i--;)
592 x[i] = C.x[i];
593};
594
595/** Calculate the inverse of a 3x3 matrix.
596 * \param *matrix NDIM_NDIM array
597 */
598double * Vector::InverseMatrix(double *A)
599{
600 double *B = (double *) Malloc(sizeof(double)*NDIM*NDIM, "Vector::InverseMatrix: *B");
601 double detA = RDET3(A);
602 double detAReci;
603
604 for (int i=0;i<NDIM*NDIM;++i)
605 B[i] = 0.;
606 // calculate the inverse B
607 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
608 detAReci = 1./detA;
609 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
610 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
611 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
612 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
613 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
614 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
615 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
616 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
617 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
618 }
619 return B;
620};
621
622/** Do a matrix multiplication with the \a *A' inverse.
623 * \param *matrix NDIM_NDIM array
624 */
625void Vector::InverseMatrixMultiplication(double *A)
626{
627 Vector C;
628 double B[NDIM*NDIM];
629 double detA = RDET3(A);
630 double detAReci;
631
632 // calculate the inverse B
633 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
634 detAReci = 1./detA;
635 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
636 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
637 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
638 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
639 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
640 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
641 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
642 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
643 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
644
645 // do the matrix multiplication
646 C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
647 C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
648 C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
649 // transfer the result into this
650 for (int i=NDIM;i--;)
651 x[i] = C.x[i];
652 } else {
653 cerr << "ERROR: inverse of matrix does not exists: det A = " << detA << "." << endl;
654 }
655};
656
657
658/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
659 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
660 * \param *x1 first vector
661 * \param *x2 second vector
662 * \param *x3 third vector
663 * \param *factors three-component vector with the factor for each given vector
664 */
665void Vector::LinearCombinationOfVectors(const Vector *x1, const Vector *x2, const Vector *x3, double *factors)
666{
667 for(int i=NDIM;i--;)
668 x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
669};
670
671/** Mirrors atom against a given plane.
672 * \param n[] normal vector of mirror plane.
673 */
674void Vector::Mirror(const Vector *n)
675{
676 double projection;
677 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one)
678 // withdraw projected vector twice from original one
679 cout << Verbose(1) << "Vector: ";
680 Output((ofstream *)&cout);
681 cout << "\t";
682 for (int i=NDIM;i--;)
683 x[i] -= 2.*projection*n->x[i];
684 cout << "Projected vector: ";
685 Output((ofstream *)&cout);
686 cout << endl;
687};
688
689/** Calculates normal vector for three given vectors (being three points in space).
690 * Makes this vector orthonormal to the three given points, making up a place in 3d space.
691 * \param *y1 first vector
692 * \param *y2 second vector
693 * \param *y3 third vector
694 * \return true - success, vectors are linear independent, false - failure due to linear dependency
695 */
696bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2, const Vector *y3)
697{
698 Vector x1, x2;
699
700 x1.CopyVector(y1);
701 x1.SubtractVector(y2);
702 x2.CopyVector(y3);
703 x2.SubtractVector(y2);
704 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
705 cout << Verbose(4) << "Given vectors are linear dependent." << endl;
706 return false;
707 }
708// cout << Verbose(4) << "relative, first plane coordinates:";
709// x1.Output((ofstream *)&cout);
710// cout << endl;
711// cout << Verbose(4) << "second plane coordinates:";
712// x2.Output((ofstream *)&cout);
713// cout << endl;
714
715 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
716 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
717 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
718 Normalize();
719
720 return true;
721};
722
723
724/** Calculates orthonormal vector to two given vectors.
725 * Makes this vector orthonormal to two given vectors. This is very similar to the other
726 * vector::MakeNormalVector(), only there three points whereas here two difference
727 * vectors are given.
728 * \param *x1 first vector
729 * \param *x2 second vector
730 * \return true - success, vectors are linear independent, false - failure due to linear dependency
731 */
732bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2)
733{
734 Vector x1,x2;
735 x1.CopyVector(y1);
736 x2.CopyVector(y2);
737 Zero();
738 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
739 cout << Verbose(4) << "Given vectors are linear dependent." << endl;
740 return false;
741 }
742// cout << Verbose(4) << "relative, first plane coordinates:";
743// x1.Output((ofstream *)&cout);
744// cout << endl;
745// cout << Verbose(4) << "second plane coordinates:";
746// x2.Output((ofstream *)&cout);
747// cout << endl;
748
749 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
750 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
751 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
752 Normalize();
753
754 return true;
755};
756
757/** Calculates orthonormal vector to one given vectors.
758 * Just subtracts the projection onto the given vector from this vector.
759 * \param *x1 vector
760 * \return true - success, false - vector is zero
761 */
762bool Vector::MakeNormalVector(const Vector *y1)
763{
764 bool result = false;
765 double factor = y1->Projection(this)/y1->Norm()/y1->Norm();
766 Vector x1;
767 x1.CopyVector(y1);
768 x1.Scale(factor);
769 SubtractVector(&x1);
770 for (int i=NDIM;i--;)
771 result = result || (fabs(x[i]) > MYEPSILON);
772
773 return result;
774};
775
776/** Creates this vector as one of the possible orthonormal ones to the given one.
777 * Just scan how many components of given *vector are unequal to zero and
778 * try to get the skp of both to be zero accordingly.
779 * \param *vector given vector
780 * \return true - success, false - failure (null vector given)
781 */
782bool Vector::GetOneNormalVector(const Vector *GivenVector)
783{
784 int Components[NDIM]; // contains indices of non-zero components
785 int Last = 0; // count the number of non-zero entries in vector
786 int j; // loop variables
787 double norm;
788
789 cout << Verbose(4);
790 GivenVector->Output((ofstream *)&cout);
791 cout << endl;
792 for (j=NDIM;j--;)
793 Components[j] = -1;
794 // find two components != 0
795 for (j=0;j<NDIM;j++)
796 if (fabs(GivenVector->x[j]) > MYEPSILON)
797 Components[Last++] = j;
798 cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
799
800 switch(Last) {
801 case 3: // threecomponent system
802 case 2: // two component system
803 norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
804 x[Components[2]] = 0.;
805 // in skp both remaining parts shall become zero but with opposite sign and third is zero
806 x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
807 x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
808 return true;
809 break;
810 case 1: // one component system
811 // set sole non-zero component to 0, and one of the other zero component pendants to 1
812 x[(Components[0]+2)%NDIM] = 0.;
813 x[(Components[0]+1)%NDIM] = 1.;
814 x[Components[0]] = 0.;
815 return true;
816 break;
817 default:
818 return false;
819 }
820};
821
822/** Determines paramter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
823 * \param *A first plane vector
824 * \param *B second plane vector
825 * \param *C third plane vector
826 * \return scaling parameter for this vector
827 */
828double Vector::CutsPlaneAt(Vector *A, Vector *B, Vector *C)
829{
830// cout << Verbose(3) << "For comparison: ";
831// cout << "A " << A->Projection(this) << "\t";
832// cout << "B " << B->Projection(this) << "\t";
833// cout << "C " << C->Projection(this) << "\t";
834// cout << endl;
835 return A->Projection(this);
836};
837
838/** Creates a new vector as the one with least square distance to a given set of \a vectors.
839 * \param *vectors set of vectors
840 * \param num number of vectors
841 * \return true if success, false if failed due to linear dependency
842 */
843bool Vector::LSQdistance(Vector **vectors, int num)
844{
845 int j;
846
847 for (j=0;j<num;j++) {
848 cout << Verbose(1) << j << "th atom's vector: ";
849 (vectors[j])->Output((ofstream *)&cout);
850 cout << endl;
851 }
852
853 int np = 3;
854 struct LSQ_params par;
855
856 const gsl_multimin_fminimizer_type *T =
857 gsl_multimin_fminimizer_nmsimplex;
858 gsl_multimin_fminimizer *s = NULL;
859 gsl_vector *ss, *y;
860 gsl_multimin_function minex_func;
861
862 size_t iter = 0, i;
863 int status;
864 double size;
865
866 /* Initial vertex size vector */
867 ss = gsl_vector_alloc (np);
868 y = gsl_vector_alloc (np);
869
870 /* Set all step sizes to 1 */
871 gsl_vector_set_all (ss, 1.0);
872
873 /* Starting point */
874 par.vectors = vectors;
875 par.num = num;
876
877 for (i=NDIM;i--;)
878 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
879
880 /* Initialize method and iterate */
881 minex_func.f = &LSQ;
882 minex_func.n = np;
883 minex_func.params = (void *)&par;
884
885 s = gsl_multimin_fminimizer_alloc (T, np);
886 gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
887
888 do
889 {
890 iter++;
891 status = gsl_multimin_fminimizer_iterate(s);
892
893 if (status)
894 break;
895
896 size = gsl_multimin_fminimizer_size (s);
897 status = gsl_multimin_test_size (size, 1e-2);
898
899 if (status == GSL_SUCCESS)
900 {
901 printf ("converged to minimum at\n");
902 }
903
904 printf ("%5d ", (int)iter);
905 for (i = 0; i < (size_t)np; i++)
906 {
907 printf ("%10.3e ", gsl_vector_get (s->x, i));
908 }
909 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
910 }
911 while (status == GSL_CONTINUE && iter < 100);
912
913 for (i=(size_t)np;i--;)
914 this->x[i] = gsl_vector_get(s->x, i);
915 gsl_vector_free(y);
916 gsl_vector_free(ss);
917 gsl_multimin_fminimizer_free (s);
918
919 return true;
920};
921
922/** Adds vector \a *y componentwise.
923 * \param *y vector
924 */
925void Vector::AddVector(const Vector *y)
926{
927 for (int i=NDIM;i--;)
928 this->x[i] += y->x[i];
929}
930
931/** Adds vector \a *y componentwise.
932 * \param *y vector
933 */
934void Vector::SubtractVector(const Vector *y)
935{
936 for (int i=NDIM;i--;)
937 this->x[i] -= y->x[i];
938}
939
940/** Copy vector \a *y componentwise.
941 * \param *y vector
942 */
943void Vector::CopyVector(const Vector *y)
944{
945 for (int i=NDIM;i--;)
946 this->x[i] = y->x[i];
947}
948
949
950/** Asks for position, checks for boundary.
951 * \param cell_size unitary size of cubic cell, coordinates must be within 0...cell_size
952 * \param check whether bounds shall be checked (true) or not (false)
953 */
954void Vector::AskPosition(double *cell_size, bool check)
955{
956 char coords[3] = {'x','y','z'};
957 int j = -1;
958 for (int i=0;i<3;i++) {
959 j += i+1;
960 do {
961 cout << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";
962 cin >> x[i];
963 } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check));
964 }
965};
966
967/** Solves a vectorial system consisting of two orthogonal statements and a norm statement.
968 * This is linear system of equations to be solved, however of the three given (skp of this vector\
969 * with either of the three hast to be zero) only two are linear independent. The third equation
970 * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution
971 * where very often it has to be checked whether a certain value is zero or not and thus forked into
972 * another case.
973 * \param *x1 first vector
974 * \param *x2 second vector
975 * \param *y third vector
976 * \param alpha first angle
977 * \param beta second angle
978 * \param c norm of final vector
979 * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
980 * \bug this is not yet working properly
981 */
982bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c)
983{
984 double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C;
985 double ang; // angle on testing
986 double sign[3];
987 int i,j,k;
988 A = cos(alpha) * x1->Norm() * c;
989 B1 = cos(beta + M_PI/2.) * y->Norm() * c;
990 B2 = cos(beta) * x2->Norm() * c;
991 C = c * c;
992 cout << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;
993 int flag = 0;
994 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
995 if (fabs(x1->x[1]) > MYEPSILON) {
996 flag = 1;
997 } else if (fabs(x1->x[2]) > MYEPSILON) {
998 flag = 2;
999 } else {
1000 return false;
1001 }
1002 }
1003 switch (flag) {
1004 default:
1005 case 0:
1006 break;
1007 case 2:
1008 flip(&x1->x[0],&x1->x[1]);
1009 flip(&x2->x[0],&x2->x[1]);
1010 flip(&y->x[0],&y->x[1]);
1011 //flip(&x[0],&x[1]);
1012 flip(&x1->x[1],&x1->x[2]);
1013 flip(&x2->x[1],&x2->x[2]);
1014 flip(&y->x[1],&y->x[2]);
1015 //flip(&x[1],&x[2]);
1016 case 1:
1017 flip(&x1->x[0],&x1->x[1]);
1018 flip(&x2->x[0],&x2->x[1]);
1019 flip(&y->x[0],&y->x[1]);
1020 //flip(&x[0],&x[1]);
1021 flip(&x1->x[1],&x1->x[2]);
1022 flip(&x2->x[1],&x2->x[2]);
1023 flip(&y->x[1],&y->x[2]);
1024 //flip(&x[1],&x[2]);
1025 break;
1026 }
1027 // now comes the case system
1028 D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
1029 D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2];
1030 D3 = y->x[0]/x1->x[0]*A-B1;
1031 cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
1032 if (fabs(D1) < MYEPSILON) {
1033 cout << Verbose(2) << "D1 == 0!\n";
1034 if (fabs(D2) > MYEPSILON) {
1035 cout << Verbose(3) << "D2 != 0!\n";
1036 x[2] = -D3/D2;
1037 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
1038 E2 = -x1->x[1]/x1->x[0];
1039 cout << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";
1040 F1 = E1*E1 + 1.;
1041 F2 = -E1*E2;
1042 F3 = E1*E1 + D3*D3/(D2*D2) - C;
1043 cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
1044 if (fabs(F1) < MYEPSILON) {
1045 cout << Verbose(4) << "F1 == 0!\n";
1046 cout << Verbose(4) << "Gleichungssystem linear\n";
1047 x[1] = F3/(2.*F2);
1048 } else {
1049 p = F2/F1;
1050 q = p*p - F3/F1;
1051 cout << Verbose(4) << "p " << p << "\tq " << q << endl;
1052 if (q < 0) {
1053 cout << Verbose(4) << "q < 0" << endl;
1054 return false;
1055 }
1056 x[1] = p + sqrt(q);
1057 }
1058 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
1059 } else {
1060 cout << Verbose(2) << "Gleichungssystem unterbestimmt\n";
1061 return false;
1062 }
1063 } else {
1064 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1;
1065 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2];
1066 cout << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";
1067 F1 = E2*E2 + D2*D2/(D1*D1) + 1.;
1068 F2 = -(E1*E2 + D2*D3/(D1*D1));
1069 F3 = E1*E1 + D3*D3/(D1*D1) - C;
1070 cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
1071 if (fabs(F1) < MYEPSILON) {
1072 cout << Verbose(3) << "F1 == 0!\n";
1073 cout << Verbose(3) << "Gleichungssystem linear\n";
1074 x[2] = F3/(2.*F2);
1075 } else {
1076 p = F2/F1;
1077 q = p*p - F3/F1;
1078 cout << Verbose(3) << "p " << p << "\tq " << q << endl;
1079 if (q < 0) {
1080 cout << Verbose(3) << "q < 0" << endl;
1081 return false;
1082 }
1083 x[2] = p + sqrt(q);
1084 }
1085 x[1] = (-D2 * x[2] - D3)/D1;
1086 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
1087 }
1088 switch (flag) { // back-flipping
1089 default:
1090 case 0:
1091 break;
1092 case 2:
1093 flip(&x1->x[0],&x1->x[1]);
1094 flip(&x2->x[0],&x2->x[1]);
1095 flip(&y->x[0],&y->x[1]);
1096 flip(&x[0],&x[1]);
1097 flip(&x1->x[1],&x1->x[2]);
1098 flip(&x2->x[1],&x2->x[2]);
1099 flip(&y->x[1],&y->x[2]);
1100 flip(&x[1],&x[2]);
1101 case 1:
1102 flip(&x1->x[0],&x1->x[1]);
1103 flip(&x2->x[0],&x2->x[1]);
1104 flip(&y->x[0],&y->x[1]);
1105 //flip(&x[0],&x[1]);
1106 flip(&x1->x[1],&x1->x[2]);
1107 flip(&x2->x[1],&x2->x[2]);
1108 flip(&y->x[1],&y->x[2]);
1109 flip(&x[1],&x[2]);
1110 break;
1111 }
1112 // one z component is only determined by its radius (without sign)
1113 // thus check eight possible sign flips and determine by checking angle with second vector
1114 for (i=0;i<8;i++) {
1115 // set sign vector accordingly
1116 for (j=2;j>=0;j--) {
1117 k = (i & pot(2,j)) << j;
1118 cout << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;
1119 sign[j] = (k == 0) ? 1. : -1.;
1120 }
1121 cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
1122 // apply sign matrix
1123 for (j=NDIM;j--;)
1124 x[j] *= sign[j];
1125 // calculate angle and check
1126 ang = x2->Angle (this);
1127 cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
1128 if (fabs(ang - cos(beta)) < MYEPSILON) {
1129 break;
1130 }
1131 // unapply sign matrix (is its own inverse)
1132 for (j=NDIM;j--;)
1133 x[j] *= sign[j];
1134 }
1135 return true;
1136};
Note: See TracBrowser for help on using the repository browser.