source: src/vector.cpp@ 4a7776a

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 4a7776a was d09ff7, checked in by Frederik Heber <heber@…>, 16 years ago

new function Vector::WrapPeriodically() and Vector::(Inverse)MatrixMultiplication has const parameters now.

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