source: src/vector.cpp@ 1ca488

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 1ca488 was b998c3, checked in by Frederik Heber <heber@…>, 15 years ago

Fix attempts of the Tesselation.

One problem still remains:

  • degenerated triangles are created more than two times.

The following has been changed:

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