source: src/vector.cpp@ 89c8b2

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 89c8b2 was 89c8b2, checked in by Saskia Metzler <metzler@…>, 16 years ago

Ticket 17: Write new function molecule::CopyMoleculeFromSubRegion()

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