source: src/vector.cpp@ 2793ef

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 2793ef was 9d6308, checked in by Frederik Heber <heber@…>, 16 years ago

Vector::GetIntersectionOfTwoLinesOnPlane() rewritten.

Use routine suggested by Eric Weisstein of Wolfram, which is basically what we did before the matrix attempt.

Signed-off-by: Frederik Heber <heber@tabletINS.(none)>

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